Skip to content

Commit

Permalink
[ENG-1840, ENG-1842] Add native dependencies for iOS (#2693)
Browse files Browse the repository at this point in the history
* Implement dowload of mobile native deps
 - Add a spinner animation to `pnpm prep`
 - Change abandoned dependency @iarna/toml with smol-toml
 - Validate cargo config.toml after generating it from mustache template
 - Disabled HTTP2 downloads with udici, it is very broken

* Initial ios native deps xcframework logic

* Remove logic for handling dynamic iOS libs, using static libs now

* Fix PATH in build-rust.sh
 - Remove app.json

* Restore crates/images/src/pdf.rs

* minor fix .editorconfig

* Finally ios successfully compiles with ffmpeg enabled
 - Change SDCore.podspec to add extra libraries required by ffmpeg
 - Fix heif linking for ios in config.toml template
 - Add symlink logic for extra libraries required to compile ios in build-rust.sh
  • Loading branch information
HeavenVolkoff authored Sep 17, 2024
1 parent 1db7aff commit 3bbea40
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 109 deletions.
23 changes: 23 additions & 0 deletions .cargo/config.toml.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ rustflags = ["-L", "{{{nativeDeps}}}/lib", "-Csplit-debuginfo=unpacked"]
[target.aarch64-apple-darwin.heif]
rustc-link-search = ["{{{nativeDeps}}}/lib"]
rustc-link-lib = ["heif"]

{{#hasiOS}}
[target.aarch64-apple-ios]
rustflags = ["-L", "{{{mobileNativeDeps}}}/aarch64-apple-ios/lib", "-Csplit-debuginfo=unpacked"]

[target.aarch64-apple-ios.heif]
rustc-link-search = ["{{{mobileNativeDeps}}}/aarch64-apple-ios/lib"]
rustc-link-lib = ["static:+bundle=heif"]

[target.aarch64-apple-ios-sim]
rustflags = ["-L", "{{{mobileNativeDeps}}}/aarch64-apple-ios-sim/lib", "-Csplit-debuginfo=unpacked"]

[target.aarch64-apple-ios-sim.heif]
rustc-link-search = ["{{{mobileNativeDeps}}}/aarch64-apple-ios-sim/lib"]
rustc-link-lib = ["static:+bundle=heif"]

[target.x86_64-apple-ios]
rustflags = ["-L", "{{{mobileNativeDeps}}}/x86_64-apple-ios/lib", "-Csplit-debuginfo=unpacked"]

[target.x86_64-apple-ios.heif]
rustc-link-search = ["{{{mobileNativeDeps}}}/x86_64-apple-ios/lib"]
rustc-link-lib = ["static:+bundle=heif"]
{{/hasiOS}}
{{/isMacOS}}

{{#isWin}}
Expand Down
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ indent_style = space
indent_size = 4
indent_style = space

# Ruby
# http://www.caliban.org/ruby/rubyguide.shtml#indentation
[*.{rb,podspec}]
indent_size = 2
indent_style = space

# YAML
# http://yaml.org/spec/1.2/2009-07-21/spec.html#id2576668
[*.{yaml,yml}]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ packages/turbo-server/data/
packages/turbo-server/uploads/
apps/*/stats.html
apps/.deps
apps/mobile/.deps
apps/releases/.vscode
apps/desktop/src-tauri/tauri.conf.patch.json
apps/desktop/src-tauri/*.dll
Expand Down
4 changes: 3 additions & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('node:path');

/**
* {@type require('prettier').Config}
*/
Expand All @@ -24,6 +26,6 @@ module.exports = {
],
importOrderParserPlugins: ['typescript', 'jsx', 'decorators'],
importOrderTypeScriptVersion: '5.0.0',
tailwindConfig: './packages/ui/tailwind.config.js',
tailwindConfig: path.resolve(path.join(__dirname, 'packages/ui/tailwind.config.js')),
plugins: ['@ianvs/prettier-plugin-sort-imports', 'prettier-plugin-tailwindcss']
};
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,5 @@
"i18n-ally.keystyle": "flat",
// You need to add this to your locale settings file "i18n-ally.translate.google.apiKey": "xxx"
"i18n-ally.translate.engines": ["google"],
"prettier.configPath": ".prettierrc.js",
"prettier.prettierPath": "./node_modules/prettier",
"evenBetterToml.taplo.configFile.path": ".taplo.toml"
}
12 changes: 9 additions & 3 deletions apps/mobile/modules/sd-core/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ license.workspace = true
repository.workspace = true
rust-version = "1.64"

[dependencies]
# Spacedrive Sub-crates
sd-core = { path = "../../../../../core", features = ["mobile"], default-features = false }
[target.'cfg(target_os = "android")'.dependencies]
sd-core = { default-features = false, features = ["mobile"], path = "../../../../../core" }

[target.'cfg(target_os = "ios")'.dependencies]
sd-core = { default-features = false, features = [
"ffmpeg",
"heif",
"mobile"
], path = "../../../../../core" }

# Workspace dependencies
futures = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions apps/mobile/modules/sd-core/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(any(target_os = "android", target_os = "ios"))]

use futures::{future::join_all, StreamExt};
use futures_channel::mpsc;
use once_cell::sync::{Lazy, OnceCell};
Expand Down
67 changes: 39 additions & 28 deletions apps/mobile/modules/sd-core/ios/SDCore.podspec
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
#
# You will probs wanna add `use_frameworks! :linkage => :static` into your `ios/Podfile` as well.
#

require 'json'
require "json"

Pod::Spec.new do |s|
s.name = 'SDCore'
s.version = '0.0.0'
s.summary = 'Spacedrive core for React Native'
s.description = 'Spacedrive core for React Native'
s.author = 'Oscar Beaumont'
s.license = 'APGL-3.0'
s.platform = :ios, '14.0'
s.source = { git: 'https://github.com/spacedriveapp/spacedrive' }
s.homepage = 'https://www.spacedrive.com'
s.name = "SDCore"
s.version = "0.0.0"
s.summary = "Spacedrive core for React Native"
s.description = "Spacedrive core for React Native"
s.author = "Spacedrive Technology Inc"
s.license = "AGPL-3.0"
s.platform = :ios, "14.0"
s.source = { git: "https://github.com/spacedriveapp/spacedrive" }
s.homepage = "https://www.spacedrive.com"
s.static_framework = true

s.dependency 'ExpoModulesCore'
s.dependency "ExpoModulesCore"

s.pod_target_xcconfig = {
'DEFINES_MODULE' => 'YES',
'SWIFT_COMPILATION_MODE' => 'wholemodule'
"DEFINES_MODULE" => "YES",
"SWIFT_COMPILATION_MODE" => "wholemodule",
}

s.script_phase = {
:name => "Build Spacedrive Core!",
:script => "exec \"${PODS_TARGET_SRCROOT}/build-rust.sh\"",
:execution_position => :before_compile,
}

s.script_phase = {
:name => 'Build Spacedrive Core!',
:script => 'env -i SPACEDRIVE_CI=$SPACEDRIVE_CI CONFIGURATION=$CONFIGURATION PLATFORM_NAME=$PLATFORM_NAME ${PODS_TARGET_SRCROOT}/build-rust.sh',
:execution_position => :before_compile
}
# Add libraries
ffmpeg_libraries = [
"-lmp3lame", "-lsoxr", "-ltheora", "-lopus", "-lvorbisenc", "-lx265",
"-lpostproc", "-ltheoraenc", "-ltheoradec", "-lde265", "-lvorbisfile",
"-logg", "-lSvtAv1Enc", "-lvpx", "-lhdr10plus", "-lx264", "-lvorbis",
"-lzimg", "-lsoxr-lsr", "-liconv", "-lbz2", "-llzma"
].join(' ')

s.xcconfig = {
'LIBRARY_SEARCH_PATHS' => '"' + JSON.parse(`cargo metadata`)["target_directory"].to_s + '"',
'OTHER_LDFLAGS[sdk=iphoneos*]' => '$(inherited) -lsd_mobile_ios',
'OTHER_LDFLAGS[sdk=iphonesimulator*]' => '$(inherited) -lsd_mobile_iossim'
}
# Add frameworks
ffmpeg_frameworks = [
"-framework AudioToolbox",
"-framework VideoToolbox",
"-framework AVFoundation"
].join(' ')

s.xcconfig = {
"LIBRARY_SEARCH_PATHS" => '"' + JSON.parse(`cargo metadata`)["target_directory"].to_s + '"',
"OTHER_LDFLAGS[sdk=iphoneos*]" => "$(inherited) -lsd_mobile_ios #{ffmpeg_libraries} #{ffmpeg_frameworks}",
"OTHER_LDFLAGS[sdk=iphonesimulator*]" => "$(inherited) -lsd_mobile_iossim #{ffmpeg_libraries} #{ffmpeg_frameworks}",
}

s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}"
s.module_map = "#{s.name}.modulemap"
s.module_map = "#{s.name}.modulemap"
end
52 changes: 40 additions & 12 deletions apps/mobile/modules/sd-core/ios/build-rust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ err() {
exit 1
}

symlink_libs() {
if [ $# -ne 2 ]; then
err "Invalid number of arguments. Usage: symlink_libs <dir1> <dir2>"
fi

if [ ! -d "$1" ]; then
err "Directory '$1' does not exist."
fi

if [ ! -d "$2" ]; then
err "Directory '$2' does not exist."
fi

find "$1" -type f -name '*.a' -exec ln -sf "{}" "$2" \;
}

if [ -z "${HOME:-}" ]; then
HOME="$(CDPATH='' cd -- "$(osascript -e 'set output to (POSIX path of (path to home folder))')" && pwd -P)"
export HOME
Expand All @@ -21,35 +37,47 @@ fi
echo "Building 'sd-mobile-ios' library..."

__dirname="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"
DEPS="${__dirname}/../../../.deps/"
DEPS="$(CDPATH='' cd -- "$DEPS" && pwd -P)"
CARGO_CONFIG="${__dirname}/../../../../../.cargo"
CARGO_CONFIG="$(CDPATH='' cd -- "$CARGO_CONFIG" && pwd -P)/config.toml"

# Ensure target dir exists
TARGET_DIRECTORY="${__dirname}/../../../../../target"
mkdir -p "$TARGET_DIRECTORY"
TARGET_DIRECTORY="$(CDPATH='' cd -- "$TARGET_DIRECTORY" && pwd -P)"

# if [ "${CONFIGURATION:-}" != "Debug" ]; then
# CARGO_FLAGS=--release
# export CARGO_FLAGS
# fi
TARGET_CONFIG=debug
if [ "${CONFIGURATION:-}" = "Release" ]; then
set -- --release
TARGET_CONFIG=release
fi

# Required for CI and for everyone I guess?
export PATH="${CARGO_HOME:-"${HOME}/.cargo"}/bin:$PATH"
trap 'if [ -e "${CARGO_CONFIG}.bak" ]; then mv "${CARGO_CONFIG}.bak" "$CARGO_CONFIG"; fi' EXIT

# Required for `cargo` to correctly compile the library
RUST_PATH="${CARGO_HOME:-"${HOME}/.cargo"}/bin:$(brew --prefix)/bin:$(env -i /bin/bash --noprofile --norc -c 'echo $PATH')"
if [ "${PLATFORM_NAME:-}" = "iphonesimulator" ]; then
case "$(uname -m)" in
"arm64" | "aarch64") # M series
cargo build -p sd-mobile-ios --target aarch64-apple-ios-sim
lipo -create -output "$TARGET_DIRECTORY"/libsd_mobile_iossim.a "$TARGET_DIRECTORY"/aarch64-apple-ios-sim/debug/libsd_mobile_ios.a
sed -i.bak "s|FFMPEG_DIR = { force = true, value = \".*\" }|FFMPEG_DIR = { force = true, value = \"${DEPS}/aarch64-apple-ios-sim\" }|" "$CARGO_CONFIG"
env CARGO_FEATURE_STATIC=1 PATH="$RUST_PATH" cargo build -p sd-mobile-ios --target aarch64-apple-ios-sim "$@"
lipo -create -output "$TARGET_DIRECTORY"/libsd_mobile_iossim.a "${TARGET_DIRECTORY}/aarch64-apple-ios-sim/${TARGET_CONFIG}/libsd_mobile_ios.a"
symlink_libs "${DEPS}/aarch64-apple-ios-sim/lib" "$TARGET_DIRECTORY"
;;
"x86_64") # Intel
cargo build -p sd-mobile-ios --target x86_64-apple-ios
lipo -create -output "$TARGET_DIRECTORY"/libsd_mobile_iossim.a "$TARGET_DIRECTORY"/x86_64-apple-ios/debug/libsd_mobile_ios.a
sed -i.bak "s|FFMPEG_DIR = { force = true, value = \".*\" }|FFMPEG_DIR = { force = true, value = \"${DEPS}/x86_64-apple-ios\" }|" "$CARGO_CONFIG"
env CARGO_FEATURE_STATIC=1 PATH="$RUST_PATH" cargo build -p sd-mobile-ios --target x86_64-apple-ios "$@"
lipo -create -output "$TARGET_DIRECTORY"/libsd_mobile_iossim.a "${TARGET_DIRECTORY}/x86_64-apple-ios/${TARGET_CONFIG}/libsd_mobile_ios.a"
symlink_libs "${DEPS}/x86_64-apple-ios/lib" "$TARGET_DIRECTORY"
;;
*)
err 'Unsupported architecture.'
;;
esac
else
cargo build -p sd-mobile-ios --target aarch64-apple-ios --release
lipo -create -output "$TARGET_DIRECTORY"/libsd_mobile_ios.a "$TARGET_DIRECTORY"/aarch64-apple-ios/release/libsd_mobile_ios.a
sed -i.bak "s|FFMPEG_DIR = { force = true, value = \".*\" }|FFMPEG_DIR = { force = true, value = \"${DEPS}/aarch64-apple-ios\" }|" "$CARGO_CONFIG"
env CARGO_FEATURE_STATIC=1 PATH="$RUST_PATH" cargo build -p sd-mobile-ios --target aarch64-apple-ios "$@"
lipo -create -output "$TARGET_DIRECTORY"/libsd_mobile_ios.a "${TARGET_DIRECTORY}/aarch64-apple-ios/${TARGET_CONFIG}/libsd_mobile_ios.a"
symlink_libs "${DEPS}/aarch64-apple-ios/lib" "$TARGET_DIRECTORY"
fi
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@
"eslintConfig": {
"root": true
},
"packageManager": "pnpm@9.7.0"
"packageManager": "pnpm@9.9.0"
}
30 changes: 17 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"trailingComma": "es5"
},
"dependencies": {
"@iarna/toml": "^3.0.0",
"smol-toml": "^1.3.0",
"archive-wasm": "^1.7.0",
"mustache": "^4.2.0",
"os-proxy-config": "^1.1.1",
"plist": "^3.1.0",
"semver": "^7.6.3",
"undici": "^6.19.7"
"undici": "^6.19.8"
},
"devDependencies": {
"@babel/core": "^7.24.0",
Expand Down
Loading

0 comments on commit 3bbea40

Please sign in to comment.