git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 12/12] Teach Makefile to check header dependencies

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jan 26, 2010, 15:57 UTC
Message-ID
<20100126155714.GM4895@progeny.tock>
In-Reply-To
<20100126154357.GA4895@progeny.tock>

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>
---
That’s the end of the series.  Thanks for reading.
 Makefile |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 80 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile
index ceaae1c..be357a8 100644
--- a/Makefile
+++ b/Makefile
@@ -221,6 +221,9 @@ all::
 # Define COMPUTE_HEADER_DEPENDENCIES if your compiler supports the -MMD option
 # and you want to avoid rebuilding objects when an unrelated header file
 # changes.
+#
+# Define CHECK_HEADER_DEPENDENCIES to check for problems in the hard-coded
+# dependency rules.
 
 GIT-VERSION-FILE: FORCE
 	@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1088,6 +1091,14 @@ endif
 -include config.mak.autogen
 -include config.mak
 
+ifdef CHECK_HEADER_DEPENDENCIES
+USE_COMPUTED_HEADER_DEPENDENCIES =
+endif
+
+ifdef COMPUTE_HEADER_DEPENDENCIES
+USE_COMPUTED_HEADER_DEPENDENCIES = YesPlease
+endif
+
 ifdef SANE_TOOL_PATH
 SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH))
 BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix $(SANE_TOOL_PATH_SQ)|'
@@ -1683,9 +1694,7 @@ XDIFF_OBJS = xdiff/xdiffi.o xdiff/xprepare.o xdiff/xutils.o xdiff/xemit.o \
 	xdiff/xmerge.o xdiff/xpatience.o
 OBJECTS := $(GIT_OBJS) $(XDIFF_OBJS)
 
-ASM_SRC := $(wildcard $(OBJECTS:o=S))
-ASM_OBJ := $(ASM_SRC:S=o)
-C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
+dep_files := $(foreach f,$(OBJECTS),$(dir $f)deps/$(notdir $f).d)
 
 ifdef COMPUTE_HEADER_DEPENDENCIES
 dep_dirs := $(addsuffix deps,$(sort $(dir $(OBJECTS))))
@@ -1693,33 +1702,89 @@ $(dep_dirs):
 	mkdir -p $@
 
 missing_dep_dirs := $(filter-out $(wildcard $(dep_dirs)),$(dep_dirs))
-else
+dep_file = $(dir $@)deps/$(notdir $@).d
+dep_args = -MF $(dep_file) -MMD -MP
+ifdef CHECK_HEADER_DEPENDENCIES
+$(error cannot compute header dependencies outside a normal build. \
+Please unset CHECK_HEADER_DEPENDENCIES and try again)
+endif
+endif
+
+ifndef COMPUTE_HEADER_DEPENDENCIES
+ifndef CHECK_HEADER_DEPENDENCIES
 dep_dirs =
 missing_dep_dirs =
+dep_args =
+endif
+endif
+
+ifdef CHECK_HEADER_DEPENDENCIES
+ifndef PRINT_HEADER_DEPENDENCIES
+missing_deps = $(filter-out $(notdir $^), \
+	$(notdir $(shell $(MAKE) -s $@ \
+		CHECK_HEADER_DEPENDENCIES=YesPlease \
+		USE_COMPUTED_HEADER_DEPENDENCIES=YesPlease \
+		PRINT_HEADER_DEPENDENCIES=YesPlease)))
+endif
 endif
 
+ASM_SRC := $(wildcard $(OBJECTS:o=S))
+ASM_OBJ := $(ASM_SRC:S=o)
+C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
+
 .SUFFIXES:
 
+ifdef PRINT_HEADER_DEPENDENCIES
+$(C_OBJ): %.o: %.c FORCE
+	echo $^
+$(ASM_OBJ): %.o: %.S FORCE
+	echo $^
+
+ifndef CHECK_HEADER_DEPENDENCIES
+$(error cannot print header dependencies during a normal build. \
+Please set CHECK_HEADER_DEPENDENCIES and try again)
+endif
+endif
+
+ifndef PRINT_HEADER_DEPENDENCIES
+ifdef CHECK_HEADER_DEPENDENCIES
+$(C_OBJ): %.o: %.c $(dep_files) FORCE
+	@set -e; echo CHECK $@; \
+	missing_deps="$(missing_deps)"; \
+	if test "$$missing_deps"; \
+	then \
+		echo missing dependencies: $$missing_deps; \
+		false; \
+	fi
+$(ASM_OBJ): %.o: %.S $(dep_files) FORCE
+	@set -e; echo CHECK $@; \
+	missing_deps="$(missing_deps)"; \
+	if test "$$missing_deps"; \
+	then \
+		echo missing dependencies: $$missing_deps; \
+		false; \
+	fi
+endif
+endif
+
+ifndef CHECK_HEADER_DEPENDENCIES
 $(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs)
 	$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
-%.s: %.c GIT-CFLAGS FORCE
-	$(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $<
 $(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs)
 	$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
+endif
 
-ifdef COMPUTE_HEADER_DEPENDENCIES
+%.s: %.c GIT-CFLAGS FORCE
+	$(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $<
+
+ifdef USE_COMPUTED_HEADER_DEPENDENCIES
 # Take advantage of gcc's on-the-fly dependency generation
 # See <http://gcc.gnu.org/gcc-3.0/features.html>.
-dep_files := $(wildcard $(foreach f,$(OBJECTS),$(dir f)deps/$(notdir $f).d))
-ifneq ($(dep_files),)
-include $(dep_files)
+dep_files_present := $(wildcard $(dep_files))
+ifneq ($(dep_files_present),)
+include $(dep_files_present)
 endif
-
-dep_file = $(dir $@)deps/$(notdir $@).d
-dep_args = -MF $(dep_file) -MMD -MP
 else
-dep_args =
-
 # Dependencies on header files, for platforms that do not support
 # the gcc -MMD option.
 #
-- 
1.6.6
Previous: Jonathan NiederNext: Junio C Hamano
Message 21 of 27 in “Makefile: add missing header dependency rules”
  1. 0/5 Makefile: add missing header dependency rulesJonathan Nieder, Jan 23, 2010
  2. 1/5 Makefile: add missing header file dependenciesJonathan Nieder, Jan 23, 2010
  3. 2/5 Makefile: make sure test helpers are rebuilt when headers changeJonathan Nieder, Jan 23, 2010
  4. 3/5 Makefile: remove wt-status.h from LIB_HJonathan Nieder, Jan 23, 2010
  5. 4/5 Makefile: clean up http-walker.o dependency rulesJonathan Nieder, Jan 23, 2010
  6. 5/5 Makefile: drop dependency on $(wildcard */*.h)Jonathan Nieder, Jan 23, 2010
  7. Junio C HamanoJan 25, 2010
  8. 00/12 Re: Makefile: add missing header dependency rulesJonathan Nieder, Jan 26, 2010
  9. 01/12 Makefile: add missing header file dependenciesJonathan Nieder, Jan 26, 2010
  10. 02/12 Makefile: make sure test helpers are rebuilt when headers changeJonathan Nieder, Jan 26, 2010
  11. 03/12 Makefile: remove wt-status.h from LIB_HJonathan Nieder, Jan 26, 2010
  12. 04/12 Makefile: clean up http-walker.o dependency rulesJonathan Nieder, Jan 26, 2010
  13. 05/12 Makefile: drop dependency on $(wildcard */*.h)Jonathan Nieder, Jan 26, 2010
  14. 06/12 Makefile: transport.o depends on branch.h nowJonathan Nieder, Jan 26, 2010
  15. 07/12 Makefile: rearrange dependency rulesJonathan Nieder, Jan 26, 2010
  16. 08/12 Makefile: disable default implicit rulesJonathan Nieder, Jan 26, 2010
  17. 09/12 Makefile: list generated object files in OBJECTSJonathan Nieder, Jan 26, 2010
  18. 10/12 Makefile: lazily compute header dependenciesJonathan Nieder, Jan 26, 2010
  19. 11/12 Makefile: list standalone program object files in PROGRAM_OBJSJonathan Nieder, Jan 26, 2010
  20. 11/12 Makefile: list standalone program object files in PROGRAM_OBJSJonathan Nieder, Jan 27, 2010
  21. 12/12 Teach Makefile to check header dependenciesJonathan Nieder, Jan 26, 2010
  22. Junio C HamanoJan 31, 2010
  23. Jonathan NiederJan 31, 2010
  24. Junio C HamanoJan 31, 2010
  25. 14/12 Makefile: always remove .depend directories on 'make clean'Jonathan Nieder, Jan 31, 2010
  26. 13/12 Makefile: tuck away generated makefile fragments in .dependJonathan Nieder, Jan 31, 2010
  27. Jonathan NiederJan 26, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.