From c9c370e466da1018d4c0da8b862ca6a779baa9a5 Mon Sep 17 00:00:00 2001 From: Graeme Smecher Date: Tue, 30 May 2023 15:01:56 -0700 Subject: [PATCH] Makefile: automatically generate dependencies. This uses a trick from scottmcpeak.com/autodepend/autodepend.html. It may seem like overkill for such a small codebase, but note that header modifications did not trigger rebuilds in the affected object files in the existing Makefile. --- .gitignore | 1 + Makefile | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 1f00731..984d393 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.o +*.d ssd1306_bin diff --git a/Makefile b/Makefile index 1db20aa..bef08e3 100644 --- a/Makefile +++ b/Makefile @@ -4,12 +4,22 @@ LDFLAGS=-static OBJS=main.o ssd1306.o linux_i2c.o BIN=ssd1306_bin -%.o:%.c - $(CC) -c -o $@ $< $(CFLAGS) +default: $(BIN) +.PHONY: default clean + +# Adapted from scottmcpeak.com/autodepend/autodepend.html +-include $(OBJS:.o=.d) +%.o: %.c + $(CC) -c $(CFLAGS) $< -o $*.o + $(CC) -MM $(CFLAGS) $< > $*.d + @cp -f $*.d $*.d.tmp + @sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \ + sed -e 's/^ *//' -e 's/$$/:/' >> $*.d + @rm -f $*.d.tmp $(BIN):$(OBJS) $(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS) clean: - rm -f ./*.o $(BIN) + rm -f *.o *.d $(BIN)