#!/usr/bin/env bash set -e dir=build-ios mkdir -p $dir cd $dir onnxruntime_version=${SHERPA_ONNX_ONNXRUNTIME_VERSION:-1.28.2} onnxruntime_dir=ios-onnxruntime/$onnxruntime_version if [ ! -f $onnxruntime_dir/onnxruntime.xcframework/ios-arm64/onnxruntime.framework/onnxruntime ]; then mkdir -p $onnxruntime_dir pushd $onnxruntime_dir wget -c https://github.com/csukuangfj/onnxruntime-libs/releases/download/v${onnxruntime_version}/onnxruntime-ios-static-xcframework-${onnxruntime_version}.xcframework.zip unzip onnxruntime-ios-static-xcframework-${onnxruntime_version}.xcframework.zip rm onnxruntime-ios-static-xcframework-${onnxruntime_version}.xcframework.zip cd .. ln -sf $onnxruntime_version/onnxruntime.xcframework . popd fi # First, for simulator echo "Building for simulator (x86_64)" export SHERPA_ONNXRUNTIME_LIB_DIR=$PWD/ios-onnxruntime/onnxruntime.xcframework/ios-arm64_x86_64-simulator export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$PWD/ios-onnxruntime/onnxruntime.xcframework/ios-arm64_x86_64-simulator/onnxruntime.framework/Headers echo "SHERPA_ONNXRUNTIME_LIB_DIR: $SHERPA_ONNXRUNTIME_LIB_DIR" echo "SHERPA_ONNXRUNTIME_INCLUDE_DIR $SHERPA_ONNXRUNTIME_INCLUDE_DIR" # Note: We use -DENABLE_ARC=1 here to fix the linking error: # # The symbol _NSLog is not defined # cmake \ -S .. \ -DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \ -DPLATFORM=SIMULATOR64 \ -DENABLE_BITCODE=0 \ -DENABLE_ARC=1 \ -DENABLE_VISIBILITY=0 \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ -DSHERPA_ONNX_ENABLE_BINARY=OFF \ -DSHERPA_ONNX_ENABLE_TESTS=OFF \ -DSHERPA_ONNX_ENABLE_CHECK=OFF \ -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ -DSHERPA_ONNX_ENABLE_JNI=OFF \ -DSHERPA_ONNX_ENABLE_C_API=ON \ -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ -DDEPLOYMENT_TARGET=13.0 \ -B build/simulator_x86_64 cmake --build build/simulator_x86_64 -j 4 echo "Building for simulator (arm64)" cmake \ -S .. \ -DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \ -DPLATFORM=SIMULATORARM64 \ -DENABLE_BITCODE=0 \ -DENABLE_ARC=1 \ -DENABLE_VISIBILITY=0 \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=./install \ -DBUILD_SHARED_LIBS=OFF \ -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ -DSHERPA_ONNX_ENABLE_BINARY=OFF \ -DSHERPA_ONNX_ENABLE_TESTS=OFF \ -DSHERPA_ONNX_ENABLE_CHECK=OFF \ -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ -DSHERPA_ONNX_ENABLE_JNI=OFF \ -DSHERPA_ONNX_ENABLE_C_API=ON \ -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ -DDEPLOYMENT_TARGET=13.0 \ -B build/simulator_arm64 cmake --build build/simulator_arm64 -j 4 echo "Building for arm64" export SHERPA_ONNXRUNTIME_LIB_DIR=$PWD/ios-onnxruntime/onnxruntime.xcframework/ios-arm64 export SHERPA_ONNXRUNTIME_INCLUDE_DIR=$PWD/ios-onnxruntime/onnxruntime.xcframework/ios-arm64/onnxruntime.framework/Headers cmake \ -S .. \ -DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake \ -DPLATFORM=OS64 \ -DENABLE_BITCODE=0 \ -DENABLE_ARC=1 \ -DENABLE_VISIBILITY=0 \ -DCMAKE_INSTALL_PREFIX=./install \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ -DSHERPA_ONNX_ENABLE_BINARY=OFF \ -DSHERPA_ONNX_ENABLE_TESTS=OFF \ -DSHERPA_ONNX_ENABLE_CHECK=OFF \ -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ -DSHERPA_ONNX_ENABLE_JNI=OFF \ -DSHERPA_ONNX_ENABLE_C_API=ON \ -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ -DDEPLOYMENT_TARGET=13.0 \ -B build/os64 cmake --build build/os64 -j 4 # Generate headers for sherpa-onnx.xcframework cmake --build build/os64 --target install echo "Generate xcframework" mkdir -p "build/simulator/lib" for f in libkaldi-native-fbank-core.a libkissfft-float.a libsherpa-onnx-c-api.a libsherpa-onnx-core.a \ libsherpa-onnx-fstfar.a libssentencepiece_core.a libutf8proc.a \ libsherpa-onnx-fst.a libsherpa-onnx-kaldifst-core.a libkaldi-decoder-core.a; do lipo -create build/simulator_arm64/lib/${f} \ build/simulator_x86_64/lib/${f} \ -output build/simulator/lib/${f} done # Merge archive first, because the following xcodebuild create xcframework # cannot accept multi archive with the same architecture. libtool -static -o build/simulator/libsherpa-onnx.a \ build/simulator/lib/libkaldi-native-fbank-core.a \ build/simulator/lib/libkissfft-float.a \ build/simulator/lib/libsherpa-onnx-c-api.a \ build/simulator/lib/libsherpa-onnx-core.a \ build/simulator/lib/libsherpa-onnx-fstfar.a \ build/simulator/lib/libsherpa-onnx-fst.a \ build/simulator/lib/libsherpa-onnx-kaldifst-core.a \ build/simulator/lib/libkaldi-decoder-core.a \ build/simulator/lib/libssentencepiece_core.a \ build/simulator/lib/libutf8proc.a libtool -static -o build/os64/libsherpa-onnx.a \ build/os64/lib/libkaldi-native-fbank-core.a \ build/os64/lib/libkissfft-float.a \ build/os64/lib/libsherpa-onnx-c-api.a \ build/os64/lib/libsherpa-onnx-core.a \ build/os64/lib/libsherpa-onnx-fstfar.a \ build/os64/lib/libsherpa-onnx-fst.a \ build/os64/lib/libsherpa-onnx-kaldifst-core.a \ build/os64/lib/libkaldi-decoder-core.a \ build/os64/lib/libssentencepiece_core.a \ build/os64/lib/libutf8proc.a # Rename to match the shared library naming convention mv -v build/os64/libsherpa-onnx.a build/os64/libsherpa-onnx-c-api.a mv -v build/simulator/libsherpa-onnx.a build/simulator/libsherpa-onnx-c-api.a rm -rf sherpa-onnx.xcframework # Create framework bundles (like onnxruntime does) so SPM can resolve the module create_framework() { local lib_path=$1 local output_dir=$2 local fw_dir=$output_dir/SherpaOnnxC.framework rm -rf $fw_dir mkdir -p $fw_dir/Headers/sherpa-onnx/c-api mkdir -p $fw_dir/Modules cp $lib_path $fw_dir/SherpaOnnxC cp install/include/sherpa-onnx/c-api/c-api.h $fw_dir/Headers/sherpa-onnx/c-api/ cat > $fw_dir/Modules/module.modulemap << 'MEOF' framework module SherpaOnnxC { header "sherpa-onnx/c-api/c-api.h" export * } MEOF cat > $fw_dir/Info.plist << 'PEOF' CFBundleIdentifier com.k2-fsa.sherpa-onnx CFBundleName SherpaOnnxC CFBundlePackageType FMWK CFBundleExecutable SherpaOnnxC CFBundleVersion 20261026 CFBundleShortVersionString 2.0.1 MinimumOSVersion 13.0 CFBundleSupportedPlatforms iPhoneOS PEOF } create_framework build/os64/libsherpa-onnx-c-api.a build/os64 create_framework build/simulator/libsherpa-onnx-c-api.a build/simulator xcodebuild -create-xcframework \ -framework "build/os64/SherpaOnnxC.framework" \ -framework "build/simulator/SherpaOnnxC.framework" \ -output sherpa-onnx.xcframework SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ../CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) rm -f sherpa-onnx-${SHERPA_ONNX_VERSION}-ios-static.xcframework.zip zip -r -y sherpa-onnx-${SHERPA_ONNX_VERSION}-ios-static.xcframework.zip sherpa-onnx.xcframework echo "Checksum:" swift package compute-checksum sherpa-onnx-${SHERPA_ONNX_VERSION}-ios-static.xcframework.zip | tee checksum.txt