3
# This Source Code Form is subject to the terms of the Mozilla Public
4
# License, v. 2.0. If a copy of the MPL was not distributed with this
5
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
# mddepend.pl - Reads in dependencies generated my -MD flag. Prints list
8
# of objects that need to be rebuilt. These can then be added to the
9
# PHONY target. Using this script copes with the problem of header
10
# files that have been removed from the build.
13
# mddepend.pl <output_file> <dependency_files...>
15
# Send comments, improvements, bugs to Steve Lamm (slamm@netscape.com).
19
use constant DEBUG => 0;
21
my $outfile = shift @ARGV;
22
my $silent = $ENV{MAKEFLAGS} =~ /^\w*s|\s-s/;
26
# Parse dependency files
28
s/\r?\n$//; # Handle both unix and DOS line endings
37
my ($obj,$rest) = split /\s*:\s+/, $line, 2;
39
next if !$obj || !$rest;
41
my @deps = split /\s+/, $rest;
42
push @{$alldeps{$obj}}, @deps;
44
foreach my $dep (@deps) { print STDERR "add $obj $dep\n"; }
50
my @objs; # force rebuild on these
51
OBJ_LOOP: foreach my $obj (keys %alldeps) {
52
my $mtime = (stat $obj)[9] or next;
55
my $deps = $alldeps{$obj};
56
foreach my $dep_file (@{$deps}) {
57
my $dep_mtime = $modtimes{$dep_file};
58
if (not defined $dep_mtime) {
59
print STDERR "Skipping $dep_file for $obj, will stat() later\n" if DEBUG >= 2;
60
$not_in_cache{$dep_file} = 1;
64
print STDERR "Found $dep_file in cache\n" if DEBUG >= 2;
66
if ($dep_mtime > $mtime) {
67
print STDERR "$dep_file($dep_mtime) newer than $obj($mtime)\n" if DEBUG;
69
elsif ($dep_mtime == -1) {
70
print STDERR "Couldn't stat $dep_file for $obj\n" if DEBUG;
73
print STDERR "$dep_file($dep_mtime) older than $obj($mtime)\n" if DEBUG >= 2;
77
push @objs, $obj; # dependency is missing or newer
78
next OBJ_LOOP; # skip checking the rest of the dependencies
81
foreach my $dep_file (keys %not_in_cache) {
82
print STDERR "STAT $dep_file for $obj\n" if DEBUG >= 2;
83
my $dep_mtime = $modtimes{$dep_file} = (stat $dep_file)[9] || -1;
85
if ($dep_mtime > $mtime) {
86
print STDERR "$dep_file($dep_mtime) newer than $obj($mtime)\n" if DEBUG;
88
elsif ($dep_mtime == -1) {
89
print STDERR "Couldn't stat $dep_file for $obj\n" if DEBUG;
92
print STDERR "$dep_file($dep_mtime) older than $obj($mtime)\n" if DEBUG >= 2;
96
push @objs, $obj; # dependency is missing or newer
97
next OBJ_LOOP; # skip checking the rest of the dependencies
100
# If we get here it means nothing needs to be done for $obj
103
# Output objects to rebuild (if needed).
104
if ($outfile eq '-') {
106
print "@objs: FORCE\n";
110
my $new_output = "@objs: FORCE\n";
112
# Read in the current dependencies file.
113
open(OLD, "<$outfile")
114
and $old_output = <OLD>;
117
# Only write out the dependencies if they are different.
118
if ($new_output ne $old_output) {
119
open(OUT, ">$outfile") and print OUT "$new_output";
120
print "Updating dependencies file, $outfile\n" unless $silent;
122
print "new: $new_output\n";
123
print "was: $old_output\n" if $old_output ne '';
126
} elsif (-s $outfile) {
127
# Remove the old dependencies because all objects are up to date.
128
print "Removing old dependencies file, $outfile\n" unless $silent;
132
open(OLD, "<$outfile")
133
and $old_output = <OLD>;
135
print "was: $old_output\n";