TARGET				:= tracker

BUILDDIR			:= build
.DEFAULT_GOAL		:= all

SRCDIRS				:= src
SRCFILES			:= $(shell find $(SRCDIRS) -type f -name "*.c")
OBJFILES			:= $(SRCFILES:%.c=$(BUILDDIR)/%.c.o)
DEPFILES			:= $(SRCFILES:%.c=$(BUILDDIR)/%.c.d)

CC					:= gcc
CFLAGS				:= -Wall -Wextra -g
CPPFLAGS			:= -I src $(shell pkg-config --cflags sdl2)
LDLIBS				:= $(shell pkg-config --libs sdl2)
LDFLAGS				:= -g

.PHONY: $(TARGET) all clean
all: $(TARGET)

# PHONY RULES
# -----------

$(TARGET): $(BUILDDIR)/$(TARGET)

clean:
	echo CLEAN FILES FOR $(TARGET)
	rm -rf $(BUILDDIR)

# SPECIFIC BUILD RULES
# --------------------

$(BUILDDIR)/$(TARGET): $(OBJFILES)
	echo 'LD ' $@
	mkdir -p $(@D)
	$(CC) $(LDFLAGS) -o $@ $(OBJFILES) $(LDLIBS)

$(OBJFILES): $(BUILDDIR)/%.c.o: %.c
	echo 'CC ' $@
	mkdir -p $(@D)
	$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -o $@ -c $<

# Header dependencies generated by -MMD. Keep this after the real targets so
# included files can never accidentally become Make's default goal.
-include $(DEPFILES)
