Add reference to %+ from macro parameter concatenation
[nasm.git] / mkdep.pl
blob9245b183353cd5b35fccab5f249ac47e08bc654a
1 #!/usr/bin/perl
3 # Script to create Makefile-style dependencies.
5 # Usage: perl mkdep.pl [-s path-separator] [-o obj-ext] dir... > deps
8 use File::Spec;
9 use File::Basename;
10 use Fcntl;
12 $barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n";
15 # Scan files for dependencies
17 sub scandeps($) {
18 my($file) = @_;
19 my($line, $nf);
20 my(@xdeps) = ();
21 my(@mdeps) = ();
23 sysopen(FILE, $file, O_RDONLY)
24 or return; # If not openable, assume generated
26 while ( defined($line = <FILE>) ) {
27 chomp $line;
28 $line =~ s:/\*.*\*/::g;
29 $line =~ s://.*$::;
30 if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
31 $nf = $1;
32 push(@mdeps, $nf);
33 push(@xdeps, $nf) unless ( defined($deps{$nf}) );
36 close(FILE);
37 $deps{$file} = [@mdeps];
39 foreach $file ( @xdeps ) {
40 scandeps($file);
44 # %deps contains direct dependencies. This subroutine resolves
45 # indirect dependencies that result.
46 sub alldeps($) {
47 my($file) = @_;
48 my(%adeps);
49 my($dep,$idep);
51 foreach $dep ( @{$deps{$file}} ) {
52 $adeps{$dep} = 1;
53 foreach $idep ( alldeps($dep) ) {
54 $adeps{$idep} = 1;
57 return sort(keys(%adeps));
60 # This converts a filename from host syntax to target syntax
61 # This almost certainly works only on relative filenames...
62 sub convert_file($$) {
63 my($file,$sep) = @_;
64 my(@fspec) = (basename($file));
65 while ( ($file = dirname($file)) ne File::Spec->curdir() &&
66 $file ne File::Spec->rootdir() ) {
67 unshift(@fspec, basename($file));
70 if ( $sep eq '' ) {
71 # This means kill path completely. Used with Makes who do
72 # path searches, but doesn't handle output files in subdirectories,
73 # like OpenWatcom WMAKE.
74 return $fspec[scalar(@fspec)-1];
75 } else {
76 return join($sep, @fspec);
81 # Insert dependencies into a Makefile
83 sub insert_deps($) {
84 my($file) = @_;
85 $nexttemp++; # Unique serial number for each temp file
86 my($tmp) = File::Spec->catfile(dirname($file), 'tmp.'.$nexttemp);
88 sysopen(IN, $file, O_RDONLY)
89 or die "$0: Cannot open input: $file\n";
90 sysopen(OUT, $tmp, O_WRONLY|O_CREAT|O_TRUNC, 0666)
91 or die "$0: Cannot open output: $tmp\n";
93 my($line,$parm,$val);
94 my($obj) = '.o'; # Defaults
95 my($sep) = '/';
96 my($cont) = "\\";
97 my($maxline) = 78; # Seems like a reasonable default
98 my @exclude = (); # Don't exclude anything
100 while ( defined($line = <IN>) ) {
101 if ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
102 $parm = $1; $val = $2;
103 if ( $parm eq 'object-ending' ) {
104 $obj = $val;
105 } elsif ( $parm eq 'path-separator' ) {
106 $sep = $val;
107 } elsif ( $parm eq 'line-width' ) {
108 $maxline = $val+0;
109 } elsif ( $parm eq 'continuation' ) {
110 $cont = $val;
111 } elsif ( $parm eq 'exclude' ) {
112 @exclude = split(/\,/, $val);
114 } elsif ( $line eq $barrier ) {
115 last; # Stop reading input at barrier line
117 print OUT $line;
119 close(IN);
121 my $e;
122 my %do_exclude = ();
123 foreach $e (@exclude) {
124 $do_exclude{$e} = 1;
127 my $dfile, $ofile, $str, $sl, $len;
128 my @deps, $dep;
130 print OUT $barrier;
132 foreach $dfile ( sort(keys(%deps)) ) {
133 if ( $dfile =~ /\.[Cc]$/ ) {
134 $ofile = $dfile; $ofile =~ s/\.[Cc]$//;
135 $str = convert_file($ofile,$sep).$obj.':';
136 $len = length($str);
137 print OUT $str;
138 foreach $dep ($dfile, alldeps($dfile)) {
139 unless ($do_exclude{$dep}) {
140 $str = convert_file($dep,$sep);
141 $sl = length($str)+1;
142 if ( $len+$sl > $maxline-2 ) {
143 print OUT ' ', $cont, "\n ", $str;
144 $len = $sl;
145 } else {
146 print OUT ' ', $str;
147 $len += $sl;
151 print OUT "\n";
154 close(OUT);
156 (unlink($file) && rename($tmp, $file))
157 or die "$0: Failed to change $tmp -> $file\n";
161 # Main program
164 %deps = ();
165 @files = ();
166 @mkfiles = ();
167 $mkmode = 0;
169 while ( defined($arg = shift(@ARGV)) ) {
170 if ( $arg eq '-m' ) {
171 $arg = shift(@ARGV);
172 push(@mkfiles, $arg);
173 } elsif ( $arg eq '-M' ) {
174 $mkmode = 1; # Futher filenames are output Makefile names
175 } elsif ( $arg eq '--' && $mkmode ) {
176 $mkmode = 0;
177 } elsif ( $arg =~ /^-/ ) {
178 die "Unknown option: $arg\n";
179 } else {
180 if ( $mkmode ) {
181 push(@mkfiles, $arg);
182 } else {
183 push(@files, $arg);
188 foreach $dir ( @files ) {
189 opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
191 while ( $file = readdir(DIR) ) {
192 $path = ($dir eq File::Spec->curdir())
193 ? $file : File::Spec->catfile($dir,$file);
194 if ( $file =~ /\.[Cc]$/ ) {
195 scandeps($path);
198 closedir(DIR);
201 foreach $mkfile ( @mkfiles ) {
202 insert_deps($mkfile);