Files
lintunes/packaging/mac/build.sh
T
trav f21c31940f
macOS / Tests (AppleSilicon) (push) Canceled after 0s
macOS / Tests (Intel) (push) Canceled after 0s
macOS / Build (AppleSilicon) (push) Canceled after 0s
macOS / Build (Intel) (push) Canceled after 0s
macOS / Publish the GitHub Release (push) Canceled after 0s
v0.97.0: macOS port (Round 129): OS-aware paths, config and cookies; Mac now-playing, packaging (PyInstaller .app, dmg build, signing notes, CI workflow) and docs
2026-11-17 22:41:48 -08:00

218 lines
8.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build LinTunes.app and LinTunes-<version>-<AppleSilicon|Intel>.dmg.
#
# Runs on a Mac only, and builds for the Mac it runs on (an Apple Silicon
# Mac makes the AppleSilicon dmg, an Intel Mac the Intel one). The same
# script runs in GitHub Actions and on a desk:
#
# packaging/mac/build.sh
#
# Needs: Python 3.11+ for this Mac's architecture (python.org, Homebrew or
# pyenv; set PYTHON=/path/to/python3 to pick one) and Xcode's command line
# tools (codesign, hdiutil, lipo come with macOS / the CLT). No Homebrew
# packages: ffmpeg and fpcalc are downloaded and checked against the
# SHA-256 pinned in tools.json.
#
# Signing. Without secrets the app is ad-hoc signed ("codesign --sign -"),
# which Apple Silicon requires before it will run anything, and users see
# Gatekeeper's "Open Anyway" once (docs/mac.md). With all of these set it
# is signed with a Developer ID, hardened runtime and entitlements.plist,
# then notarized and stapled instead (docs/mac-release.md):
# MACOS_CERTIFICATE base64 of the Developer ID Application .p12
# MACOS_CERTIFICATE_PASSWORD that .p12's password
# MACOS_SIGNING_IDENTITY e.g. "Developer ID Application: Name (TEAMID)"
# MACOS_NOTARY_APPLE_ID Apple ID used for notarization
# MACOS_NOTARY_TEAM_ID the 10-character team id
# MACOS_NOTARY_PASSWORD an app-specific password for that Apple ID
#
# Output: build/mac/LinTunes-<version>-<label>.dmg and a .sha256 beside it.
set -euo pipefail
PYINSTALLER_VERSION="6.22.3"
HOOKS_CONTRIB_VERSION="2026.10"
die() { echo "build.sh: $*" >&2; exit 1; }
step() { echo; echo "==> $*"; }
[ "$(uname -s)" = "Darwin" ] || die "this builds a macOS app; run it on a Mac"
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$HERE/../.." && pwd)"
BUILD="$ROOT/build/mac"
VENV="$BUILD/venv"
PYTHON="${PYTHON:-python3}"
ARCH="$(uname -m)"
case "$ARCH" in
arm64) LABEL="AppleSilicon" ;;
x86_64) LABEL="Intel" ;;
*) die "unknown architecture $ARCH" ;;
esac
VERSION="$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' "$ROOT/lintunes/__init__.py")"
[ -n "$VERSION" ] || die "couldn't read __version__ from lintunes/__init__.py"
APP_NAME="LinTunes"
DMG="$BUILD/$APP_NAME-$VERSION-$LABEL.dmg"
step "LinTunes $VERSION for $LABEL ($ARCH)"
# A Python running under Rosetta (or a universal one started as the other
# arch) would build an app for the wrong kind of Mac.
PY_ARCH="$("$PYTHON" -c 'import platform; print(platform.machine())')"
[ "$PY_ARCH" = "$ARCH" ] || die "$PYTHON runs as $PY_ARCH on this $ARCH Mac"
"$PYTHON" -c 'import sys; sys.exit(sys.version_info < (3, 11))' \
|| die "$PYTHON is older than 3.11"
step "Virtual environment ($("$PYTHON" --version))"
mkdir -p "$BUILD"
"$PYTHON" -m venv --clear "$VENV"
PY="$VENV/bin/python"
"$PY" -m pip install --quiet --upgrade pip
# Compiled dependencies as wheels only: newer cryptography and zeroconf
# releases stopped publishing Intel Mac wheels, and pip would otherwise try
# (and fail) to build them from source. With --only-binary it picks the
# newest release that has a wheel for this Mac.
"$PY" -m pip install --quiet --prefer-binary \
--only-binary=PyQt6,PyQt6-Qt6,PyQt6-sip,numpy,cryptography,zeroconf,pydantic-core,aiohttp,miniaudio,protobuf \
-e "$ROOT" \
"pyinstaller==$PYINSTALLER_VERSION" \
"pyinstaller-hooks-contrib==$HOOKS_CONTRIB_VERSION"
"$PY" -m pip list --format=freeze > "$BUILD/pip-freeze-$LABEL.txt"
export SSL_CERT_FILE="${SSL_CERT_FILE:-$("$PY" -m certifi)}"
step "Bundled tools"
"$PY" -I "$HERE/fetch_tools.py" --arch "$ARCH" --out "$BUILD/tools"
for tool in ffmpeg ffprobe fpcalc; do
[[ " $(lipo -archs "$BUILD/tools/$tool") " == *" $ARCH "* ]] \
|| die "$tool isn't built for $ARCH"
done
# What the app needs from ffmpeg (loudness, BPM, DJ decode, AirPlay,
# lossless conversion, stems as Opus, stem cache keys). Listed into
# variables first: grep -q closing a pipe early would trip pipefail.
FF="$BUILD/tools/ffmpeg"
ENCODERS="$("$FF" -hide_banner -encoders 2>/dev/null)"
DECODERS="$("$FF" -hide_banner -decoders 2>/dev/null)"
FILTERS="$("$FF" -hide_banner -filters 2>/dev/null)"
MUXERS="$("$FF" -hide_banner -muxers 2>/dev/null)"
has() { grep -Eq "^ [A-Z.]{6} $2 " <<<"$1"; }
for enc in libmp3lame libopus flac alac pcm_s16be; do
has "$ENCODERS" "$enc" || die "ffmpeg lacks the $enc encoder"
done
for dec in mp3 aac alac flac opus vorbis pcm_s16le pcm_s16be; do
has "$DECODERS" "$dec" || die "ffmpeg lacks the $dec decoder"
done
grep -Eq " ebur128 " <<<"$FILTERS" || die "ffmpeg lacks the ebur128 filter"
grep -Eq " hash " <<<"$MUXERS" || die "ffmpeg lacks the hash muxer"
"$BUILD/tools/ffprobe" -version >/dev/null || die "ffprobe doesn't run"
"$BUILD/tools/fpcalc" -version >/dev/null || die "fpcalc doesn't run"
step "PyInstaller $PYINSTALLER_VERSION"
rm -rf "$BUILD/dist" "$BUILD/work"
(cd "$ROOT" && LINTUNES_ARCH="$ARCH" "$VENV/bin/pyinstaller" --noconfirm --clean \
--distpath "$BUILD/dist" --workpath "$BUILD/work" "$HERE/LinTunes.spec")
APP="$BUILD/dist/$APP_NAME.app"
[ -x "$APP/Contents/MacOS/$APP_NAME" ] || die "PyInstaller made no $APP"
# The helpers sit beside the app's own executable: where Apple wants
# nested command-line tools, and the first folder paths.mac_tool_dirs()
# puts on PATH.
install -m 755 "$BUILD/tools/ffmpeg" "$BUILD/tools/ffprobe" "$BUILD/tools/fpcalc" "$APP/Contents/MacOS/"
# Nothing of the Rabbit R1's may ride along.
if find "$APP" -name '*.apk' -o -name 'andTunes.json' | grep -q .; then
die "the R1 app ended up in the bundle"
fi
xattr -cr "$APP"
sign_adhoc() {
step "Ad-hoc signing"
codesign --force --deep --sign - "$APP"
echo "Developer ID signing skipped (no MACOS_CERTIFICATE): users will need \"Open Anyway\" once."
}
sign_developer_id() {
step "Developer ID signing"
KEYCHAIN="$BUILD/signing.keychain-db"
KEYCHAIN_PASSWORD="$(uuidgen)"
P12="$BUILD/certificate.p12"
trap 'security delete-keychain "$KEYCHAIN" >/dev/null 2>&1 || true; rm -f "$P12"' EXIT
echo "$MACOS_CERTIFICATE" | base64 --decode > "$P12"
security create-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN"
security set-keychain-settings -lut 21600 "$KEYCHAIN"
security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN"
security import "$P12" -k "$KEYCHAIN" -P "$MACOS_CERTIFICATE_PASSWORD" \
-T /usr/bin/codesign
security set-key-partition-list -S apple-tool:,apple: -s \
-k "$KEYCHAIN_PASSWORD" "$KEYCHAIN" >/dev/null
security list-keychains -d user -s "$KEYCHAIN" \
$(security list-keychains -d user | tr -d '"')
local sign=(codesign --force --timestamp --options runtime
--entitlements "$HERE/entitlements.plist"
--keychain "$KEYCHAIN" --sign "$MACOS_SIGNING_IDENTITY")
# Inside out: every Mach-O file, deepest first, then the frameworks,
# then the app. (--deep is not trusted for a Developer ID signature.)
while IFS= read -r f; do
if file -b "$f" | grep -q "Mach-O"; then "${sign[@]}" "$f"; fi
done < <(find "$APP/Contents" -type f ! -path "*/MacOS/$APP_NAME" \
| awk '{ print length, $0 }' | sort -rn | cut -d' ' -f2-)
while IFS= read -r fw; do "${sign[@]}" "$fw"; done \
< <(find "$APP/Contents" -type d -name '*.framework' \
| awk '{ print length, $0 }' | sort -rn | cut -d' ' -f2-)
"${sign[@]}" "$APP"
}
notarize() {
step "Notarizing"
codesign --force --timestamp --keychain "$KEYCHAIN" \
--sign "$MACOS_SIGNING_IDENTITY" "$DMG"
xcrun notarytool submit "$DMG" --wait \
--apple-id "$MACOS_NOTARY_APPLE_ID" \
--team-id "$MACOS_NOTARY_TEAM_ID" \
--password "$MACOS_NOTARY_PASSWORD"
xcrun stapler staple "$DMG"
xcrun stapler validate "$DMG"
}
DEVELOPER_ID=0
if [ -n "${MACOS_CERTIFICATE:-}" ] && [ -n "${MACOS_CERTIFICATE_PASSWORD:-}" ] \
&& [ -n "${MACOS_SIGNING_IDENTITY:-}" ]; then
DEVELOPER_ID=1
sign_developer_id
else
sign_adhoc
fi
codesign --verify --deep --strict --verbose=1 "$APP"
step "Disk image"
STAGE="$BUILD/dmg-stage"
rm -rf "$STAGE" "$DMG"
mkdir -p "$STAGE"
ditto "$APP" "$STAGE/$APP_NAME.app"
ln -s /Applications "$STAGE/Applications"
# hdiutil sometimes fails with "Resource busy" on CI runners; it's
# harmless to try again.
for attempt in 1 2 3 4 5; do
if hdiutil create -volname "$APP_NAME" -srcfolder "$STAGE" -ov \
-fs HFS+ -format UDZO "$DMG"; then
break
fi
[ "$attempt" -lt 5 ] || die "hdiutil create failed"
echo "hdiutil failed; retrying in 5 s"
sleep 5
done
rm -rf "$STAGE"
if [ "$DEVELOPER_ID" = 1 ]; then
if [ -n "${MACOS_NOTARY_APPLE_ID:-}" ] && [ -n "${MACOS_NOTARY_TEAM_ID:-}" ] \
&& [ -n "${MACOS_NOTARY_PASSWORD:-}" ]; then
notarize
else
echo "Notarization skipped (no MACOS_NOTARY_* secrets)."
fi
fi
(cd "$BUILD" && shasum -a 256 "$(basename "$DMG")" > "$(basename "$DMG").sha256")
step "Done"
echo "$DMG"
cat "$DMG.sha256"