# Singularity for macOS (Apple silicon): static recompilation build.
#
#   make gen        translate the game's executable and DLLs to C, and make the changed menu package in
#                   build/Override (needs the game folder, see GAME)
#   make            compile the translated code and the runtime, and link build/Singularity
#   make app        package build/Singularity.app
#
# The translated code, everything linked from it and build/Override contain the game's code and data: never
# distribute them.

GAME    ?= ..
PYTHON  ?= python3
PHYSX   ?= build/physx
GEN     := build/gen
# game files that the runtime opens instead of the game's own files (runtime/files.c); they contain game data
OVERRIDE := build/Override
OBJ     := build/obj
CC      := clang
ARCH    := -arch arm64 -mmacosx-version-min=14.0

GEN_CFLAGS := $(ARCH) -O2 -std=gnu11 -ffp-contract=off -fno-math-errno -Iruntime -Wall \
              -Wno-unused-label -Wno-unused-variable -Wno-unused-but-set-variable
RT_CFLAGS  := $(ARCH) -O2 -g -std=gnu11 -ffp-contract=off -Iruntime -Wall -Wextra -Wno-unused-parameter \
              -Wno-sign-compare -Wno-missing-field-initializers
RT_OBJCFLAGS := $(RT_CFLAGS) -fobjc-arc

TRANSLATED := binkw32.dll,vorbisfile.dll,vorbis.dll,ogg.dll,physxextensions.dll,physxloader.dll,physxcore.dll,physxcooking.dll,nxcooking.dll,physxdevice.dll

GEN_SRC := $(wildcard $(GEN)/*/code_*.c) $(wildcard $(GEN)/*/tables.c)
GEN_OBJ := $(patsubst $(GEN)/%.c,$(OBJ)/gen/%.o,$(GEN_SRC))

RT_C    := $(wildcard runtime/*.c)
RT_M    := $(wildcard runtime/*.m)
RT_OBJ  := $(patsubst runtime/%.c,$(OBJ)/rt/%.o,$(RT_C)) $(patsubst runtime/%.m,$(OBJ)/rt/%.o,$(RT_M))
METAL   := $(wildcard runtime/*.metal)

FRAMEWORKS := -framework Cocoa -framework Metal -framework MetalFX -framework QuartzCore -framework CoreAudio \
              -framework AudioToolbox -framework GameController -framework CoreHaptics -framework IOKit -framework CoreFoundation -framework CoreText

all: build/Singularity

$(OBJ)/gen/%.o: $(GEN)/%.c runtime/cpu.h runtime/module.h
	@mkdir -p $(dir $@)
	@$(CC) $(GEN_CFLAGS) -c $< -o $@

$(OBJ)/rt/%.o: runtime/%.c runtime/*.h
	@mkdir -p $(dir $@)
	$(CC) $(RT_CFLAGS) -c $< -o $@

# the game folder of the build: the launcher window shows it when the folder of the last Play cannot be used (main.m)
# (the shell, not $(abspath): a game folder path can have spaces)
GAME_ABS := $(shell cd "$(GAME)" 2>/dev/null && pwd)
$(OBJ)/rt/main.o: RT_OBJCFLAGS += -DSING_BUILD_GAME_DIR='"$(GAME_ABS)"'

# LOGGING=1 makes a development build: the runtime writes ~/Library/Logs/SingularityMac.log and stderr.
# The default (0) writes no log: the builds of the players. local.mk (not in git) can set LOGGING on a
# development Mac. The stamp file makes a change of LOGGING compile the files that use it again.
LOGGING ?= 0
-include local.mk
LOG_USERS := $(OBJ)/rt/rt.o $(OBJ)/rt/files.o $(OBJ)/rt/launcher.o
LOG_STAMP := $(OBJ)/logging-$(LOGGING)
$(LOG_STAMP):
	@mkdir -p $(OBJ)
	@rm -f $(OBJ)/logging-*
	@touch $@
$(LOG_USERS): $(LOG_STAMP)
$(LOG_USERS): RT_CFLAGS += -DSING_LOGGING=$(LOGGING)
$(OBJ)/rt/launcher.o: RT_OBJCFLAGS += -DSING_LOGGING=$(LOGGING)

$(OBJ)/rt/%.o: runtime/%.m runtime/*.h
	@mkdir -p $(dir $@)
	$(CC) $(RT_OBJCFLAGS) -c $< -o $@

build/libgame.a: $(GEN_OBJ)
	@rm -f $@
	@echo "archive $(words $(GEN_OBJ)) translated objects"
	@libtool -static -o $@ $(GEN_OBJ) 2>/dev/null

build/Singularity: build/libgame.a $(RT_OBJ)
	$(CC) $(ARCH) -o $@ $(RT_OBJ) -Wl,-all_load build/libgame.a $(FRAMEWORKS) -lc++

gen:
	"$(PYTHON)" tools/translate.py "$(GAME)"/Binaries/Singularity.exe Singularity.exe $(GEN)/singularity --translated-dlls $(TRANSLATED) --hooks hooks.txt
	"$(PYTHON)" tools/patch_ui.py "$(GAME)"/RvGame/CookedPC/UI_SinglePlayer_SF.xxx $(OVERRIDE)/RvGame/CookedPC/UI_SinglePlayer_SF.xxx
	"$(PYTHON)" tools/translate.py "$(GAME)"/Binaries/binkw32.dll binkw32.dll $(GEN)/binkw32 --base 18000000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(GAME)"/Binaries/ogg.dll ogg.dll $(GEN)/ogg --base 10900000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(GAME)"/Binaries/vorbis.dll vorbis.dll $(GEN)/vorbis --base 10A00000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(GAME)"/Binaries/vorbisfile.dll vorbisfile.dll $(GEN)/vorbisfile --base 10B00000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(GAME)"/Binaries/PhysXExtensions.dll PhysXExtensions.dll $(GEN)/physxextensions --base 10700000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(GAME)"/Binaries/NxCooking.dll NxCooking.dll $(GEN)/nxcooking --base 10800000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(PHYSX)"/PhysXLoader.dll PhysXLoader.dll $(GEN)/physxloader --base 10000000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(PHYSX)"/PhysXCore.dll PhysXCore.dll $(GEN)/physxcore --base 10200000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(PHYSX)"/PhysXCooking.dll PhysXCooking.dll $(GEN)/physxcooking --base 10600000 --translated-dlls $(TRANSLATED)
	"$(PYTHON)" tools/translate.py "$(PHYSX)"/PhysXDevice.dll PhysXDevice.dll $(GEN)/physxdevice --base 10C00000 --translated-dlls $(TRANSLATED)

genobj: $(GEN_OBJ)

# The app bundle for this Mac. It contains the game's code and art: do not distribute it.
# The art comes from the game folder (GAME): the icon of the GOG release is the app icon (AppIcon.icns), and the
# splash image of the game is the logo of the launcher window (Splash.bmp, launcher.m).
APP := build/Singularity.app
GAME_ICON := $(GAME)/goggame-1207659239.ico
GAME_SPLASH := $(GAME)/RvGame/Splash/PC/Splash.bmp
app: build/Singularity runtime/Info.plist $(OVERRIDE)/RvGame/CookedPC/UI_SinglePlayer_SF.xxx
	@test -f "$(GAME_ICON)" || { echo "The game folder has no $(GAME_ICON) (the icon of the GOG release)."; exit 1; }
	@test -f "$(GAME_SPLASH)" || { echo "The game folder has no $(GAME_SPLASH) (the splash image of the game)."; exit 1; }
	rm -rf $(APP)
	mkdir -p $(APP)/Contents/MacOS
	cp build/Singularity $(APP)/Contents/MacOS/Singularity
	cp runtime/Info.plist $(APP)/Contents/Info.plist
	mkdir -p $(APP)/Contents/Resources
	cp -R $(OVERRIDE) $(APP)/Contents/Resources/Override
	sips -s format png "$(GAME_ICON)" --out build/AppIcon.png >/dev/null
	sips -s format icns build/AppIcon.png --out $(APP)/Contents/Resources/AppIcon.icns >/dev/null
	cp "$(GAME_SPLASH)" $(APP)/Contents/Resources/Splash.bmp
	plutil -lint $(APP)/Contents/Info.plist
	codesign --force --sign - $(APP)

clean:
	rm -rf $(OBJ) build/libgame.a build/Singularity build/Singularity.app build/AppIcon.png

.PHONY: all gen genobj app clean

# The app of a GitHub release (release/first_run.m): no game code and no game files, only the source code of this
# repository (git ls-files) in Contents/Resources/Source. On the first start it asks for the game folder of the player
# and runs install.sh with it, which makes the game app on that Mac. Build it from a clean checkout.
RELEASE := build/release
RELEASE_APP := $(RELEASE)/Singularity.app
RELEASE_ZIP := $(RELEASE)/Singularity-Mac-ARM64.zip
release: release/first_run.m release/Info.plist
	rm -rf $(RELEASE)
	mkdir -p $(RELEASE_APP)/Contents/MacOS $(RELEASE_APP)/Contents/Resources/Source
	$(CC) $(ARCH) -O2 -std=gnu11 -Wall -Wextra -fobjc-arc -o $(RELEASE_APP)/Contents/MacOS/Singularity release/first_run.m -framework Cocoa
	cp release/Info.plist $(RELEASE_APP)/Contents/Info.plist
	git ls-files -z | xargs -0 -I{} rsync -R "{}" $(RELEASE_APP)/Contents/Resources/Source/
	plutil -lint $(RELEASE_APP)/Contents/Info.plist
	codesign --force --sign - $(RELEASE_APP)
	cd $(RELEASE) && ditto -c -k --keepParent Singularity.app Singularity-Mac-ARM64.zip

.PHONY: release
