Merge tag 'nasm-2.12.02rc9'
[nasm.git] / tools / mkdep.pl
blob50d509c35e01e8c7398b5e21b7bcd09840995219
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2016 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.
18 ##
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: perl mkdep.pl [-s path-separator] [-o obj-ext] dir... > deps
41 use File::Spec;
42 use File::Basename;
43 use Fcntl;
45 $barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n";
47 # This converts from filenames to full pathnames for our dependencies
48 %dep_path = {};
51 # Scan files for dependencies
53 sub scandeps($) {
54 my($file) = @_;
55 my($line, $nf);
56 my(@xdeps) = ();
57 my(@mdeps) = ();
59 open(my $fh, '<', $file)
60 or return; # If not openable, assume generated
62 while ( defined($line = <$fh>) ) {
63 chomp $line;
64 $line =~ s:/\*.*\*/::g;
65 $line =~ s://.*$::;
66 if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
67 $nf = $1;
68 push(@mdeps, $nf);
69 push(@xdeps, $nf) unless ( defined($deps{$nf}) );
72 close($fh);
73 $deps{$file} = [@mdeps];
75 foreach $file ( @xdeps ) {
76 scandeps($file);
80 # %deps contains direct dependencies. This subroutine resolves
81 # indirect dependencies that result.
82 sub alldeps($) {
83 my($file) = @_;
84 my(%adeps);
85 my($dep,$idep);
87 foreach $dep ( @{$deps{$file}} ) {
88 $adeps{$dep} = 1;
89 foreach $idep ( alldeps($dep) ) {
90 $adeps{$idep} = 1;
93 return sort(keys(%adeps));
96 # This converts a filename from host syntax to target syntax
97 # This almost certainly works only on relative filenames...
98 sub convert_file($$) {
99 my($file,$sep) = @_;
100 my(@fspec) = (basename($file));
101 while ( ($file = dirname($file)) ne File::Spec->curdir() &&
102 $file ne File::Spec->rootdir() ) {
103 unshift(@fspec, basename($file));
106 if ( $sep eq '' ) {
107 # This means kill path completely. Used with Makes who do
108 # path searches, but doesn't handle output files in subdirectories,
109 # like OpenWatcom WMAKE.
110 return $fspec[scalar(@fspec)-1];
111 } else {
112 return join($sep, @fspec);
117 # Insert dependencies into a Makefile
119 sub insert_deps($) {
120 my($file) = @_;
121 $nexttemp++; # Unique serial number for each temp file
122 my($tmp) = File::Spec->catfile(dirname($file), 'tmp.'.$nexttemp);
124 open(my $in, '<', $file)
125 or die "$0: Cannot open input: $file\n";
126 open(my $out, '>', $tmp)
127 or die "$0: Cannot open output: $tmp\n";
129 my($line,$parm,$val);
130 my($obj) = '.o'; # Defaults
131 my($sep) = '/';
132 my($cont) = "\\";
133 my($maxline) = 78; # Seems like a reasonable default
134 my @exclude = (); # Don't exclude anything
135 my @genhdrs = ();
137 while ( defined($line = <$in>) ) {
138 if ( $line =~ /^([^\s\#\$\:]+\.h):/ ) {
139 # Note: we trust the first Makefile given best
140 my $fpath = $1;
141 my $fbase = basename($fpath);
142 if (!defined($dep_path{$fbase})) {
143 $dep_path{$fbase} = $fpath;
144 print STDERR "Makefile: $fbase -> $fpath\n";
146 } elsif ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
147 $parm = $1; $val = $2;
148 if ( $parm eq 'object-ending' ) {
149 $obj = $val;
150 } elsif ( $parm eq 'path-separator' ) {
151 $sep = $val;
152 } elsif ( $parm eq 'line-width' ) {
153 $maxline = $val+0;
154 } elsif ( $parm eq 'continuation' ) {
155 $cont = $val;
156 } elsif ( $parm eq 'exclude' ) {
157 @exclude = split(/\,/, $val);
159 } elsif ( $line eq $barrier ) {
160 last; # Stop reading input at barrier line
162 print $out $line;
164 close($in);
166 my $e;
167 my %do_exclude = ();
168 foreach $e (@exclude) {
169 $do_exclude{$e} = 1;
172 my $dfile, $ofile, $str, $sl, $len;
173 my @deps, $dep;
175 print $out $barrier;
177 foreach $dfile ( sort(keys(%deps)) ) {
178 if ( $dfile =~ /^(.*)\.[Cc]$/ ) {
179 $ofile = $1;
180 $str = convert_file($ofile, $sep).$obj.':';
181 $len = length($str);
182 print $out $str;
183 foreach $dep ($dfile, alldeps($dfile)) {
184 unless ($do_exclude{$dep}) {
185 if (!defined($dep_path{$dep})) {
186 if ($dep eq $dfile) {
187 $dep_path{$dep} = $dfile;
188 print STDERR "Self: $dep -> $dfile\n";
189 } else {
190 die "$0: unknown dependency: $dep\n";
193 $str = convert_file($dep_path{$dep}, $sep);
194 $sl = length($str)+1;
195 if ( $len+$sl > $maxline-2 ) {
196 print $out ' ', $cont, "\n ", $str;
197 $len = $sl;
198 } else {
199 print $out ' ', $str;
200 $len += $sl;
204 print $out "\n";
207 close($out);
209 (unlink($file) && rename($tmp, $file))
210 or die "$0: Failed to change $tmp -> $file\n";
214 # Main program
217 my %deps = ();
218 my @files = ();
219 my @mkfiles = ();
220 my $mkmode = 0;
222 while ( defined(my $arg = shift(@ARGV)) ) {
223 if ( $arg eq '-m' ) {
224 $arg = shift(@ARGV);
225 push(@mkfiles, $arg);
226 } elsif ( $arg eq '-M' ) {
227 $mkmode = 1; # Futher filenames are output Makefile names
228 } elsif ( $arg eq '--' && $mkmode ) {
229 $mkmode = 0;
230 } elsif ( $arg =~ /^-/ ) {
231 die "Unknown option: $arg\n";
232 } else {
233 if ( $mkmode ) {
234 push(@mkfiles, $arg);
235 } else {
236 push(@files, $arg);
241 foreach my $dir ( @files ) {
242 opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
244 while ( my $file = readdir(DIR) ) {
245 $path = ($dir eq File::Spec->curdir())
246 ? $file : File::Spec->catfile($dir,$file);
247 if ( $file =~ /\.[Cc]$/ ) {
248 scandeps($path);
249 } elsif ( $file =~ /\.[Hh]$/ ) {
250 print STDERR "Filesystem: $file -> $path\n";
251 $dep_path{$file} = $path;
254 closedir(DIR);
257 foreach $mkfile ( @mkfiles ) {
258 insert_deps($mkfile);