FLAGS = -g
CFLAGS = -g
#-O
MACOS_VERSION = 13.0
SDK = $(shell xcrun --show-sdk-path)
XCODE_PATH = $(shell xcode-select --print-path)

EXEC = CmdBar

SRCMODULES = UpdateManager.swift LicenseManager.swift \
	AboutViewController.swift AppDelegate.swift CBMenuBarItem.swift \
	CircularProgressView.swift CmdManager.swift CmdViewController.swift \
	CopyableTextView.swift EditableNSTextField.swift EventMonitor.swift \
	Helpers.swift MenulessWindow.swift PopoverPanel.swift \
	QuietView.swift UpdateViewController.swift main.swift
ARMOBJMODULES = $(addprefix ./arm64/,$(SRCMODULES:.swift=.o))
X86OBJMODULES = $(addprefix ./x86_64/,$(SRCMODULES:.swift=.o))

FRAMEWORKS = -framework AppKit -framework ServiceManagement
#-emit-module-path

LIBS = -lzip

zip:
	@$(MAKE) -C libs/Zip/Zip FLAGS=$(FLAGS) CFLAGS=$(CFLAGS) \
		MACOS_VERSION=$(MACOS_VERSION) all

cmdbar_updater:
	@$(MAKE) -C updater FLAGS=$(FLAGS) all
	@cp updater/$@ .

./arm64/%.o: %.swift
	swift -frontend -c $(if $(DEBUG), -D DEBUG,) \
		-target arm64-apple-macos$(MACOS_VERSION) $(FLAGS) \
		-I./libs/Zip/Zip -I./libs/Zip/Zip/arm64 -L./libs/Zip/Zip/arm64 \
		-primary-file $< $(filter-out $<, $(SRCMODULES)) $(LIBS) \
		-sdk $(SDK) -module-name $(EXEC) -o $@ -emit-module && touch $@

ifdef UNIVERSAL
./x86_64/%.o: %.swift
	@swift -frontend -c $(if $(DEBUG), -D DEBUG,) \
		-target x86_64-apple-macos$(MACOS_VERSION) $(FLAGS) \
		-I./libs/Zip/Zip -I./libs/Zip/Zip/x86_64 -L./libs/Zip/Zip/x86_64 \
		-primary-file $< $(filter-out $<, $(SRCMODULES)) $(LIBS) \
		-sdk $(SDK) -module-name $(EXEC) -o $@ -emit-module && touch $@
endif
./arm64/$(EXEC): $(ARMOBJMODULES)
	@ld -syslibroot $(SDK) -lSystem -arch arm64 -macos_version_min \
		$(MACOS_VERSION).0 \
		/Library/Developer/CommandLineTools/usr/lib/swift/macosx/libswiftCompatibilityPacks.a \
		-sectcreate __TEXT __info_plist Info.plist \
		-L /Library/Developer/CommandLineTools/usr/lib/swift/macosx -L \
		/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/swift \
		-no_objc_category_merging -L $(XCODE_PATH) -rpath \
		Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx \
		./arm64/main.o $(filter-out ./arm64/main.o, $(ARMOBJMODULES)) \
		./libs/Zip/Zip/arm64/libzip.a -o $@

ifdef UNIVERSAL
./x86_64/$(EXEC): $(X86OBJMODULES)
	@ld -syslibroot $(SDK) -lSystem -arch x86_64 -macos_version_min \
		$(MACOS_VERSION).0 \
		/Library/Developer/CommandLineTools/usr/lib/swift/macosx/libswiftCompatibilityPacks.a \
		-sectcreate __TEXT __info_plist Info.plist \
		-L /Library/Developer/CommandLineTools/usr/lib/swift/macosx -L \
		/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/swift \
		-no_objc_category_merging -L $(XCODE_PATH) -rpath \
		Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx \
		./x86_64/main.o $(filter-out ./x86_64/main.o, $(X86OBJMODULES)) \
		./libs/Zip/Zip/x86_64/libzip.a -o $@
endif

ifdef UNIVERSAL
$(EXEC): ./arm64/$(EXEC) ./x86_64/$(EXEC)
	@lipo -create -output $(EXEC) $^
else
$(EXEC): ./arm64/$(EXEC)
	lipo -create -output $(EXEC) $^
endif

$(EXEC).app: $(EXEC)
	@rm -rf $@
	@mkdir -p $@/Contents/MacOS/ && \
	mkdir -p $@/Contents/Resources/ && \
	cp Info.plist $@/Contents/ && \
	cp resources/AppIcon.icns $@/Contents/Resources/ && \
	cp $(EXEC) $@/Contents/MacOS/ && \
	cp cmdbar_updater $@/Contents/MacOS/ && \
	$(if $(DEBUG), \
		codesign --entitlements Grapp.entitlements \
		-s ${APPLE_DEVELOPMENT} -f --timestamp -o runtime $(EXEC).app, \
		codesign -s ${APPLE_DEVELOPER_ID_APPLICATION} -f --timestamp \
		-o runtime $(EXEC).app)

all: zip cmdbar_updater $(EXEC).app

run: all
	@open $(EXEC).app

clean:
	rm -rf $(EXEC) $(EXEC).app cmdbar_updater arm64 x86_64
	mkdir arm64 x86_64

clean-all: clean
	$(MAKE) -C libs/Zip/Zip clean-all
	$(MAKE) -C updater clean
