Skip to content

Commit f2fabbf

Browse files
committed
Teach Makefile to check header dependencies
Add a target to use the gcc-generated makefile snippets for dependencies on header files to check the hard-coded dependencies. With this patch applied, if any dependencies are missing, then make clean make COMPUTE_HEADER_DEPENDENCIES=YesPlease make CHECK_HEADER_DEPENDENCIES=YesPlease will produce an error message like the following: CHECK fast-import.o missing dependencies: exec_cmd.h make: *** [fast-import.o] Error 1 Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
1 parent 1b22c99 commit f2fabbf

File tree

1 file changed

+80
-15
lines changed

1 file changed

+80
-15
lines changed

Makefile

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ all::
221221
# Define COMPUTE_HEADER_DEPENDENCIES if your compiler supports the -MMD option
222222
# and you want to avoid rebuilding objects when an unrelated header file
223223
# changes.
224+
#
225+
# Define CHECK_HEADER_DEPENDENCIES to check for problems in the hard-coded
226+
# dependency rules.
224227

225228
GIT-VERSION-FILE: FORCE
226229
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1088,6 +1091,14 @@ endif
10881091
-include config.mak.autogen
10891092
-include config.mak
10901093

1094+
ifdef CHECK_HEADER_DEPENDENCIES
1095+
USE_COMPUTED_HEADER_DEPENDENCIES =
1096+
endif
1097+
1098+
ifdef COMPUTE_HEADER_DEPENDENCIES
1099+
USE_COMPUTED_HEADER_DEPENDENCIES = YesPlease
1100+
endif
1101+
10911102
ifdef SANE_TOOL_PATH
10921103
SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH))
10931104
BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix $(SANE_TOOL_PATH_SQ)|'
@@ -1681,43 +1692,97 @@ XDIFF_OBJS = xdiff/xdiffi.o xdiff/xprepare.o xdiff/xutils.o xdiff/xemit.o \
16811692
xdiff/xmerge.o xdiff/xpatience.o
16821693
OBJECTS := $(GIT_OBJS) $(XDIFF_OBJS)
16831694

1684-
ASM_SRC := $(wildcard $(OBJECTS:o=S))
1685-
ASM_OBJ := $(ASM_SRC:S=o)
1686-
C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
1695+
dep_files := $(foreach f,$(OBJECTS),$(dir $f)deps/$(notdir $f).d)
16871696

16881697
ifdef COMPUTE_HEADER_DEPENDENCIES
16891698
dep_dirs := $(addsuffix deps,$(sort $(dir $(OBJECTS))))
16901699
$(dep_dirs):
16911700
mkdir -p $@
16921701

16931702
missing_dep_dirs := $(filter-out $(wildcard $(dep_dirs)),$(dep_dirs))
1694-
else
1703+
dep_file = $(dir $@)deps/$(notdir $@).d
1704+
dep_args = -MF $(dep_file) -MMD -MP
1705+
ifdef CHECK_HEADER_DEPENDENCIES
1706+
$(error cannot compute header dependencies outside a normal build. \
1707+
Please unset CHECK_HEADER_DEPENDENCIES and try again)
1708+
endif
1709+
endif
1710+
1711+
ifndef COMPUTE_HEADER_DEPENDENCIES
1712+
ifndef CHECK_HEADER_DEPENDENCIES
16951713
dep_dirs =
16961714
missing_dep_dirs =
1715+
dep_args =
1716+
endif
1717+
endif
1718+
1719+
ifdef CHECK_HEADER_DEPENDENCIES
1720+
ifndef PRINT_HEADER_DEPENDENCIES
1721+
missing_deps = $(filter-out $(notdir $^), \
1722+
$(notdir $(shell $(MAKE) -s $@ \
1723+
CHECK_HEADER_DEPENDENCIES=YesPlease \
1724+
USE_COMPUTED_HEADER_DEPENDENCIES=YesPlease \
1725+
PRINT_HEADER_DEPENDENCIES=YesPlease)))
1726+
endif
16971727
endif
16981728

1729+
ASM_SRC := $(wildcard $(OBJECTS:o=S))
1730+
ASM_OBJ := $(ASM_SRC:S=o)
1731+
C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
1732+
16991733
.SUFFIXES:
17001734

1735+
ifdef PRINT_HEADER_DEPENDENCIES
1736+
$(C_OBJ): %.o: %.c FORCE
1737+
echo $^
1738+
$(ASM_OBJ): %.o: %.S FORCE
1739+
echo $^
1740+
1741+
ifndef CHECK_HEADER_DEPENDENCIES
1742+
$(error cannot print header dependencies during a normal build. \
1743+
Please set CHECK_HEADER_DEPENDENCIES and try again)
1744+
endif
1745+
endif
1746+
1747+
ifndef PRINT_HEADER_DEPENDENCIES
1748+
ifdef CHECK_HEADER_DEPENDENCIES
1749+
$(C_OBJ): %.o: %.c $(dep_files) FORCE
1750+
@set -e; echo CHECK $@; \
1751+
missing_deps="$(missing_deps)"; \
1752+
if test "$$missing_deps"; \
1753+
then \
1754+
echo missing dependencies: $$missing_deps; \
1755+
false; \
1756+
fi
1757+
$(ASM_OBJ): %.o: %.S $(dep_files) FORCE
1758+
@set -e; echo CHECK $@; \
1759+
missing_deps="$(missing_deps)"; \
1760+
if test "$$missing_deps"; \
1761+
then \
1762+
echo missing dependencies: $$missing_deps; \
1763+
false; \
1764+
fi
1765+
endif
1766+
endif
1767+
1768+
ifndef CHECK_HEADER_DEPENDENCIES
17011769
$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs)
17021770
$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
1703-
%.s: %.c GIT-CFLAGS FORCE
1704-
$(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $<
17051771
$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs)
17061772
$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
1773+
endif
17071774

1708-
ifdef COMPUTE_HEADER_DEPENDENCIES
1775+
%.s: %.c GIT-CFLAGS FORCE
1776+
$(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $<
1777+
1778+
ifdef USE_COMPUTED_HEADER_DEPENDENCIES
17091779
# Take advantage of gcc's on-the-fly dependency generation
17101780
# See <http://gcc.gnu.org/gcc-3.0/features.html>.
1711-
dep_files := $(wildcard $(foreach f,$(OBJECTS),$(dir f)deps/$(notdir $f).d))
1712-
ifneq ($(dep_files),)
1713-
include $(dep_files)
1781+
dep_files_present := $(wildcard $(dep_files))
1782+
ifneq ($(dep_files_present),)
1783+
include $(dep_files_present)
17141784
endif
1715-
1716-
dep_file = $(dir $@)deps/$(notdir $@).d
1717-
dep_args = -MF $(dep_file) -MMD -MP
17181785
else
1719-
dep_args =
1720-
17211786
# Dependencies on header files, for platforms that do not support
17221787
# the gcc -MMD option.
17231788
#

0 commit comments

Comments
 (0)