1 |
#!/usr/bin/env perl |
2 |
|
3 |
# ***** BEGIN LICENSE BLOCK ***** |
4 |
# Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
5 |
# |
6 |
# The contents of this file are subject to the Mozilla Public License Version |
7 |
# 1.1 (the "License"); you may not use this file except in compliance with |
8 |
# the License. You may obtain a copy of the License at |
9 |
# http://www.mozilla.org/MPL/ |
10 |
# |
11 |
# Software distributed under the License is distributed on an "AS IS" basis, |
12 |
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
13 |
# for the specific language governing rights and limitations under the |
14 |
# License. |
15 |
# |
16 |
# The Original Code is this file as it was released upon March 8, 1999. |
17 |
# |
18 |
# The Initial Developer of the Original Code is |
19 |
# Netscape Communications Corporation. |
20 |
# Portions created by the Initial Developer are Copyright (C) 1999 |
21 |
# the Initial Developer. All Rights Reserved. |
22 |
# |
23 |
# Contributor(s): |
24 |
# |
25 |
# Alternatively, the contents of this file may be used under the terms of |
26 |
# either of the GNU General Public License Version 2 or later (the "GPL"), |
27 |
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
28 |
# in which case the provisions of the GPL or the LGPL are applicable instead |
29 |
# of those above. If you wish to allow use of your version of this file only |
30 |
# under the terms of either the GPL or the LGPL, and not to allow others to |
31 |
# use your version of this file under the terms of the MPL, indicate your |
32 |
# decision by deleting the provisions above and replace them with the notice |
33 |
# and other provisions required by the GPL or the LGPL. If you do not delete |
34 |
# the provisions above, a recipient may use your version of this file under |
35 |
# the terms of any one of the MPL, the GPL or the LGPL. |
36 |
# |
37 |
# ***** END LICENSE BLOCK ***** |
38 |
|
39 |
# mddepend.pl - Reads in dependencies generated my -MD flag. Prints list |
40 |
# of objects that need to be rebuilt. These can then be added to the |
41 |
# PHONY target. Using this script copes with the problem of header |
42 |
# files that have been removed from the build. |
43 |
# |
44 |
# Usage: |
45 |
# mddepend.pl <output_file> <dependency_files...> |
46 |
# |
47 |
# Send comments, improvements, bugs to Steve Lamm (slamm@netscape.com). |
48 |
|
49 |
use strict; |
50 |
|
51 |
use constant DEBUG => 0; |
52 |
|
53 |
my $outfile = shift @ARGV; |
54 |
my $silent = $ENV{MAKEFLAGS} =~ /^\w*s|\s-s/; |
55 |
|
56 |
my $line = ''; |
57 |
my %alldeps; |
58 |
# Parse dependency files |
59 |
while (<>) { |
60 |
s/\r?\n$//; # Handle both unix and DOS line endings |
61 |
$line .= $_; |
62 |
if ($line =~ /\\$/) { |
63 |
chop $line; |
64 |
next; |
65 |
} |
66 |
|
67 |
my ($obj,$rest) = split /\s*:\s+/, $line, 2; |
68 |
$line = ''; |
69 |
next if !$obj || !$rest; |
70 |
|
71 |
my @deps = split /\s+/, $rest; |
72 |
push @{$alldeps{$obj}}, @deps; |
73 |
if (DEBUG >= 2) { |
74 |
foreach my $dep (@deps) { print "add $obj $dep\n"; } |
75 |
} |
76 |
} |
77 |
|
78 |
# Test dependencies |
79 |
my %modtimes; # cache |
80 |
my @objs; # force rebuild on these |
81 |
OBJ_LOOP: foreach my $obj (keys %alldeps) { |
82 |
my $mtime = (stat $obj)[9] or next; |
83 |
|
84 |
my %not_in_cache; |
85 |
my $deps = $alldeps{$obj}; |
86 |
foreach my $dep_file (@{$deps}) { |
87 |
my $dep_mtime = $modtimes{$dep_file}; |
88 |
if (not defined $dep_mtime) { |
89 |
print "Skipping $dep_file for $obj, will stat() later\n" if DEBUG >= 2; |
90 |
$not_in_cache{$dep_file} = 1; |
91 |
next; |
92 |
} |
93 |
|
94 |
print "Found $dep_file in cache\n" if DEBUG >= 2; |
95 |
|
96 |
if ($dep_mtime > $mtime) { |
97 |
print "$dep_file($dep_mtime) newer than $obj($mtime)\n" if DEBUG; |
98 |
} |
99 |
elsif ($dep_mtime == -1) { |
100 |
print "Couldn't stat $dep_file for $obj\n" if DEBUG; |
101 |
} |
102 |
else { |
103 |
print "$dep_file($dep_mtime) older than $obj($mtime)\n" if DEBUG >= 2; |
104 |
next; |
105 |
} |
106 |
|
107 |
push @objs, $obj; # dependency is missing or newer |
108 |
next OBJ_LOOP; # skip checking the rest of the dependencies |
109 |
} |
110 |
|
111 |
foreach my $dep_file (keys %not_in_cache) { |
112 |
print "STAT $dep_file for $obj\n" if DEBUG >= 2; |
113 |
my $dep_mtime = $modtimes{$dep_file} = (stat $dep_file)[9] || -1; |
114 |
|
115 |
if ($dep_mtime > $mtime) { |
116 |
print "$dep_file($dep_mtime) newer than $obj($mtime)\n" if DEBUG; |
117 |
} |
118 |
elsif ($dep_mtime == -1) { |
119 |
print "Couldn't stat $dep_file for $obj\n" if DEBUG; |
120 |
} |
121 |
else { |
122 |
print "$dep_file($dep_mtime) older than $obj($mtime)\n" if DEBUG >= 2; |
123 |
next; |
124 |
} |
125 |
|
126 |
push @objs, $obj; # dependency is missing or newer |
127 |
next OBJ_LOOP; # skip checking the rest of the dependencies |
128 |
} |
129 |
|
130 |
# If we get here it means nothing needs to be done for $obj |
131 |
} |
132 |
|
133 |
# Output objects to rebuild (if needed). |
134 |
if (@objs) { |
135 |
my $old_output; |
136 |
my $new_output = "@objs: FORCE\n"; |
137 |
|
138 |
# Read in the current dependencies file. |
139 |
open(OLD, "<$outfile") |
140 |
and $old_output = <OLD>; |
141 |
close(OLD); |
142 |
|
143 |
# Only write out the dependencies if they are different. |
144 |
if ($new_output ne $old_output) { |
145 |
open(OUT, ">$outfile") and print OUT "$new_output"; |
146 |
print "Updating dependencies file, $outfile\n" unless $silent; |
147 |
if (DEBUG) { |
148 |
print "new: $new_output\n"; |
149 |
print "was: $old_output\n" if $old_output ne ''; |
150 |
} |
151 |
} |
152 |
} elsif (-s $outfile) { |
153 |
# Remove the old dependencies because all objects are up to date. |
154 |
print "Removing old dependencies file, $outfile\n" unless $silent; |
155 |
|
156 |
if (DEBUG) { |
157 |
my $old_output; |
158 |
open(OLD, "<$outfile") |
159 |
and $old_output = <OLD>; |
160 |
close(OLD); |
161 |
print "was: $old_output\n"; |
162 |
} |
163 |
|
164 |
unlink $outfile; |
165 |
} |