#!/bin/sh set -eu unset BASH_ENV ENV root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd -P) mode=build case "${1:-}" in --prepare|--clean-modules) mode=$1; shift ;; esac scratch="$root/.build" previous= for argument in "$@"; do if [ "$previous" = --scratch-path ]; then scratch=$argument; fi case "$argument" in --scratch-path=*) scratch=${argument#--scratch-path=} ;; --package-path|--package-path=*) printf 'Use the build wrapper from the package being built.\n' >&2; exit 2 ;; esac previous=$argument done [ "$previous" != --scratch-path ] || { printf 'Missing scratch path.\n' >&2; exit 2; } case "$scratch" in /*) ;; *) scratch="$root/$scratch" ;; esac case "$scratch/" in *'/../'*|*'/./'*) printf 'Build scratch cannot contain dot components.\n' >&2; exit 2 ;; esac case "$scratch" in "$root/.build"|"$root/.build/"*) ;; *) printf 'Build scratch must be inside .build.\n' >&2; exit 2 ;; esac # Never follow a cache symlink while deleting compiled state. path=$scratch while [ "$path" != / ]; do if [ -L "$path" ]; then printf 'Refusing a symlinked build cache: %s\n' "$path" >&2; exit 2; fi path=$(dirname -- "$path") done mkdir -p "$scratch" scratch=$(CDPATH='' cd -- "$scratch" && pwd -P) case "$scratch" in "$root/.build"|"$root/.build/"*) ;; *) printf 'Build scratch must be inside .build.\n' >&2; exit 2 ;; esac inputs="$scratch/toolchain.inputs.$$" locked=0 cleanup() { rm -f "$inputs" if [ "$locked" = 1 ]; then rm -f "$scratch/parcel-build.lock"; fi } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM HUP attempts=0 until /usr/bin/shlock -p "$$" -f "$scratch/parcel-build.lock"; do attempts=$((attempts + 1)) if [ "$attempts" -ge 6000 ]; then printf 'Build cache is still locked: %s\n' "$scratch" >&2; exit 2; fi sleep 0.1 done locked=1 fingerprint() { { printf 'parcel-build-stamp-v1\n' command -v swift swift --version xcrun --find swift xcrun --show-sdk-path xcodebuild -version printf '%s\n' "${DEVELOPER_DIR:-}" "${TOOLCHAINS:-}" "${SWIFT_EXEC:-}" "${PARCEL_BUNDLE_BUILD:-}" "${PARCEL_REALITY:-}" shasum -a 256 "$root/Package.swift" if [ -f "$root/Package.resolved" ]; then shasum -a 256 "$root/Package.resolved"; else printf 'Package.resolved missing\n'; fi } > "$inputs" } clean_modules() { # Keep checkouts, repositories, artifacts, workspace state and independent scratch dirs. for path in "$scratch"/*; do case "${path##*/}" in *-apple-*|*-unknown-*) rm -rf "$path" ;; esac done rm -rf "$scratch/debug" "$scratch/release" "$scratch/Modules" "$scratch/ModuleCache" \ "$scratch/plugins/cache" "$scratch/plugins/outputs" "$scratch/apple" \ "$scratch/"*.yaml "$scratch/"*.db "$scratch/"*.db-shm "$scratch/"*.db-wal rm -f "$scratch/toolchain.inputs" "$scratch/inputs" } fingerprint if [ "$mode" = --clean-modules ] || ! cmp -s "$inputs" "$scratch/toolchain.inputs"; then printf 'Discarding compiled modules in %s (build stamp changed or clean requested).\n' "$scratch" >&2 clean_modules fi if [ "$mode" = build ]; then (cd "$root" && swift build "$@") # Resolution may create or update Package.resolved during the build. fingerprint fi mv "$inputs" "$scratch/toolchain.inputs"