NASM 2.09rc3
[nasm/avx512.git] / mkdep.pl
blob9f80ed3e655d93b53ed748720a795b909e7489af
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2009 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
7 ##
8 ## Copyright 1996-2009 the NASM Authors - All rights reserved.
9 ##
10 ## Redistribution and use in source and binary forms, with or without
11 ## modification, are permitted provided that the following
12 ## conditions are met:
14 ## * Redistributions of source code must retain the above copyright
15 ## notice, this list of conditions and the following disclaimer.
16 ## * Redistributions in binary form must reproduce the above
17 ## copyright notice, this list of conditions and the following
18 ## disclaimer in the documentation and/or other materials provided
19 ## with the distribution.
20 ##
21 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 ## --------------------------------------------------------------------------
38 # Script to create Makefile-style dependencies.
40 # Usage: perl mkdep.pl [-s path-separator] [-o obj-ext] dir... > deps
43 use File::Spec;
44 use File::Basename;
45 use Fcntl;
47 $barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n";
50 # Scan files for dependencies
52 sub scandeps($) {
53 my($file) = @_;
54 my($line, $nf);
55 my(@xdeps) = ();
56 my(@mdeps) = ();
58 sysopen(FILE, $file, O_RDONLY)
59 or return; # If not openable, assume generated
61 while ( defined($line = <FILE>) ) {
62 chomp $line;
63 $line =~ s:/\*.*\*/::g;
64 $line =~ s://.*$::;
65 if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
66 $nf = $1;
67 push(@mdeps, $nf);
68 push(@xdeps, $nf) unless ( defined($deps{$nf}) );
71 close(FILE);
72 $deps{$file} = [@mdeps];
74 foreach $file ( @xdeps ) {
75 scandeps($file);
79 # %deps contains direct dependencies. This subroutine resolves
80 # indirect dependencies that result.
81 sub alldeps($) {
82 my($file) = @_;
83 my(%adeps);
84 my($dep,$idep);
86 foreach $dep ( @{$deps{$file}} ) {
87 $adeps{$dep} = 1;
88 foreach $idep ( alldeps($dep) ) {
89 $adeps{$idep} = 1;
92 return sort(keys(%adeps));
95 # This converts a filename from host syntax to target syntax
96 # This almost certainly works only on relative filenames...
97 sub convert_file($$) {
98 my($file,$sep) = @_;
99 my(@fspec) = (basename($file));
100 while ( ($file = dirname($file)) ne File::Spec->curdir() &&
101 $file ne File::Spec->rootdir() ) {
102 unshift(@fspec, basename($file));
105 if ( $sep eq '' ) {
106 # This means kill path completely. Used with Makes who do
107 # path searches, but doesn't handle output files in subdirectories,
108 # like OpenWatcom WMAKE.
109 return $fspec[scalar(@fspec)-1];
110 } else {
111 return join($sep, @fspec);
116 # Insert dependencies into a Makefile
118 sub insert_deps($) {
119 my($file) = @_;
120 $nexttemp++; # Unique serial number for each temp file
121 my($tmp) = File::Spec->catfile(dirname($file), 'tmp.'.$nexttemp);
123 sysopen(IN, $file, O_RDONLY)
124 or die "$0: Cannot open input: $file\n";
125 sysopen(OUT, $tmp, O_WRONLY|O_CREAT|O_TRUNC, 0666)
126 or die "$0: Cannot open output: $tmp\n";
128 my($line,$parm,$val);
129 my($obj) = '.o'; # Defaults
130 my($sep) = '/';
131 my($cont) = "\\";
132 my($maxline) = 78; # Seems like a reasonable default
133 my @exclude = (); # Don't exclude anything
135 while ( defined($line = <IN>) ) {
136 if ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
137 $parm = $1; $val = $2;
138 if ( $parm eq 'object-ending' ) {
139 $obj = $val;
140 } elsif ( $parm eq 'path-separator' ) {
141 $sep = $val;
142 } elsif ( $parm eq 'line-width' ) {
143 $maxline = $val+0;
144 } elsif ( $parm eq 'continuation' ) {
145 $cont = $val;
146 } elsif ( $parm eq 'exclude' ) {
147 @exclude = split(/\,/, $val);
149 } elsif ( $line eq $barrier ) {
150 last; # Stop reading input at barrier line
152 print OUT $line;
154 close(IN);
156 my $e;
157 my %do_exclude = ();
158 foreach $e (@exclude) {
159 $do_exclude{$e} = 1;
162 my $dfile, $ofile, $str, $sl, $len;
163 my @deps, $dep;
165 print OUT $barrier;
167 foreach $dfile ( sort(keys(%deps)) ) {
168 if ( $dfile =~ /\.[Cc]$/ ) {
169 $ofile = $dfile; $ofile =~ s/\.[Cc]$//;
170 $str = convert_file($ofile,$sep).$obj.':';
171 $len = length($str);
172 print OUT $str;
173 foreach $dep ($dfile, alldeps($dfile)) {
174 unless ($do_exclude{$dep}) {
175 $str = convert_file($dep,$sep);
176 $sl = length($str)+1;
177 if ( $len+$sl > $maxline-2 ) {
178 print OUT ' ', $cont, "\n ", $str;
179 $len = $sl;
180 } else {
181 print OUT ' ', $str;
182 $len += $sl;
186 print OUT "\n";
189 close(OUT);
191 (unlink($file) && rename($tmp, $file))
192 or die "$0: Failed to change $tmp -> $file\n";
196 # Main program
199 %deps = ();
200 @files = ();
201 @mkfiles = ();
202 $mkmode = 0;
204 while ( defined($arg = shift(@ARGV)) ) {
205 if ( $arg eq '-m' ) {
206 $arg = shift(@ARGV);
207 push(@mkfiles, $arg);
208 } elsif ( $arg eq '-M' ) {
209 $mkmode = 1; # Futher filenames are output Makefile names
210 } elsif ( $arg eq '--' && $mkmode ) {
211 $mkmode = 0;
212 } elsif ( $arg =~ /^-/ ) {
213 die "Unknown option: $arg\n";
214 } else {
215 if ( $mkmode ) {
216 push(@mkfiles, $arg);
217 } else {
218 push(@files, $arg);
223 foreach $dir ( @files ) {
224 opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
226 while ( $file = readdir(DIR) ) {
227 $path = ($dir eq File::Spec->curdir())
228 ? $file : File::Spec->catfile($dir,$file);
229 if ( $file =~ /\.[Cc]$/ ) {
230 scandeps($path);
233 closedir(DIR);
236 foreach $mkfile ( @mkfiles ) {
237 insert_deps($mkfile);