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.
This commit is contained in:
2026-01-22 22:38:32 +00:00
parent 9b7adbbbe1
commit 55640a36ae

View File

@@ -2,6 +2,8 @@ CC=cc
DIST=build DIST=build
OUT=$(DIST)/arl.out OUT=$(DIST)/arl.out
MODULES=main lib/vec lib/sv parser/ast parser/parser
OBJECTS:=$(patsubst %,$(DIST)/%.o, $(MODULES))
LDFLAGS= LDFLAGS=
GFLAGS=-Wall -Wextra -Wpedantic -std=c23 -I./src/ GFLAGS=-Wall -Wextra -Wpedantic -std=c23 -I./src/
@@ -15,27 +17,26 @@ else
CFLAGS=$(GFLAGS) $(DFLAGS) CFLAGS=$(GFLAGS) $(DFLAGS)
endif endif
HEADERS=$(shell find "src" -type 'f' -name '*.h') # Dependency generation
MODULES=main lib/vec lib/sv parser/ast parser/parser DEPFLAGS=-MT $@ -MMD -MP -MF
OBJECTS=$(patsubst %,$(DIST)/%.o, $(MODULES)) DEPDIR=$(DIST)/deps
$(OUT): $(OBJECTS) | $(DIST) $(OUT): $(OBJECTS) | $(DIST)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
$(DIST)/%.o: src/arl/%.c $(HEADERS) | $(DIST) $(DIST)/%.o: src/arl/%.c | $(DIST) $(DEPDIR)
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) $(DEPFLAGS) $(DEPDIR)/$*.d -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): $(DIST):
mkdir -p $(DIST) mkdir -p $(DIST)
mkdir -p $(DIST)/lib mkdir -p $(DIST)/lib
mkdir -p $(DIST)/parser mkdir -p $(DIST)/parser
$(DEPDIR):
mkdir -p $(DEPDIR)
mkdir -p $(DEPDIR)/lib
mkdir -p $(DEPDIR)/parser
.PHONY: run clean .PHONY: run clean
ARGS= ARGS=
run: $(OUT) run: $(OUT)
@@ -43,3 +44,6 @@ run: $(OUT)
clean: clean:
rm -rf $(DIST) rm -rf $(DIST)
DEPS:=$(patsubst %,$(DEPDIR)/%.d, $(MODULES))
include $(wildcard $(DEPS))