| 1 |
package Tk::MakeDepend; |
| 2 |
use strict; |
| 3 |
use vars qw(%define); |
| 4 |
use Config; |
| 5 |
|
| 6 |
my @include; |
| 7 |
|
| 8 |
use Carp; |
| 9 |
|
| 10 |
$SIG{__DIE__} = \&Carp::confess; |
| 11 |
|
| 12 |
|
| 13 |
use vars qw($VERSION); |
| 14 |
$VERSION = '4.016'; |
| 15 |
|
| 16 |
sub scan_file; |
| 17 |
|
| 18 |
sub do_include |
| 19 |
{ |
| 20 |
my ($inc,$dep,@include) = @_; |
| 21 |
return if $inc eq 'ppport.h'; # Gets confused and thinks this is a perl |
| 22 |
# file. It won't add dependencies anyway. |
| 23 |
foreach my $dir (@include) |
| 24 |
{ |
| 25 |
my $path = "$dir/$inc"; |
| 26 |
if (-f $path) |
| 27 |
{ |
| 28 |
scan_file($path,$dep) unless exists $dep->{$path}; |
| 29 |
return; |
| 30 |
} |
| 31 |
} |
| 32 |
warn "Cannot find '$inc' assume made\n"; |
| 33 |
$dep->{$inc} = 1; |
| 34 |
} |
| 35 |
|
| 36 |
sub remove_comment |
| 37 |
{ |
| 38 |
s#^\s*/\*.*?\*/\s*##g; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
sub term |
| 43 |
{ |
| 44 |
remove_comment(); |
| 45 |
return !term() if s/^\s*!//; |
| 46 |
return exists($define{$1}) if s/^\s*defined\s*\(([_A-Za-z][_\w]*)\s*\)//; |
| 47 |
return exists($define{$1}) if s/^\s*defined\s*([_A-Za-z][_\w]*)//; |
| 48 |
return eval "$1" if s/^\s*(0x[0-9a-f]+)L?//i; |
| 49 |
return $1 if s/^\s*(\d+)L?//; |
| 50 |
return $define{$1} || 0 if s/^\s*([_A-Za-z][_\w]*)//; |
| 51 |
if (s/^\s*\(//) |
| 52 |
{ |
| 53 |
my $val = expression(0); |
| 54 |
warn "Missing ')'\n" unless s/^\s*\)//; |
| 55 |
return $val; |
| 56 |
} |
| 57 |
warn "Invalid term:$_"; |
| 58 |
return undef; |
| 59 |
} |
| 60 |
|
| 61 |
my %pri = ( '&&' => 4, |
| 62 |
'||' => 3, |
| 63 |
'>=' => 2, '<=' => 2, '<' => 2, '>' => 2, |
| 64 |
'==' => 1, '!=' => 1 ); |
| 65 |
|
| 66 |
sub expression |
| 67 |
{ |
| 68 |
my $pri = shift; |
| 69 |
# printf STDERR "%d# expr . $_\n"; |
| 70 |
my $invert = 0; |
| 71 |
my $lhs = term() || 0; |
| 72 |
remove_comment(); |
| 73 |
while (/^\s*(&&|\|\||>=?|<=?|==|!=)/) |
| 74 |
{ |
| 75 |
my $op = $1; |
| 76 |
last unless ($pri{$op} >= $pri); |
| 77 |
s/^\s*\Q$op\E//; |
| 78 |
# printf STDERR "%d# $lhs $op . $_\n"; |
| 79 |
my $rhs = expression($pri{$op}) || 0; |
| 80 |
my $e = "$lhs $op $rhs"; |
| 81 |
$lhs = eval "$e" || 0; |
| 82 |
die "'$e' $@" if $@; |
| 83 |
remove_comment(); |
| 84 |
} |
| 85 |
return $lhs; |
| 86 |
} |
| 87 |
|
| 88 |
sub do_if |
| 89 |
{ |
| 90 |
my ($key,$expr) = @_; |
| 91 |
chomp($expr); |
| 92 |
if ($key eq 'ifdef' || $key eq 'ifndef') |
| 93 |
{ |
| 94 |
if ($expr =~ /^\s*(\w+)/) |
| 95 |
{ |
| 96 |
my $val = exists $define{$1}; |
| 97 |
$val = !$val if ($key eq 'ifndef'); |
| 98 |
# printf STDERR "%d from $key $expr\n",$val; |
| 99 |
return $val; |
| 100 |
} |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
local $_ = $expr; |
| 105 |
my $val = expression(0) != 0; |
| 106 |
warn "trailing: $_" if /\S/; |
| 107 |
#printf STDERR "%d from $key $expr\n",$val; |
| 108 |
return $val; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
sub scan_file |
| 113 |
{ |
| 114 |
no strict 'refs'; |
| 115 |
my ($file,$dep) = @_; |
| 116 |
open($file,"<$file") || die "Cannot open $file:$!"; |
| 117 |
local $_; |
| 118 |
my ($srcdir) = $file =~ m#^(.*)[\\/][^\\/]*$#; |
| 119 |
$srcdir = '.' unless defined $srcdir; |
| 120 |
my $live = 1; |
| 121 |
$dep->{$file} = 1; |
| 122 |
my @stack; |
| 123 |
while (<$file>) |
| 124 |
{ |
| 125 |
$_ .= <$file> while (s/\\\n/ /); |
| 126 |
if (/^\s*#\s*(\w+)\s*(.*?)\s*$/) |
| 127 |
{ |
| 128 |
my $ol = $live; |
| 129 |
my $key = $1; |
| 130 |
my $rest = $2; |
| 131 |
if ($key =~ /^if(.*)$/) |
| 132 |
{ |
| 133 |
push(@stack,$live); |
| 134 |
$live &&= do_if($key,$rest); |
| 135 |
} |
| 136 |
elsif ($key eq 'elif') |
| 137 |
{ |
| 138 |
$live = ($live) ? 0 : $stack[-1]; |
| 139 |
$live &&= do_if('if',$rest); |
| 140 |
} |
| 141 |
elsif ($key eq 'else') |
| 142 |
{ |
| 143 |
$live = ($live) ? 0 : $stack[-1]; |
| 144 |
} |
| 145 |
elsif ($key eq 'endif') |
| 146 |
{ |
| 147 |
if (@stack) |
| 148 |
{ |
| 149 |
$live = pop(@stack); |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
die "$file:$.: Mismatched #endif\n"; |
| 154 |
} |
| 155 |
} |
| 156 |
elsif ($live) |
| 157 |
{ |
| 158 |
if ($key eq 'include') |
| 159 |
{ |
| 160 |
do_include($1,$dep,$srcdir,@include) if $rest =~ /^"(.*)"/; |
| 161 |
} |
| 162 |
elsif ($key eq 'define') |
| 163 |
{ |
| 164 |
if ($rest =~ /^\s*([_A-Za-z][\w_]*)\s*(.*)$/) |
| 165 |
{ |
| 166 |
my $sym = $1; |
| 167 |
my $val = $2 || 1; |
| 168 |
$val =~ s#\s*/\*.*?\*/\s*# #g; |
| 169 |
$define{$sym} = $val; |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
warn "ignore '$key $rest'\n"; |
| 174 |
} |
| 175 |
} |
| 176 |
elsif ($key eq 'undef') |
| 177 |
{ |
| 178 |
if ($rest =~ /^\s*([_A-Za-z][\w_]*)/) |
| 179 |
{ |
| 180 |
delete $define{$1}; |
| 181 |
} |
| 182 |
} |
| 183 |
elsif ($key =~ /^(line|pragma)$/) |
| 184 |
{ |
| 185 |
|
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
warn "ignore '$key $rest'\n"; |
| 190 |
} |
| 191 |
} |
| 192 |
# printf STDERR "$file:$.: %d $key $rest\n",$live if ($ol != $live); |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
# print if $live; |
| 197 |
} |
| 198 |
} |
| 199 |
close($file); |
| 200 |
if (@stack) |
| 201 |
{ |
| 202 |
warn "$file:$.: unclosed #if\n"; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
sub command_line |
| 207 |
{ |
| 208 |
@include = (); |
| 209 |
local %define = ('__STDC__' => 1 ); |
| 210 |
my $data = ''; |
| 211 |
my @files; |
| 212 |
while (@_ && $_[-1] !~ /^-/) |
| 213 |
{ |
| 214 |
unshift(@files,pop(@_)); |
| 215 |
} |
| 216 |
my $flags = $Config{ccflags}; |
| 217 |
$flags =~ s/^\s+|\s+$//g; |
| 218 |
my @opt = (@_, split(/\s+/,$flags)); |
| 219 |
while (@opt) |
| 220 |
{ |
| 221 |
local $_ = shift(@opt); |
| 222 |
if (/^-I(.*)$/) |
| 223 |
{ |
| 224 |
push @include,$1; |
| 225 |
} |
| 226 |
elsif (/^-D([^=]+)(?:=(.*))?$/) |
| 227 |
{ |
| 228 |
$define{$1} = $2 || 1; |
| 229 |
} |
| 230 |
elsif (/^-U(.*)$/) |
| 231 |
{ |
| 232 |
delete $define{$1}; |
| 233 |
} |
| 234 |
elsif (/^(-.*)$/) |
| 235 |
{ |
| 236 |
# Some option |
| 237 |
if ($opt[0] !~ /^-/) |
| 238 |
{ |
| 239 |
# next arg does not start with '-' assume it |
| 240 |
# belongs to this option and discard it silently |
| 241 |
shift(@opt); |
| 242 |
} |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
# We got confused |
| 247 |
warn "Ignoring $1\n"; |
| 248 |
} |
| 249 |
} |
| 250 |
# force /usr/include (and other system directories) to be last element of @include |
| 251 |
if ($Config{ccname} eq 'gcc') |
| 252 |
{ |
| 253 |
push @include, parse_gcc_inc_search_list(); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
push @include, $Config{'usrinc'} |
| 258 |
if (defined $Config{'usrinc'} and $Config{'usrinc'} ne ''); |
| 259 |
} |
| 260 |
# warn "Include:@include\n"; |
| 261 |
while (@files) |
| 262 |
{ |
| 263 |
local $_ = shift(@files); |
| 264 |
unless (/^(.*)\.[^\.]+$/) |
| 265 |
{ |
| 266 |
warn "Skip $_"; |
| 267 |
next; |
| 268 |
} |
| 269 |
local %define = %define; |
| 270 |
my $base = $1; |
| 271 |
my $file = $_; |
| 272 |
my %dep; |
| 273 |
warn "Finding dependencies for $file\n"; |
| 274 |
scan_file($_,\%dep); |
| 275 |
my $str = "\n$base\$(OBJ_EXT) : $base.c"; |
| 276 |
delete $dep{$file}; |
| 277 |
my @dep = (sort(keys %dep)); |
| 278 |
while (@dep) |
| 279 |
{ |
| 280 |
my $dep = shift(@dep); |
| 281 |
$dep =~ s#^\./##; |
| 282 |
if (length($str)+length($dep) > 70) |
| 283 |
{ |
| 284 |
$data .= "$str \\\n"; |
| 285 |
$str = ' '; |
| 286 |
} |
| 287 |
else |
| 288 |
{ |
| 289 |
$str .= ' '; |
| 290 |
} |
| 291 |
$str .= $dep; |
| 292 |
} |
| 293 |
$data .= "$str\n"; |
| 294 |
} |
| 295 |
return $data; |
| 296 |
} |
| 297 |
|
| 298 |
sub parse_gcc_inc_search_list |
| 299 |
{ |
| 300 |
our @GCC_INC_SEARCH_LIST; |
| 301 |
return @GCC_INC_SEARCH_LIST if @GCC_INC_SEARCH_LIST; |
| 302 |
|
| 303 |
require File::Spec; |
| 304 |
my $cmd = "$Config{cc} -xc -E -v -"; |
| 305 |
my @inc; |
| 306 |
eval |
| 307 |
{ |
| 308 |
open my $fh, "-|", "$cmd 2>&1 <".File::Spec->devnull |
| 309 |
or die "Error while running $cmd: $!"; |
| 310 |
|
| 311 |
my $in_search_list; |
| 312 |
while(<$fh>) |
| 313 |
{ |
| 314 |
if (/^#include <\.\.\.> search starts here:/) |
| 315 |
{ |
| 316 |
$in_search_list = 1; |
| 317 |
} |
| 318 |
elsif ($in_search_list) |
| 319 |
{ |
| 320 |
last if /^End of search list/; |
| 321 |
chomp; |
| 322 |
s/^\s+//; |
| 323 |
push @inc, $_; |
| 324 |
} |
| 325 |
} |
| 326 |
}; |
| 327 |
if ($@) |
| 328 |
{ |
| 329 |
warn $@; |
| 330 |
} |
| 331 |
if (!@inc) |
| 332 |
{ |
| 333 |
# Fallback: add /usr/include |
| 334 |
@inc = $Config{'usrinc'} |
| 335 |
if (defined $Config{'usrinc'} and $Config{'usrinc'} ne ''); |
| 336 |
} |
| 337 |
@GCC_INC_SEARCH_LIST = @inc; |
| 338 |
@inc; |
| 339 |
} |
| 340 |
|
| 341 |
1; |
| 342 |
__END__ |
| 343 |
|
| 344 |
|