Repository navigation
Expand file tree
/
Copy pathpackage.sh
More file actions
executable file
·403 lines (374 loc) · 18.3 KB
/
Copy pathpackage.sh
File metadata and controls
executable file
·403 lines (374 loc) · 18.3 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
# Turns an engine into Clair.app (change 1 design D3, D4, D5): embeds
# ClairUI.framework and Sparkle.framework, writes the Clair version, the build
# number and Sparkle's keys into Info.plist, signs every nested bundle inside
# out without --deep, builds the DMG with its window layout (resources/dmg/), and
# notarizes and staples both when it's given an App Store Connect API key.
#
# package.sh [options] <engine> <out-dir>
#
# <engine> is the engine's Clair.app, or its zip from chromium-engine-<key>.
# <out-dir> receives Clair.app and Clair-<version>-arm64.dmg; it must not hold
# a Clair.app already.
#
# Options:
# --clairui <dir> a built ClairUI.framework (default: build it from
# native/ClairUI.xcodeproj, Release)
# --sparkle <dir> a directory from scripts/fetch-sparkle.sh (default:
# fetch into build/sparkle)
# --dmgbuild <path> dmgbuild, as scripts/fetch-dmgbuild.sh prints it
# (default: install into build/dmgbuild)
# --version <x.y.z> the Clair version (default: version.txt)
# --build <n> the build number (default: git rev-list --count HEAD)
# --public-key <file> the file holding Sparkle's public EdDSA key (default:
# .sparkle/public-ed-key.txt). Without one, the package
# has no SUPublicEDKey and Sparkle installs no update.
# --identity <name> the Developer ID Application identity to sign with,
# from the keychain search list. Without it, the app is
# signed ad hoc and marked non-official (ClairUI's test
# overrides work only there).
# --keychain <file> look for the identity only in this keychain. A release
# needs this, since the same certificate is in the owner's
# login keychain and codesign refuses an ambiguous name.
# --notary-key <file> the App Store Connect API key's .p8. Together with the
# --notary-key-id <id> next two, this notarizes and staples the app, and then
# --notary-issuer <id> the DMG built from it. Needs --identity.
# --no-dmg stop after signing the app (and notarizing it)
set -euo pipefail
root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
bundle_id="com.clairbrowser.clair"
feed_url="https://github.com/clairbrowser/clair/raw/main/.sparkle/appcast.xml"
product="Clair"
die() {
echo "package: $1" >&2
exit 1
}
usage() {
sed -n '/^# package.sh/,/^$/p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//' >&2
exit 2
}
clairui="" sparkle="" dmgbuild="" version="" build="" public_key_file="$root/.sparkle/public-ed-key.txt"
identity="" keychain="" dmg=true notary_key="" notary_key_id="" notary_issuer=""
args=()
while (( $# > 0 )); do
case "$1" in
--clairui) clairui="${2-}"; shift 2 || usage ;;
--sparkle) sparkle="${2-}"; shift 2 || usage ;;
--dmgbuild) dmgbuild="${2-}"; shift 2 || usage ;;
--version) version="${2-}"; shift 2 || usage ;;
--build) build="${2-}"; shift 2 || usage ;;
--public-key) public_key_file="${2-}"; shift 2 || usage ;;
--identity) identity="${2-}"; shift 2 || usage ;;
--keychain) keychain="${2-}"; shift 2 || usage ;;
--notary-key) notary_key="${2-}"; shift 2 || usage ;;
--notary-key-id) notary_key_id="${2-}"; shift 2 || usage ;;
--notary-issuer) notary_issuer="${2-}"; shift 2 || usage ;;
--no-dmg) dmg=false; shift ;;
-h|--help) usage ;;
-*) die "unknown option: $1" ;;
*) args+=("$1"); shift ;;
esac
done
(( ${#args[@]} == 2 )) || usage
engine="${args[0]%/}"
out="${args[1]%/}"
# --- inputs ---------------------------------------------------------------------
if [[ -z "$version" ]]; then
[[ -f "$root/version.txt" ]] || die "no --version and no version.txt"
version="$(cat "$root/version.txt")"
fi
[[ "$version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]] || die "the version must be X.Y.Z: $version"
if [[ -z "$build" ]]; then
build="$(git -C "$root" rev-list --count HEAD)" || die "can't count commits for the build number; pass --build"
fi
[[ "$build" =~ ^[1-9][0-9]*$ ]] || die "the build number must be a positive integer: $build"
public_key=""
if [[ -f "$public_key_file" ]]; then
public_key="$(tr -d '[:space:]' <"$public_key_file")"
# An Ed25519 public key: 32 bytes, base64.
[[ "$public_key" =~ ^[A-Za-z0-9+/]{43}=$ ]] || die "$public_key_file doesn't hold a base64 Ed25519 public key"
else
echo "package: warning: no Sparkle public key at $public_key_file; this package can't install updates" >&2
fi
keychain_args=()
if [[ -n "$keychain" ]]; then
[[ -n "$identity" ]] || die "--keychain needs --identity"
[[ -f "$keychain" ]] || die "no keychain at $keychain"
keychain_args=(--keychain "$keychain")
fi
if [[ -n "$identity" ]]; then
security find-identity -v -p codesigning ${keychain:+"$keychain"} | grep -qF "\"$identity\"" ||
die "no valid signing identity named \"$identity\" in ${keychain:-the keychain search list}"
fi
notarizing=false
if [[ -n "$notary_key$notary_key_id$notary_issuer" ]]; then
[[ -n "$notary_key" && -n "$notary_key_id" && -n "$notary_issuer" ]] ||
die "notarizing needs --notary-key, --notary-key-id and --notary-issuer together"
[[ -n "$identity" ]] || die "notarizing needs --identity: Apple refuses an ad hoc signature"
[[ -f "$notary_key" ]] || die "no App Store Connect API key at $notary_key"
[[ "$notary_key_id" =~ ^[A-Z0-9]{10}$ ]] || die "the notary key ID must be 10 letters and digits: $notary_key_id"
[[ "$notary_issuer" =~ ^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$ ]] ||
die "the notary issuer must be a UUID: $notary_issuer"
notarizing=true
fi
if $dmg; then
if [[ -z "$dmgbuild" ]]; then
dmgbuild="$("$root/scripts/fetch-dmgbuild.sh" "$root/build/dmgbuild")" || die "couldn't install dmgbuild"
fi
[[ -f "$dmgbuild" && -x "$dmgbuild" ]] || die "no dmgbuild at $dmgbuild"
fi
[[ -e "$engine" ]] || die "no engine at $engine"
[[ ! -e "$out/$product.app" ]] || die "$out/$product.app already exists"
mkdir -p "$out"
out="$(cd "$out" && pwd -P)"
work="$(mktemp -d "${TMPDIR:-/tmp}/clair-package.XXXXXX")"
trap 'rm -rf "$work"' EXIT
# --- the engine -------------------------------------------------------------------
app="$work/$product.app"
if [[ -d "$engine" ]]; then
[[ "$(basename "$engine")" == "$product.app" ]] || die "the engine app must be named $product.app: $engine"
ditto "$engine" "$app"
else
ditto -x -k "$engine" "$work/engine" || die "can't unpack $engine"
[[ -d "$work/engine/$product.app" ]] || die "$engine holds no $product.app at its top level"
mv "$work/engine/$product.app" "$app"
fi
info="$app/Contents/Info.plist"
[[ "$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$info" 2>/dev/null)" == "$bundle_id" ]] ||
die "the engine's bundle identifier is not $bundle_id"
frameworks="$app/Contents/Frameworks"
chromium_framework="$frameworks/$product Framework.framework"
[[ -d "$chromium_framework" ]] || die "the engine has no $product Framework.framework"
for embedded in ClairUI.framework Sparkle.framework; do
[[ ! -e "$frameworks/$embedded" ]] || die "the engine already contains $embedded"
done
# Extended attributes, such as quarantine, would break the signatures.
xattr -cr "$app"
# The app's icon is also the DMG's volume icon.
icon_file="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIconFile' "$info" 2>/dev/null)" ||
die "the engine's Info.plist has no CFBundleIconFile"
[[ "$icon_file" == *.icns ]] || icon_file="$icon_file.icns"
[[ "$icon_file" != */* && -f "$app/Contents/Resources/$icon_file" ]] ||
die "the engine has no icon at Contents/Resources/$icon_file"
# --- ClairUI ------------------------------------------------------------------------
if [[ -z "$clairui" ]]; then
echo "package: building ClairUI" >&2
xcodebuild -project "$root/native/ClairUI.xcodeproj" -scheme ClairUI \
-configuration Release -destination "platform=macOS,arch=arm64" \
-derivedDataPath "$work/DerivedData" CODE_SIGNING_ALLOWED=NO -quiet build >&2 ||
die "ClairUI didn't build"
clairui="$work/DerivedData/Build/Products/Release/ClairUI.framework"
fi
clairui="${clairui%/}"
[[ -f "$clairui/Resources/Info.plist" && "$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$clairui/Resources/Info.plist" 2>/dev/null)" == "$bundle_id.ClairUI" ]] ||
die "not a ClairUI.framework: $clairui"
ditto "$clairui" "$frameworks/ClairUI.framework"
# --- Sparkle ------------------------------------------------------------------------
if [[ -z "$sparkle" ]]; then
sparkle="$("$root/scripts/fetch-sparkle.sh" "$root/build/sparkle")"
fi
sparkle="${sparkle%/}"
[[ -f "$sparkle/.sparkle-release" && -d "$sparkle/Sparkle.framework" ]] ||
die "not a directory from fetch-sparkle.sh: $sparkle"
ditto "$sparkle/Sparkle.framework" "$frameworks/Sparkle.framework"
# Clair isn't sandboxed, so Sparkle needs no XPC services (Sparkle's
# documentation); leaving them out leaves less to sign.
rm -rf "$frameworks/Sparkle.framework/Versions/B/XPCServices" "$frameworks/Sparkle.framework/XPCServices"
# Sparkle ships universal binaries; Clair is arm64 only.
while IFS= read -r -d '' file; do
archs="$(lipo -archs "$file" 2>/dev/null)" || continue
if [[ "$archs" != arm64 ]]; then
[[ " $archs " == *" arm64 "* ]] || die "${file#"$work/"} has no arm64 code"
lipo -thin arm64 -output "$file.arm64" "$file"
mv "$file.arm64" "$file"
fi
done < <(find "$frameworks/Sparkle.framework/Versions/B" -type f -print0)
# --- Info.plist ---------------------------------------------------------------------
plist_set() {
local key="$1" type="$2" value="$3"
/usr/libexec/PlistBuddy -c "Delete :$key" "$info" >/dev/null 2>&1 || true
/usr/libexec/PlistBuddy -c "Add :$key $type $value" "$info"
}
plist_set CFBundleShortVersionString string "$version"
plist_set CFBundleVersion string "$build"
plist_set SUFeedURL string "$feed_url"
plist_set SUEnableAutomaticChecks bool true
plist_set SUAutomaticallyUpdate bool true
plist_set SUScheduledCheckInterval integer 86400
if [[ -n "$public_key" ]]; then
plist_set SUPublicEDKey string "$public_key"
fi
# Only ClairUI's test overrides read this, so an ad hoc package can use a local
# appcast. A release package doesn't have the key, and its signature keeps it
# from being added.
if [[ -z "$identity" ]]; then
plist_set ClairNonOfficialPackage bool true
fi
# The type ClairUI's sidebar rows and tiles carry while dragged
# (SidebarDragItem.type), declared as an exported type must be, as text: a
# list row dragged with a type that's only data carries a file promise, which
# drops outside the list, such as on the favorites, don't accept.
declarations=UTExportedTypeDeclarations
/usr/libexec/PlistBuddy -c "Print :$declarations" "$info" >/dev/null 2>&1 ||
/usr/libexec/PlistBuddy -c "Add :$declarations array" "$info"
/usr/libexec/PlistBuddy \
-c "Add :$declarations:0 dict" \
-c "Add :$declarations:0:UTTypeIdentifier string com.clairbrowser.clair.sidebar-item" \
-c "Add :$declarations:0:UTTypeDescription string Clair sidebar item" \
-c "Add :$declarations:0:UTTypeConformsTo array" \
-c "Add :$declarations:0:UTTypeConformsTo:0 string public.utf8-plain-text" \
"$info"
# --- signing ------------------------------------------------------------------------
# Inside out, each bundle with its own identifier (see ungoogled-chromium-macos
# issue 361), never with --deep. Ported from ungoogled-chromium-macos's
# sign_and_package_app.sh (BSD-3-Clause, see CREDITS.md).
sign() {
if [[ -n "$identity" ]]; then
codesign --sign "$identity" ${keychain_args[@]+"${keychain_args[@]}"} --force --timestamp "$@"
return
fi
# An ad hoc signature has no Team ID, so the library validation that the
# hardened runtime turns on would refuse every framework, Chromium's own
# included. Ad hoc packages, which only tests use, run without it.
local args=()
while (( $# > 0 )); do
if [[ "$1" == --options ]]; then
shift 2
continue
fi
args+=("$1")
shift
done
codesign --sign - --force "${args[@]}"
}
entitlements="$root/resources/entitlements"
helpers="$chromium_framework/Helpers"
libraries="$chromium_framework/Libraries"
hardened=(--options "restrict,library,runtime,kill")
sign --identifier chrome_crashpad_handler "${hardened[@]}" "$helpers/chrome_crashpad_handler"
# The Aperitif helpers come in the same four kinds, when the engine has them.
sign_helpers() {
local main="$1" kind="$2"
local base="$helpers/$product Helper"
sign --identifier "$bundle_id.helper" "${hardened[@]}" "$main"
sign --identifier "$bundle_id.helper.renderer" --options restrict,kill,runtime \
--entitlements "$entitlements/helper-renderer-entitlements.plist" "$base (${kind}Renderer).app"
sign --identifier "$bundle_id.helper" --options restrict,kill,runtime \
--entitlements "$entitlements/helper-gpu-entitlements.plist" "$base (${kind}GPU).app"
sign --identifier "$bundle_id.framework.AlertNotificationService" "${hardened[@]}" "$base (${kind}Alerts).app"
}
sign_helpers "$helpers/$product Helper.app" ""
if [[ -d "$helpers/$product Helper (Aperitif).app" ]]; then
sign_helpers "$helpers/$product Helper (Aperitif).app" "Aperitif "
fi
sign --identifier app_mode_loader "${hardened[@]}" "$helpers/app_mode_loader"
sign --identifier web_app_shortcut_copier "${hardened[@]}" "$helpers/web_app_shortcut_copier"
for dylib in "$libraries"/*.dylib; do
[[ -e "$dylib" ]] || continue
sign --identifier "$(basename "$dylib" .dylib)" "$dylib"
done
sign --identifier "$bundle_id.framework" "$chromium_framework"
sparkle_framework="$frameworks/Sparkle.framework"
sign --options runtime "$sparkle_framework/Versions/B/Autoupdate"
sign --options runtime "$sparkle_framework/Versions/B/Updater.app"
sign --options runtime "$sparkle_framework"
sign --options runtime "$frameworks/ClairUI.framework"
if [[ -n "$identity" ]]; then
sign --identifier "$bundle_id" "${hardened[@]}" --entitlements "$entitlements/app-entitlements.plist" \
--requirements "=designated => identifier \"$bundle_id\" and anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */" \
"$app"
else
sign --identifier "$bundle_id" "${hardened[@]}" --entitlements "$entitlements/app-entitlements.plist" "$app"
fi
# --- verification -----------------------------------------------------------------
nested=(
"$frameworks/ClairUI.framework"
"$sparkle_framework/Versions/B/Autoupdate"
"$sparkle_framework/Versions/B/Updater.app"
"$sparkle_framework"
"$chromium_framework"
"$helpers"/*.app
"$app"
)
for code in "${nested[@]}"; do
codesign --verify --strict "$code" || die "the signature of ${code#"$work/"} doesn't verify"
done
# Every bundle is signed as its own bundle identifier.
for bundle in "$app" "$helpers"/*.app "$frameworks/ClairUI.framework" "$chromium_framework"; do
plist="$bundle/Contents/Info.plist"
[[ -f "$plist" ]] || plist="$bundle/Resources/Info.plist"
expected="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$plist")"
signed="$(codesign -dv "$bundle" 2>&1 | sed -n 's/^Identifier=//p')"
[[ "$signed" == "$expected" ]] || die "${bundle#"$work/"} is signed as $signed, not $expected"
done
mv "$app" "$out/$product.app"
echo "package: $out/$product.app ($version, build $build, $([[ -n "$identity" ]] && echo "signed by $identity" || echo "signed ad hoc"))"
# --- notarization -----------------------------------------------------------------
# The key goes to notarytool as a file and IDs, never as a keychain profile, so
# nothing stays on the Mac (design D11).
notary_auth=(--key "$notary_key" --key-id "$notary_key_id" --issuer "$notary_issuer")
# Waits for Apple's verdict on SUBMISSION, and shows Apple's log if it refuses.
# The verdict comes from the result itself rather than notarytool's exit status,
# so that anything other than "Accepted" stops the package.
notarize() {
local submission="$1" name result status id
name="$(basename "$submission")"
result="$work/notary-$name.json"
echo "package: notarizing $name" >&2
xcrun notarytool submit "$submission" "${notary_auth[@]}" --wait --timeout 1h --output-format json >"$result" || true
status="$(plutil -extract status raw -o - "$result" 2>/dev/null || true)"
if [[ "$status" != Accepted ]]; then
id="$(plutil -extract id raw -o - "$result" 2>/dev/null || true)"
if [[ -n "$id" ]]; then
xcrun notarytool log "$id" "${notary_auth[@]}" >&2 || true
fi
die "Apple didn't accept $name (status: ${status:-no result})"
fi
}
staple() {
xcrun stapler staple "$1" >&2 || die "couldn't staple $(basename "$1")"
xcrun stapler validate "$1" >&2 || die "the ticket stapled to $(basename "$1") doesn't validate"
}
if $notarizing; then
# Apple takes an app only as an archive; the ticket goes on the app itself.
ditto -c -k --keepParent "$out/$product.app" "$work/$product.zip"
notarize "$work/$product.zip"
staple "$out/$product.app"
echo "package: notarized and stapled $product.app"
fi
# --- DMG --------------------------------------------------------------------------
if $dmg; then
dmg_path="$out/$product-$version-arm64.dmg"
rm -f "$dmg_path"
# dmgbuild copies the app, makes the Applications link and writes the window
# layout (style-release-dmg design D4). On a hosted runner, for about a
# minute after the job's first xcodebuild, simdiskimaged keeps retrying an
# unmount of a simulator runtime by its disk number. A DMG attached then can
# reuse that number and be unmounted under dmgbuild ("No such file or
# directory: '/Volumes/Clair/...'"). Waiting 30 seconds between attempts
# gets past that window.
layout="$root/resources/dmg"
created=false
for attempt in 1 2 3; do
if "$dmgbuild" --no-hidpi -s "$layout/settings.py" \
-D app="$out/$product.app" -D icon="$out/$product.app/Contents/Resources/$icon_file" \
-D background="$layout/background.tiff" -D layout="$layout/layout.json" \
"$product" "$dmg_path" >/dev/null; then
created=true
break
fi
echo "package: dmgbuild failed (attempt $attempt of 3)" >&2
rm -f "$dmg_path"
[[ "$attempt" == 3 ]] || sleep 30
done
$created || die "dmgbuild couldn't create the DMG"
if [[ -n "$identity" ]]; then
codesign --sign "$identity" ${keychain_args[@]+"${keychain_args[@]}"} --timestamp "$dmg_path"
fi
if $notarizing; then
notarize "$dmg_path"
staple "$dmg_path"
echo "package: notarized and stapled $(basename "$dmg_path")"
fi
echo "package: $dmg_path"
fi