#!/bin/sh # Builds native/build/RNNoise.xcframework: xiph's RNNoise (BSD-3) as a # static library for Mac, iPhone and the iPhone simulator. # # Built rather than committed, as CallsBridge is: the model's weights come # as a generated C file of 78 MB (rnnoise_data.c), which is no more source # than a framework binary is. Both the code and the model are pinned — the # code to a commit, the model to its SHA-256, which is also its name. set -eu cd "$(dirname "$0")/.." commit=70f1d256acd4b34a572f999a05c87bf00b67730d model=0a8755f8e2d834eff6a54714ecc7d75f9932e845df35f8b59bc52a7cfe6e8b37 out=build/RNNoise.xcframework stamp="$out/.built-$commit-$model" if [ -f "$stamp" ]; then exit 0; fi work=build/rnnoise mkdir -p "$work" if [ ! -d "$work/src/.git" ]; then rm -rf "$work/src" git clone --quiet https://gitlab.xiph.org/xiph/rnnoise.git "$work/src" fi git -C "$work/src" -c advice.detachedHead=false checkout --quiet "$commit" tarball="$work/rnnoise_data-$model.tar.gz" if [ ! -f "$tarball" ]; then curl --fail --silent --show-error --location -o "$tarball.part" \ "https://media.xiph.org/rnnoise/models/rnnoise_data-$model.tar.gz" mv "$tarball.part" "$tarball" fi echo "$model $tarball" | shasum -a 256 -c --status || { echo "build-rnnoise: model checksum mismatch; delete $tarball and retry" >&2 exit 1 } tar -xzf "$tarball" -C "$work/src" src/rnnoise_data.c src/rnnoise_data.h 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" # One static library per platform, fat across its architectures. build_slice() { # name sdk "triple triple..." name=$1 sdk=$2 triples=$3 root=$(xcrun --sdk "$sdk" --show-sdk-path) libs="" for triple in $triples; do objdir="$work/obj/$name/$triple" mkdir -p "$objdir" for f in $sources; do clang -c -O3 -target "$triple" -isysroot "$root" -fvisibility=hidden -DRNNOISE_BUILD \ -I"$work/src/include" -I"$work/src/src" "$work/src/src/$f" -o "$objdir/${f%.c}.o" & done wait libtool -static -o "$objdir/librnnoise.a" "$objdir"/*.o libs="$libs $objdir/librnnoise.a" done mkdir -p "$work/lib/$name" # shellcheck disable=SC2086 lipo -create $libs -output "$work/lib/$name/librnnoise.a" } build_slice macos macosx "arm64-apple-macos14.0 x86_64-apple-macos14.0" build_slice ios iphoneos "arm64-apple-ios17.0" build_slice simulator iphonesimulator "arm64-apple-ios17.0-simulator x86_64-apple-ios17.0-simulator" # Headers in a subdirectory of their own: Xcode copies every static # xcframework's headers into one include directory, and two module maps at # its root collide. headers="$work/headers" rm -rf "$headers" mkdir -p "$headers/RNNoise" cp "$work/src/include/rnnoise.h" "$headers/RNNoise/" cat >"$headers/RNNoise/module.modulemap" <<'EOF' module RNNoise { header "rnnoise.h" link "rnnoise" export * } EOF rm -rf "$out" xcodebuild -create-xcframework \ -library "$work/lib/macos/librnnoise.a" -headers "$headers" \ -library "$work/lib/ios/librnnoise.a" -headers "$headers" \ -library "$work/lib/simulator/librnnoise.a" -headers "$headers" \ -output "$out" >/dev/null cp "$work/src/COPYING" "$out/COPYING" touch "$stamp" printf 'Built %s/%s\n' "$PWD" "$out"