build: Let .d files depend only on .c.
authorAndre Noll <maan@tuebingen.mpg.de>
Sat, 16 Jul 2016 20:24:12 +0000 (22:24 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 26 Mar 2017 09:02:28 +0000 (11:02 +0200)
Currently we let each .d file depend on the same set of files as the
.o file by having two targets specified as arguments to -MT options
in the cc command that creates the .d file: one for the .d file and
one for the .o file.

This approach runs into problems if header files are removed and the
now stale .d file depends on a file which no longer exists.

It is enough to let the .d file depend only on the corresponding
.c file. In case an included file changes or is removed, the .c
file changes as well, so we already know we must build the object
file and can update the .d file at the same time, without running
cc twice. The -MMD option which is added to the usual cc command
instructs the compiler to create both the .d and the .o file in one go.

When creating the .o and .d files simultaneously, we don't need a
sed command to prepend the directory containing the .lsg.h headers
as these files are prerequisites of the object file being created,
so they are known to exist when the cc command is executed.

Makefile.real

index cb3bcaa3f16463e0b0f4a9167d225f669483683f..3192bf1e35e73fbc7bd3df66743bf3192886d242 100644 (file)
@@ -248,14 +248,15 @@ $(object_dir)/compress_filter.o: CFLAGS += -O3
 
 $(object_dir)/%.o: %.c | $(object_dir)
        @[ -z "$(Q)" ] || echo 'CC $<'
-       $(Q) $(CC) -c -o $@ $(CPPFLAGS) $(STRICT_CFLAGS) $<
+       $(Q) $(CC) -c -o $@ -MMD -MF $(dep_dir)/$(*F).d -MT $@ $(CPPFLAGS) \
+               $(STRICT_CFLAGS) $<
 
 # The compiler outputs dependencies either as foo.h or as some_directory/foo.h,
 # depending on whether the latter file exists. Since make needs the directory
 # part we prefix the dependency as appropriate.
 $(dep_dir)/%.d: %.c | $(dep_dir)
        @[ -z "$(Q)" ] || echo 'DEP $<'
-       $(Q) $(CC) $(CPPFLAGS) -MM -MG -MT $@ -MT $(object_dir)/$(*F).o $< \
+       $(Q) $(CC) $(CPPFLAGS) -MM -MG -MT $(object_dir)/$(*F).o $< \
                | sed -e "s@ \([a-zA-Z0-9_]\{1,\}.lsg.h\)@ $(lls_suite_dir)/\1@g" > $@
 
 para_recv para_afh para_play para_server: LDFLAGS += $(id3tag_ldflags)