build_darwin.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh
  2. set -e
  3. status() { echo >&2 ">>> $@"; }
  4. usage() {
  5. echo "usage: $(basename $0) [build [sign]]"
  6. exit 1
  7. }
  8. export VERSION=${VERSION:-$(git describe --tags --first-parent --abbrev=7 --long --dirty --always | sed -e "s/^v//g")}
  9. export GOFLAGS="'-ldflags=-w -s \"-X=github.com/ollama/ollama/version.Version=${VERSION#v}\" \"-X=github.com/ollama/ollama/server.mode=release\"'"
  10. export CGO_CPPFLAGS='-mmacosx-version-min=11.3'
  11. ARCHS="arm64 amd64"
  12. while getopts "a:h" OPTION; do
  13. case $OPTION in
  14. a) ARCHS=$OPTARG ;;
  15. h) usage ;;
  16. esac
  17. done
  18. shift $(( $OPTIND - 1 ))
  19. _build_darwin() {
  20. for ARCH in $ARCHS; do
  21. status "Building darwin $ARCH"
  22. INSTALL_PREFIX=dist/darwin-$ARCH/
  23. GOOS=darwin GOARCH=$ARCH CGO_ENABLED=1 go build -o $INSTALL_PREFIX .
  24. if [ "$ARCH" = "amd64" ]; then
  25. status "Building darwin $ARCH dynamic backends"
  26. cmake -B build/darwin-$ARCH \
  27. -DCMAKE_OSX_ARCHITECTURES=x86_64 \
  28. -DCMAKE_OSX_DEPLOYMENT_TARGET=11.3 \
  29. -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX
  30. cmake --build build/darwin-$ARCH --target ggml-cpu -j
  31. cmake --install build/darwin-$ARCH --component CPU
  32. fi
  33. done
  34. }
  35. _sign_darwin() {
  36. status "Creating universal binary..."
  37. mkdir -p dist/darwin
  38. lipo -create -output dist/darwin/ollama dist/darwin-*/ollama
  39. chmod +x dist/darwin/ollama
  40. if [ -n "$APPLE_IDENTITY" ]; then
  41. for F in dist/darwin/ollama dist/darwin-amd64/lib/ollama/*; do
  42. codesign -f --timestamp -s "$APPLE_IDENTITY" --identifier ai.ollama.ollama --options=runtime $F
  43. done
  44. # create a temporary zip for notarization
  45. TEMP=$(mktemp -u).zip
  46. ditto -c -k --keepParent dist/darwin/ollama "$TEMP"
  47. xcrun notarytool submit "$TEMP" --wait --timeout 10m --apple-id $APPLE_ID --password $APPLE_PASSWORD --team-id $APPLE_TEAM_ID
  48. rm -f "$TEMP"
  49. fi
  50. status "Creating universal tarball..."
  51. tar -cf dist/ollama-darwin.tar --strip-components 2 dist/darwin/ollama
  52. tar -rf dist/ollama-darwin.tar --strip-components 4 dist/darwin-amd64/lib/
  53. gzip -9vc <dist/ollama-darwin.tar >dist/ollama-darwin.tgz
  54. }
  55. _build_macapp() {
  56. # build and optionally sign the mac app
  57. npm install --prefix macapp
  58. if [ -n "$APPLE_IDENTITY" ]; then
  59. npm run --prefix macapp make:sign
  60. else
  61. npm run --prefix macapp make
  62. fi
  63. mv ./macapp/out/make/zip/darwin/universal/Ollama-darwin-universal-$VERSION.zip dist/Ollama-darwin.zip
  64. }
  65. if [ "$#" -eq 0 ]; then
  66. _build_darwin
  67. _sign_darwin
  68. _build_macapp
  69. exit 0
  70. fi
  71. for CMD in "$@"; do
  72. case $CMD in
  73. build) _build_darwin ;;
  74. sign) _sign_darwin ;;
  75. macapp) _build_macapp ;;
  76. *) usage ;;
  77. esac
  78. done