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.
This commit is contained in:
Phil Hord 2013-10-28 23:41:09 -04:00 committed by Markus Hitter
parent d8f61faaac
commit 28cc8d3f84
2 changed files with 20 additions and 17 deletions

View File

@ -42,9 +42,6 @@ SOURCES = $(wildcard *.c)
SIM_SOURCES = $(subst $(SIM_PATH)/,,$(wildcard $(SIM_PATH)/*.c)) SIM_SOURCES = $(subst $(SIM_PATH)/,,$(wildcard $(SIM_PATH)/*.c))
SOURCES := $(filter-out $(subst _sim.c,.c,$(SIM_SOURCES)),$(SOURCES)) $(SIM_SOURCES) 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)) OBJ = $(patsubst %.c,$(BUILDDIR)/%.sim.o,$(SOURCES))
CFLAGS = -g -Wall -Wstrict-prototypes -Wno-format -Os $(DEFS) -std=gnu99 CFLAGS = -g -Wall -Wstrict-prototypes -Wno-format -Os $(DEFS) -std=gnu99
@ -58,9 +55,10 @@ CFLAGS += -D__AVR_ATmega1280__
include Makefile-common include Makefile-common
$(BUILDDIR)/%.sim.o: %.c $(HEADERS) $(BUILDDIR)/%.sim.o: %.c | $(BUILDDIR)
@echo " CC $@" @echo " CC $@"
@cc -c $(CFLAGS) -o $@ $< @cc -c $(CFLAGS) -MMD -o $@ $<
@$(call CCPOST)
$(TARGET): $(OBJ) $(TARGET): $(OBJ)
@echo " LINK $@" @echo " LINK $@"

View File

@ -32,8 +32,6 @@
############################################################################## ##############################################################################
BUILDDIR = build BUILDDIR = build
DEPEND = $(BUILDDIR)/depend
CFLAGS += -save-temps=obj CFLAGS += -save-temps=obj
CC = $(TOOLCHAIN)gcc 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 @$(OBJDUMP) -h $^ | grep '\.text\.' | perl -ne '/\.text\.(\S+)\s+([0-9a-f]+)/ && printf "%u\t%s\n", eval("0x$$2"), $$1;' | sort -n
$(BUILDDIR): $(BUILDDIR):
mkdir $(BUILDDIR) mkdir -p $(BUILDDIR)
$(DEPEND): $(SOURCES)
@echo " DEP $@"
@rm -f $(DEPEND)
@mkdir -p $(@D)
@$(CC) $(CFLAGS) -MM $^ > $@
# pull in dependency info # 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 $@" @echo " CC $@"
@$(CC) -c $(CFLAGS) -Wa,-adhlns=$(@:.o=.al) -o $@ $< @$(CC) -c $(CFLAGS) -MMD -Wa,-adhlns=$(@:.o=.al) -o $@ $<
@$(call CCPOST)
$(BUILDDIR)/%.elf: $(OBJ) $(BUILDDIR)/%.elf: $(OBJ)
@echo " LINK $@" @echo " LINK $@"