mergedep.pl (2331B)
1 #!/usr/bin/perl 2 # Copyright 2003 Bryan Ford 3 # Distributed under the GNU General Public License. 4 # 5 # Usage: mergedep <main-depfile> [<new-depfiles> ...] 6 # 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. 13 # 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': 17 # 18 # .deps: $(wildcard *.d) 19 # perl mergedep $@ $^ 20 # -include .deps 21 # 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. 25 # 26 27 sub readdeps { 28 my $filename = shift; 29 30 open(DEPFILE, $filename) or return 0; 31 while (<DEPFILE>) { 32 if (/([^:]*):([^\\:]*)([\\]?)$/) { 33 my $target = $1; 34 my $deplines = $2; 35 my $slash = $3; 36 while ($slash ne '') { 37 $_ = <DEPFILE>; 38 defined($_) or die 39 "Unterminated dependency in $filename"; 40 /(^[ \t][^\\]*)([\\]?)$/ or die 41 "Bad continuation line in $filename"; 42 $deplines = "$deplines\\\n$1"; 43 $slash = $2; 44 } 45 #print "DEPENDENCY [[$target]]: [[$deplines]]\n"; 46 $dephash{$target} = $deplines; 47 } elsif (/^[#]?[ \t]*$/) { 48 # ignore blank lines and comments 49 } else { 50 die "Bad dependency line in $filename: $_"; 51 } 52 } 53 close DEPFILE; 54 return 1; 55 } 56 57 58 if ($#ARGV < 0) { 59 print "Usage: mergedep <main-depfile> [<new-depfiles> ..]\n"; 60 exit(1); 61 } 62 63 %dephash = (); 64 65 # Read the main dependency file 66 $maindeps = $ARGV[0]; 67 readdeps($maindeps); 68 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]"; 72 } 73 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}"; 78 } 79 close DEPFILE; 80 rename("$maindeps.tmp", "$maindeps") or die "Can't overwrite $maindeps"; 81 82 # Finally, delete the new dependency files 83 foreach $i (1 .. $#ARGV) { 84 unlink($ARGV[$i]) or print "Error removing $ARGV[$i]\n"; 85 } 86