Ariver
2026-06-03 7bedb180b762fb8f5378db45865cd06f77fbc37d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
#
# build-appstore-developerid-macos.sh - Build a distributable App Sandbox POC DMG.
#
# This builds the appstore-tagged sandbox variant, signs it with Developer ID,
# submits the DMG for notarization, and staples the notarization ticket. Use this
# for cross-machine Sandbox POC testing before moving to App Store Connect.
#
# Prerequisites:
#   1. Install the Developer ID Application certificate in Keychain.
#   2. Store notarization credentials, for example:
#      xcrun notarytool store-credentials "privatevoice" \
#        --apple-id "your@apple.id" --team-id "3HY4G7M2AL" \
#        --password "app-specific-password"
#
# Usage:
#   cd VoiceSnapGo
#   ./scripts/build-appstore-developerid-macos.sh [arm64|x86_64|all]
#
set -euo pipefail
 
APP_NAME="PrivateVoice Input"
DEVELOPER_IDENTITY="${PRIVATEVOICE_DEVELOPER_IDENTITY:-Developer ID Application: Ningbo Zhuozhi Innovation Network Technology Co., Ltd. (3HY4G7M2AL)}"
KEYCHAIN_PROFILE="${PRIVATEVOICE_NOTARY_PROFILE:-privatevoice}"
SIGNING_KEYCHAIN="${PRIVATEVOICE_SIGNING_KEYCHAIN:-}"
APPSTORE_MIN_SYSTEM_VERSION="${PRIVATEVOICE_APPSTORE_MIN_SYSTEM_VERSION:-13.4}"
 
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
 
INFO_PLIST="$PROJECT_DIR/build/darwin/Info.plist"
ENTITLEMENTS="$PROJECT_DIR/build/darwin/entitlements-appstore.plist"
GOMODCACHE="$(go env GOMODCACHE)"
 
VERSION="$(defaults read "$INFO_PLIST" CFBundleShortVersionString 2>/dev/null || echo "2.1.12")"
BUILD_ID="$(defaults read "$INFO_PLIST" CFBundleVersion 2>/dev/null || date +%Y%m%d.%H%M)"
 
step() {
  echo ""
  echo "==> $1"
}
 
fail() {
  echo "ERROR: $1" >&2
  exit 1
}
 
security_find_identities() {
  if [[ -n "$SIGNING_KEYCHAIN" ]]; then
    security find-identity -v -p codesigning "$SIGNING_KEYCHAIN"
  else
    security find-identity -v -p codesigning
  fi
}
 
resolve_arch() {
  local target="$1"
  case "$target" in
    arm64)
      GOARCH="arm64"
      SHERPA_ARCH="aarch64-apple-darwin"
      OUT_ARCH="arm64"
      ;;
    x86_64|amd64)
      GOARCH="amd64"
      SHERPA_ARCH="x86_64-apple-darwin"
      OUT_ARCH="x86_64"
      ;;
    *)
      fail "Unknown architecture: $target (use arm64, x86_64, or all)"
      ;;
  esac
 
  BUILD_DIR="$PROJECT_DIR/build/appstore-developerid/$OUT_ARCH"
  APP_BUNDLE="$BUILD_DIR/$APP_NAME.app"
  DMG_PATH="$BUILD_DIR/$APP_NAME-$VERSION-build$BUILD_ID-$OUT_ARCH-developerid-sandbox-poc.dmg"
  SHERPA_LIB_DIR="$GOMODCACHE/github.com/k2-fsa/sherpa-onnx-go-macos@v1.12.24/lib/$SHERPA_ARCH"
}
 
sign_plain() {
  local item="$1"
  codesign --force \
    --options runtime \
    --timestamp \
    --sign "$DEVELOPER_IDENTITY" \
    "$item"
}
 
sign_sandboxed() {
  local item="$1"
  codesign --force \
    --options runtime \
    --timestamp \
    --entitlements "$ENTITLEMENTS" \
    --sign "$DEVELOPER_IDENTITY" \
    "$item"
}
 
build_one_arch() {
  local target="$1"
  resolve_arch "$target"
 
  step "Preflight checks ($OUT_ARCH)"
  [[ -f "$INFO_PLIST" ]] || fail "Info.plist not found at $INFO_PLIST"
  [[ -f "$ENTITLEMENTS" ]] || fail "App Store entitlements not found at $ENTITLEMENTS"
  [[ -f "$PROJECT_DIR/build/darwin/icon.icns" ]] || fail "icon.icns not found"
  [[ -d "$SHERPA_LIB_DIR" ]] || fail "sherpa-onnx dylibs not found at $SHERPA_LIB_DIR"
  command -v xcrun >/dev/null || fail "xcrun not found"
  security_find_identities | grep -Fq "$DEVELOPER_IDENTITY" ||
    fail "Developer ID signing identity not found: $DEVELOPER_IDENTITY"
 
  if [[ "${PRIVATEVOICE_SKIP_NOTARY_PROFILE_CHECK:-0}" != "1" ]]; then
    xcrun notarytool history --keychain-profile "$KEYCHAIN_PROFILE" >/dev/null 2>&1 ||
      fail "Notary keychain profile is not usable: $KEYCHAIN_PROFILE"
  fi
 
  step "Cleaning old Developer ID sandbox build"
  rm -rf "$APP_BUNDLE" "$DMG_PATH"
  mkdir -p "$BUILD_DIR"
 
  step "Building frontend"
  cd "$PROJECT_DIR/frontend"
  if [[ ! -d node_modules ]]; then
    npm ci --prefer-offline
  fi
  npm run build
 
  step "Building Go binary ($OUT_ARCH, appstore tag)"
  cd "$PROJECT_DIR"
  CGO_ENABLED=1 GOOS=darwin GOARCH="$GOARCH" \
    MACOSX_DEPLOYMENT_TARGET="$APPSTORE_MIN_SYSTEM_VERSION" \
    CGO_CFLAGS="${CGO_CFLAGS:-} -mmacosx-version-min=$APPSTORE_MIN_SYSTEM_VERSION" \
    CGO_LDFLAGS="${CGO_LDFLAGS:-} -mmacosx-version-min=$APPSTORE_MIN_SYSTEM_VERSION" \
    go build -tags appstore -buildvcs=false -gcflags=all="-l" -ldflags="-s -w" \
    -o "$BUILD_DIR/$APP_NAME"
 
  step "Assembling $APP_NAME.app"
  mkdir -p "$APP_BUNDLE/Contents/MacOS"
  mkdir -p "$APP_BUNDLE/Contents/Frameworks"
  mkdir -p "$APP_BUNDLE/Contents/Resources"
 
  cp "$INFO_PLIST" "$APP_BUNDLE/Contents/Info.plist"
  /usr/libexec/PlistBuddy -c "Set :LSMinimumSystemVersion $APPSTORE_MIN_SYSTEM_VERSION" \
    "$APP_BUNDLE/Contents/Info.plist"
  cp "$BUILD_DIR/$APP_NAME" "$APP_BUNDLE/Contents/MacOS/$APP_NAME"
  cp "$PROJECT_DIR/build/darwin/icon.icns" "$APP_BUNDLE/Contents/Resources/icon.icns"
  cp "$SHERPA_LIB_DIR/libsherpa-onnx-c-api.dylib" "$APP_BUNDLE/Contents/Frameworks/"
  cp "$SHERPA_LIB_DIR/libonnxruntime.1.23.2.dylib" "$APP_BUNDLE/Contents/Frameworks/"
 
  step "Fixing rpaths"
  install_name_tool -add_rpath @executable_path/../Frameworks \
    "$APP_BUNDLE/Contents/MacOS/$APP_NAME" 2>/dev/null || true
  install_name_tool -id @rpath/libsherpa-onnx-c-api.dylib \
    "$APP_BUNDLE/Contents/Frameworks/libsherpa-onnx-c-api.dylib"
  install_name_tool -id @rpath/libonnxruntime.1.23.2.dylib \
    "$APP_BUNDLE/Contents/Frameworks/libonnxruntime.1.23.2.dylib"
 
  step "Code signing with Developer ID"
  sign_plain "$APP_BUNDLE/Contents/Frameworks/libonnxruntime.1.23.2.dylib"
  sign_plain "$APP_BUNDLE/Contents/Frameworks/libsherpa-onnx-c-api.dylib"
  sign_sandboxed "$APP_BUNDLE/Contents/MacOS/$APP_NAME"
  sign_sandboxed "$APP_BUNDLE"
 
  step "Verifying signature and entitlements"
  codesign -vvv --deep --strict "$APP_BUNDLE"
  codesign -d --entitlements :- "$APP_BUNDLE"
 
  step "Creating DMG"
  local dmg_temp="$BUILD_DIR/dmg-staging"
  rm -rf "$dmg_temp"
  mkdir -p "$dmg_temp"
  cp -R "$APP_BUNDLE" "$dmg_temp/"
  ln -s /Applications "$dmg_temp/Applications"
  hdiutil create -volname "$APP_NAME Sandbox POC" \
    -srcfolder "$dmg_temp" \
    -ov -format UDZO \
    "$DMG_PATH"
  rm -rf "$dmg_temp"
 
  step "Signing DMG"
  codesign --force --timestamp --sign "$DEVELOPER_IDENTITY" "$DMG_PATH"
 
  step "Submitting DMG for notarization"
  xcrun notarytool submit "$DMG_PATH" \
    --keychain-profile "$KEYCHAIN_PROFILE" \
    --wait
 
  step "Stapling notarization ticket"
  xcrun stapler staple "$DMG_PATH"
  xcrun stapler validate "$DMG_PATH"
  spctl -a -vvv -t open "$DMG_PATH"
 
  step "Developer ID sandbox POC build complete"
  echo "  App: $APP_BUNDLE"
  echo "  DMG: $DMG_PATH"
  echo "  Signing identity: $DEVELOPER_IDENTITY"
  echo "  Notary profile: $KEYCHAIN_PROFILE"
}
 
TARGET_ARCH="${1:-$(uname -m)}"
if [[ "$TARGET_ARCH" == "all" ]]; then
  build_one_arch arm64
  build_one_arch x86_64
else
  build_one_arch "$TARGET_ARCH"
fi