| 1 | #!/bin/sh |
| 2 | # Builds native/build/RNNoise.xcframework: xiph's RNNoise (BSD-3) as a |
| 3 | # static library for Mac, iPhone and the iPhone simulator. |
| 4 | # |
| 5 | # Built rather than committed, as CallsBridge is: the model's weights come |
| 6 | # as a generated C file of 78 MB (rnnoise_data.c), which is no more source |
| 7 | # than a framework binary is. Both the code and the model are pinned — the |
| 8 | # code to a commit, the model to its SHA-256, which is also its name. |
| 9 | set -eu |
| 10 | cd "$(dirname "$0")/.." |
| 11 | |
| 12 | commit=70f1d256acd4b34a572f999a05c87bf00b67730d |
| 13 | model=0a8755f8e2d834eff6a54714ecc7d75f9932e845df35f8b59bc52a7cfe6e8b37 |
| 14 | out=build/RNNoise.xcframework |
| 15 | stamp="$out/.built-$commit-$model" |
| 16 | if [ -f "$stamp" ]; then exit 0; fi |
| 17 | |
| 18 | work=build/rnnoise |
| 19 | mkdir -p "$work" |
| 20 | if [ ! -d "$work/src/.git" ]; then |
| 21 | rm -rf "$work/src" |
| 22 | git clone --quiet https://gitlab.xiph.org/xiph/rnnoise.git "$work/src" |
| 23 | fi |
| 24 | git -C "$work/src" -c advice.detachedHead=false checkout --quiet "$commit" |
| 25 | |
| 26 | tarball="$work/rnnoise_data-$model.tar.gz" |
| 27 | if [ ! -f "$tarball" ]; then |
| 28 | curl --fail --silent --show-error --location -o "$tarball.part" \ |
| 29 | "https://media.xiph.org/rnnoise/models/rnnoise_data-$model.tar.gz" |
| 30 | mv "$tarball.part" "$tarball" |
| 31 | fi |
| 32 | echo "$model $tarball" | shasum -a 256 -c --status || { |
| 33 | echo "build-rnnoise: model checksum mismatch; delete $tarball and retry" >&2 |
| 34 | exit 1 |
| 35 | } |
| 36 | tar -xzf "$tarball" -C "$work/src" src/rnnoise_data.c src/rnnoise_data.h |
| 37 | |
| 38 | sources="denoise.c rnn.c pitch.c kiss_fft.c celt_lpc.c nnet.c nnet_default.c parse_lpcnet_weights.c rnnoise_tables.c rnnoise_data.c" |
| 39 | |
| 40 | # One static library per platform, fat across its architectures. |
| 41 | build_slice() { # name sdk "triple triple..." |
| 42 | name=$1 sdk=$2 triples=$3 |
| 43 | root=$(xcrun --sdk "$sdk" --show-sdk-path) |
| 44 | libs="" |
| 45 | for triple in $triples; do |
| 46 | objdir="$work/obj/$name/$triple" |
| 47 | mkdir -p "$objdir" |
| 48 | for f in $sources; do |
| 49 | clang -c -O3 -target "$triple" -isysroot "$root" -fvisibility=hidden -DRNNOISE_BUILD \ |
| 50 | -I"$work/src/include" -I"$work/src/src" "$work/src/src/$f" -o "$objdir/${f%.c}.o" & |
| 51 | done |
| 52 | wait |
| 53 | libtool -static -o "$objdir/librnnoise.a" "$objdir"/*.o |
| 54 | libs="$libs $objdir/librnnoise.a" |
| 55 | done |
| 56 | mkdir -p "$work/lib/$name" |
| 57 | # shellcheck disable=SC2086 |
| 58 | lipo -create $libs -output "$work/lib/$name/librnnoise.a" |
| 59 | } |
| 60 | build_slice macos macosx "arm64-apple-macos14.0 x86_64-apple-macos14.0" |
| 61 | build_slice ios iphoneos "arm64-apple-ios17.0" |
| 62 | build_slice simulator iphonesimulator "arm64-apple-ios17.0-simulator x86_64-apple-ios17.0-simulator" |
| 63 | |
| 64 | # Headers in a subdirectory of their own: Xcode copies every static |
| 65 | # xcframework's headers into one include directory, and two module maps at |
| 66 | # its root collide. |
| 67 | headers="$work/headers" |
| 68 | rm -rf "$headers" |
| 69 | mkdir -p "$headers/RNNoise" |
| 70 | cp "$work/src/include/rnnoise.h" "$headers/RNNoise/" |
| 71 | cat >"$headers/RNNoise/module.modulemap" <<'EOF' |
| 72 | module RNNoise { |
| 73 | header "rnnoise.h" |
| 74 | link "rnnoise" |
| 75 | export * |
| 76 | } |
| 77 | EOF |
| 78 | |
| 79 | rm -rf "$out" |
| 80 | xcodebuild -create-xcframework \ |
| 81 | -library "$work/lib/macos/librnnoise.a" -headers "$headers" \ |
| 82 | -library "$work/lib/ios/librnnoise.a" -headers "$headers" \ |
| 83 | -library "$work/lib/simulator/librnnoise.a" -headers "$headers" \ |
| 84 | -output "$out" >/dev/null |
| 85 | cp "$work/src/COPYING" "$out/COPYING" |
| 86 | touch "$stamp" |
| 87 | printf 'Built %s/%s\n' "$PWD" "$out" |
| 88 | |