SRCS=$(shell find ./src -name '*.c' -type f)
OBJS=$(patsubst %.c, %.o, ${SRCS})
DEPS=$(patsubst %.c, %.d, ${SRCS})
HDRS=$(shell find ./src -name '*.h' -type f)

CFLAGS+=-ansi -m64 -Og -ggdb \
-pedantic -Wall -Wextra -Wshadow \
-Wcast-align -Wunused -Wconversion -Wmisleading-indentation \
-Wdouble-promotion -Wformat=2 -Wbad-function-cast \
-Wmissing-declarations \
-Wmissing-prototypes -Wnested-externs -Werror

LDFLAGS+= -fsanitize=address

ifneq ($(findstring -static, ${LDFLAGS}), )
	LDFLAGS:=$(filter-out -fsanitize=address, ${LDFLAGS})
	_PKGT_STATIC:=--static
endif

BIN := $(subst src/,,$(patsubst %.c, %, ${SRCS}))

sdl3: CFLAGS+=$(shell pkg-config ${_PKG_STATIC} --cflags sdl3)
sdl3: LDFLAGS+=$(shell pkg-config ${_PKG_STATIC} --libs sdl3)

rl: CFLAGS+=-I./raylib-5.5_linux_amd64/include -std=c99
rl: LDFLAGS+=-L./raylib-5.5_linux_amd64/lib -lraylib -Wl,-rpath=./raylib-5.5_linux_amd64/lib

raw_x11: CFLAGS+=$(shell pkg-config ${_PKG_STATIC} --cflags x11)
raw_x11: LDFLAGS+=$(shell pkg-config ${_PKG_STATIC} --libs x11)

cad: CFLAGS+=$(shell pkg-config ${_PKG_STATIC} --cflags x11)
cad: LDFLAGS+=$(shell pkg-config ${_PKG_STATIC} --libs x11)

all: ${BIN}

${BIN}: %: src/%.o
	@echo -n "[LD] "
	gcc ${LDFLAGS} ${_LDFLAGS_$@} $^ -o $@

%.d: %.c
	gcc ${CFLAGS} -MM $^ > $@

include ${DEPS}

tags: ${SRCS} ${HDRS}
	ctags --kinds-all=* --totals=yes $^

clean:
	rm -fv ${BIN} ${OBJS}

fullclean: clean
	rm -fv ${DEPS}
