1
0

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.
This commit is contained in:
Graeme Smecher 2023-05-30 15:01:56 -07:00
parent 89e96e759a
commit c9c370e466
2 changed files with 14 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.o *.o
*.d
ssd1306_bin ssd1306_bin

View File

@ -4,12 +4,22 @@ LDFLAGS=-static
OBJS=main.o ssd1306.o linux_i2c.o OBJS=main.o ssd1306.o linux_i2c.o
BIN=ssd1306_bin BIN=ssd1306_bin
%.o:%.c default: $(BIN)
$(CC) -c -o $@ $< $(CFLAGS) .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) $(BIN):$(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)
clean: clean:
rm -f ./*.o $(BIN) rm -f *.o *.d $(BIN)