# Sizzle for iOS/iPadOS — build entrypoint (separate from the Android Makefile).
#
# app/    the Sizzle app (cc.tiredsysadmin.ios.sizzle): Sizzle's devices as AUv3
#         plugins for hosts like AUM, installed with TrollStore. iOS/iPadOS 15+.
# core/   the C++ DSP core those plugins run: a port of the Android app's Kotlin
#         DSP, checked sample by sample against audio the Kotlin code renders.
# spike/  the TrollStore AUv3 feasibility spike (kept for reference).
#
#   make help        what each target does
#   make golden      export DeviceSpecs.gen.hpp + Kotlin reference audio (needs the
#                    Android toolchain; runs IosGoldenExport in ../app)
#   make core-test   build the C++ core and compare it with the reference audio
#                    (TYPES="FILTER EQ10" limits it to some devices)
#   make au-test     macOS check of the AUv3 wrapper: every effect through AVAudioEngine
#                    must match the core, plus parameters, presets and saved state
#   make project     generate app/Sizzle.xcodeproj (components + XcodeGen)
#   make ipa         unsigned device build, ad-hoc signed with entitlements, packed as
#                    dist/cc.tiredsysadmin.ios.sizzle_<versionCode>.ipa for TrollStore
#   make bump-minor  new feature  -> x.(y+1).0, versionCode +1 (also bump-patch/-major)
#   make spike-ipa   the spike's .ipa (dist/cc.tiredsysadmin.ios.sizzlespike_<build>.ipa)
#   make clean       remove build/ and dist/
#
# Needs Xcode and XcodeGen (brew install xcodegen). Builds stay local (CLAUDE.md).

SHELL      := /bin/sh
VERSIONS   := ../versions.properties
VERSION    := $(shell sed -n 's/^versionName[[:space:]]*=[[:space:]]*//p' $(VERSIONS) | tr -d ' ')
BUILD_NO   := $(shell sed -n 's/^versionCode[[:space:]]*=[[:space:]]*//p' $(VERSIONS) | tr -d ' ')

APP_ID     := cc.tiredsysadmin.ios.sizzle
PROJECT    := app/Sizzle.xcodeproj
APP_SRC    := $(shell find app ../core/src ../core/include -type f -not -path '*.xcodeproj/*')
DEV_APP    := build/dev/Build/Products/Release-iphoneos/Sizzle.app
IPA        := dist/$(APP_ID)_$(BUILD_NO).ipa

# C++ DSP core: reference audio lives in ../core/golden/ (regenerated, not committed).
CORE_SRC   := $(wildcard ../core/src/*.cpp ../core/src/*/*.cpp)
CORE_HDR   := $(wildcard ../core/include/sizzle/*.hpp ../core/src/*/*.hpp)
CXX        ?= c++
CXXFLAGS   := -std=c++17 -O2 -Wall -Wextra -I../core/include -I../core/src -I../core/src/fx -lz
GOLDEN     := ../core/golden
SPECS_GEN  := ../core/include/sizzle/DeviceSpecs.gen.hpp
# Gradle needs the Android SDK; same default location as the Android Makefile.
ANDROID_SDK ?= $(or $(ANDROID_HOME),$(HOME)/Library/Android/sdk)

SPIKE_PROJ := spike/SizzleSpike.xcodeproj
SPIKE_SRC  := $(shell find spike -type f -not -path '*.xcodeproj/*')
SPIKE_NO   := $(shell sed -n 's/^ *CURRENT_PROJECT_VERSION: *"\(.*\)"/\1/p' spike/project.yml)
SPIKE_IPA  := dist/cc.tiredsysadmin.ios.sizzlespike_$(SPIKE_NO).ipa

.DEFAULT_GOAL := help

help:
	@sed -n 's/^#   //p' Makefile
	@echo ""
	@echo "current: $(APP_ID) $(VERSION) (build $(BUILD_NO))"

# ------------------------------------------------------------------ core
golden:
	cd .. && ANDROID_HOME=$(ANDROID_SDK) SIZZLE_GOLDEN_DIR=$(CURDIR)/$(GOLDEN) \
		SIZZLE_SPECS_OUT=$(CURDIR)/$(SPECS_GEN) SIZZLE_LAYOUTS_OUT=$(CURDIR)/app/FX/DeviceLayouts.json \
		./gradlew -q :app:testDebugUnitTest --tests '*IosGolden*' --rerun
	@ls $(GOLDEN)/*.scn | wc -l | xargs echo "reference scenarios:"

build/core/golden_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/golden_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/golden_test.cpp

build/core/sampler_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/sampler_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -I../core/src/sampler -o $@ $(CORE_SRC) ../core/tests/sampler_test.cpp

build/core/engine_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/engine_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/engine_test.cpp

build/core/io_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/io_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/io_test.cpp

build/core/host_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/host_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/host_test.cpp

build/core/midimap_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/midimap_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/midimap_test.cpp

build/core/bundle_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/bundle_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/bundle_test.cpp

build/core/oneshot_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/oneshot_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/oneshot_test.cpp

build/core/midi_test: ../core/include/sizzle/Midi.hpp ../core/tests/midi_test.cpp
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -o $@ ../core/tests/midi_test.cpp

build/core/zip_test: ../core/src/io/Zip.cpp ../core/src/io/Zip.hpp ../core/tests/zip_test.cpp
	mkdir -p build/core/zipfix/dir
	$(CXX) $(CXXFLAGS) -o $@ ../core/src/io/Zip.cpp ../core/tests/zip_test.cpp -lz

# A Java-written archive (ZipOutputStream, like Android's) for zip_test, and ours checked by unzip.
zip-test: build/core/zip_test
	printf 'hello\n' > build/core/zipfix/a.txt
	head -c 4096 /dev/urandom > build/core/zipfix/dir/b.bin
	cd build/core/zipfix && rm -f ../java.zip && jar cfM ../java.zip a.txt dir/b.bin
	build/core/zip_test build/core/java.zip build/core/ours.zip
	unzip -tq build/core/ours.zip

core-test: build/core/golden_test build/core/sampler_test build/core/engine_test build/core/io_test build/core/midi_test build/core/host_test build/core/midimap_test build/core/bundle_test build/core/oneshot_test zip-test
	@test -f $(GOLDEN)/input.f32 || $(MAKE) --no-print-directory golden
	build/core/golden_test $(GOLDEN) $(TYPES)
	build/core/sampler_test
	build/core/engine_test $(GOLDEN)/songs
	build/core/io_test $(GOLDEN)/songs ../app/src/test/resources/mikroseq
	build/core/midi_test
	build/core/host_test $(GOLDEN)/songs
	build/core/midimap_test ../app/src/test/resources/midimap/filter.szm
	build/core/oneshot_test
	build/core/bundle_test $(GOLDEN)/bundles/android_sampler.zip ../app/src/test/resources/presetbundle/ios_sampler.zip

# ------------------------------------------------------------------ app
AU_SRC     := app/FX/SizzleDeviceAU.mm app/FX/SizzleEffectAU.mm app/FX/SizzleInstrumentAU.mm app/FX/SizzleDeviceAU+Sources.mm
build/core/au_test: $(AU_SRC) $(wildcard app/FX/*.h app/FX/*.hpp) app/tests/au_test.mm $(CORE_SRC) $(CORE_HDR)
	mkdir -p build/core
	$(CXX) $(CXXFLAGS) -Wno-deprecated-declarations -fobjc-arc -Iapp/FX -o $@ $(AU_SRC) app/tests/au_test.mm $(CORE_SRC) \
		-framework AVFoundation -framework AudioToolbox -framework Foundation -framework CoreMIDI

au-test: build/core/au_test
	@test -f $(GOLDEN)/input.f32 || $(MAKE) --no-print-directory golden
	build/core/au_test $(GOLDEN)/input.f32

# Mac snapshots of the SwiftUI device editors (AUAudioUnit and SwiftUI exist on
# macOS too): the AU classes + core as a dylib, the editor Swift on top.
EDITOR_SWIFT := $(shell find app/FX/Editor -name '*.swift')
build/snapshots/libsizzlesnap.dylib: $(AU_SRC) app/tests/SnapshotComponents.mm $(CORE_SRC) $(CORE_HDR)
	mkdir -p build/snapshots
	$(CXX) $(CXXFLAGS) -Wno-deprecated-declarations -fobjc-arc -Iapp/FX -dynamiclib -install_name @rpath/libsizzlesnap.dylib \
		-o $@ $(AU_SRC) app/tests/SnapshotComponents.mm $(CORE_SRC) -framework AVFoundation -framework AudioToolbox -framework Foundation

build/snapshots/editor_snapshots: build/snapshots/libsizzlesnap.dylib $(EDITOR_SWIFT) app/tests/editor_snapshots.swift
	swiftc -O -parse-as-library -import-objc-header app/tests/SnapshotBridging.h -Iapp/FX -Iapp/tests \
		-o $@ $(EDITOR_SWIFT) app/tests/editor_snapshots.swift -Lbuild/snapshots -lsizzlesnap -Xlinker -rpath -Xlinker @executable_path
	cp app/FX/DeviceLayouts.json build/snapshots/

editor-snapshots: build/snapshots/editor_snapshots
	cd build/snapshots && ./editor_snapshots png

SNAPDIR ?= build/screens
# Mac snapshots of the app's tabs with the demo song (SizzleSession runs on macOS
# too, without audio). Screens must stay SwiftUI-only (no UIKit) to render here.
SCREEN_SWIFT := $(EDITOR_SWIFT) app/Host/AppModel.swift app/Host/RootView.swift app/Host/SlotDeviceModel.swift \
	app/Host/KeyboardView.swift app/Host/SlotDataSource.swift app/Host/SampleRecorder.swift app/Host/AppSettings.swift app/Host/AppBoot.swift app/Host/ThemeLibrary.swift app/Host/BluetoothMidi.swift \
	$(shell find app/Host/Screens app/Host/Bespoke app/Host/Input -name '*.swift')
$(SNAPDIR)/libsizzlescreens.dylib: $(AU_SRC) $(wildcard app/Host/SizzleSession*.mm app/Host/SizzleSession*.h) app/Host/SizzleMidi.mm app/Host/SizzleMidi.h app/tests/SnapshotComponents.mm $(CORE_SRC) $(CORE_HDR)
	mkdir -p $(SNAPDIR)
	$(CXX) $(CXXFLAGS) -Wno-deprecated-declarations -fobjc-arc -Iapp/FX -Iapp/Host -dynamiclib -install_name @rpath/libsizzlescreens.dylib \
		-o $@ $(AU_SRC) $(wildcard app/Host/SizzleSession*.mm) app/Host/SizzleMidi.mm app/tests/SnapshotComponents.mm $(CORE_SRC) \
		-framework AVFoundation -framework AudioToolbox -framework Foundation -framework CoreMIDI

$(SNAPDIR)/screen_snapshots: $(SNAPDIR)/libsizzlescreens.dylib $(SCREEN_SWIFT) app/tests/screen_snapshots.swift
	swiftc -O -parse-as-library -import-objc-header app/tests/ScreenBridging.h -Iapp/FX -Iapp/Host -Iapp/tests \
		-o $@ $(SCREEN_SWIFT) app/tests/screen_snapshots.swift -L$(SNAPDIR) -lsizzlescreens -Xlinker -rpath -Xlinker @executable_path
	cp app/FX/DeviceLayouts.json $(SNAPDIR)/

screen-snapshots: $(SNAPDIR)/screen_snapshots
	cd $(SNAPDIR) && ./screen_snapshots png $(CURDIR)/../samples/demo.sng

app/FX/Info.plist app/FX/Components.gen.h: app/components.txt tools/gen_components.py
	python3 tools/gen_components.py app/components.txt app/FX/Info.plist app/FX/Components.gen.h

project: $(PROJECT)/project.pbxproj
# The folders too: a directory's time changes when a file is added or removed, and
# the generated project lists every source file.
SRC_DIRS   := $(shell find app ../core/src ../core/include -type d -not -path '*.xcodeproj*')
$(PROJECT)/project.pbxproj: app/project.yml app/FX/Info.plist $(SRC_DIRS)
	cd app && xcodegen generate --spec project.yml --quiet
	touch $@

# Device: build without signing, then ad-hoc sign inside-out (extension first) with
# each target's entitlements. TrollStore re-signs at install and keeps them.
ipa: $(IPA)
$(IPA): $(PROJECT)/project.pbxproj $(APP_SRC) $(VERSIONS)
	xcodebuild -quiet -project $(PROJECT) -scheme Sizzle -configuration Release \
		-sdk iphoneos -derivedDataPath build/dev CODE_SIGNING_ALLOWED=NO \
		MARKETING_VERSION=$(VERSION) CURRENT_PROJECT_VERSION=$(BUILD_NO) build
	rm -rf build/Payload && mkdir -p build/Payload dist
	cp -R $(DEV_APP) build/Payload/
	codesign -f -s - --entitlements app/FX/FX.entitlements build/Payload/Sizzle.app/PlugIns/SizzleFX.appex
	codesign -f -s - --entitlements app/Host/Host.entitlements build/Payload/Sizzle.app
	rm -f $@ && cd build && zip -qr ../$@ Payload
	@echo "wrote $@"

# ------------------------------------------------------------------ versions
bump-major: ; @$(MAKE) --no-print-directory bump FIELD=1
bump-minor: ; @$(MAKE) --no-print-directory bump FIELD=2
bump-patch: ; @$(MAKE) --no-print-directory bump FIELD=3
bump:   # one version for Android and iOS: the root Makefile owns it
	@$(MAKE) -C .. --no-print-directory bump FIELD=$(FIELD)

# ------------------------------------------------------------------ spike
$(SPIKE_PROJ)/project.pbxproj: spike/project.yml
	cd spike && xcodegen generate --spec project.yml --quiet

spike-ipa: $(SPIKE_IPA)
$(SPIKE_IPA): $(SPIKE_PROJ)/project.pbxproj $(SPIKE_SRC)
	xcodebuild -quiet -project $(SPIKE_PROJ) -scheme SizzleSpike -configuration Release \
		-sdk iphoneos -derivedDataPath build/spike CODE_SIGNING_ALLOWED=NO build
	rm -rf build/SpikePayload && mkdir -p build/SpikePayload/Payload dist
	cp -R build/spike/Build/Products/Release-iphoneos/SizzleSpike.app build/SpikePayload/Payload/
	codesign -f -s - --entitlements spike/AU/AU.entitlements \
		build/SpikePayload/Payload/SizzleSpike.app/PlugIns/SizzleSpikeAU.appex
	codesign -f -s - --entitlements spike/App/App.entitlements build/SpikePayload/Payload/SizzleSpike.app
	rm -f $@ && cd build/SpikePayload && zip -qr ../../$@ Payload
	@echo "wrote $@"

clean:
	rm -rf build dist

.PHONY: help golden core-test zip-test au-test project ipa bump-major bump-minor bump-patch bump spike-ipa clean
