summaryrefslogtreecommitdiff
path: root/eg/muck
blob: 873539b10c6d6f82e2689838a1aa932c0bb42f74 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!../perl

$M = '-M';
$M = '-m' if -d '/usr/uts' && -f '/etc/master';

do 'getopt.pl';
do Getopt('f');

if ($opt_f) {
    $makefile = $opt_f;
}
elsif (-f 'makefile') {
    $makefile = 'makefile';
}
elsif (-f 'Makefile') {
    $makefile = 'Makefile';
}
else {
    die "No makefile\n";
}

$MF = 'mf00';

while(($key,$val) = each(ENV)) {
    $mac{$key} = $val;
}

do scan($makefile);

$co = $action{'.c.o'};
$co = ' ' unless $co;

$missing = "Missing dependencies:\n";
foreach $key (sort keys(o)) {
    if ($oc{$key}) {
	$src = $oc{$key};
	$action = $action{$key};
    }
    else {
	$action = '';
    }
    if (!$action) {
	if ($co && ($c = $key) =~ s/\.o$/.c/ && -f $c) {
	    $src = $c;
	    $action = $co;
	}
	else {
	    print "No source found for $key $c\n";
	    next;
	}
    }
    $I = '';
    $D = '';
    $I .= $1 while $action =~ s/(-I\S+\s*)//;
    $D .= $1 . ' ' while $action =~ s/(-D\w+)//;
    if ($opt_v) {
	$cmd = "Checking $key: cc $M $D $I $src";
	$cmd =~ s/\s\s+/ /g;
	print stderr $cmd,"\n";
    }
    open(CPP,"cc $M $D $I $src|") || die "Can't run C preprocessor: $!";
    while (<CPP>) {
	($name,$dep) = split;
	$dep =~ s|^\./||;
	(print $missing,"$key: $dep\n"),($missing='')
	    unless ($dep{"$key: $dep"} += 2) > 2;
    }
}

$extra = "\nExtraneous dependencies:\n";
foreach $key (sort keys(dep)) {
    if ($key =~ /\.o: .*\.h$/ && $dep{$key} == 1) {
	print $extra,$key,"\n";
	$extra = '';
    }
}

sub scan {
    local($makefile) = @_;
    local($MF) = $MF;
    print stderr "Analyzing $makefile.\n" if $opt_v;
    $MF++;
    open($MF,$makefile) || die "Can't open $makefile: $!";
    while (<$MF>) {
	chop;
	chop($_ = $_ . <$MF>) while s/\\$//;
	next if /^#/;
	next if /^$/;
	s/\$\((\w+):([^=)]*)=([^)]*)\)/do subst("$1","$2","$3")/eg;
	s/\$\((\w+)\)/$mac{$1}/eg;
	$mac{$1} = $2, next if /^(\w+)\s*=\s*(.*)/;
	if (/^include\s+(.*)/) {
	    do scan($1);
	    print stderr "Continuing $makefile.\n" if $opt_v;
	    next;
	}
	if (/^([^:]+):\s*(.*)/) {
	    $left = $1;
	    $right = $2;
	    if ($right =~ /^([^;]*);(.*)/) {
		$right = $1;
		$action = $2;
	    }
	    else {
		$action = '';
	    }
	    while (<$MF>) {
		last unless /^\t/;
		chop;
		chop($_ = $_ . <$MF>) while s/\\$//;
		next if /^#/;
		last if /^$/;
		s/\$\((\w+):([^=)]*)=([^)]*)\)/do subst("$1","$2","$3")/eg;
		s/\$\((\w+)\)/$mac{$1}/eg;
		$action .= $_;
	    }
	    foreach $targ (split(' ',$left)) {
		$targ =~ s|^\./||;
		foreach $src (split(' ',$right)) {
		    $src =~ s|^\./||;
		    $deplist{$targ} .= ' ' . $src;
		    $dep{"$targ: $src"} = 1;
		    $o{$src} = 1 if $src =~ /\.o$/;
		    $oc{$targ} = $src if $targ =~ /\.o$/ && $src =~ /\.[yc]$/;
		}
		$action{$targ} .= $action;
	    }
	    redo if $_;
	}
    }
    close($MF);
}

sub subst {
    local($foo,$from,$to) = @_;
    $foo = $mac{$foo};
    $from =~ s/\./[.]/;
    y/a/a/;
    $foo =~ s/\b$from\b/$to/g;
    $foo;
}