Makefile: Fix dependency generation to make .d files themselves dependent.

I saw this recommendation in the implementation notes for "Recursive
Make Considered Harmful" and then the further recommendation for
implementing the idea in the GNU make manual.

The idea is that if any of the files change then we need to regenerate
the dependency file before we regenerate any targets.

The approach from the GNU make manual is simpler in that it just uses
a sed script to fix up the output of an extra invocation of the
compiler, (as opposed to the approach in the implementation notes from
the paper's author which use a wrapper script for the compiler that's
always invoked rather than the compiler itself).
This commit is contained in:
Carl Worth 2009-11-10 08:04:54 -08:00
parent c5dccd851a
commit 97c7cffdc6
2 changed files with 32 additions and 22 deletions

View file

@ -19,14 +19,21 @@ include lib/Makefile.local
%.o: %.c %.o: %.c
$(CC) -c $(CFLAGS) $(NOTMUCH_CFLAGS) $< -o $@ $(CC) -c $(CFLAGS) $(NOTMUCH_CFLAGS) $< -o $@
.depends: $(SRCS) .deps/%.d: %.c
$(CXX) -M $(CPPFLAGS) $(NOTMUCH_DEPENDS_FLAGS) \ @set -e; rm -f $@; mkdir -p $$(dirname $@) ; \
$(NOTMUCH_CXX_DEPENDS_FLAGS) $^ > $@ $(CC) -M $(CPPFLAGS) $(NOTMUCH_DEPENDS_FLAGS) $< > $@.$$$$; \
-include .depends sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
CLEAN := $(CLEAN) .depends .deps/%.d: %.cc
@set -e; rm -f $@; mkdir -p $$(dirname $@) ; \
$(CXX) -M $(CPPFLAGS) $(NOTMUCH_CXX_DEPENDS_FLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
DEPS := $(SRCS:%.c=.deps/%.d)
DEPS := $(DEPS:%.cc=.deps/%.d)
-include $(DEPS)
clean: clean:
rm -f $(CLEAN) rm -f $(CLEAN); rm -rf .deps

View file

@ -1,19 +1,22 @@
dir=lib dir=lib
lib_notmuch_modules = \ libnotmuch_c_srcs = \
$(dir)/database.o \ $(dir)/libsha1.c \
$(dir)/index.o \ $(dir)/message-file.c \
$(dir)/$(dir)sha1.o \ $(dir)/sha1.c \
$(dir)/message.o \ $(dir)/tags.c \
$(dir)/message-file.o \ $(dir)/xutil.c
$(dir)/query.o \
$(dir)/sha1.o \
$(dir)/tags.o \
$(dir)/thread.o \
$(dir)/xutil.o
$(dir)/notmuch.a: $(lib_notmuch_modules) libnotmuch_cxx_srcs = \
$(dir)/database.cc \
$(dir)/index.cc \
$(dir)/message.cc \
$(dir)/query.cc \
$(dir)/thread.cc
libnotmuch_modules = $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)
$(dir)/notmuch.a: $(libnotmuch_modules)
$(AR) rcs $@ $^ $(AR) rcs $@ $^
SRCS := $(SRCS) lib/*.c lib/*.cc SRCS := $(SRCS) $(libnotmuch_c_srcs) $(libnotmuch_cxx_srcs)
CLEAN := $(CLEAN) $(dir)/*.o $(dir)/notmuch.a CLEAN := $(CLEAN) $(libnotmuch_modules) $(dir)/notmuch.a