@matheusbsbs / matheus-bagliano / commits / dfea575017
Fork 0

Makefile: lazily compute header dependencies

Use the gcc -MMD -MP -MF options to generate dependency rules as a byproduct when building .o files if the COMPUTE_HEADER_DEPENDENCIES variable is defined. That variable is left undefined by default for now. As each object file is built, write a makefile fragment containing its dependencies in the deps/ subdirectory of its containing directory. The deps/ directories should be generated if they are missing at the start of each build. So let each object file depend on $(missing_dep_dirs), which lists only the directories of this kind that are missing to avoid needlessly regenerating files when the directories' timestamps change. gcc learned the -MMD -MP -MF options in version 3.0, so most gcc users should have them by now. The dependencies this option computes are more specific than the rough estimates hard-coded in the Makefile, greatly speeding up rebuilds when only a little-used header file has changed. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed Jan 26, 2010 at 09:52 UTC dfea575017ddc2ae7c9e8dcb978f4557f07715d3
2 files changed +45 -5
.gitignore
+1
@@ -177,6 +177,7 @@
177 *.exe
178 *.[aos]
179 *.py[co]
180 +*.o.d
181 *+
182 /config.mak
183 /autom4te.cache
Makefile
+44 -5
@@ -217,6 +217,10 @@ all::
217 # DEFAULT_EDITOR='~/bin/vi',
218 # DEFAULT_EDITOR='$GIT_FALLBACK_EDITOR',
219 # DEFAULT_EDITOR='"C:\Program Files\Vim\gvim.exe" --nofork'
220 +#
221 +# Define COMPUTE_HEADER_DEPENDENCIES if your compiler supports the -MMD option
222 +# and you want to avoid rebuilding objects when an unrelated header file
223 +# changes.
224
225 GIT-VERSION-FILE: FORCE
226 @$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1677,14 +1681,48 @@ ASM_SRC := $(wildcard $(OBJECTS:o=S))
1681 ASM_OBJ := $(ASM_SRC:S=o)
1682 C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
1683
1684 +ifdef COMPUTE_HEADER_DEPENDENCIES
1685 +dep_dirs := $(addsuffix deps,$(sort $(dir $(OBJECTS))))
1686 +$(dep_dirs):
1687 + mkdir -p $@
1688 +
1689 +missing_dep_dirs := $(filter-out $(wildcard $(dep_dirs)),$(dep_dirs))
1690 +else
1691 +dep_dirs =
1692 +missing_dep_dirs =
1693 +endif
1694 +
1695 .SUFFIXES:
1696
1682 -$(C_OBJ): %.o: %.c GIT-CFLAGS
1683 - $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $<
1697 +$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs)
1698 + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
1699 %.s: %.c GIT-CFLAGS FORCE
1700 $(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $<
1686 -$(ASM_OBJ): %.o: %.S GIT-CFLAGS
1687 - $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $<
1701 +$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs)
1702 + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
1703 +
1704 +ifdef COMPUTE_HEADER_DEPENDENCIES
1705 +# Take advantage of gcc's on-the-fly dependency generation
1706 +# See <http://gcc.gnu.org/gcc-3.0/features.html>.
1707 +dep_files := $(wildcard $(foreach f,$(OBJECTS),$(dir f)deps/$(notdir $f).d))
1708 +ifneq ($(dep_files),)
1709 +include $(dep_files)
1710 +endif
1711 +
1712 +dep_file = $(dir $@)deps/$(notdir $@).d
1713 +dep_args = -MF $(dep_file) -MMD -MP
1714 +else
1715 +dep_args =
1716 +
1717 +# Dependencies on header files, for platforms that do not support
1718 +# the gcc -MMD option.
1719 +#
1720 +# Dependencies on automatically generated headers such as common-cmds.h
1721 +# should _not_ be included here, since they are necessary even when
1722 +# building an object for the first time.
1723 +#
1724 +# XXX. Please check occasionally that these include all dependencies
1725 +# gcc detects!
1726
1727 $(GIT_OBJS): $(LIB_H)
1728 builtin-branch.o builtin-checkout.o builtin-clone.o builtin-reset.o branch.o transport.o: branch.h
@@ -1700,10 +1738,10 @@ builtin-pack-objects.o: thread-utils.h
1738 http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h
1739 http.o http-walker.o http-push.o remote-curl.o: http.h
1740
1703 -
1741 xdiff-interface.o $(XDIFF_OBJS): \
1742 xdiff/xinclude.h xdiff/xmacros.h xdiff/xdiff.h xdiff/xtypes.h \
1743 xdiff/xutils.h xdiff/xprepare.h xdiff/xdiffi.h xdiff/xemit.h
1744 +endif
1745
1746 exec_cmd.s exec_cmd.o: ALL_CFLAGS += \
1747 '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
@@ -2011,6 +2049,7 @@ clean:
2049 $(RM) $(ALL_PROGRAMS) $(BUILT_INS) git$X
2050 $(RM) $(TEST_PROGRAMS)
2051 $(RM) -r bin-wrappers
2052 + $(RM) -r $(dep_dirs)
2053 $(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags cscope*
2054 $(RM) -r autom4te.cache
2055 $(RM) config.log config.mak.autogen config.mak.append config.status config.cache