NASM 2.14rc3
[nasm.git] / tools / mkdep.pl
blobe84cc3581bbde75dadc1a2200e3659bf33fc0461
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2017 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 = {};
55 # Scan files for dependencies
57 sub scandeps($) {
58 my($file) = @_;
59 my $line;
60 my %xdeps;
61 my %mdeps;
63 open(my $fh, '<', $file)
64 or return; # If not openable, assume generated
66 while ( defined($line = <$fh>) ) {
67 chomp $line;
68 $line =~ s:/\*.*\*/::g;
69 $line =~ s://.*$::;
70 if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
71 my $nf = $1;
72 if (!defined($dep_path{$nf})) {
73 die "$0: cannot determine path for dependency: $file -> $nf\n";
75 $nf = $dep_path{$nf};
76 $mdeps{$nf}++;
77 $xdeps{$nf}++ unless ( defined($deps{$nf}) );
80 close($fh);
81 $deps{$file} = [keys(%mdeps)];
83 foreach my $xf ( keys(%xdeps) ) {
84 scandeps($xf);
88 # %deps contains direct dependencies. This subroutine resolves
89 # indirect dependencies that result.
90 sub alldeps($$) {
91 my($file, $level) = @_;
92 my %adeps;
94 foreach my $dep ( @{$deps{$file}} ) {
95 $adeps{$dep} = 1;
96 foreach my $idep ( alldeps($dep, $level+1) ) {
97 $adeps{$idep} = 1;
100 return sort(keys(%adeps));
103 # This converts a filename from host syntax to target syntax
104 # This almost certainly works only on relative filenames...
105 sub convert_file($$) {
106 my($file,$sep) = @_;
108 my @fspec = (basename($file));
109 while ( ($file = dirname($file)) ne File::Spec->curdir() &&
110 $file ne File::Spec->rootdir() ) {
111 unshift(@fspec, basename($file));
114 if ( $sep eq '' ) {
115 # This means kill path completely. Used with Makes who do
116 # path searches, but doesn't handle output files in subdirectories,
117 # like OpenWatcom WMAKE.
118 return $fspec[scalar(@fspec)-1];
119 } else {
120 return join($sep, @fspec);
125 # Insert dependencies into a Makefile
127 sub _insert_deps($$) {
128 my($file, $out) = @_;
130 open(my $in, '<', $file)
131 or die "$0: Cannot open input: $file\n";
133 my $line, $parm, $val;
134 my $obj = '.o'; # Defaults
135 my $sep = '/';
136 my $cont = "\\";
137 my $include_command = undef;
138 my $selfrule = 0;
139 my $do_external = 0;
140 my $maxline = 78; # Seems like a reasonable default
141 my @exclude = (); # Don't exclude anything
142 my @genhdrs = ();
143 my $external = undef;
144 my $raw_output = 0;
145 my @outfile = ();
146 my $done = 0;
148 while ( defined($line = <$in>) && !$done ) {
149 if ( $line =~ /^([^\s\#\$\:]+\.h):/ ) {
150 # Note: we trust the first Makefile given best
151 my $fpath = $1;
152 my $fbase = basename($fpath);
153 if (!defined($dep_path{$fbase})) {
154 $dep_path{$fbase} = $fpath;
155 print STDERR "Makefile: $fbase -> $fpath\n";
157 } elsif ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
158 $parm = $1; $val = $2;
159 if ( $parm eq 'object-ending' ) {
160 $obj = $val;
161 } elsif ( $parm eq 'path-separator' ) {
162 $sep = $val;
163 } elsif ( $parm eq 'line-width' ) {
164 $maxline = $val+0;
165 } elsif ( $parm eq 'continuation' ) {
166 $cont = $val;
167 } elsif ( $parm eq 'exclude' ) {
168 @exclude = split(/\,/, $val);
169 } elsif ( $parm eq 'include-command' ) {
170 $include_command = $val;
171 } elsif ( $parm eq 'external' ) {
172 # Keep dependencies in an external file
173 $external = $val;
174 } elsif ( $parm eq 'selfrule' ) {
175 $selfrule = !!$val;
177 } elsif ( $line =~ /^(\s*\#?\s*EXTERNAL_DEPENDENCIES\s*=\s*)([01])\s*$/ ) {
178 $is_external = $externalize ? 1 : $force_inline ? 0 : $2+0;
179 $line = $1.$is_external."\n";
180 } elsif ( $line eq $barrier ) {
181 $done = 1; # Stop reading input at barrier line
184 push @outfile, $line;
186 close($in);
188 $is_external = $is_external && defined($external);
190 if ( !$is_external || $externalize ) {
191 print $out @outfile;
192 } else {
193 print $out $barrier; # Start generated file with barrier
196 if ( $externalize ) {
197 if ( $is_external && defined($include_command) ) {
198 print $out "$include_command $external\n";
200 return undef;
203 my $e;
204 my %do_exclude = ();
205 foreach $e (@exclude) {
206 $do_exclude{$e} = 1;
209 foreach my $dfile ($external, sort(keys(%deps)) ) {
210 my $ofile;
211 my @deps;
213 if ( $selfrule && $dfile eq $external ) {
214 $ofile = convert_file($dfile, $sep).':';
215 @deps = sort(keys(%deps));
216 } elsif ( $dfile =~ /^(.*)\.[Cc]$/ ) {
217 $ofile = convert_file($1, $sep).$obj.':';
218 @deps = ($dfile,alldeps($dfile,1));
221 if (defined($ofile)) {
222 my $len = length($ofile);
223 print $out $ofile;
224 foreach my $dep (@deps) {
225 unless ($do_exclude{$dep}) {
226 my $str = convert_file($dep, $sep);
227 my $sl = length($str)+1;
228 if ( $len+$sl > $maxline-2 ) {
229 print $out ' ', $cont, "\n ", $str;
230 $len = $sl;
231 } else {
232 print $out ' ', $str;
233 $len += $sl;
237 print $out "\n";
241 return $external;
244 sub insert_deps($)
246 my($mkfile) = @_;
247 my $tmp = File::Temp->new(DIR => dirname($mkfile));
248 my $tmpname = $tmp->filename;
250 my $newname = _insert_deps($mkfile, $tmp);
251 close($tmp);
253 $newname = $mkfile unless(defined($newname));
255 move($tmpname, $newname);
259 # Main program
262 my %deps = ();
263 my @files = ();
264 my @mkfiles = ();
265 my $mkmode = 0;
266 $force_inline = 0;
267 $externalize = 0;
268 $debug = 0;
270 while ( defined(my $arg = shift(@ARGV)) ) {
271 if ( $arg eq '-m' ) {
272 $arg = shift(@ARGV);
273 push(@mkfiles, $arg);
274 } elsif ( $arg eq '-i' ) {
275 $force_inline = 1;
276 } elsif ( $arg eq '-e' ) {
277 $externalize = 1;
278 } elsif ( $arg eq '-d' ) {
279 $debug++;
280 } elsif ( $arg eq '-M' ) {
281 $mkmode = 1; # Futher filenames are output Makefile names
282 } elsif ( $arg eq '--' && $mkmode ) {
283 $mkmode = 0;
284 } elsif ( $arg =~ /^-/ ) {
285 die "Unknown option: $arg\n";
286 } else {
287 if ( $mkmode ) {
288 push(@mkfiles, $arg);
289 } else {
290 push(@files, $arg);
295 my @cfiles = ();
297 foreach my $dir ( @files ) {
298 opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
300 while ( my $file = readdir(DIR) ) {
301 $path = ($dir eq File::Spec->curdir())
302 ? $file : File::Spec->catfile($dir,$file);
303 if ( $file =~ /\.[Cc]$/ ) {
304 push(@cfiles, $path);
305 } elsif ( $file =~ /\.[Hh]$/ ) {
306 print STDERR "Filesystem: $file -> $path\n" if ( $debug );
307 $dep_path{$file} = $path; # Allow the blank filename
308 $dep_path{$path} = $path; # Also allow the full pathname
311 closedir(DIR);
314 foreach my $cfile ( @cfiles ) {
315 scandeps($cfile);
318 foreach my $mkfile ( @mkfiles ) {
319 insert_deps($mkfile);