#!/bin/bash set -euo pipefail cd "$(dirname "$0")/.." source Scripts/product.env source Scripts/version.sh source Scripts/packaging-paths.sh if [[ $# -gt 2 || ( ${1:-debug} != debug && ${1:-debug} != release ) ]]; then echo "Usage: $0 [debug|release] [--inspectable|--reality (debug only) | --preview (release only)]" >&2 exit 2 fi if [[ $# -eq 2 ]]; then case "$1:$2" in debug:--inspectable|debug:--reality|release:--preview) ;; *) echo "Unsupported bundle option: $1 $2" >&2; exit 2 ;; esac fi configuration=${1:-debug} preview=0 if [[ ${2:-} == --preview ]]; then preview=1; fi inspectable=0 if [[ ${2:-} == --inspectable ]]; then inspectable=1; fi reality=0 if [[ ${2:-} == --reality ]]; then reality=1 [[ -x build/reality-helper/ParcelRealityDriver ]] || { printf 'Build the reality helper first.\n' >&2; exit 1; } fi bundle_id=${PARCEL_BUNDLE_ID:-$PRODUCT_BUNDLE_ID} version=${PARCEL_VERSION-0.1.0} build_number=${PARCEL_BUILD_NUMBER-1} identity=${PARCEL_SIGN_IDENTITY:-} if [[ -z $identity ]]; then keychain="$HOME/Library/Keychains/login.keychain-db" if ! identities=$(security find-identity -p codesigning "$keychain"); then echo 'Could not inspect signing identities in the login keychain.' >&2 exit 1 fi matches=() while IFS= read -r line; do if [[ $line =~ ([[:xdigit:]]{40})[[:space:]]+\"([^\"]+)\" && ${BASH_REMATCH[2]} == "$PRODUCT_NAME Development" ]]; then matches+=("${BASH_REMATCH[1]}") fi done <<< "$identities" if (( ${#matches[@]} > 1 )); then echo "More than one $PRODUCT_NAME Development identity found; set PARCEL_SIGN_IDENTITY to its fingerprint." >&2 exit 1 elif (( ${#matches[@]} == 1 )); then identity=${matches[0]} else identity=- fi fi if [[ ! $bundle_id =~ ^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+$ ]]; then echo "Invalid bundle identifier: $bundle_id" >&2 exit 1 fi validate_release_version "$version" "$build_number" if [[ -n $PRODUCT_SUPPORT_EMAIL && ! $PRODUCT_SUPPORT_EMAIL =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+$ ]]; then printf 'Invalid PRODUCT_SUPPORT_EMAIL in Scripts/product.env.\n' >&2 exit 1 fi if (( reality )) && [[ $identity == '-' ]]; then printf 'Reality bundles require the existing Parcel Development signing identity.\n' >&2 exit 1 fi if [[ $configuration == release && $identity == '-' && ${PARCEL_ALLOW_ADHOC:-0} != 1 ]]; then echo 'Ad-hoc release bundles require PARCEL_ALLOW_ADHOC=1 and are not for distribution.' >&2 exit 1 fi packaging_build_directory # SwiftBuild retains resource-copy state; manifest/compiler variants must not share it. scratch="$PWD/.build/bundle/normal" if (( reality )); then scratch="$PWD/.build/bundle/reality"; fi for path in .build .build/bundle "$scratch"; do [[ ! -L $path ]] || { printf 'Refusing a symlinked bundle cache: %s\n' "$path" >&2; exit 1; } done Scripts/check-app-icons.sh --resources Sources/ParcelUI/Resources/AppIcons PARCEL_DEV_BINARY=$(Scripts/parcel-dev.sh --path) export PARCEL_DEV_BINARY mkdir -p build work=$(mktemp -d "$PWD/build/.bundle.XXXXXX") trap 'rm -rf "$work"' EXIT # SwiftBuild's resource accessors can find bundles inside a signed app. export PARCEL_BUNDLE_BUILD=1 # Keep checkout paths out of shipped binaries. checkout=$(realpath .) build_args=(--scratch-path "$scratch" --build-system swiftbuild -c "$configuration" -Xswiftc -file-prefix-map -Xswiftc "$checkout=." -Xswiftc -debug-prefix-map -Xswiftc "$checkout=." -Xswiftc -file-compilation-dir -Xswiftc . -Xcc "-ffile-prefix-map=$checkout=." -Xcc "-fdebug-prefix-map=$checkout=.") if (( reality )); then build_args+=(-Xswiftc -DPARCEL_REALITY); fi # Multiple --arch options silently select the legacy build backend, which cannot build our resource plugin. # SwiftBuild's release default is universal; verify both slices below. for product in "$PRODUCT_APP_TARGET" "$PRODUCT_CLI"; do if ! Scripts/swift-build.sh "${build_args[@]}" --product "$product" > "$work/build.log" 2>&1; then /bin/cat "$work/build.log" >&2 exit 1 fi done bin_dir=$(swift build "${build_args[@]}" --show-bin-path) if [[ $configuration == release ]]; then lipo "$bin_dir/$PRODUCT_APP_TARGET" -verify_arch arm64 x86_64 lipo "$bin_dir/$PRODUCT_CLI" -verify_arch arm64 x86_64 fi app="$work/$PRODUCT_NAME.app" resources="$app/Contents/Resources" mkdir -p "$app/Contents/MacOS" "$resources/bin" cp Packaging/Sparkle-LICENSE "$resources/Sparkle-LICENSE" cp "$bin_dir/$PRODUCT_APP_TARGET" "$app/Contents/MacOS/$PRODUCT_NAME" cp "$bin_dir/$PRODUCT_CLI" "$resources/bin/$PRODUCT_CLI" if (( reality )); then mkdir "$app/Contents/Helpers" cp build/reality-helper/ParcelRealityDriver "$app/Contents/Helpers/ParcelRealityDriver" fi cp Resources/Info.plist "$app/Contents/Info.plist" /usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $bundle_id" "$app/Contents/Info.plist" /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $version" "$app/Contents/Info.plist" /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $build_number" "$app/Contents/Info.plist" /usr/libexec/PlistBuddy -c "Set :ParcelSupportEmail \"$PRODUCT_SUPPORT_EMAIL\"" "$app/Contents/Info.plist" update_configuration=$configuration if (( preview )); then update_configuration=debug; fi Scripts/parcel-dev.sh update-metadata configure "$app/Contents/Info.plist" "$update_configuration" framework_source=${PARCEL_SPARKLE_FRAMEWORK:-$scratch/artifacts/sparkle/Sparkle/Sparkle.xcframework/macos-arm64_x86_64/Sparkle.framework} [[ -d $framework_source ]] || { echo "Sparkle framework not found: $framework_source" >&2; exit 1; } mkdir -p "$app/Contents/Frameworks" ditto "$framework_source" "$app/Contents/Frameworks/Sparkle.framework" framework="$app/Contents/Frameworks/Sparkle.framework" lipo "$framework/Versions/B/Sparkle" -verify_arch arm64 x86_64 if ! otool -l "$app/Contents/MacOS/$PRODUCT_NAME" | grep -F '@executable_path/../Frameworks' > /dev/null; then install_name_tool -add_rpath '@executable_path/../Frameworks' "$app/Contents/MacOS/$PRODUCT_NAME" fi rsync -a --exclude='/Info.plist' --exclude='*.xcassets' --exclude='*.xcstrings' Resources/ "$resources/" while IFS= read -r -d '' bundle; do bundle_name=$(basename "$bundle") if [[ -e $resources/$bundle_name || -L $resources/$bundle_name || -e $resources/bin/$bundle_name || -L $resources/bin/$bundle_name ]]; then echo "Resource bundle already exists: $bundle_name" >&2 exit 1 fi ditto "$bundle" "$resources/$bundle_name" # The standalone CLI looks for target bundles beside its executable. ln -s "../$bundle_name" "$resources/bin/$bundle_name" done < <(find "$bin_dir" -maxdepth 1 -type d -name '*.bundle' -print0) catalogs=() while IFS= read -r -d '' catalog; do catalogs+=("$catalog") done < <(find Resources -type d -name '*.xcassets' -print0) if (( ${#catalogs[@]} )); then icons=() while IFS= read -r -d '' icon; do icons+=("$icon") done < <(find "${catalogs[@]}" -type d -name '*.appiconset' -print0) asset_args=(--compile "$resources" --platform macosx --minimum-deployment-target 15.0 --output-partial-info-plist "$work/assets.plist" --warnings --errors) if (( ${#icons[@]} > 1 )); then echo 'More than one app icon set found; select a single app icon.' >&2 exit 1 elif (( ${#icons[@]} == 1 )); then Scripts/check-app-icons.sh --catalog "${icons[0]}" icon_name=$(basename "${icons[0]}" .appiconset) asset_args+=(--app-icon "$icon_name") fi if ! xcrun actool "${asset_args[@]}" "${catalogs[@]}" > "$work/actool.log" 2>&1; then /bin/cat "$work/actool.log" >&2 exit 1 fi /usr/libexec/PlistBuddy -c "Merge \"$work/assets.plist\"" "$app/Contents/Info.plist" fi compile_catalog() { local catalog=$1 destination=$2 compiled relative target prepared mkdir "$work/strings" prepared="$work/$(basename "$catalog")" Scripts/parcel-dev.sh prepare-catalog "$catalog" "$prepared" if ! xcrun xcstringstool compile "$prepared" --output-directory "$work/strings" > "$work/strings.log" 2>&1; then /bin/cat "$work/strings.log" >&2 return 1 fi while IFS= read -r -d '' compiled; do relative=${compiled#"$work/strings/"} target="$destination/$relative" if [[ -e $target ]]; then echo "Resource already exists: $relative" >&2 return 1 else mkdir -p "$(dirname "$target")" cp "$compiled" "$target" fi done < <(find "$work/strings" -type f -print0) rm -rf "$work/strings" } while IFS= read -r -d '' catalog; do compile_catalog "$catalog" "$resources" done < <(find Resources -type f -name '*.xcstrings' -print0) while IFS= read -r -d '' catalog; do rm "$catalog" done < <(find "$resources" -type f -name '*.xcstrings' -print0) # Pseudo catalogs are generated only into staged debug resources, never into SwiftPM's cache. if [[ $configuration == debug ]]; then # Main may have no catalog of its own; let module bundles choose their developer locale. /usr/libexec/PlistBuddy -c 'Add :CFBundleAllowMixedLocalizations bool true' "$app/Contents/Info.plist" shopt -s nullglob root_catalogs=(Resources/*.xcstrings) if (( ${#root_catalogs[@]} )); then Scripts/check-localization-pseudo.sh Resources "$resources" fi for directory in Sources/*/Resources; do module_catalogs=("$directory"/*.xcstrings) (( ${#module_catalogs[@]} )) || continue module=${directory#Sources/} module=${module%/Resources} module_bundles=("$resources"/*_"$module".bundle) (( ${#module_bundles[@]} )) || continue if (( ${#module_bundles[@]} != 1 )); then printf 'Ambiguous resource bundle for %s\n' "$module" >&2 exit 1 fi destination=${module_bundles[0]} if [[ -d $destination/Contents/Resources ]]; then destination+=/Contents/Resources; fi Scripts/check-localization-pseudo.sh "$directory" "$destination" done else # Refuse leaked developer resources rather than shipping them from a stale or supplied bundle. if [[ -n $(find "$resources" -type d -iname 'en-XA.lproj' -print -quit) ]]; then printf 'Release bundle contains pseudo-localization resources.\n' >&2 exit 1 fi fi # The main bundle advertises shipping languages only; pseudo is not a picker option. localizations=$(find "$resources" -type d -name '*.lproj' -exec basename {} .lproj \; | LC_ALL=C sort -u) /usr/libexec/PlistBuddy -c 'Add :CFBundleLocalizations array' "$app/Contents/Info.plist" while IFS= read -r localization; do [[ $localization == Base || $localization == en-XA ]] && continue [[ $localization =~ ^[A-Za-z0-9_-]+$ ]] || { echo "Invalid localization: $localization" >&2; exit 1; } /usr/libexec/PlistBuddy -c "Add :CFBundleLocalizations: string $localization" "$app/Contents/Info.plist" done <<< "$localizations" # The CLI needs this identity/localization metadata; signing rejects a link outside Resources. cp "$app/Contents/Info.plist" "$resources/bin/Info.plist" for binary in "$app/Contents/MacOS/$PRODUCT_NAME" "$resources/bin/$PRODUCT_CLI"; do Scripts/parcel-dev.sh strip-binary-paths "$binary" "$PWD" done if [[ $configuration == release ]]; then Scripts/parcel-dev.sh check-binaries "$app" "$PRODUCT_NAME" "$PWD" else Scripts/parcel-dev.sh check-binaries --sdk-only "$app" "$PRODUCT_NAME" "$PWD" fi sign() { local args=(--force --sign "$identity" --options runtime) if [[ $configuration == release && $preview == 0 && $identity != '-' ]]; then args+=(--timestamp) else args+=(--timestamp=none) fi if ! codesign "${args[@]}" "$@" > "$work/sign.log" 2>&1; then /bin/cat "$work/sign.log" >&2 return 1 fi } sign "$framework/Versions/B/XPCServices/Installer.xpc" sign --preserve-metadata=entitlements "$framework/Versions/B/XPCServices/Downloader.xpc" sign "$framework/Versions/B/Autoupdate" sign "$framework/Versions/B/Updater.app" sign "$framework" sign "$resources/bin/$PRODUCT_CLI" # Without a team ID the hardened runtime refuses to load our own embedded Sparkle, so local builds opt out of library validation. if ! codesign -dv "$framework" > "$work/framework-signature.log" 2>&1; then /bin/cat "$work/framework-signature.log" >&2 exit 1 fi if ! grep -q '^TeamIdentifier=' "$work/framework-signature.log"; then echo 'The framework signature has no team identifier metadata.' >&2 exit 1 fi if grep -q '^TeamIdentifier=not set$' "$work/framework-signature.log"; then if [[ $configuration == release && $preview == 0 && ${PARCEL_ALLOW_ADHOC:-0} != 1 ]]; then echo 'Release bundles need a team-signed identity; use --preview for an unnotarized beta or PARCEL_ALLOW_ADHOC=1 for rehearsals.' >&2 exit 1 fi /usr/libexec/PlistBuddy -c 'Add :com.apple.security.cs.disable-library-validation bool true' "$work/local.entitlements" > /dev/null fi if [[ $inspectable == 1 ]]; then /usr/libexec/PlistBuddy -c 'Add :com.apple.security.get-task-allow bool true' "$work/local.entitlements" > /dev/null fi if [[ -f $work/local.entitlements ]]; then sign --entitlements "$work/local.entitlements" "$app" else sign "$app" fi codesign --verify --strict --deep "$app" if (( ! reality )); then Scripts/check-reality-isolation.sh "$app"; fi if (( reality )) && [[ -e build/$PRODUCT_NAME.app ]]; then Scripts/designated-requirement.sh "build/$PRODUCT_NAME.app" "$app" fi if [[ $identity == '-' ]]; then echo 'Ad-hoc builds lose their Accessibility grant on every rebuild; use Scripts/make-dev-cert.sh or PARCEL_SIGN_IDENTITY to keep it.' >&2 fi Scripts/bundle-preflight.sh "$app" if [[ -e build/$PRODUCT_NAME.app || -L build/$PRODUCT_NAME.app ]]; then # Only filesystem safety matters for the replaced bundle; older versions lack alternate artwork. Scripts/parcel-dev.sh bundle-preflight "build/$PRODUCT_NAME.app" fi Scripts/parcel-dev.sh install-bundle "$app" "build/$PRODUCT_NAME.app"