# compiler ------------------------------------------------------------------@/
CC	:= clang
CXX	:= clang++

LIBS := -s -static
LIBS += $(shell pkg-config --libs -static SDL2_image)

CFLAGS := -Wall -Wshadow -Iinclude
CFLAGS += --std=c23 -O3
CFLAGS += --write-user-dependencies -MP

# output --------------------------------------------------------------------@/
OBJ_DIR  := build
SRC_DIR  := source
OUTPUT   := bin/pong.exe
SRCS_C   := $(shell find $(SRC_DIR) -name *.c)
SRCS_CPP := $(shell find $(SRC_DIR) -name *.cpp)

OBJS := $(subst $(SRC_DIR),$(OBJ_DIR),$(SRCS_C:.c=.o))
OBJS += $(subst $(SRC_DIR),$(OBJ_DIR),$(SRCS_CPP:.cpp=.o))
DEPS := $(OBJS:.o=.d)

# * DEPS, --write-user-dependencies, and -MP are used to
#   generate .d files alongside the .o files in build/.
# * they allow the compiler to rebuild source files that
#   should be recompiled if a .h file was changed.
-include $(DEPS)

.PHONY: clean
all: $(OUTPUT)

# building ------------------------------------------------------------------@/
$(OUTPUT): $(OBJS)
	@echo -e "\tlinking..."
	$(CXX) $^ $(LIBS) -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
	$(CXX) $(CFLAGS) -c $< -o $@

clean:
	rm -rf $(OBJS) $(DEPS) $(OUTPUT)

clean_bin:
	rm -rf $(OUTPUT)

# * rebuild targets so we don't gotta "make clean && make all" all the time
# * by the way, "targetX .WAIT targetY" delays targetY from being run until
#   targetX has finished.
# * .WAIT *has* to be used, otherwise this makefile fails when used with -j4,
#   as it will attempt to run "clean" and "all" at the same time.
rebuild: clean .WAIT all
soft_rebuild: clean_bin .WAIT all

