# Build a runnable Purr.app bundle from the SwiftPM executable.
#
# Why this Makefile exists: macOS gates the microphone and accessibility
# permissions on a real .app bundle with a code signature and an Info.plist.
# `swift run` ships a bare Mach-O without those, so TCC silently denies
# everything. This Makefile assembles a minimal .app and code-signs it so the
# OS will route TCC prompts to it.

APP_NAME      := Purr
CONFIG        := release
BUILD_DIR     := .build
APP_DIR       := dist/$(APP_NAME).app
DMG           := dist/$(APP_NAME).dmg
DMG_RW        := dist/$(APP_NAME)-rw.dmg
DMG_STAGE     := dist/dmg-stage
DMG_MNT       := dist/dmg-mnt
DMG_BG        := Resources/dmg-launch-window.png
# Pre-baked Finder window settings (window bounds, icon positions, background
# picture pointer). Copied verbatim into every DMG so the layout doesn't depend
# on scripting Finder, which fails intermittently on macOS Sequoia/Tahoe
# (TCC Automation prompts, AppleEvent timeouts, hdiutil-vs-Finder races - see
# create-dmg/create-dmg issues #72, #154, #185).
DMG_DS_STORE  := Resources/dmg-DS_Store
CONTENTS      := $(APP_DIR)/Contents
MACOS_DIR     := $(CONTENTS)/MacOS
RES_DIR       := $(CONTENTS)/Resources
FRAMEWORKS_DIR:= $(CONTENTS)/Frameworks
ENTITLEMENTS  := Resources/$(APP_NAME).entitlements
INFO_PLIST    := Resources/Info.plist
# Developer ID code-signing identity and notarization keychain profile.
# Defaults to ad-hoc ("-") so `make app` builds and runs on a clean checkout
# with no certificate in the keychain. For a real release, override DEV_ID
# with an identity from `security find-identity -p codesigning -v`; the
# NOTARY_PROFILE keychain profile is created once with
# `xcrun notarytool store-credentials`.
DEV_ID         ?= -
NOTARY_PROFILE ?= purr-app
# Hardened Runtime (Library Validation) refuses to load an ad-hoc-signed
# embedded framework at all, even alongside an ad-hoc-signed outer app -
# https://developer.apple.com/forums/thread/695481 - so it can only be
# enabled once a real Developer ID identity is signing everything. Ad-hoc
# local builds skip it entirely and fall back to the legacy, more permissive
# validation path; notarization (which requires it) always supplies a real
# DEV_ID via `make dmg`.
ifeq ($(DEV_ID),-)
CODESIGN_OPTS :=
else
CODESIGN_OPTS := --options runtime --timestamp
endif
# Where SwiftPM put the build. Its layout depends on the toolchain (Xcode 27
# builds into .build/out/Products/Release), so ask SwiftPM instead of assuming
# one. `$$(...)` runs in the recipe's shell, which gets the DEVELOPER_DIR
# exported below; make 3.81's $(shell) wouldn't, and could ask another Swift.
BIN_DIR = $$(swift build -c $(CONFIG) --arch arm64 --show-bin-path)
# llama.cpp is shipped as an XCFramework binary target. SwiftPM stages
# its macOS framework (arm64 and x86_64) next to the built executable; we
# copy that into the bundle's Frameworks/, keep only arm64, and add the
# rpath the executable needs to find it at runtime (the linker's default
# @loader_path works when the binary sits beside the framework, but the
# .app layout splits them).
LLAMA_FRAMEWORK_SRC = $(BIN_DIR)/llama.framework
# Sparkle, the updater, arrives the same way. Its sign_update tool signs each
# release's DMG with the EdDSA key that `generate_keys --account purr` put in
# the login keychain. Installed copies trust only updates signed with that
# key (SUPublicEDKey in Info.plist), so keep it backed up.
SPARKLE_FRAMEWORK_SRC = $(BIN_DIR)/Sparkle.framework
SPARKLE_BIN    := $(BUILD_DIR)/artifacts/sparkle/Sparkle/bin
SPARKLE_KEY    ?= purr
APPCAST        := dist/appcast.xml

# The @Generable / @Guide macros (FoundationModels) need Xcode's compiler
# plugin (libFoundationModelsMacros), which the standalone Command Line
# Tools toolchain does not ship - `swift build` under CLT fails with
# "plugin for module 'FoundationModelsMacros' not found". Pin the build to
# Xcode's toolchain. Override on the command line if Xcode lives elsewhere:
# `make dmg DEVELOPER_DIR=/path/to/Xcode.app/Contents/Developer`.
DEVELOPER_DIR ?= /Applications/Xcode.app/Contents/Developer
export DEVELOPER_DIR

.PHONY: app run dmg notarize-app appcast clean

app:
	swift build -c $(CONFIG) --arch arm64
	@mkdir -p $(MACOS_DIR) $(RES_DIR) $(FRAMEWORKS_DIR)
	@cp "$(BIN_DIR)/$(APP_NAME)" $(MACOS_DIR)/$(APP_NAME)
	@cp $(INFO_PLIST) $(CONTENTS)/Info.plist
	@# Legacy package-type marker that every standard .app carries; some macOS
	@# subsystems still read it. Cheap to include, and a hand-assembled SwiftPM
	@# bundle omits it by default.
	@printf 'APPL????' > $(CONTENTS)/PkgInfo
	@cp Resources/AppIcon.icns $(RES_DIR)/AppIcon.icns
	@cp Resources/purr_menubar_glyph.pdf $(RES_DIR)/purr_menubar_glyph.pdf
	@# Embed llama.cpp dynamic framework. -R preserves the Headers ->
	@# Versions/Current/Headers symlinks; dereferencing them (cp -RHL)
	@# flattens the framework into a layout codesign rejects as
	@# ambiguous. -p keeps perms/timestamps so the signature stays
	@# reproducible across builds.
	@rm -rf $(FRAMEWORKS_DIR)/llama.framework
	@cp -Rp "$(LLAMA_FRAMEWORK_SRC)" $(FRAMEWORKS_DIR)/llama.framework
	@# Purr is built for arm64 only, so the x86_64 half is dead weight.
	@lipo $(FRAMEWORKS_DIR)/llama.framework/Versions/A/llama -thin arm64 -output $(FRAMEWORKS_DIR)/llama.framework/Versions/A/llama
	@# Add @executable_path/../Frameworks to the binary's rpath. Quiet if
	@# already present (re-runs of `make app` would otherwise fail).
	@install_name_tool -add_rpath @executable_path/../Frameworks $(MACOS_DIR)/$(APP_NAME) 2>/dev/null || true
	@# Sign the embedded framework first so the nested code is already valid
	@# when we sign the outer bundle (no --deep: Apple recommends signing
	@# inside-out for distribution). CODESIGN_OPTS adds Hardened Runtime +
	@# a secure timestamp only when DEV_ID is a real identity (see above).
	@codesign --force $(CODESIGN_OPTS) --sign "$(DEV_ID)" $(FRAMEWORKS_DIR)/llama.framework
	@# Sparkle's XPC services are only for sandboxed apps, so they're left
	@# out. Its installer and updater helpers are signed before the framework.
	@rm -rf $(FRAMEWORKS_DIR)/Sparkle.framework
	@cp -Rp "$(SPARKLE_FRAMEWORK_SRC)" $(FRAMEWORKS_DIR)/Sparkle.framework
	@rm -rf $(FRAMEWORKS_DIR)/Sparkle.framework/Versions/B/XPCServices
	@codesign --force $(CODESIGN_OPTS) --sign "$(DEV_ID)" $(FRAMEWORKS_DIR)/Sparkle.framework/Versions/B/Autoupdate
	@codesign --force $(CODESIGN_OPTS) --sign "$(DEV_ID)" $(FRAMEWORKS_DIR)/Sparkle.framework/Versions/B/Updater.app
	@codesign --force $(CODESIGN_OPTS) --sign "$(DEV_ID)" $(FRAMEWORKS_DIR)/Sparkle.framework
	@codesign --force $(CODESIGN_OPTS) --sign "$(DEV_ID)" --entitlements $(ENTITLEMENTS) $(APP_DIR)
	@# Bump the bundle's mtime. macOS keys its icon/LaunchServices cache on the
	@# bundle modification date; copying a new AppIcon.icns into an existing
	@# .app leaves the bundle dir mtime stale, so Finder keeps serving the old
	@# cached icon. Touching the bundle invalidates that cache on next read.
	@touch $(APP_DIR)
	@echo "Built $(APP_DIR) - open it once from Finder so macOS registers the bundle, then permissions can be granted in System Settings."

run: app
	open $(APP_DIR)

# Notarize the .app: zip it, submit to Apple's notary service, wait for the
# ticket, then staple it onto the bundle so the app passes Gatekeeper offline
# even after the updater copies it out of the DMG into /Applications.
notarize-app: app
	@ditto -c -k --keepParent $(APP_DIR) $(APP_DIR).zip
	@xcrun notarytool submit $(APP_DIR).zip --keychain-profile "$(NOTARY_PROFILE)" --wait
	@xcrun stapler staple $(APP_DIR)
	@rm -f $(APP_DIR).zip

# Wraps the signed app bundle in a compressed read-only .dmg, notarizes the
# app and the disk image, and staples both, so Gatekeeper accepts a normal
# double-click (no right-click -> Open needed). Needs DEV_ID + NOTARY_PROFILE.
#
# The DMG is laid out as a "drag to Applications" install window: a custom
# background image with a curved arrow plus an Applications symlink. The
# Finder window settings (icon positions + background picture) live in a
# pre-baked .DS_Store (Resources/dmg-DS_Store) we copy in directly.
dmg: notarize-app
	@rm -rf $(DMG) $(DMG_RW) $(DMG_STAGE)
	@mkdir -p $(DMG_STAGE)/.background
	@cp -R $(APP_DIR) $(DMG_STAGE)/
	@cp $(DMG_BG) $(DMG_STAGE)/.background/background.png
	@cp $(DMG_DS_STORE) $(DMG_STAGE)/.DS_Store
	@# Volume icon: Finder shows .VolumeIcon.icns for the mounted disk (and the
	@# .dmg file itself) once the volume root carries the kHasCustomIcon bit.
	@cp Resources/AppIcon.icns $(DMG_STAGE)/.VolumeIcon.icns
	@ln -s /Applications $(DMG_STAGE)/Applications
	@hdiutil create -volname "$(APP_NAME)" \
	  -srcfolder $(DMG_STAGE) \
	  -ov -format UDRW \
	  -fs HFS+ \
	  $(DMG_RW) >/dev/null
	@# Set kHasCustomIcon on the volume root so .VolumeIcon.icns takes effect.
	@# Done by mounting the read-write image and writing the attribute directly
	@# (SetFile, not osascript/Finder), so it keeps the AppleEvents-free
	@# guarantee that motivated the pre-baked .DS_Store.
	@rm -rf $(DMG_MNT) && mkdir -p $(DMG_MNT)
	@hdiutil attach $(DMG_RW) -nobrowse -noverify -noautoopen -mountpoint $(DMG_MNT) >/dev/null
	@SetFile -a C $(DMG_MNT)
	@hdiutil detach $(DMG_MNT) >/dev/null
	@rmdir $(DMG_MNT)
	@# ULFO (LZFSE) decompresses in-kernel and mounts faster than UDZO
	@# zlib-9, so Finder renders the window icons with less first-open lag.
	@# Requires macOS 10.11+ (we target 14+); the image is also slightly smaller.
	@hdiutil convert $(DMG_RW) -format ULFO -o $(DMG) >/dev/null
	@rm -rf $(DMG_RW) $(DMG_STAGE)
	@# Code-sign the disk image itself (Developer ID + secure timestamp) before
	@# notarizing, so the notary ticket covers the image and `spctl -a -t open`
	@# accepts the DMG directly. Apple doesn't require a signed DMG, but signing
	@# folds the image into the ticket for a clean Gatekeeper assessment. Order
	@# matters: sign -> notarize -> staple.
	@codesign --force --timestamp --sign "$(DEV_ID)" $(DMG)
	@# Notarize the disk image and staple the ticket onto it so the DMG is
	@# recognized offline. Stapling rewrites the DMG, so it MUST happen before
	@# the sha256 below or the published hash won't match the shipped file.
	@xcrun notarytool submit $(DMG) --keychain-profile "$(NOTARY_PROFILE)" --wait
	@xcrun stapler staple $(DMG)
	@# Sidecar SHA-256 published alongside the DMG. Computed last, on the
	@# stapled image, so it matches what users download. Format is
	@# `<hex>  Purr.dmg\n`, matching `shasum -a 256` output so anyone can
	@# `shasum -a 256 -c` it.
	@shasum -a 256 "$(DMG)" | awk '{ printf "%s  $(APP_NAME).dmg\n", $$1 }' > $(DMG).sha256
	@echo "Built $(DMG) ($$(du -h $(DMG) | cut -f1)) + $(DMG).sha256"

# The update feed for the release, from the DMG `make dmg` built: its EdDSA
# signature, and this version's CHANGELOG.md section as the release notes.
# Publish it as a release asset next to Purr.dmg, since installed copies read
# it from the latest release.
appcast:
	@scripts/appcast.sh $(DMG) "$(SPARKLE_BIN)/sign_update" $(SPARKLE_KEY) > $(APPCAST).tmp
	@mv $(APPCAST).tmp $(APPCAST)
	@echo "Built $(APPCAST)"

clean:
	rm -rf $(BUILD_DIR) dist
