From 28cc8d3f841c06bf06b4016c611a98e94760c337 Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Mon, 28 Oct 2013 23:41:09 -0400 Subject: [PATCH] Makefiles: auto-dependencies cleanup. Dependency automation seems a bit confused in these makefiles. Clean it up following ancient sage wisdom here: http://mad-scientist.net/make/autodep.html In short, use -MMD to generate dependency files for each .o target, and then use a sed script to extend that to avoiding "missing file" dependencies when headers are renamed/removed. Also, make everything dependent on makefiles other than our autodependency (*.P) makefiles. Make the BUILDDIR an order-only prerequisite for our targets so any changed file therein does not cause all other files to appear to be out of date. --- Makefile-SIM | 8 +++----- Makefile-common | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Makefile-SIM b/Makefile-SIM index 80ecfd6..89c1711 100644 --- a/Makefile-SIM +++ b/Makefile-SIM @@ -42,9 +42,6 @@ SOURCES = $(wildcard *.c) SIM_SOURCES = $(subst $(SIM_PATH)/,,$(wildcard $(SIM_PATH)/*.c)) SOURCES := $(filter-out $(subst _sim.c,.c,$(SIM_SOURCES)),$(SOURCES)) $(SIM_SOURCES) -HEADERS = config.h serial.h dda.h timer.h clock.h temp.h sermsg.h -HEADERS += dda_queue.h debug.h sersendf.h heater.h analog.h delay.h -HEADERS += simulator.h OBJ = $(patsubst %.c,$(BUILDDIR)/%.sim.o,$(SOURCES)) CFLAGS = -g -Wall -Wstrict-prototypes -Wno-format -Os $(DEFS) -std=gnu99 @@ -58,9 +55,10 @@ CFLAGS += -D__AVR_ATmega1280__ include Makefile-common -$(BUILDDIR)/%.sim.o: %.c $(HEADERS) +$(BUILDDIR)/%.sim.o: %.c | $(BUILDDIR) @echo " CC $@" - @cc -c $(CFLAGS) -o $@ $< + @cc -c $(CFLAGS) -MMD -o $@ $< + @$(call CCPOST) $(TARGET): $(OBJ) @echo " LINK $@" diff --git a/Makefile-common b/Makefile-common index b287d6b..298b265 100644 --- a/Makefile-common +++ b/Makefile-common @@ -32,8 +32,6 @@ ############################################################################## BUILDDIR = build -DEPEND = $(BUILDDIR)/depend - CFLAGS += -save-temps=obj CC = $(TOOLCHAIN)gcc @@ -63,20 +61,27 @@ functionsbysize: $(OBJ) @$(OBJDUMP) -h $^ | grep '\.text\.' | perl -ne '/\.text\.(\S+)\s+([0-9a-f]+)/ && printf "%u\t%s\n", eval("0x$$2"), $$1;' | sort -n $(BUILDDIR): - mkdir $(BUILDDIR) - -$(DEPEND): $(SOURCES) - @echo " DEP $@" - @rm -f $(DEPEND) - @mkdir -p $(@D) - @$(CC) $(CFLAGS) -MM $^ > $@ + mkdir -p $(BUILDDIR) # pull in dependency info --include $(DEPEND) +-include $(OBJ:%.o=%.P) -$(BUILDDIR)/%.o: %.c config.h $(MAKEFILE_LIST) +# Everything depends on makefiles, excluding auto-built makefiles +$(OBJ): $(filter-out %.P,$(MAKEFILE_LIST)) + +# Auto-depends extension +df = $(@:.o=) +define CCPOST + cp $(df).d $(df).P; \ + sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \ + -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \ + rm -f $(df).d +endef + +$(BUILDDIR)/%.o: %.c | $(BUILDDIR) @echo " CC $@" - @$(CC) -c $(CFLAGS) -Wa,-adhlns=$(@:.o=.al) -o $@ $< + @$(CC) -c $(CFLAGS) -MMD -Wa,-adhlns=$(@:.o=.al) -o $@ $< + @$(call CCPOST) $(BUILDDIR)/%.elf: $(OBJ) @echo " LINK $@"