| 1 | #!/bin/sh |
| 2 | # Builds native/build/DeepFilter.xcframework and copies the DeepFilterNet 3 |
| 3 | # model beside it: Rikorose/DeepFilterNet's libDF (MIT or Apache-2.0) with |
| 4 | # its C API, as a static library for Mac, iPhone and the iPhone simulator |
| 5 | # (Apple silicon). |
| 6 | # |
| 7 | # Built rather than committed, as CallsBridge and RNNoise are. Needs rustup |
| 8 | # (https://rustup.rs); the toolchain, its targets and cbindgen are pinned |
| 9 | # and installed on first run. |
| 10 | set -eu |
| 11 | cd "$(dirname "$0")/.." |
| 12 | |
| 13 | commit=d375b2d8309e0935d165700c91da9de862a99c31 |
| 14 | toolchain=1.92.0 |
| 15 | cbindgen_version=0.29.2 |
| 16 | out=build/DeepFilter.xcframework |
| 17 | model=build/DeepFilterNet3_onnx.tar.gz |
| 18 | stamp="$out/.built-$commit-$toolchain" |
| 19 | if [ -f "$stamp" ] && [ -f "$model" ]; then exit 0; fi |
| 20 | |
| 21 | PATH="$HOME/.cargo/bin:$PATH" |
| 22 | command -v rustup >/dev/null || { |
| 23 | echo "build-deepfilter: rustup is required (https://rustup.rs)" >&2 |
| 24 | exit 1 |
| 25 | } |
| 26 | targets="aarch64-apple-darwin aarch64-apple-ios aarch64-apple-ios-sim" |
| 27 | # shellcheck disable=SC2086 |
| 28 | rustup toolchain install "$toolchain" --profile minimal $(printf ' -t %s' $targets) >/dev/null |
| 29 | if [ "$(cbindgen --version 2>/dev/null)" != "cbindgen $cbindgen_version" ]; then |
| 30 | cargo "+$toolchain" install cbindgen --version "$cbindgen_version" --locked |
| 31 | fi |
| 32 | |
| 33 | work=build/deepfilter |
| 34 | mkdir -p "$work" |
| 35 | if [ ! -d "$work/src/.git" ]; then |
| 36 | rm -rf "$work/src" |
| 37 | git clone --quiet https://github.com/Rikorose/DeepFilterNet.git "$work/src" |
| 38 | fi |
| 39 | git -C "$work/src" -c advice.detachedHead=false checkout --quiet --force "$commit" |
| 40 | |
| 41 | # Deployment targets matching the apps, so the linker does not warn that |
| 42 | # the library was built for newer systems than they run on. |
| 43 | export MACOSX_DEPLOYMENT_TARGET=14.0 IPHONEOS_DEPLOYMENT_TARGET=17.0 |
| 44 | for target in $targets; do |
| 45 | cargo "+$toolchain" build --manifest-path "$work/src/Cargo.toml" -p deep_filter --lib \ |
| 46 | --release --features capi --target "$target" --target-dir "$work/target" |
| 47 | done |
| 48 | |
| 49 | headers="$work/headers" |
| 50 | rm -rf "$headers" |
| 51 | mkdir -p "$headers/DeepFilter" |
| 52 | cbindgen --config "$work/src/cbindgen.toml" --crate deep_filter \ |
| 53 | --output "$headers/DeepFilter/deep_filter.h" "$work/src/libDF" |
| 54 | cat >"$headers/DeepFilter/module.modulemap" <<'EOF' |
| 55 | module DeepFilter { |
| 56 | header "deep_filter.h" |
| 57 | link "df" |
| 58 | export * |
| 59 | } |
| 60 | EOF |
| 61 | |
| 62 | rm -rf "$out" |
| 63 | xcodebuild -create-xcframework \ |
| 64 | -library "$work/target/aarch64-apple-darwin/release/libdf.a" -headers "$headers" \ |
| 65 | -library "$work/target/aarch64-apple-ios/release/libdf.a" -headers "$headers" \ |
| 66 | -library "$work/target/aarch64-apple-ios-sim/release/libdf.a" -headers "$headers" \ |
| 67 | -output "$out" >/dev/null |
| 68 | cp "$work/src/models/DeepFilterNet3_onnx.tar.gz" "$model" |
| 69 | cp "$work/src/LICENSE-MIT" "$out/LICENSE-MIT" |
| 70 | touch "$stamp" |
| 71 | printf 'Built %s/%s\n' "$PWD" "$out" |
| 72 | |