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 |
$line =~ s|\\|/|g; |
68 |
|
69 |
my ($obj,$rest) = split /\s*:\s+/, $line, 2; |
70 |
$line = ''; |
71 |
next if !$obj || !$rest; |
72 |
|
73 |
my @deps = split /\s+/, $rest; |
74 |
push @{$alldeps{$obj}}, @deps; |
75 |
if (DEBUG >= 2) { |
76 |
foreach my $dep (@deps) { print "add $obj $dep\n"; } |
77 |
} |
78 |
} |
79 |
|
80 |
# Test dependencies |
81 |
my %modtimes; # cache |
82 |
my @objs; # force rebuild on these |
83 |
OBJ_LOOP: foreach my $obj (keys %alldeps) { |
84 |
my $mtime = (stat $obj)[9] or next; |
85 |
|
86 |
my %not_in_cache; |
87 |
my $deps = $alldeps{$obj}; |
88 |
foreach my $dep_file (@{$deps}) { |
89 |
my $dep_mtime = $modtimes{$dep_file}; |
90 |
if (not defined $dep_mtime) { |
91 |
print "Skipping $dep_file for $obj, will stat() later\n" if DEBUG >= 2; |
92 |
$not_in_cache{$dep_file} = 1; |
93 |
next; |
94 |
} |
95 |
|
96 |
print "Found $dep_file in cache\n" if DEBUG >= 2; |
97 |
|
98 |
if ($dep_mtime > $mtime) { |
99 |
print "$dep_file($dep_mtime) newer than $obj($mtime)\n" if DEBUG; |
100 |
} |
101 |
elsif ($dep_mtime == -1) { |
102 |
print "Couldn't stat $dep_file for $obj\n" if DEBUG; |
103 |
} |
104 |
else { |
105 |
print "$dep_file($dep_mtime) older than $obj($mtime)\n" if DEBUG >= 2; |
106 |
next; |
107 |
} |
108 |
|
109 |
push @objs, $obj; # dependency is missing or newer |
110 |
next OBJ_LOOP; # skip checking the rest of the dependencies |
111 |
} |
112 |
|
113 |
foreach my $dep_file (keys %not_in_cache) { |
114 |
print "STAT $dep_file for $obj\n" if DEBUG >= 2; |
115 |
my $dep_mtime = $modtimes{$dep_file} = (stat $dep_file)[9] || -1; |
116 |
|
117 |
if ($dep_mtime > $mtime) { |
118 |
print "$dep_file($dep_mtime) newer than $obj($mtime)\n" if DEBUG; |
119 |
} |
120 |
elsif ($dep_mtime == -1) { |
121 |
print "Couldn't stat $dep_file for $obj\n" if DEBUG; |
122 |
} |
123 |
else { |
124 |
print "$dep_file($dep_mtime) older than $obj($mtime)\n" if DEBUG >= 2; |
125 |
next; |
126 |
} |
127 |
|
128 |
push @objs, $obj; # dependency is missing or newer |
129 |
next OBJ_LOOP; # skip checking the rest of the dependencies |
130 |
} |
131 |
|
132 |
# If we get here it means nothing needs to be done for $obj |
133 |
} |
134 |
|
135 |
# Output objects to rebuild (if needed). |
136 |
if (@objs) { |
137 |
my $old_output; |
138 |
my $new_output = "@objs: FORCE\n"; |
139 |
|
140 |
# Read in the current dependencies file. |
141 |
open(OLD, "<$outfile") |
142 |
and $old_output = <OLD>; |
143 |
close(OLD); |
144 |
|
145 |
# Only write out the dependencies if they are different. |
146 |
if ($new_output ne $old_output) { |
147 |
open(OUT, ">$outfile") and print OUT "$new_output"; |
148 |
print "Updating dependencies file, $outfile\n" unless $silent; |
149 |
if (DEBUG) { |
150 |
print "new: $new_output\n"; |
151 |
print "was: $old_output\n" if $old_output ne ''; |
152 |
} |
153 |
} |
154 |
} elsif (-s $outfile) { |
155 |
# Remove the old dependencies because all objects are up to date. |
156 |
print "Removing old dependencies file, $outfile\n" unless $silent; |
157 |
|
158 |
if (DEBUG) { |
159 |
my $old_output; |
160 |
open(OLD, "<$outfile") |
161 |
and $old_output = <OLD>; |
162 |
close(OLD); |
163 |
print "was: $old_output\n"; |
164 |
} |
165 |
|
166 |
unlink $outfile; |
167 |
} |