rbtree: add rb_search_exact()
[nasm.git] / tools / mkdep.pl
blob27342c7fa1b9d780d7b8a8bee1e4586bf61b5820
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2020 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
7 ##
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following
10 ## conditions are met:
12 ## * Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## * Redistributions in binary form must reproduce the above
15 ## copyright notice, this list of conditions and the following
16 ## disclaimer in the documentation and/or other materials provided
17 ## with the distribution.
19 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ## --------------------------------------------------------------------------
36 # Script to create Makefile-style dependencies.
38 # Usage:
39 # perl mkdep.pl [-s path-separator][-o obj-ext] dir... > deps
40 # perl mkdep.pl [-i][-e][-m makefile]...[-M makefile... --] dir...
43 use File::Spec;
44 use File::Basename;
45 use File::Copy;
46 use File::Temp;
47 use Fcntl;
49 $barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n";
51 # This converts from filenames to full pathnames for our dependencies
52 %dep_path = ();
54 # List of files that cannot be found; these *must* be excluded
55 @must_exclude = ();
58 # Scan files for dependencies
60 sub scandeps($) {
61 my($file) = @_;
62 my $line;
63 my %xdeps;
64 my %mdeps;
66 open(my $fh, '<', $file)
67 or return; # If not openable, assume generated
69 while ( defined($line = <$fh>) ) {
70 chomp $line;
71 $line =~ s:/\*.*\*/::g;
72 $line =~ s://.*$::;
73 if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
74 my $nf = $1;
75 if (!defined($dep_path{$nf})) {
76 push(@must_exclude, $nf);
77 next;
79 $nf = $dep_path{$nf};
80 $mdeps{$nf}++;
81 $xdeps{$nf}++ unless ( defined($deps{$nf}) );
84 close($fh);
85 $deps{$file} = [keys(%mdeps)];
87 foreach my $xf ( keys(%xdeps) ) {
88 scandeps($xf);
92 # %deps contains direct dependencies. This subroutine resolves
93 # indirect dependencies that result.
94 sub alldeps($$) {
95 my($file, $level) = @_;
96 my %adeps;
98 foreach my $dep ( @{$deps{$file}} ) {
99 $adeps{$dep} = 1;
100 foreach my $idep ( alldeps($dep, $level+1) ) {
101 $adeps{$idep} = 1;
104 return sort(keys(%adeps));
107 # This converts a filename from host syntax to target syntax
108 # This almost certainly works only on relative filenames...
109 sub convert_file($$) {
110 my($file,$sep) = @_;
112 my @fspec = (basename($file));
113 while ( ($file = dirname($file)) ne File::Spec->curdir() &&
114 $file ne File::Spec->rootdir() ) {
115 unshift(@fspec, basename($file));
118 if ( $sep eq '' ) {
119 # This means kill path completely. Used with Makes who do
120 # path searches, but doesn't handle output files in subdirectories,
121 # like OpenWatcom WMAKE.
122 return $fspec[scalar(@fspec)-1];
123 } else {
124 return join($sep, @fspec);
129 # Insert dependencies into a Makefile
131 sub _insert_deps($$) {
132 my($file, $out) = @_;
134 open(my $in, '<', $file)
135 or die "$0: Cannot open input: $file\n";
137 my ($line, $parm, $val);
138 my $obj = '.o'; # Defaults
139 my $sep = '/';
140 my $cont = "\\";
141 my $include_command = undef;
142 my $selfrule = 0;
143 my $maxline = 78; # Seems like a reasonable default
144 my %exclude = (); # Don't exclude anything
145 my @genhdrs = ();
146 my $external = undef;
147 my $raw_output = 0;
148 my @outfile = ();
149 my $is_external = 0;
151 while ( defined($line = <$in>) ) {
152 if ( $line =~ /^([^\s\#\$\:]+\.h):/ ) {
153 # Note: we trust the first Makefile given best
154 my $fpath = $1;
155 my $fbase = basename($fpath);
156 if (!defined($dep_path{$fbase})) {
157 $dep_path{$fbase} = $fpath;
158 print STDERR "Makefile: $fbase -> $fpath\n";
160 } elsif ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
161 $parm = $1; $val = $2;
162 if ( $parm eq 'object-ending' ) {
163 $obj = $val;
164 } elsif ( $parm eq 'path-separator' ) {
165 $sep = $val;
166 } elsif ( $parm eq 'line-width' ) {
167 $maxline = $val+0;
168 } elsif ( $parm eq 'continuation' ) {
169 $cont = $val;
170 } elsif ( $parm eq 'exclude' ) {
171 $excludes{$val}++;
172 } elsif ( $parm eq 'include-command' ) {
173 $include_command = $val;
174 } elsif ( $parm eq 'external' ) {
175 # Keep dependencies in an external file
176 $external = $val;
177 } elsif ( $parm eq 'selfrule' ) {
178 $selfrule = !!$val;
180 } elsif ( $line =~ /^(\s*\#?\s*EXTERNAL_DEPENDENCIES\s*=\s*)([01])\s*$/ ) {
181 # If this line is not present, we cannot externalize
182 $is_external = $externalize ? 1 : $force_inline ? 0 : $2+0;
183 $line = $1.$is_external."\n";
184 } elsif ( $line eq $barrier ) {
185 last; # Stop reading at barrier line
188 push @outfile, $line;
190 close($in);
192 $is_external = $is_external && defined($external);
193 undef $external if ( !$is_external );
195 if ( !$is_external || $externalize ) {
196 print $out @outfile;
199 print $out $barrier;
201 if ( $externalize ) {
202 # Just strip internal file dependency information
203 if (defined($include_command)) {
204 print $out "$include_command $external\n";
206 unlink($external);
207 return undef;
210 my $e;
212 foreach my $dfile ($external, sort(keys(%deps)) ) {
213 my $ofile;
214 my @deps;
216 next unless (defined($dfile));
218 if ( $selfrule && $dfile eq $external ) {
219 $ofile = convert_file($dfile, $sep).':';
220 @deps = sort(keys(%deps));
221 } elsif ( $dfile =~ /^(.*)\.[Cc]$/ ) {
222 $ofile = convert_file($1, $sep).$obj.':';
223 @deps = ($dfile,alldeps($dfile,1));
226 if (defined($ofile)) {
227 my $len = length($ofile);
228 print $out $ofile;
229 foreach my $dep (@deps) {
230 unless ($excludes{$dep}) {
231 my $str = convert_file($dep, $sep);
232 my $sl = length($str)+1;
233 if ( $len+$sl > $maxline-2 ) {
234 print $out ' ', $cont, "\n ", $str;
235 $len = $sl;
236 } else {
237 print $out ' ', $str;
238 $len += $sl;
242 print $out "\n";
246 return $external;
249 sub insert_deps($)
251 my($mkfile) = @_;
252 my $tmp = File::Temp->new(DIR => dirname($mkfile));
253 my $tmpname = $tmp->filename;
255 my $newname = _insert_deps($mkfile, $tmp);
256 close($tmp);
258 $newname = $mkfile unless(defined($newname));
260 move($tmpname, $newname);
264 # Main program
267 my %deps = ();
268 my @files = ();
269 my @mkfiles = ();
270 my $mkmode = 0;
271 $force_inline = 0;
272 $externalize = 0;
273 $debug = 0;
275 while ( defined(my $arg = shift(@ARGV)) ) {
276 if ( $arg eq '-m' ) {
277 $arg = shift(@ARGV);
278 push(@mkfiles, $arg);
279 } elsif ( $arg eq '-i' ) {
280 $force_inline = 1;
281 } elsif ( $arg eq '-e' ) {
282 $externalize = 1;
283 } elsif ( $arg eq '-d' ) {
284 $debug++;
285 } elsif ( $arg eq '-M' ) {
286 $mkmode = 1; # Futher filenames are output Makefile names
287 } elsif ( $arg eq '--' && $mkmode ) {
288 $mkmode = 0;
289 } elsif ( $arg =~ /^-/ ) {
290 die "Unknown option: $arg\n";
291 } else {
292 if ( $mkmode ) {
293 push(@mkfiles, $arg);
294 } else {
295 push(@files, $arg);
300 my @cfiles = ();
302 foreach my $dir ( @files ) {
303 opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
305 while ( my $file = readdir(DIR) ) {
306 $path = ($dir eq File::Spec->curdir())
307 ? $file : File::Spec->catfile($dir,$file);
308 if ( $file =~ /\.[Cc]$/ ) {
309 push(@cfiles, $path);
310 } elsif ( $file =~ /\.[Hh]$/ ) {
311 print STDERR "Filesystem: $file -> $path\n" if ( $debug );
312 $dep_path{$file} = $path; # Allow the blank filename
313 $dep_path{$path} = $path; # Also allow the full pathname
316 closedir(DIR);
319 foreach my $cfile ( @cfiles ) {
320 scandeps($cfile);
323 foreach my $mkfile ( @mkfiles ) {
324 insert_deps($mkfile);