Path: blob/master/Utilities/Release/macos/sign-notarize.bash
5025 views
#!/usr/bin/env bash1set -e2readonly usage='usage: sign-notarize.bash <options>... [--] <package>.dmg34Sign and notarize the "CMake.app" bundle inside the given "<package>.dmg" disk image.5Also produce a "<package>.tar.gz" tarball containing the same "CMake.app".67Options:89-id <id> Signing Identity10-kp <keychain-profile> Keychain profile containing notarization credentials.11-kc <keychain-path> Keychain path containing named profile.1213Create the keychain profile ahead of time using1415xcrun notarytool store-credentials <keychain-profile> \16--apple-id <dev-acct> --team-id <team-id> \17[--keychain <keychain-path>] [--password <app-specific-password>]1819where:2021<dev-acct> is an Apple ID of a developer account22<team-id> is from https://developer.apple.com/account/#!/membership23<app-specific-password> is generated via https://support.apple.com/en-us/HT20439724If --password is omitted, notarytool will prompt for it.2526This creates a keychain item called "com.apple.gke.notary.tool" with an27account name "com.apple.gke.notary.tool.saved-creds.<keychain-profile>".28'2930cleanup() {31if test -d "$tmpdir"; then32rm -rf "$tmpdir"33fi34if test -d "$vol_path"; then35hdiutil detach "$vol_path"36fi37}3839trap "cleanup" EXIT4041die() {42echo "$@" 1>&2; exit 143}4445id=''46keychain=''47keychain_profile=''48while test "$#" != 0; do49case "$1" in50-id|-i) shift; id="$1" ;;51-kc|--keychain) shift; keychain="$1" ;;52-kp|-k|--keychain-profile) shift; keychain_profile="$1" ;;53--) shift ; break ;;54-*) die "$usage" ;;55*) break ;;56esac57shift58done59case "$1" in60*.dmg) readonly dmg="$1"; shift ;;61*) die "$usage" ;;62esac63test "$#" = 0 || die "$usage"6465# Verify environment.66if ! xcrun --find notarytool 2>/dev/null; then67die "'xcrun notarytool' not found"68fi6970# If a signing identity is not provided on the command-line,71# check for a GitLab CI variable in the environment, and then72# fall back to finding one automatically.73if test -z "$id" -a -n "$CODESIGN_IDENTITY"; then74id="$CODESIGN_IDENTITY"75elif test -z "$id" && found_id="$(security find-identity -v -p codesigning 2>/dev/null | grep -E -m 1 -o '\<[0-9A-F]{40}\>')"; then76id="$found_id"77else78echo "No codesigning identity detected." 1>&279fi8081# If a keychain path/profile is not provided on the command-line,82# check for a GitLab CI variable in the environment.83if test -z "$keychain" -a -n "$NOTARYTOOL_KEYCHAIN"; then84keychain="$NOTARYTOOL_KEYCHAIN"85fi86if test -z "$keychain_profile" -a -n "$NOTARYTOOL_KEYCHAIN_PROFILE"; then87keychain_profile="$NOTARYTOOL_KEYCHAIN_PROFILE"88fi8990# Verify arguments.91if test -z "$id" -o -z "$keychain_profile"; then92die "$usage"93fi9495readonly tmpdir="$(mktemp -d)"9697# Prepare entitlements.98readonly entitlements_xml="$tmpdir/entitlements.xml"99echo '<?xml version="1.0" encoding="UTF-8"?>100<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">101<plist version="1.0">102<dict>103<key>com.apple.security.cs.allow-dyld-environment-variables</key>104<true/>105</dict>106</plist>' > "$entitlements_xml"107108# Convert from read-only original image to read-write.109readonly udrw_dmg="$tmpdir/udrw.dmg"110hdiutil convert "$dmg" -format UDRW -o "${udrw_dmg}"111112# Mount the temporary udrw image.113readonly vol_name="$(basename "${dmg%.dmg}")"114readonly vol_path="/Volumes/$vol_name"115hdiutil attach "${udrw_dmg}"116117# Sign the application.118codesign --verify --timestamp --options=runtime --verbose --force \119-s "$id" \120--entitlements "$entitlements_xml" \121"$vol_path"/CMake.app/Contents/bin/cmake \122"$vol_path"/CMake.app/Contents/bin/ccmake \123"$vol_path"/CMake.app/Contents/bin/ctest \124"$vol_path"/CMake.app/Contents/bin/cpack \125"$vol_path"/CMake.app/Contents/Frameworks/*.framework/Versions/[A56]/Qt* \126"$vol_path"/CMake.app/Contents/PlugIns/*/lib*.dylib \127"$vol_path"/CMake.app128129# Prepare an application archive.130ditto -c -k --keepParent "$vol_path/CMake.app" "$tmpdir/CMake.app.zip"131132# Notarize the application.133notarize="xcrun notarytool submit '$tmpdir/CMake.app.zip'"134if test -n "$keychain_profile"; then135notarize="$notarize --keychain-profile '$keychain_profile'"136if test -n "$keychain"; then137notarize="$notarize --keychain '$keychain'"138fi139fi140notarize="$notarize --wait"141eval "$notarize"142143# Staple the notarization.144xcrun stapler staple "$vol_path/CMake.app"145146# Create a tarball of the volume next to the original disk image.147readonly tar_gz="${dmg/%.dmg/.tar.gz}"148tar cvzf "$tar_gz" -C /Volumes "$vol_name/CMake.app"149150# Unmount the modified udrw image.151hdiutil detach "$vol_path"152153# Convert back to read-only, compressed image.154hdiutil convert "${udrw_dmg}" -format UDZO -imagekey zlib-level=9 -ov -o "$dmg"155156157