summaryrefslogtreecommitdiff
path: root/make/calcdep.pl
blob: 92d2cc3e07a6622af67a34bc351b16d4ec903c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl
use strict;
use warnings;

# This used to be a wrapper around cc -M; however, this is a very slow
# operation and we don't conditionally include our own files often enough
# to justify the full preprocesor invocation for all ~200 files.

my %f2dep;

sub gendep;
sub gendep {
	my $f = shift;
	my $basedir = $f =~ m#(.*)/# ? $1 : '.';
	return $f2dep{$f} if exists $f2dep{$f};
	$f2dep{$f} = '';
	my %dep;
	open my $in, '<', $f;
	while (<$in>) {
		if (/^\s*#\s*include\s*"([^"]+)"/) {
			my $inc = $1;
			for my $loc ("$basedir/$inc", "../include/$inc") {
				next unless -e $loc;
				$dep{$loc}++;
				$dep{$_}++ for split / /, gendep $loc;
			}
		}
	}
	close $in;
	$f2dep{$f} = join ' ', sort keys %dep;
	$f2dep{$f};
}

for my $file (@ARGV) {
	next unless $file =~ /cpp$/;
	gendep $file;
	my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp#;
	my $cmd = "$path.$base.d";
	my $ext = $path eq 'modules/' || $path eq 'commands/' ? '.so' : '.o';
	my $out = "$path$base$ext";

	open OUT, '>', $cmd;
	print OUT "$out: $file $f2dep{$file}\n";
	print OUT "\t@../make/unit-cc.pl \$(VERBOSE) $file $out\n";
	print OUT "$cmd: $file $f2dep{$file}\n";
	print OUT "\t../make/calcdep.pl $file\n";
}