#!/bin/sh set -eu unset BASH_ENV ENV root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd -P) export PARCEL_DEV_ROOT="$root" cache="$root/.build/parcel-dev" binary=${PARCEL_DEV_BINARY:-"$cache/release/parcel-dev"} if [ -z "${PARCEL_DEV_BINARY:-}" ]; then mkdir -p "$cache" inputs="$cache/inputs.$$" sources="$cache/sources.$$" records="$cache/records.$$" locked=0 cleanup() { rm -f "$inputs" "$sources" "$records" if [ "$locked" = 1 ]; then rm -f "$cache/build.lock"; fi } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM HUP fingerprint() { find "$root/Sources/ParcelDev" "$root/Sources/parcel-dev" \( -type f -o -type l \) -name '*.swift' -print0 > "$sources" xargs -0 shasum -a 256 < "$sources" > "$records" shasum -a 256 "$root/Package.swift" "$root/Scripts/parcel-dev.sh" "$root/Scripts/swift-build.sh" >> "$records" if [ -f "$root/Package.resolved" ]; then shasum -a 256 "$root/Package.resolved" >> "$records"; fi compiler_version=$(swift --version 2>&1) || { printf '%s\n' "$compiler_version" >&2; exit 2; } # SwiftPM exports SDKROOT; hooks may leave the same SDK implicit. sdk=$(xcrun --show-sdk-path) sdk=$(CDPATH='' cd -- "$sdk" && pwd -P) { command -v swift xcrun --find swift printf '%s\n' "$compiler_version" printf '%s\n' "${DEVELOPER_DIR:-}" "$sdk" "${TOOLCHAINS:-}" "${SWIFT_EXEC:-}" LC_ALL=C sort "$records" } > "$inputs" } fingerprint if [ ! -x "$binary" ] || ! cmp -s "$inputs" "$cache/inputs"; then attempts=0 # shlock atomically claims the cache and recovers abandoned process locks. until /usr/bin/shlock -p "$$" -f "$cache/build.lock"; do attempts=$((attempts + 1)) if [ "$attempts" -ge 6000 ]; then printf 'Developer tool cache is still locked: %s\n' "$cache/build.lock" >&2 exit 2 fi sleep 0.1 done locked=1 fingerprint if [ ! -x "$binary" ] || ! cmp -s "$inputs" "$cache/inputs"; then rm -f "$cache/inputs" # App test processes can already hold the main SwiftPM cache lock. "$root/Scripts/swift-build.sh" --scratch-path "$cache" -c release --product parcel-dev >&2 if [ ! -x "$binary" ]; then printf 'Developer tool build did not produce an executable: %s\n' "$binary" >&2 exit 2 fi mv "$inputs" "$cache/inputs" fi fi cleanup trap - EXIT INT TERM HUP fi if [ ! -x "$binary" ]; then printf 'Developer tool is missing or not executable: %s\n' "$binary" >&2 exit 2 fi if [ "${1:-}" = --path ]; then printf '%s\n' "$binary" exit 0 fi exec "$binary" "$@"