set -euo pipefail
export DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer
cd ~/Daily/serifu-whisper-build
MACOS_MIN_OS_VERSION=13.3
COMMON_C_FLAGS="-Wno-macro-redefined -Wno-shorten-64-to-32 -Wno-unused-command-line-argument -g"
cmake -B build-macos -G Xcode \
    -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO \
    -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY="" \
    -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO \
    -DCMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT="dwarf-with-dsym" \
    -DCMAKE_XCODE_ATTRIBUTE_GCC_GENERATE_DEBUGGING_SYMBOLS=YES \
    -DCMAKE_XCODE_ATTRIBUTE_COPY_PHASE_STRIP=NO \
    -DCMAKE_XCODE_ATTRIBUTE_STRIP_INSTALLED_PRODUCT=NO \
    -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml \
    -DBUILD_SHARED_LIBS=OFF \
    -DWHISPER_BUILD_EXAMPLES=OFF \
    -DWHISPER_BUILD_TESTS=OFF \
    -DWHISPER_BUILD_SERVER=OFF \
    -DGGML_METAL_EMBED_LIBRARY=ON \
    -DGGML_METAL=ON \
    -DCMAKE_OSX_DEPLOYMENT_TARGET=$MACOS_MIN_OS_VERSION \
    -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
    -DCMAKE_C_FLAGS="$COMMON_C_FLAGS" \
    -DCMAKE_CXX_FLAGS="$COMMON_C_FLAGS" \
    -DWHISPER_COREML=ON \
    -DWHISPER_COREML_ALLOW_FALLBACK=ON \
    -S .
cmake --build build-macos --config Release -- -quiet
echo "BUILD DONE"

# ---- 打包（构建完成后手动跑）----
# libtool -static -o /tmp/whisper-merge/libwhisper-combined.a \
#   build-macos/src/Release/libwhisper.a build-macos/src/Release/libparakeet.a \
#   build-macos/src/Release/libwhisper.coreml.a build-macos/ggml/src/Release/libggml-base.a \
#   build-macos/ggml/src/Release/libggml-cpu.a build-macos/ggml/src/Release/libggml.a \
#   build-macos/ggml/src/ggml-blas/Release/libggml-blas.a build-macos/ggml/src/ggml-metal/Release/libggml-metal.a
# mkdir -p /tmp/whisper-merge/include && cp include/whisper.h ggml/include/ggml*.h /tmp/whisper-merge/include/
# （写 module.modulemap: module whisper { header "whisper.h"; link "c++"; export * }）
# xcodebuild -create-xcframework -library /tmp/whisper-merge/libwhisper-combined.a \
#   -headers /tmp/whisper-merge/include -output app/vendor/Whisper/xcframeworks/whisper.xcframework
