include_rules

# core
ifneq (@(PICO),y)
SRC += src/main.c
SRC += src/core/os_$(PLATFORM).c
endif
SRC += src/core/util.c
SRC += src/core/fs.c
SRC += src/core/map.c
SRC += src/core/zip.c

# modules
SRC_@(AUDIO) += src/modules/audio/audio.c
SRC_@(AUDIO) += src/modules/audio/spatializer_simple.c
SRC_@(AUDIO)@(STEAM_AUDIO) += src/modules/audio/spatializer_phonon.c
SRC_@(AUDIO)@(OCULUS_AUDIO) += src/modules/audio/spatializer_oculus.c
SRC_@(DATA) += src/modules/data/*.c
SRC_@(EVENT) += src/modules/event/*.c
SRC_@(FILESYSTEM) += src/modules/filesystem/*.c
SRC_@(GRAPHICS) += src/modules/graphics/*.c
SRC_@(HEADSET) += src/modules/headset/headset.c
SRC_@(HEADSET)@(SIMULATOR) += src/modules/headset/headset_desktop.c
SRC_@(HEADSET)@(OPENVR) += src/modules/headset/headset_openvr.c
SRC_@(HEADSET)@(OPENXR) += src/modules/headset/headset_openxr.c
SRC_@(HEADSET)@(OCULUS) += src/modules/headset/headset_oculus.c
SRC_@(HEADSET)@(VRAPI) += src/modules/headset/headset_vrapi.c
SRC_@(HEADSET)@(PICO) += src/modules/headset/headset_pico.c
SRC_@(HEADSET)@(WEBXR) += src/modules/headset/headset_webxr.c
SRC_@(MATH) += src/modules/math/*.c
SRC_@(PHYSICS) += src/modules/physics/*.c
SRC_@(SYSTEM) += src/modules/system/*.c
SRC_@(THREAD) += src/modules/thread/*.c
SRC_@(TIMER) += src/modules/timer/*.c

# lib
SRC += src/lib/stb/*.c
SRC_@(DATA) += src/lib/jsmn/jsmn.c
SRC_@(DATA) += src/lib/minimp3/minimp3.c
SRC_@(GRAPHICS) += src/lib/glad/glad.c
SRC_@(AUDIO)@(DATA) += src/lib/miniaudio/miniaudio.c
SRC_@(MATH) += src/lib/noise1234/noise1234.c
SRC_@(THREAD) += src/lib/tinycthread/tinycthread.c

# api
SRC += src/api/api.c
SRC += src/api/l_lovr.c
SRC_@(AUDIO) += src/api/l_audio*.c
SRC_@(DATA) += src/api/l_data*.c
SRC_@(EVENT) += src/api/l_event*.c
SRC_@(FILESYSTEM) += src/api/l_filesystem*.c
SRC_@(GRAPHICS) += src/api/l_graphics*.c
SRC_@(HEADSET) += src/api/l_headset*.c
SRC_@(MATH) += src/api/l_math*.c
SRC_@(PHYSICS) += src/api/l_physics*.c
SRC_@(SYSTEM) += src/api/l_system*.c
SRC_@(THREAD) += src/api/l_thread*.c
SRC_@(TIMER) += src/api/l_timer*.c

# resources
RES += src/resources/boot.lua
RES += src/resources/VarelaRound.ttf
RES_@(OPENVR) += src/resources/*.json
SRC_@(GRAPHICS) += src/resources/shaders.c

## build:
# 1 [XD] resources -> bin headers
# 2 [CC] compile .c -> .o
# 3 [LD] link .o -> exe
# 4 [CP] copy external libs -> libs folder
: foreach $(RES) $(RES_y) |> !xd |> %f.h
: foreach $(SRC) $(SRC_y) $(SRC_yy) | src/resources/*.h |> !cc |> .obj/%B.o {obj}
: {obj} |> !ld |> bin/$(PREFIX)lovr$(SUFFIX) | $(EXTRAS) $(EXTRAS_y)
: foreach $(LIBS) |> !cp |> $(LIB)/%b

# APK
ifeq ($(PLATFORM),android)
  JAVA = src/resources/Activity.java
  CLASS = org/lovr/app/Activity.class
  JAR = bin/lovr.jar
  DEX = bin/apk/classes.dex
  MANIFEST = bin/AndroidManifest.xml

  : src/resources/$(ACTIVITY_y).java |> !cp |> $(JAVA)
  : $(ANDROID_MANIFEST_y) |> !cp |> $(MANIFEST)

  # java -> class -> jar -> dex
  : $(JAVA) |> ^ JAVAC %b^ javac -classpath $(CLASSPATH) -d bin %f |> bin/$(CLASS)
  : bin/$(CLASS) |> ^ JAR %b^ jar -cf %o -C bin $(CLASS) |> $(JAR)
  : $(JAR) $(EXTRA_JARS) |> !dx |> $(DEX)

  # aapt
  : $(MANIFEST) | $(LIB)/*.so $(DEX) |> ^ AAPT %b^ aapt package $(PACKAGE_RENAME) -F %o -M %f -0 so -I $(ANDROID_JAR) $(ANDROID_ASSETS) bin/apk |> bin/.lovr.unaligned.apk

  # align n sign
  : bin/.lovr.unaligned.apk |> !zipalign |> bin/.lovr.unsigned.apk
  : bin/.lovr.unsigned.apk |> !apksigner |> bin/lovr.apk | bin/lovr.apk.idsig
endif
