default: lib

ifeq ($(shell uname -s),Darwin)
  MACOS_BUILD = 1
else ifeq ($(OS),Windows_NT)
  WINDOWS_BUILD = 1
endif

ifdef LIBSM64_MUSL
  CC      := musl-gcc
  CXX     := musl-g++
  LDFLAGS := -lm -static -shared
else
  CC      := cc
  CXX     := c++
  LDFLAGS := -lm -shared
endif
CFLAGS := -fno-strict-aliasing -g -Wall -Wno-unused-function -fPIC -fvisibility=hidden -DSM64_LIB_EXPORT -DGBI_FLOATS -DVERSION_US -DENABLE_RUMBLE -DNO_SEGMENTED_MEMORY

SRC_DIRS  := src src/decomp src/decomp/engine src/decomp/include/PR src/decomp/game src/decomp/pc src/decomp/pc/audio src/decomp/mario src/decomp/tools src/decomp/audio
BUILD_DIR := build
DIST_DIR  := dist
ALL_DIRS  := $(addprefix $(BUILD_DIR)/,$(SRC_DIRS))

LIB_FILE   := $(DIST_DIR)/sm64.so
LIB_H_FILE := $(DIST_DIR)/include/libsm64.h
TEST_FILE  := run-test

C_IMPORTED := src/decomp/mario/geo.inc.c src/decomp/mario/model.inc.c
H_IMPORTED := $(C_IMPORTED:.c=.h)
IMPORTED   := $(C_IMPORTED) $(H_IMPORTED)

C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) $(C_IMPORTED)
ifdef MACOS_BUILD
  O_FILES_x86_64 := $(foreach file,$(C_FILES),$(BUILD_DIR)/$(file:.c=_x86_64.o))
  O_FILES_arm64  := $(foreach file,$(C_FILES),$(BUILD_DIR)/$(file:.c=_arm64.o))
  DEP_FILES      := $(O_FILES_x86_64:.o=.d) $(O_FILES_arm64:.o=.d)
else
  O_FILES        := $(foreach file,$(C_FILES),$(BUILD_DIR)/$(file:.c=.o))
  DEP_FILES      := $(O_FILES:.o=.d)
endif

TEST_SRCS_C   := test/level.c test/gl33core/gl33core_renderer.c test/gl20/gl20_renderer.c
TEST_SRCS_CPP := test/main.cpp test/audio.cpp test/context.cpp \
                 test/imgui/imgui.cpp \
                 test/imgui/imgui_demo.cpp \
                 test/imgui/imgui_draw.cpp \
                 test/imgui/imgui_tables.cpp \
                 test/imgui/imgui_widgets.cpp \
                 test/imgui/backends/imgui_impl_sdl2.cpp \
                 test/imgui/backends/imgui_impl_opengl3.cpp
TEST_OBJS     := $(foreach file,$(TEST_SRCS_C),$(BUILD_DIR)/$(file:.c=.o)) $(foreach file,$(TEST_SRCS_CPP),$(BUILD_DIR)/$(file:.cpp=.o))

ifdef WINDOWS_BUILD
  LIB_FILE := $(DIST_DIR)/sm64.dll
  TEST_FILE := $(DIST_DIR)/run-test.exe
else ifdef MACOS_BUILD
  LIB_FILE := $(DIST_DIR)/sm64.dylib
endif

DUMMY := $(shell mkdir -p $(ALL_DIRS) build/test build/test/gl33core build/test/gl20 build/test/imgui build/test/imgui/backends src/decomp/mario $(DIST_DIR)/include)

$(filter-out src/decomp/mario/geo.inc.c,$(IMPORTED)): src/decomp/mario/geo.inc.c
src/decomp/mario/geo.inc.c: ./import-mario-geo.py
	./import-mario-geo.py

ifdef MACOS_BUILD
$(BUILD_DIR)/%_x86_64.o: %.c $(IMPORTED)
	@$(CC) $(CFLAGS) -arch x86_64 -I src/decomp/include -MM -MP -MT $@ -MF $(BUILD_DIR)/$*_x86_64.d $<
	$(CC) -c $(CFLAGS) -arch x86_64 -I src/decomp/include -o $@ $<

$(BUILD_DIR)/%_arm64.o: %.c $(IMPORTED)
	@$(CC) $(CFLAGS) -arch arm64 -I src/decomp/include -MM -MP -MT $@ -MF $(BUILD_DIR)/$*_arm64.d $<
	$(CC) -c $(CFLAGS) -arch arm64 -I src/decomp/include -o $@ $<

$(LIB_FILE): $(O_FILES_x86_64) $(O_FILES_arm64)
	$(CC) $(LDFLAGS) -arch arm64 -o $@.arm64 $(O_FILES_arm64)
	$(CC) $(LDFLAGS) -arch x86_64 -o $@.x86_64 $(O_FILES_x86_64)
	lipo -create -output $@ $@.arm64 $@.x86_64
	rm $@.arm64 $@.x86_64
else
$(BUILD_DIR)/%.o: %.c $(IMPORTED)
	@$(CC) $(CFLAGS) -I src/decomp/include -MM -MP -MT $@ -MF $(BUILD_DIR)/$*.d $<
	$(CC) -c $(CFLAGS) -I src/decomp/include -o $@ $<

$(LIB_FILE): $(O_FILES)
	$(CC) $(LDFLAGS) -o $@ $^
endif

$(LIB_H_FILE): src/libsm64.h
	cp -f $< $@

test/level.c: ./import-test-collision.py
	./import-test-collision.py

test/main.cpp test/gl20/gl20_renderer.c test/gl33core/gl33core_renderer.c: test/level.c

SDL2_RAW_CFLAGS := $(shell sdl2-config --cflags)
SDL2_CFLAGS := $(filter-out -Dmain=SDL_main,$(SDL2_RAW_CFLAGS))

$(BUILD_DIR)/test/%.o: test/%.c
	$(CC) -c $(CFLAGS) $(SDL2_CFLAGS) -I src/decomp/include -o $@ $<

$(BUILD_DIR)/test/%.o: test/%.cpp
	$(CXX) -c $(CFLAGS) $(SDL2_CFLAGS) -I src/decomp/include -std=c++17 -o $@ $<

$(TEST_FILE): $(LIB_FILE) $(TEST_OBJS)
ifdef WINDOWS_BUILD
	$(CXX) -o $@ $(TEST_OBJS) $(LIB_FILE) -lglew32 -lopengl32 -lSDL2 -lm
else ifdef MACOS_BUILD
	$(CXX) -o $@ $(TEST_OBJS) $(LIB_FILE) -framework OpenGL -lGLEW -lSDL2 -lSDL2main -lm -lpthread
else
	$(CXX) -o $@ $(TEST_OBJS) $(LIB_FILE) -lGLEW -lGL $(shell sdl2-config --libs) -lm -lpthread
endif

lib: $(LIB_FILE) $(LIB_H_FILE)

test: $(TEST_FILE) $(LIB_H_FILE)

run: test
ifdef WINDOWS_BUILD
	cd dist && ./run-test
else
	./$(TEST_FILE)
endif

clean:
	rm -rf $(BUILD_DIR) $(DIST_DIR) $(TEST_FILE)

-include $(DEP_FILES)