# =============================================================================
# Generic Makefile - C Programming course (ESIGELEC)
#
# - Put your .c files in src/ and your .h files in include/.
# - You do NOT need to edit this file when you add new source files.
#
# Default configuration: Windows 11 + MSYS2 UCRT64  ->  run with: mingw32-make
# For WSL/Linux: comment the lines marked "Windows" and uncomment the lines
# marked "WSL/Linux"                                 ->  run with: make
#
# Targets:
#   mingw32-make          build the program (build/app.exe)
#   mingw32-make run      build and run the program
#   mingw32-make clean    delete the build/ folder
# =============================================================================

CC      = gcc
CFLAGS  = -std=c99 -Wall -Wextra -pedantic -g -Iinclude -MMD -MP
LDFLAGS =

SRC     = $(wildcard src/*.c)
OBJ     = $(SRC:src/%.c=build/%.o)

# --- Windows (MSYS2 UCRT64, commands executed by cmd.exe) ---------------------
TARGET  = build/app.exe
RUN     = build\app.exe
MKDIR   = if not exist build mkdir build
RMDIR   = if exist build rmdir /S /Q build

# --- WSL/Linux ----------------------------------------------------------------
# TARGET  = build/app
# RUN     = ./build/app
# MKDIR   = mkdir -p build
# RMDIR   = rm -rf build

# -----------------------------------------------------------------------------
# Rules (identical for both systems)
# -----------------------------------------------------------------------------

# Link: all object files -> executable
$(TARGET): $(OBJ)
	$(CC) $(OBJ) $(LDFLAGS) -o $@

# Compile: each src/x.c -> build/x.o
build/%.o: src/%.c | build
	$(CC) $(CFLAGS) -c $< -o $@

build:
	$(MKDIR)

run: $(TARGET)
	$(RUN)

clean:
	$(RMDIR)

# Header dependencies generated by -MMD -MP (recompile when a .h changes)
-include $(OBJ:.o=.d)

.PHONY: run clean
