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

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>

Jonathan Nieder committed Jan 26, 2010 at 09:57 UTC f2fabbf76e458010852ac7ced30594c8ab96b9fd
1 file changed +80 -15
Makefile
+80 -15
@@ -221,6 +221,9 @@ all::
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 +# Define CHECK_HEADER_DEPENDENCIES to check for problems in the hard-coded
226 +# dependency rules.
227
228 GIT-VERSION-FILE: FORCE
229 @$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1088,6 +1091,14 @@ endif
1091 -include config.mak.autogen
1092 -include config.mak
1093
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 +
1102 ifdef SANE_TOOL_PATH
1103 SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH))
1104 BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix $(SANE_TOOL_PATH_SQ)|'
@@ -1681,9 +1692,7 @@ XDIFF_OBJS = xdiff/xdiffi.o xdiff/xprepare.o xdiff/xutils.o xdiff/xemit.o \
1692 xdiff/xmerge.o xdiff/xpatience.o
1693 OBJECTS := $(GIT_OBJS) $(XDIFF_OBJS)
1694
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)
1696
1697 ifdef COMPUTE_HEADER_DEPENDENCIES
1698 dep_dirs := $(addsuffix deps,$(sort $(dir $(OBJECTS))))
@@ -1691,33 +1700,89 @@ $(dep_dirs):
1700 mkdir -p $@
1701
1702 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
1713 dep_dirs =
1714 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
1727 endif
1728
1729 +ASM_SRC := $(wildcard $(OBJECTS:o=S))
1730 +ASM_OBJ := $(ASM_SRC:S=o)
1731 +C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
1732 +
1733 .SUFFIXES:
1734
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
1769 $(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs)
1770 $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
1703 -%.s: %.c GIT-CFLAGS FORCE
1704 - $(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $<
1771 $(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs)
1772 $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $<
1773 +endif
1774
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
1779 # Take advantage of gcc's on-the-fly dependency generation
1780 # 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)
1784 endif
1715 -
1716 -dep_file = $(dir $@)deps/$(notdir $@).d
1717 -dep_args = -MF $(dep_file) -MMD -MP
1785 else
1719 -dep_args =
1720 -
1786 # Dependencies on header files, for platforms that do not support
1787 # the gcc -MMD option.
1788 #