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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
package Tk::MakeDepend;
use strict;
use vars qw(%define);
use Config;
my @include;
use Carp;
$SIG{__DIE__} = \&Carp::confess;
use vars qw($VERSION);
$VERSION = sprintf '4.%03d', q$Revision: #13 $ =~ /\D(\d+)\s*$/;
sub scan_file;
sub do_include
{
my ($inc,$dep,@include) = @_;
foreach my $dir (@include)
{
my $path = "$dir/$inc";
if (-f $path)
{
scan_file($path,$dep) unless exists $dep->{$path};
return;
}
}
warn "Cannot find '$inc' assume made\n";
$dep->{$inc} = 1;
}
sub remove_comment
{
s#^\s*/\*.*?\*/\s*##g;
}
sub term
{
remove_comment();
return !term() if s/^\s*!//;
return exists($define{$1}) if s/^\s*defined\s*\(([_A-Za-z][_\w]*)\s*\)//;
return exists($define{$1}) if s/^\s*defined\s*([_A-Za-z][_\w]*)//;
return eval "$1" if s/^\s*(0x[0-9a-f]+)//i;
return $1 if s/^\s*(\d+)//;
return $define{$1} || 0 if s/^\s*([_A-Za-z][_\w]*)//;
if (s/^\s*\(//)
{
my $val = expression(0);
warn "Missing ')'\n" unless s/^\s*\)//;
return $val;
}
warn "Invalid term:$_";
return undef;
}
my %pri = ( '&&' => 4,
'||' => 3,
'>=' => 2, '<=' => 2, '<' => 2, '>' => 2,
'==' => 1, '!=' => 1 );
sub expression
{
my $pri = shift;
# printf STDERR "%d# expr . $_\n";
my $invert = 0;
my $lhs = term() || 0;
remove_comment();
while (/^\s*(&&|\|\||>=?|<=?|==|!=)/)
{
my $op = $1;
last unless ($pri{$op} >= $pri);
s/^\s*\Q$op\E//;
# printf STDERR "%d# $lhs $op . $_\n";
my $rhs = expression($pri{$op}) || 0;
my $e = "$lhs $op $rhs";
$lhs = eval "$e" || 0;
die "'$e' $@" if $@;
remove_comment();
}
return $lhs;
}
sub do_if
{
my ($key,$expr) = @_;
chomp($expr);
if ($key eq 'ifdef' || $key eq 'ifndef')
{
if ($expr =~ /^\s*(\w+)/)
{
my $val = exists $define{$1};
$val = !$val if ($key eq 'ifndef');
# printf STDERR "%d from $key $expr\n",$val;
return $val;
}
}
else
{
local $_ = $expr;
my $val = expression(0) != 0;
warn "trailing: $_" if /\S/;
#printf STDERR "%d from $key $expr\n",$val;
return $val;
}
}
sub scan_file
{
no strict 'refs';
my ($file,$dep) = @_;
open($file,"<$file") || die "Cannot open $file:$!";
local $_;
my ($srcdir) = $file =~ m#^(.*)[\\/][^\\/]*$#;
$srcdir = '.' unless defined $srcdir;
my $live = 1;
$dep->{$file} = 1;
my @stack;
while (<$file>)
{
$_ .= <$file> while (s/\\\n/ /);
if (/^\s*#\s*(\w+)\s*(.*?)\s*$/)
{
my $ol = $live;
my $key = $1;
my $rest = $2;
if ($key =~ /^if(.*)$/)
{
push(@stack,$live);
$live &&= do_if($key,$rest);
}
elsif ($key eq 'elif')
{
$live = ($live) ? 0 : $stack[-1];
$live &&= do_if('if',$rest);
}
elsif ($key eq 'else')
{
$live = ($live) ? 0 : $stack[-1];
}
elsif ($key eq 'endif')
{
if (@stack)
{
$live = pop(@stack);
}
else
{
die "$file:$.: Mismatched #endif\n";
}
}
elsif ($live)
{
if ($key eq 'include')
{
do_include($1,$dep,$srcdir,@include) if $rest =~ /^"(.*)"/;
}
elsif ($key eq 'define')
{
if ($rest =~ /^\s*([_A-Za-z][\w_]*)\s*(.*)$/)
{
my $sym = $1;
my $val = $2 || 1;
$val =~ s#\s*/\*.*?\*/\s*# #g;
$define{$sym} = $val;
}
else
{
warn "ignore '$key $rest'\n";
}
}
elsif ($key eq 'undef')
{
if ($rest =~ /^\s*([_A-Za-z][\w_]*)/)
{
delete $define{$1};
}
}
elsif ($key =~ /^(line|pragma)$/)
{
}
else
{
warn "ignore '$key $rest'\n";
}
}
# printf STDERR "$file:$.: %d $key $rest\n",$live if ($ol != $live);
}
else
{
# print if $live;
}
}
close($file);
if (@stack)
{
warn "$file:$.: unclosed #if\n";
}
}
sub command_line
{
@include = ();
local %define = ('__STDC__' => 1 );
my $data = '';
my @files;
while (@_ && $_[-1] !~ /^-/)
{
unshift(@files,pop(@_));
}
my $flags = $Config{ccflags};
$flags =~ s/^\s+|\s+$//g;
my @opt = (@_, split(/\s+/,$flags));
while (@opt)
{
local $_ = shift(@opt);
if (/^-I(.*)$/)
{
push @include,$1;
}
elsif (/^-D([^=]+)(?:=(.*))?$/)
{
$define{$1} = $2 || 1;
}
elsif (/^-U(.*)$/)
{
delete $define{$1};
}
elsif (/^(-.*)$/)
{
# Some option
if ($opt[0] !~ /^-/)
{
# next arg does not start with '-' assume it
# belongs to this option and discard it silently
shift(@opt);
}
}
else
{
# We got confused
warn "Ignoring $1\n";
}
}
# force /usr/include to be last element of @include
push @include, $Config{'usrinc'}
if (defined $Config{'usrinc'} and $Config{'usrinc'} ne '');
# warn "Include:@include\n";
while (@files)
{
local $_ = shift(@files);
unless (/^(.*)\.[^\.]+$/)
{
warn "Skip $_";
next;
}
local %define = %define;
my $base = $1;
my $file = $_;
my %dep;
warn "Finding dependencies for $file\n";
scan_file($_,\%dep);
my $str = "\n$base\$(OBJ_EXT) : $base.c";
delete $dep{$file};
my @dep = (sort(keys %dep));
while (@dep)
{
my $dep = shift(@dep);
$dep =~ s#^\./##;
if (length($str)+length($dep) > 70)
{
$data .= "$str \\\n";
$str = ' ';
}
else
{
$str .= ' ';
}
$str .= $dep;
}
$data .= "$str\n";
}
return $data;
}
1;
__END__
|