diff --git a/packaging/.shellcheckrc b/packaging/.shellcheckrc new file mode 100644 index 000000000..e981c3884 --- /dev/null +++ b/packaging/.shellcheckrc @@ -0,0 +1,3 @@ +# shellcheck config for the packaging scripts. +source-path=SCRIPTDIR +external-sources=true diff --git a/packaging/env.sh b/packaging/env.sh new file mode 100755 index 000000000..089b8e71c --- /dev/null +++ b/packaging/env.sh @@ -0,0 +1,86 @@ +# shellcheck shell=bash +# Shared setup for the packaging scripts. Source it: `. "$(dirname "$0")/../env.sh"`. +# Adapted from MailCraft and FieldCraft packaging (MIT OR Apache-2.0). +# +# Exports: +# ROOT workspace root +# VERSION [workspace.package] version from Cargo.toml (override: PCBCRAFT_VERSION) +# DIST output directory for release artifacts (default: $ROOT/dist/release) +# PCBCRAFT_BUILD_SHA git commit recorded in the macOS Info.plist +# PCBCRAFT_BUILD_DATE UTC build date, YYYY-MM-DD +# CARGO_TARGET_DIR absolute cargo target dir (default: $ROOT/target) +# APP_ID reverse-DNS macOS bundle identifier + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +export ROOT + +# Read [workspace.package] version from the root Cargo.toml. +workspace_version() { + awk ' + /^\[/ { in_pkg = ($0 == "[workspace.package]") ; next } + in_pkg && $1 == "version" { gsub(/[" ]/, "", $3); print $3; exit } + ' "$ROOT/Cargo.toml" +} + +VERSION="${PCBCRAFT_VERSION:-$(workspace_version)}" +if [ -z "$VERSION" ]; then + echo "error: could not read [workspace.package] version from $ROOT/Cargo.toml" >&2 + exit 1 +fi +export VERSION + +DIST="${DIST:-$ROOT/dist/release}" +mkdir -p "$DIST" +DIST="$(cd "$DIST" && pwd)" +export DIST + +PCBCRAFT_BUILD_SHA="${PCBCRAFT_BUILD_SHA:-$(git -C "$ROOT" rev-parse HEAD 2>/dev/null || echo unknown)}" +export PCBCRAFT_BUILD_SHA +PCBCRAFT_BUILD_DATE="${PCBCRAFT_BUILD_DATE:-$(date -u +%Y-%m-%d)}" +export PCBCRAFT_BUILD_DATE + +# Resolve any relative CARGO_TARGET_DIR override against the caller's working directory +# before subshells cd into $ROOT for cargo build. +case "${CARGO_TARGET_DIR:-}" in + "") CARGO_TARGET_DIR="$ROOT/target" ;; + /*) ;; + *) CARGO_TARGET_DIR="$PWD/$CARGO_TARGET_DIR" ;; +esac +export CARGO_TARGET_DIR + +APP_ID="io.tangencylabs.pcbcraft" +export APP_ID + +# Emit a GitHub Actions warning (plain stderr outside Actions). +warn() { + if [ -n "${GITHUB_ACTIONS:-}" ]; then echo "::warning::$*"; else echo "warning: $*" >&2; fi +} + +# Copy repository licence and readme files into a package directory. +copy_docs() { + local dest="$1" f + for f in README.md LICENSE LICENSE-MIT LICENSE-APACHE NOTICE ATTRIBUTION.md COPYRIGHT THIRD-PARTY-LICENSES.txt; do + if [ -f "$ROOT/$f" ]; then cp "$ROOT/$f" "$dest/"; fi + done + copy_font_licences "$dest" +} + +# Copy bundled font licences (including optional CRAFT_FONTS_DIR embeds) into dest. +copy_font_licences() { + local dest="$1" f + if [ -d "$ROOT/licenses/egui-default-fonts" ]; then + mkdir -p "$dest/egui-default-fonts" + for f in "$ROOT"/licenses/egui-default-fonts/*; do + if [ -f "$f" ]; then cp "$f" "$dest/egui-default-fonts/"; fi + done + fi + [ -n "${CRAFT_FONTS_DIR:-}" ] || return 0 + for f in "$CRAFT_FONTS_DIR"/fonts/*/OFL.txt; do + if [ -f "$f" ]; then cp "$f" "$dest/OFL-$(basename "$(dirname "$f")").txt"; fi + done +} + +# Portable SHA-256 checksum helper. +sha256() { + if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi +} diff --git a/packaging/macos/Info.plist.in b/packaging/macos/Info.plist.in new file mode 100644 index 000000000..86ed25ec8 --- /dev/null +++ b/packaging/macos/Info.plist.in @@ -0,0 +1,74 @@ + + + + + + CFBundleDevelopmentRegion + en + CFBundleDisplayName + PCBCraft + CFBundleName + PCBCraft + CFBundleExecutable + PCBCraft + CFBundleIdentifier + @APP_ID@ + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + APPL + CFBundleSignature + ???? + CFBundleShortVersionString + @SHORT_VERSION@ + CFBundleVersion + @SHORT_VERSION@ + PCBCraftVersion + @VERSION@ + PCBCraftBuildCommit + @BUILD_SHA@ + LSApplicationCategoryType + public.app-category.developer-tools + + LSMinimumSystemVersion + @MIN_MACOS@ + NSHighResolutionCapable + + NSSupportsAutomaticGraphicsSwitching + + NSRequiresAquaSystemAppearance + + NSHumanReadableCopyright + Copyright © PCBCraft contributors. GPL-3.0-or-later. + CFBundleDocumentTypes + + + CFBundleTypeName + PCBCraft Project Archive + CFBundleTypeRole + Editor + LSHandlerRank + Owner + CFBundleTypeExtensions + + pc + + + + CFBundleTypeName + KiCad Project + CFBundleTypeRole + Viewer + LSHandlerRank + Alternate + CFBundleTypeExtensions + + kicad_pro + kicad_sch + kicad_pcb + + + + + diff --git a/packaging/macos/entitlements.plist b/packaging/macos/entitlements.plist new file mode 100644 index 000000000..f4458ad01 --- /dev/null +++ b/packaging/macos/entitlements.plist @@ -0,0 +1,8 @@ + + + + + + + diff --git a/packaging/macos/import-cert.sh b/packaging/macos/import-cert.sh new file mode 100755 index 000000000..aab792add --- /dev/null +++ b/packaging/macos/import-cert.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# CI only: import the Developer ID certificate into a temporary keychain for codesign. +# +# Inputs (env): APPLE_CERTIFICATE (base64 .p12), APPLE_CERTIFICATE_PASSWORD, KEYCHAIN_PASSWORD +# (optional; random if unset), RUNNER_TEMP (or TMPDIR). +# Outputs: MACOS_SIGN_IDENTITY (SHA-1) and MACOS_KEYCHAIN, appended to $GITHUB_ENV in Actions. +# +# Missing certificate: emits a warning and exits 0 so package.sh falls back to ad-hoc signing. +set -euo pipefail +umask 077 +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" + +if [ -z "${APPLE_CERTIFICATE:-}" ]; then + warn "APPLE_CERTIFICATE is not set: macOS builds will be ad-hoc signed and not notarized" + exit 0 +fi + +TMP="${RUNNER_TEMP:-${TMPDIR:-/tmp}}" +KEYCHAIN="$TMP/pcbcraft-signing-$$.keychain-db" +CERT="$TMP/pcbcraft-signing-$$.p12" +KC_PASS="${KEYCHAIN_PASSWORD:-$(openssl rand -hex 24)}" +KEEP_KEYCHAIN=0 + +cleanup() { + rm -f "$CERT" + if [ "$KEEP_KEYCHAIN" != 1 ]; then + security delete-keychain "$KEYCHAIN" 2>/dev/null || rm -f "$KEYCHAIN" + fi +} +trap cleanup EXIT + +printf '%s' "$APPLE_CERTIFICATE" | base64 --decode >"$CERT" +security delete-keychain "$KEYCHAIN" 2>/dev/null || rm -f "$KEYCHAIN" +security create-keychain -p "$KC_PASS" "$KEYCHAIN" +security set-keychain-settings -lut 21600 "$KEYCHAIN" +security unlock-keychain -p "$KC_PASS" "$KEYCHAIN" +security import "$CERT" -P "${APPLE_CERTIFICATE_PASSWORD:-}" -T /usr/bin/codesign -T /usr/bin/security -t cert -f pkcs12 -k "$KEYCHAIN" +rm -f "$CERT" +# Allow codesign to access the private key without a GUI prompt. +security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$KC_PASS" "$KEYCHAIN" >/dev/null +# Put it on the search list alongside existing user keychains. +# shellcheck disable=SC2046 +security list-keychains -d user -s "$KEYCHAIN" $(security list-keychains -d user | tr -d '"') + +IDENTITY="$(security find-identity -v -p codesigning "$KEYCHAIN" | awk '/Developer ID Application/ { print $2; exit }')" +if [ -z "$IDENTITY" ]; then + security find-identity -v -p codesigning "$KEYCHAIN" >&2 + echo "error: no 'Developer ID Application' identity in APPLE_CERTIFICATE" >&2 + exit 1 +fi +KEEP_KEYCHAIN=1 +echo "Imported signing identity $IDENTITY" +if [ -n "${GITHUB_ENV:-}" ]; then + { echo "MACOS_SIGN_IDENTITY=$IDENTITY"; echo "MACOS_KEYCHAIN=$KEYCHAIN"; } >>"$GITHUB_ENV" +else + echo "MACOS_SIGN_IDENTITY=$IDENTITY" + echo "MACOS_KEYCHAIN=$KEYCHAIN" +fi diff --git a/packaging/macos/package.sh b/packaging/macos/package.sh new file mode 100755 index 000000000..4743ee336 --- /dev/null +++ b/packaging/macos/package.sh @@ -0,0 +1,223 @@ +#!/usr/bin/env bash +# Build, sign and (optionally) notarize the macOS release artifacts for PCBCraft: +# +# $DIST/pcbcraft--macos-.dmg PCBCraft.app on a drag-to-Applications DMG +# $DIST/pcbcraft-cli--macos-.zip the headless CLI with licence files +# +# Usage: packaging/macos/package.sh [--arch universal|aarch64|x86_64] [--skip-build | --build-only] [--no-run] +# +# Options: +# --arch Target architecture(s): universal (default), aarch64, or x86_64 +# --skip-build Package binaries already present in $CARGO_TARGET_DIR//release +# --build-only Compile targets and exit before signing/packaging +# --no-run Skip the final CLI smoke check (for isolated signing jobs) +# +# Signing (env): +# MACOS_SIGN_IDENTITY codesign identity (name or SHA-1). Default "-" = ad-hoc signing. +# MACOS_KEYCHAIN keychain holding MACOS_SIGN_IDENTITY (optional) +# Notarization (env; requires a real identity plus all three): +# APPLE_ID, APPLE_PASSWORD, APPLE_TEAM_ID +set -euo pipefail +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" +HERE="$ROOT/packaging/macos" + +ARCH=universal +SKIP_BUILD=0 +BUILD_ONLY=0 +NO_RUN=0 +while [ $# -gt 0 ]; do + case "$1" in + --arch) + if [ $# -lt 2 ]; then echo "missing value for --arch" >&2; exit 2; fi + ARCH="$2"; shift 2 ;; + --skip-build) SKIP_BUILD=1; shift ;; + --build-only) BUILD_ONLY=1; shift ;; + --no-run) NO_RUN=1; shift ;; + -h | --help) sed -n '2,19p' "$0"; exit 0 ;; + *) echo "unknown argument: $1" >&2; exit 2 ;; + esac +done +case "$ARCH" in + universal) TARGETS=(aarch64-apple-darwin x86_64-apple-darwin); WANT=(arm64 x86_64) ;; + aarch64) TARGETS=(aarch64-apple-darwin); WANT=(arm64) ;; + x86_64) TARGETS=(x86_64-apple-darwin); WANT=(x86_64) ;; + *) echo "unknown --arch $ARCH" >&2; exit 2 ;; +esac +if [ "$SKIP_BUILD" = 1 ] && [ "$BUILD_ONLY" = 1 ]; then + echo "--skip-build and --build-only can't be used together" >&2 + exit 2 +fi + +# Validate version and build SHA before Info.plist sed substitution and XML generation. +for value in "$VERSION" "${PCBCRAFT_BUILD_SHA:-unknown}"; do + case "$value" in + *[!0-9A-Za-z._+-]*) + echo "error: version and build SHA may contain only letters, digits, '.', '_', '+' and '-': $value" >&2 + exit 2 + ;; + esac +done + +# Keep in sync with LSMinimumSystemVersion in Info.plist.in. +export MACOSX_DEPLOYMENT_TARGET=11.0 +IDENTITY="${MACOS_SIGN_IDENTITY:--}" +SHORT_VERSION="${VERSION%%-*}" +WORK="$CARGO_TARGET_DIR/macos-package" +APP="$WORK/PCBCraft.app" +DMG="$DIST/pcbcraft-$VERSION-macos-$ARCH.dmg" +CLI_ZIP="$DIST/pcbcraft-cli-$VERSION-macos-$ARCH.zip" + +NOTARIZE=0 +SIGNING=adhoc +if [ "$IDENTITY" = "-" ]; then + warn "macOS: ad-hoc signing (no MACOS_SIGN_IDENTITY); artifacts are not notarized" +elif [ -n "${APPLE_ID:-}" ] && [ -n "${APPLE_PASSWORD:-}" ] && [ -n "${APPLE_TEAM_ID:-}" ]; then + NOTARIZE=1 + SIGNING=notarized +else + SIGNING=signed + warn "macOS: APPLE_ID / APPLE_PASSWORD / APPLE_TEAM_ID incomplete; signed but not notarized" +fi + +echo "==> PCBCraft $VERSION for macOS ($ARCH), identity: $IDENTITY, notarize: $NOTARIZE" + +# ---- build ------------------------------------------------------------------------------------- +if [ "$SKIP_BUILD" = 0 ]; then + args=() + for t in "${TARGETS[@]}"; do args+=(--target "$t"); done + (cd "$ROOT" && cargo build --release --locked -p pcbcraft -p pcbcraft-cli "${args[@]}") +fi + +if [ "$BUILD_ONLY" = 1 ]; then + echo "==> built; signing and packaging come next" + exit 0 +fi + +rm -rf "$WORK" +mkdir -p "$WORK/bin" +for bin in pcbcraft pcbcraft-cli; do + inputs=() + for t in "${TARGETS[@]}"; do inputs+=("$CARGO_TARGET_DIR/$t/release/$bin"); done + lipo -create -output "$WORK/bin/$bin" "${inputs[@]}" + lipo -info "$WORK/bin/$bin" + for arch in "${WANT[@]}"; do lipo "$WORK/bin/$bin" -verify_arch "$arch"; done +done + + +# ---- signing helpers --------------------------------------------------------------------------- +sign() { + local ts=(--timestamp) + [ "$IDENTITY" = "-" ] && ts=(--timestamp=none) + local kc=() + [ -n "${MACOS_KEYCHAIN:-}" ] && kc=(--keychain "$MACOS_KEYCHAIN") + codesign --force --sign "$IDENTITY" ${kc[@]+"${kc[@]}"} "${ts[@]}" "$@" +} + +notarize() { + local file="$1" out id status + echo "==> notarizing $(basename "$file") (this can take a few minutes)" + out="$(xcrun notarytool submit "$file" --apple-id "$APPLE_ID" --password "$APPLE_PASSWORD" \ + --team-id "$APPLE_TEAM_ID" --wait --timeout 1h --output-format json)" || true + echo "$out" + id="$(printf '%s' "$out" | plutil -extract id raw -o - - 2>/dev/null || true)" + status="$(printf '%s' "$out" | plutil -extract status raw -o - - 2>/dev/null || true)" + if [ "$status" != "Accepted" ]; then + if [ -n "$id" ]; then + xcrun notarytool log "$id" --apple-id "$APPLE_ID" --password "$APPLE_PASSWORD" --team-id "$APPLE_TEAM_ID" || true + fi + echo "error: notarization of $(basename "$file") failed (status: ${status:-unknown})" >&2 + exit 1 + fi +} + +# hdiutil attaches a device while working, which can briefly fail with "Resource busy" in CI. +hdiutil_retry() { + local what="$1" attempt + for attempt in 1 2 3 4 5; do + if hdiutil "$@"; then return 0; fi + [ "$attempt" = 5 ] && { echo "hdiutil $what failed 5 times" >&2; return 1; } + echo "hdiutil $what failed (attempt $attempt of 5); retrying in $((attempt * 5))s" >&2 + sleep $((attempt * 5)) + done +} + +# ---- PCBCraft.app ---------------------------------------------------------------------------- +echo "==> assembling $APP" +mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources/Licenses" +cp "$WORK/bin/pcbcraft" "$APP/Contents/MacOS/PCBCraft" +cp "$WORK/bin/pcbcraft-cli" "$APP/Contents/MacOS/pcbcraft-cli" +copy_docs "$APP/Contents/Resources/Licenses" + +sed -e "s/@APP_ID@/$APP_ID/g" -e "s/@VERSION@/$VERSION/g" -e "s/@SHORT_VERSION@/$SHORT_VERSION/g" \ + -e "s/@BUILD_SHA@/${PCBCRAFT_BUILD_SHA:-unknown}/g" -e "s/@MIN_MACOS@/$MACOSX_DEPLOYMENT_TARGET/g" \ + "$HERE/Info.plist.in" >"$APP/Contents/Info.plist" + +ICON_SRC="" +for candidate in "$ROOT/assets/app-icon/pcbcraft.icns" "$ROOT/assets/app-icon/icon.icns"; do + if [ -f "$candidate" ]; then ICON_SRC="$candidate"; break; fi +done +if [ -n "$ICON_SRC" ]; then + cp "$ICON_SRC" "$APP/Contents/Resources/PCBCraft.icns" + /usr/libexec/PlistBuddy -c "Add :CFBundleIconFile string PCBCraft" "$APP/Contents/Info.plist" +fi + +plutil -lint "$APP/Contents/Info.plist" +printf 'APPL????' >"$APP/Contents/PkgInfo" + +# Sign inside-out: nested executables first, then the bundle itself. +sign --options runtime --identifier "$APP_ID.cli" "$APP/Contents/MacOS/pcbcraft-cli" +sign --options runtime --entitlements "$HERE/entitlements.plist" "$APP/Contents/MacOS/PCBCraft" +sign --options runtime --entitlements "$HERE/entitlements.plist" "$APP" +codesign --verify --strict --deep --verbose=2 "$APP" + +if [ "$NOTARIZE" = 1 ]; then + ditto -c -k --keepParent "$APP" "$WORK/PCBCraft-notarize.zip" + notarize "$WORK/PCBCraft-notarize.zip" + xcrun stapler staple "$APP" + xcrun stapler validate "$APP" + spctl --assess --type execute -vvv "$APP" +fi + +# ---- DMG --------------------------------------------------------------------------------------- +echo "==> building $DMG" +STAGE="$WORK/dmg" +mkdir -p "$STAGE" +ditto "$APP" "$STAGE/PCBCraft.app" +ln -s /Applications "$STAGE/Applications" +if [ -f "$HERE/dmg/background.tiff" ] && [ -f "$HERE/dmg/dmg-layout.DS_Store" ]; then + mkdir -p "$STAGE/.background" + cp "$HERE/dmg/background.tiff" "$STAGE/.background/background.tiff" + cp "$HERE/dmg/dmg-layout.DS_Store" "$STAGE/.DS_Store" +fi +rm -f "$DMG" +hdiutil_retry create -volname "PCBCraft" -srcfolder "$STAGE" -fs HFS+ -format UDZO -imagekey zlib-level=9 -ov "$DMG" +sign "$DMG" +codesign --verify --strict --verbose=2 "$DMG" +if [ "$NOTARIZE" = 1 ]; then + notarize "$DMG" + xcrun stapler staple "$DMG" + xcrun stapler validate "$DMG" + spctl --assess --type open --context context:primary-signature -vvv "$DMG" +fi + +# ---- CLI --------------------------------------------------------------------------------------- +echo "==> building $CLI_ZIP" +CLI_DIR="$WORK/pcbcraft-cli-$VERSION-macos-$ARCH" +mkdir -p "$CLI_DIR" +cp "$WORK/bin/pcbcraft-cli" "$CLI_DIR/" +copy_docs "$CLI_DIR" +sign --options runtime --identifier "$APP_ID.cli" "$CLI_DIR/pcbcraft-cli" +codesign --verify --strict --verbose=2 "$CLI_DIR/pcbcraft-cli" +rm -f "$CLI_ZIP" +ditto -c -k --norsrc --noextattr --keepParent "$CLI_DIR" "$CLI_ZIP" +if [ "$NOTARIZE" = 1 ]; then notarize "$CLI_ZIP"; fi + +if [ "$NO_RUN" = 1 ]; then + echo "==> skipping pcbcraft-cli smoke test (--no-run)" +else + "$CLI_DIR/pcbcraft-cli" commands >/dev/null +fi +if [ -n "${GITHUB_OUTPUT:-}" ]; then echo "signing=$SIGNING" >>"$GITHUB_OUTPUT"; fi +echo "==> done (signing: $SIGNING)" +ls -lh "$DMG" "$CLI_ZIP"