Path: blob/master/Utilities/Release/macos/sign-notarize.bash
3153 views
#!/usr/bin/env bash1set -e2readonly usage='usage: sign-notarize.bash -i <id> -k <keychain-profile> [--] <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-i <id> Signing Identity10-k <keychain-profile> Keychain profile containing stored credentials1112Create the keychain profile ahead of time using1314xcrun notarytool store-credentials <keychain-profile> \15--apple-id <dev-acct> --team-id <team-id> [--password <app-specific-password>]1617where:1819<dev-acct> is an Apple ID of a developer account20<team-id> is from https://developer.apple.com/account/#!/membership21<app-specific-password> is generated via https://support.apple.com/en-us/HT20439722If --password is omitted, notarytool will prompt for it.2324This creates a keychain item called "com.apple.gke.notary.tool" with an25account name "com.apple.gke.notary.tool.saved-creds.<keychain-profile>".26'2728cleanup() {29if test -d "$tmpdir"; then30rm -rf "$tmpdir"31fi32if test -d "$vol_path"; then33hdiutil detach "$vol_path"34fi35}3637trap "cleanup" EXIT3839die() {40echo "$@" 1>&2; exit 141}4243id=''44keychain_profile=''45while test "$#" != 0; do46case "$1" in47-i) shift; id="$1" ;;48-k) shift; keychain_profile="$1" ;;49--) shift ; break ;;50-*) die "$usage" ;;51*) break ;;52esac53shift54done55case "$1" in56*.dmg) readonly dmg="$1"; shift ;;57*) die "$usage" ;;58esac59test "$#" = 0 || die "$usage"6061# Verify arguments.62if test -z "$id" -o -z "$keychain_profile"; then63die "$usage"64fi6566# Verify environment.67if ! xcrun --find notarytool 2>/dev/null; then68die "'xcrun notarytool' not found"69fi7071readonly tmpdir="$(mktemp -d)"7273# Prepare entitlements.74readonly entitlements_xml="$tmpdir/entitlements.xml"75echo '<?xml version="1.0" encoding="UTF-8"?>76<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">77<plist version="1.0">78<dict>79<key>com.apple.security.cs.allow-dyld-environment-variables</key>80<true/>81</dict>82</plist>' > "$entitlements_xml"8384# Convert from read-only original image to read-write.85readonly udrw_dmg="$tmpdir/udrw.dmg"86hdiutil convert "$dmg" -format UDRW -o "${udrw_dmg}"8788# Mount the temporary udrw image.89readonly vol_name="$(basename "${dmg%.dmg}")"90readonly vol_path="/Volumes/$vol_name"91hdiutil attach "${udrw_dmg}"9293codesign --verify --timestamp --options=runtime --verbose --deep \94-s "$id" \95--entitlements "$entitlements_xml" \96"$vol_path/CMake.app/Contents/bin/cmake" \97"$vol_path/CMake.app/Contents/bin/ccmake" \98"$vol_path/CMake.app/Contents/bin/ctest" \99"$vol_path/CMake.app/Contents/bin/cpack" \100"$vol_path/CMake.app"101102ditto -c -k --keepParent "$vol_path/CMake.app" "$tmpdir/CMake.app.zip"103xcrun notarytool submit "$tmpdir/CMake.app.zip" --keychain-profile "$keychain_profile" --wait104xcrun stapler staple "$vol_path/CMake.app"105106# Create a tarball of the volume next to the original disk image.107readonly tar_gz="${dmg/%.dmg/.tar.gz}"108tar cvzf "$tar_gz" -C /Volumes "$vol_name/CMake.app"109110# Unmount the modified udrw image.111hdiutil detach "$vol_path"112113# Convert back to read-only, compressed image.114hdiutil convert "${udrw_dmg}" -format UDZO -imagekey zlib-level=9 -ov -o "$dmg"115116117