#!/usr/bin/env -S gmake -f

MKDIR ?= mkdir -p

libs := SDL2 m z

EXTENSION ?= out
CFLAGS ?= -Wall -Wpedantic -g -Og
cflags := ${CFLAGS}
ldflags := $(addprefix -l,${libs}) ${LDFLAGS}

srcs := $(wildcard src/*.c)
objs := $(addprefix out/${NS}/,$(notdir ${srcs:.c=.o}))
deps := $(addprefix out/${NS}/,$(notdir $(patsubst %.c,%.d,$(wildcard src/*.c))))

.PHONY: all run clean

all: out/${NS}/a.${EXTENSION} out/assets.res

run: out/${NS}/a.${EXTENSION} out/assets.res
	./$^

clean:
	${RM} -r out/${NS}/

out/${NS}/:
	${MKDIR} $@

out/:
	${MKDIR} $@

out/${NS}/libplum.o: cflags += -w

out/${NS}/%.o: src/%.c out/${NS}/%.d | out/${NS}/
	${CC} -c -o $@ $< ${cflags}

out/${NS}/incbin.o: src/incbin.S | out/${NS}/
	${CC} -c -o $@ $< -Wa,-I,src/ ${cflags}

out/${NS}/%.d: src/%.c | out/${NS}/
	${CC} ${cflags} ${CPPFLAGS} -MM -MG -MF $@ -MT "${@:.d=.o} $@" $<

out/${NS}/a.${EXTENSION}: ${objs} out/${NS}/incbin.o | out/${NS}/
	${CC} -o $@ $^ ${cflags} ${ldflags}

include assets.mk

include $(wildcard utl/*.mk)

include ${deps}