still serv_close bug...
[mit-jos.git] / mergedep.pl
blob1730d5392884b8efc293810e65a72ba369e38a62
1 #!/usr/bin/perl
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)
19 # perl mergedep $@ $^
20 # -include .deps
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.
27 sub readdeps {
28 my $filename = shift;
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;
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: $_";
53 close DEPFILE;
54 return 1;
58 if ($#ARGV < 0) {
59 print "Usage: mergedep <main-depfile> [<new-depfiles> ..]\n";
60 exit(1);
63 %dephash = ();
65 # Read the main dependency file
66 $maindeps = $ARGV[0];
67 readdeps($maindeps);
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}";
79 close DEPFILE;
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";