Debian Codegraph @

tools/mkdep.pl

raw
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: BSD-2-Clause
3 # Copyright 1996-2024 The NASM Authors - All Rights Reserved
4
5 #
6 # Script to create Makefile-style dependencies.
7 #
8 # Usage:
9 # perl mkdep.pl [-i][-e][-m makefile]...[-M makefile... --] dir...
10 #
11
12 use strict;
13 use integer;
14
15 use File::Spec;
16 use File::Basename;
17 use File::Copy;
18 use File::Temp;
19 use Fcntl;
20
21 my $barrier =
22 '#-- Everything below is generated by mkdep.pl - do not edit --#';
23
24 # This converts from filenames to full pathnames for our dependencies
25 # These are arrays of [full path, Makefile path]
26 my %dep_path = ();
27
28 # List of files that cannot be found; these *must* be excluded
29 my @must_exclude = ();
30
31 #
32 # Variables derived from the command line
33 #
34 my %deps;
35 my %excludes;
36 my @files;
37 my @mkfiles;
38 my $mkmode = 0;
39 my @searchdirs = (File::Spec->curdir());
40 my %searchdirs = (File::Spec->curdir() => 1);
41 my $force_inline = 0;
42 my $externalize = 0;
43 my $debug = 0;
44
45 #
46 # Scan files for dependencies
47 # $path is the full filesystem path, $file Makefile name
48 #
49 sub scandeps {
50 my($path, $file) = @_;
51 my $line;
52 my %xdeps;
53 my %mdeps;
54
55 print STDERR "mkdep: scanning file: $path\n" if ( $debug );
56
57 my $fh;
58 if (!open($fh, '<', $path)) {
59 print STDERR " -> missing, assume generated\n" if ( $debug );
60 return;
61 }
62
63 while ( defined($line = <$fh>) ) {
64 $line =~ s/\s+$//;
65 $line =~ s:/\*.*\*/::g;
66 $line =~ s://.*$::;
67 if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
68 my $nf = $1;
69 my $dp = $dep_path{$nf};
70 if (!defined($dp)) {
71 push(@must_exclude, $nf);
72 print STDERR " -> $nf [missing]\n" if ( $debug );
73 next;
74 }
75 my $dn = $dp->[1];
76 $mdeps{$dn}++;
77 $xdeps{$dn} = $dp unless ( defined($deps{$dn}) );
78 printf STDERR " -> %s (%s)\n", $nf, $dn if ( $debug );
79 }
80 }
81 close($fh);
82 $deps{$file} = [keys(%mdeps)];
83
84 foreach my $xf ( keys(%xdeps) ) {
85 scandeps(@{$xdeps{$xf}});
86 }
87 }
88
89 # %deps contains direct dependencies. This subroutine resolves
90 # indirect dependencies that result.
91 sub _alldeps($$$) {
92 my($file, $level, $adeps) = @_;
93
94 return if ($adeps->{$file});
95
96 printf STDERR " %s-> %s\n", (' ' x $level), $file if ( $debug );
97 $adeps->{$file}++;
98
99 foreach my $dep ( @{$deps{$file}} ) {
100 _alldeps($dep, $level+1, $adeps);
101 }
102 }
103
104 sub alldeps($) {
105 my($file) = @_;
106
107 my %adeps;
108 _alldeps($file, 1, \%adeps);
109 return sort(keys(%adeps));
110 }
111
112 # This converts a filename from host syntax to target syntax
113 # This almost certainly works only on relative filenames...
114 sub convert_file($$) {
115 my($file,$sep) = @_;
116
117 if ($file eq '' || $file eq File::Spec->curdir() ||
118 $file eq File::Spec->rootdir()) {
119 return undef;
120 }
121
122 my @fspec = (basename($file));
123 while ( ($file = dirname($file)) ne File::Spec->curdir() &&
124 $file ne File::Spec->rootdir() ) {
125 unshift(@fspec, basename($file));
126 }
127
128 if ( $sep eq '' ) {
129 # This means kill path completely. Used with Makes who do
130 # path searches, but doesn't handle output files in subdirectories,
131 # like OpenWatcom WMAKE.
132 return $fspec[scalar(@fspec)-1];
133 } else {
134 return join($sep, @fspec);
135 }
136 }
137
138 #
139 # Insert dependencies into a Makefile
140 #
141 sub insert_deps($) {
142 my($file) = @_;
143
144 open(my $in, '<', $file)
145 or die "$0: Cannot open input: $file\n";
146
147 my ($line, $parm, $val);
148 my $obj = '.o'; # Defaults
149 my $sep = '/';
150 my $cont = "\\";
151 my $include_command = undef;
152 my $selfrule = 0;
153 my $maxline = 78; # Seems like a reasonable default
154 my %exclude = (); # Don't exclude anything
155 my @genhdrs = ();
156 my $external = undef;
157 my $raw_output = 0;
158 my @outfile = ();
159 my $is_external = 0;
160
161 while ( defined($line = <$in>) ) {
162 $line =~ s/\s+$//;
163 if ( $line =~ /^([^\s\#\$\:]+\.h):/ ) {
164 # Note: we trust the first Makefile given best
165 my $fpath = $1;
166 my $fbase = basename($fpath);
167 if (!defined($dep_path{$fbase})) {
168 $dep_path{$fbase} = [$fpath, $fpath];
169 print STDERR "Makefile: $fbase -> $fpath\n" if ( $debug );
170 }
171 } elsif ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
172 $parm = $1; $val = $2;
173 if ( $parm eq 'object-ending' ) {
174 $obj = $val;
175 } elsif ( $parm eq 'path-separator' ) {
176 $sep = $val;
177 } elsif ( $parm eq 'line-width' ) {
178 $maxline = $val+0;
179 } elsif ( $parm eq 'continuation' ) {
180 $cont = $val;
181 } elsif ( $parm eq 'exclude' ) {
182 $excludes{$val}++;
183 } elsif ( $parm eq 'include-command' ) {
184 $include_command = $val;
185 } elsif ( $parm eq 'external' ) {
186 # Keep dependencies in an external file
187 $external = $val;
188 } elsif ( $parm eq 'selfrule' ) {
189 $selfrule = !!$val;
190 }
191 } elsif ( $line =~ /^(\s*\#?\s*EXTERNAL_DEPENDENCIES\s*=\s*)([01])\s*$/ ) {
192 # If this line is not present, we cannot externalize
193 $is_external = $externalize ? 1 : $force_inline ? 0 : $2+0;
194 $line = $1.$is_external;
195 } elsif ( $line eq $barrier ) {
196 last; # Stop reading at barrier line
197 }
198
199 push @outfile, $line."\n";
200 }
201 close($in);
202
203 $is_external = $is_external && defined($external);
204 if ( !$is_external ) {
205 undef $external;
206 $selfrule = 0;
207 }
208
209 my $out;
210 my $outpath;
211 if ( !$is_external || $externalize ) {
212 $out = File::Temp->new(DIR => dirname($outpath = $file));
213 print $out @outfile;
214 } else {
215 $out = File::Temp->new(DIR => dirname($outpath = $external));
216 }
217
218 print $out $barrier, "\n";
219
220 if ( $externalize ) {
221 # Just strip internal file dependency information
222 if (defined($include_command)) {
223 print $out "$include_command $external\n";
224 }
225 unlink($external);
226 } else {
227 foreach my $dfile ($external, sort(keys(%deps)) ) {
228 my $ofile;
229 my @deps;
230
231 next unless (defined($dfile));
232
233 if ( $selfrule && $dfile eq $external ) {
234 $ofile = convert_file($dfile, $sep).':';
235 @deps = sort(keys(%deps));
236 } elsif ( $dfile =~ /^(.*)\.[Cc]$/ ) {
237 $ofile = convert_file($1, $sep).$obj.':';
238 print STDERR "mkdep: dependencies for: $dfile\n" if ( $debug );
239 @deps = alldeps($dfile);
240 }
241
242 if (defined($ofile)) {
243 my $len = length($ofile);
244 print $out $ofile;
245 foreach my $dep (@deps) {
246 unless ($excludes{$dep}) {
247 my $str = convert_file($dep, $sep);
248 my $sl = length($str)+1;
249 if ( $len+$sl > $maxline-2 ) {
250 print $out ' ', $cont, "\n ", $str;
251 $len = $sl;
252 } else {
253 print $out ' ', $str;
254 $len += $sl;
255 }
256 }
257 }
258 print $out "\n";
259 }
260 }
261 }
262
263 close($out);
264 move($out->filename, $outpath);
265 }
266
267 #
268 # Main program
269 #
270
271 while ( defined(my $arg = shift(@ARGV)) ) {
272 if ( $arg eq '-m' ) {
273 $arg = shift(@ARGV);
274 push(@mkfiles, $arg);
275 } elsif ( $arg eq '-s' ) {
276 $arg = shift(@ARGV);
277 push(@searchdirs, $arg);
278 } elsif ( $arg eq '-i' ) {
279 $force_inline = 1;
280 } elsif ( $arg eq '-e' ) {
281 $externalize = 1;
282 } elsif ( $arg eq '-d' ) {
283 $debug++;
284 } elsif ( $arg eq '-M' ) {
285 $mkmode = 1; # Further filenames are output Makefile names
286 } elsif ( $arg eq '--' && $mkmode ) {
287 $mkmode = 0;
288 } elsif ( $arg =~ /^-/ ) {
289 die "Unknown option: $arg\n";
290 } else {
291 if ( $mkmode ) {
292 push(@mkfiles, $arg);
293 } else {
294 push(@files, $arg);
295 }
296 }
297 }
298
299 sub mycatdir($$) {
300 my($a,$b) = @_;
301 return $b if ($a eq File::Spec->curdir());
302 return $a if ($b eq File::Spec->curdir());
303 return File::Spec->catdir($a,$b);
304 }
305
306 sub mycatfile($$) {
307 my($d,$f) = @_;
308 return $f if ($d eq File::Spec->curdir());
309 return File::Spec->catfile($d,$f);
310 }
311
312 my @cfiles = ();
313
314 my $err = 0;
315 my %scanned;
316
317 foreach my $fdir ( @files ) {
318 my $found = 0;
319
320 foreach my $sdir ( @searchdirs ) {
321 my $dir = mycatdir($sdir, $fdir);
322
323 if ($scanned{$dir}) {
324 # Have already been here
325 $found = 1;
326 next;
327 }
328
329 print STDERR "mkdep: scanning directory $dir\n" if ( $debug );
330
331 opendir(DIR, $dir) or next;
332 $scanned{$dir}++;
333 $found++;
334
335 while ( my $file = readdir(DIR) ) {
336 # $fdir is correct here, because we expect VPATH to do
337 # its job, and the output filename depends on that, not
338 # on the full source dir path.
339 my $path = mycatfile($fdir, $file);
340 my $fullpath = mycatfile($dir, $file);
341 if ( $file =~ /\.[Cc]$/ ) {
342 push(@cfiles, [$fullpath, $path]);
343 } elsif ( $file =~ /\.[Hh]$/ ) {
344 print STDERR "mkdep: filesystem: $file -> $path\n" if ( $debug );
345 if (defined($dep_path{$file})) {
346 print STDERR "mkdep: warning: more than one instance of filename $file!\n";
347 }
348 $dep_path{$file} = [$fullpath, $path];
349 $dep_path{$path} = [$fullpath, $path];
350 }
351 }
352 closedir(DIR);
353 }
354
355 if (!$found) {
356 print STDERR "$0: cannot find directory: $fdir\n";
357 $err++;
358 }
359 }
360
361 exit(1) if ($err);
362
363 foreach my $cfile ( @cfiles ) {
364 scandeps(@$cfile);
365 }
366
367 foreach my $mkfile ( @mkfiles ) {
368 insert_deps($mkfile);
369 }