2 # Copyright 2003 Bryan Ford
3 # Distributed under the GNU General Public License.
5 # Usage: mergedep <main-depfile> [<new-depfiles> ...]
7 # This script merges the contents of all <new-depfiles> specified
8 # on the command line into the single file <main-depfile>,
9 # which may or may not previously exist.
10 # Dependencies in the <new-depfiles> will override
11 # any existing dependencies for the same targets in <main-depfile>.
12 # The <new-depfiles> are deleted after <main-depfile> is updated.
14 # The <new-depfiles> are typically generated by GCC with the -MD option,
15 # and the <main-depfile> is typically included from a Makefile,
16 # as shown here for GNU 'make':
18 # .deps: $(wildcard *.d)
22 # This script properly handles multiple dependencies per <new-depfile>,
23 # including dependencies having no target,
24 # so it is compatible with GCC3's -MP option.
30 open(DEPFILE
, $filename) or return 0;
32 if (/([^:]*):([^\\:]*)([\\]?)$/) {
36 while ($slash ne '') {
39 "Unterminated dependency in $filename";
40 /(^[ \t][^\\]*)([\\]?)$/ or die
41 "Bad continuation line in $filename";
42 $deplines = "$deplines\\\n$1";
45 #print "DEPENDENCY [[$target]]: [[$deplines]]\n";
46 $dephash{$target} = $deplines;
47 } elsif (/^[#]?[ \t]*$/) {
48 # ignore blank lines and comments
50 die "Bad dependency line in $filename: $_";
59 print "Usage: mergedep <main-depfile> [<new-depfiles> ..]\n";
65 # Read the main dependency file
69 # Read and merge in the new dependency files
70 foreach $i (1 .. $#ARGV) {
71 readdeps
($ARGV[$i]) or die "Can't open $ARGV[$i]";
74 # Update the main dependency file
75 open(DEPFILE
, ">$maindeps.tmp") or die "Can't open output file $maindeps.tmp";
76 foreach $target (keys %dephash) {
77 print DEPFILE
"$target:$dephash{$target}";
80 rename("$maindeps.tmp", "$maindeps") or die "Can't overwrite $maindeps";
82 # Finally, delete the new dependency files
83 foreach $i (1 .. $#ARGV) {
84 unlink($ARGV[$i]) or print "Error removing $ARGV[$i]\n";