diff options
| -rw-r--r-- | src/Makefile | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/src/Makefile b/src/Makefile index ab0199b..8538a7d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,29 +1,42 @@ -# Compiler and flags +# Some of this is a little strange for this project, +# but this is just my default Makefile that I use. +# +# Largely inspired by this tutorial +# https://makefiletutorial.com/#top + CC := gcc CFLAGS := -Wall -Wextra -Werror -std=c99 -pedantic CFLAGS += -O2 -DEBUG_FLAGS := -g -O0 -DDEBUG +# -DDEBUG is nice, I like 'printf debugging'. Makes it easy. +DEBUG_FLAGS := -g -O0 -DDEBUG -fsanitize=address,undefined \ + -fno-omit-frame-pointer LDFLAGS := +DEBUG_LDFLAGS := -g -fsanitize=address,undefined -# Directories +# Many of these variables are redundant for this project, but I prefer +# keeping the Makefile in the project root and separating artifacts into +# 'build' and 'target' directories alongside the 'src' directory. SRC_DIR := . BIN_DIR := . OBJ_DIR := build +# Auto-generated dependency files DEP_DIR := $(OBJ_DIR)/.deps -DOC_DIR := docs -# Source files and objects +# Automatically find all C source files SOURCES := $(wildcard $(SRC_DIR)/*.c) + +# Map source files to object files: +# ./main.c -> build/main.o OBJECTS := $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +# Map object files to dependency files: +# build/main.o -> build/.deps/main.d DEPS := $(OBJECTS:$(OBJ_DIR)/%.o=$(DEP_DIR)/%.d) # Target executable TARGET := $(BIN_DIR)/detectCycles -# Phony targets .PHONY: all clean debug release help docs - -# Default target all: $(TARGET) # Build executable @@ -33,16 +46,22 @@ $(TARGET): $(OBJECTS) @echo "Build complete: $@" # Compile object files +# -MMD :: generate dependency info (exclude system headers) +# -MP :: add phony targets for headers (prevents build errors) +# -MF :: write dependencies to a '.d' file $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(OBJ_DIR) $(DEP_DIR) $(CC) $(CFLAGS) -MMD -MP \ -MF $(DEP_DIR)/$*.d -c $< -o $@ -# Include dependency files +# Include auto-generated dependency files. +# The leading '-' suppresses errors if the .d files +# do not exist yet e.g. on a clean build. -include $(DEPS) # Debug build debug: CFLAGS := $(filter-out -O2,$(CFLAGS)) $(DEBUG_FLAGS) +debug: LDFLAGS := $(LDFLAGS) $(DEBUG_LDFLAGS) debug: clean $(TARGET) @echo "Debug build complete" @@ -53,10 +72,9 @@ release: clean $(TARGET) # Generate Doxygen documentation docs: - @mkdir -p $(DOC_DIR) @if command -v doxygen >/dev/null 2>&1; then \ doxygen Doxyfile; \ - echo "Documentation generated in $(DOC_DIR)/"; \ + echo "Documentation generated"; \ else \ echo "Error: doxygen not found. Install it and try again."; \ exit 1; \ |