Skip to content

Commit dfea575

Browse files
committed
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 <[email protected]>
1 parent c373991 commit dfea575

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
*.exe
178178
*.[aos]
179179
*.py[co]
180+
*.o.d
180181
*+
181182
/config.mak
182183
/autom4te.cache

Makefile

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ all::
217217
# DEFAULT_EDITOR='~/bin/vi',
218218
# DEFAULT_EDITOR='$GIT_FALLBACK_EDITOR',
219219
# 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.
220224

221225
GIT-VERSION-FILE: FORCE
222226
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1677,14 +1681,48 @@ ASM_SRC := $(wildcard $(OBJECTS:o=S))
16771681
ASM_OBJ := $(ASM_SRC:S=o)
16781682
C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
16791683

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+
16801695
.SUFFIXES:
16811696

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) $<
16841699
%.s: %.c GIT-CFLAGS FORCE
16851700
$(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!
16881726

16891727
$(GIT_OBJS): $(LIB_H)
16901728
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
17001738
http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h
17011739
http.o http-walker.o http-push.o remote-curl.o: http.h
17021740

1703-
17041741
xdiff-interface.o $(XDIFF_OBJS): \
17051742
xdiff/xinclude.h xdiff/xmacros.h xdiff/xdiff.h xdiff/xtypes.h \
17061743
xdiff/xutils.h xdiff/xprepare.h xdiff/xdiffi.h xdiff/xemit.h
1744+
endif
17071745

17081746
exec_cmd.s exec_cmd.o: ALL_CFLAGS += \
17091747
'-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
@@ -2011,6 +2049,7 @@ clean:
20112049
$(RM) $(ALL_PROGRAMS) $(BUILT_INS) git$X
20122050
$(RM) $(TEST_PROGRAMS)
20132051
$(RM) -r bin-wrappers
2052+
$(RM) -r $(dep_dirs)
20142053
$(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags cscope*
20152054
$(RM) -r autom4te.cache
20162055
$(RM) config.log config.mak.autogen config.mak.append config.status config.cache

0 commit comments

Comments
 (0)