From 55640a36aeaf2f9d8b303903742c31fc382d7ac9 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 22 Jan 2026 22:38:32 +0000 Subject: [PATCH] Makefile: dependency generation My previous idea was to generate a list of all the headers, and add it as a dependency for all object files. This way, any changes in a header would trigger a rebuild of all object files, which would in-turn trigger a build of the binary. This will be a bit of an issue later on when we have stuff that's independent of others; a change in parser code won't necessarily affect code generation, but a change in AST will. We don't want to re-trigger builds for everything. This setup forces gcc to generate a clear set of dependencies in the build folder (in a syntax recognisable by Make), then include that in the Makefile itself. These dependencies are specific to each code unit and so only concern the headers that code unit uses. --- Makefile | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 62f95b9..24f42a5 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ CC=cc DIST=build OUT=$(DIST)/arl.out +MODULES=main lib/vec lib/sv parser/ast parser/parser +OBJECTS:=$(patsubst %,$(DIST)/%.o, $(MODULES)) LDFLAGS= GFLAGS=-Wall -Wextra -Wpedantic -std=c23 -I./src/ @@ -15,27 +17,26 @@ else CFLAGS=$(GFLAGS) $(DFLAGS) endif -HEADERS=$(shell find "src" -type 'f' -name '*.h') -MODULES=main lib/vec lib/sv parser/ast parser/parser -OBJECTS=$(patsubst %,$(DIST)/%.o, $(MODULES)) +# Dependency generation +DEPFLAGS=-MT $@ -MMD -MP -MF +DEPDIR=$(DIST)/deps $(OUT): $(OBJECTS) | $(DIST) $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -$(DIST)/%.o: src/arl/%.c $(HEADERS) | $(DIST) - $(CC) $(CFLAGS) -c -o $@ $< - -$(DIST)/%.o: src/arl/parser/%.c $(HEADERS) | $(DIST) - $(CC) $(CFLAGS) -c -o $@ $< - -$(DIST)/%.o: src/arl/lib/%.c $(HEADERS) | $(DIST) - $(CC) $(CFLAGS) -c -o $@ $< +$(DIST)/%.o: src/arl/%.c | $(DIST) $(DEPDIR) + $(CC) $(CFLAGS) $(DEPFLAGS) $(DEPDIR)/$*.d -c -o $@ $< $(DIST): mkdir -p $(DIST) mkdir -p $(DIST)/lib mkdir -p $(DIST)/parser +$(DEPDIR): + mkdir -p $(DEPDIR) + mkdir -p $(DEPDIR)/lib + mkdir -p $(DEPDIR)/parser + .PHONY: run clean ARGS= run: $(OUT) @@ -43,3 +44,6 @@ run: $(OUT) clean: rm -rf $(DIST) + +DEPS:=$(patsubst %,$(DEPDIR)/%.d, $(MODULES)) +include $(wildcard $(DEPS))