From ddbd317a575ed43fae187c9ff18c6d53ece76308 Mon Sep 17 00:00:00 2001
From: Andre Noll <maan@systemlinux.org>
Date: Sat, 19 Dec 2009 01:19:18 +0100
Subject: [PATCH] Avoid unnecessary regeneration of dependencies.

Change depend.sh so that it can handles foo.cmdline.h and cmdline/foo.cmdline.h
dependencies correctly. This way we no longer need to depend on $(cmdline_generated).
---
 Makefile.in | 11 ++++-------
 depend.sh   | 22 ++++++++++++++++++++--
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/Makefile.in b/Makefile.in
index 72f59eae..e4a3dd3f 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -188,16 +188,13 @@ $(object_dir)/%.o: %.c | $(object_dir)
 	@[ -z "$(Q)" ] || echo 'CC $<'
 	$(Q) $(CC) -c -o $@ $(CPPFLAGS) $(DEBUG_CPPFLAGS) $<
 
-# We depend on the *.cmdline.[ch] files as these must be present for depend.sh
-# to work. The first dependency is explititly given as it is used by $<.
-
-$(object_dir)/%.cmdline.d: %.cmdline.c $(cmdline_generated) | $(object_dir)
+$(object_dir)/%.cmdline.d: $(cmdline_dir)/%.cmdline.c | $(object_dir)
 	@[ -z "$(Q)" ] || echo 'DEP $<'
-	$(Q) ./depend.sh $(object_dir) $(CPPFLAGS) $< > $@
+	$(Q) ./depend.sh $(object_dir) $(cmdline_dir) $(CPPFLAGS) $< > $@
 
-$(object_dir)/%.d: %.c $(cmdline_generated) | $(object_dir)
+$(object_dir)/%.d: %.c | $(object_dir)
 	@[ -z "$(Q)" ] || echo 'DEP $<'
-	$(Q) ./depend.sh  $(object_dir) $(CPPFLAGS) $< > $@
+	$(Q) ./depend.sh $(object_dir) $(cmdline_dir) $(CPPFLAGS) $< > $@
 
 recv_objs := $(addprefix $(object_dir)/, @recv_objs@)
 filter_objs := $(addprefix $(object_dir)/, @filter_objs@)
diff --git a/depend.sh b/depend.sh
index db32f6c6..aad45a3c 100755
--- a/depend.sh
+++ b/depend.sh
@@ -1,4 +1,22 @@
 #!/bin/sh
-dir="$1"
+
+# Call gcc to output a rule suitable for make describing the dependencies of
+# the given input file and parse the output to add a *.d target with the same
+# dependencies.
+
+# The first two arguments to that script are special: $1 is the object
+# directory. This string is prefixed to both the .o and the .d target. $2 is
+# the directory that contains the *.cmdline.h files generated by gengetopt.
+
+# As gcc outputs the dependencies on the *.cmdline.h files either as either
+# foo.cmdline.h or as $cmdline_dir/foo,cmdline.h, depending on whether the
+# latter file exists, we prefix the former with $2/
+
+object_dir="$1"
+cmdline_dir="$2"
 shift
-gcc -MM -MG "$@" | sed -e "s@^\(.*\)\.o:@$dir/\1.d $dir/\1.o:@"
+shift
+
+LC_ALL=C gcc -MM -MG "$@" \
+	| sed -e "s@^\(.*\)\.o:@$object_dir/\1.d $object_dir/\1.o:@" \
+	-e "s@[ 	^]\([a-zA-Z0-9_]\+\.cmdline.h\)@ $cmdline_dir/\1@g"
-- 
2.39.5