aboutsummaryrefslogtreecommitdiffstats
path: root/tools/mkdep.pl
blob: d9373bb58cd24f03e667f788f2cac1acc194d45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/perl
## --------------------------------------------------------------------------
##
##   Copyright 1996-2017 The NASM Authors - All Rights Reserved
##   See the file AUTHORS included with the NASM distribution for
##   the specific copyright holders.
##
##   Redistribution and use in source and binary forms, with or without
##   modification, are permitted provided that the following
##   conditions are met:
##
##   * Redistributions of source code must retain the above copyright
##     notice, this list of conditions and the following disclaimer.
##   * Redistributions in binary form must reproduce the above
##     copyright notice, this list of conditions and the following
##     disclaimer in the documentation and/or other materials provided
##     with the distribution.
##
##     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
##     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
##     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
##     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
##     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
##     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
##     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
##     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
##     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
##     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
##     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
##
## --------------------------------------------------------------------------

#
# Script to create Makefile-style dependencies.
#
# Usage:
#   perl mkdep.pl [-s path-separator][-o obj-ext] dir... > deps
#   perl mkdep.pl [-i][-e][-m makefile]...[-M makefile... --] dir...
#

use File::Spec;
use File::Basename;
use File::Copy;
use File::Temp;
use Fcntl;

$barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n";

# This converts from filenames to full pathnames for our dependencies
%dep_path = {};

#
# Scan files for dependencies
#
sub scandeps($) {
    my($file) = @_;
    my $line;
    my %xdeps;
    my %mdeps;

    open(my $fh, '<', $file)
	or return;		# If not openable, assume generated

    while ( defined($line = <$fh>) ) {
	chomp $line;
	$line =~ s:/\*.*\*/::g;
	$line =~ s://.*$::;
	if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
	    my $nf = $1;
	    if (!defined($dep_path{$nf})) {
		die "$0: cannot determine path for dependency: $file -> $nf\n";
	    }
	    $nf = $dep_path{$nf};
	    $mdeps{$nf}++;
	    $xdeps{$nf}++ unless ( defined($deps{$nf}) );
	}
    }
    close($fh);
    $deps{$file} = [keys(%mdeps)];

    foreach my $xf ( keys(%xdeps) ) {
	scandeps($xf);
    }
}

# %deps contains direct dependencies.  This subroutine resolves
# indirect dependencies that result.
sub alldeps($$) {
    my($file, $level) = @_;
    my %adeps;

    foreach my $dep ( @{$deps{$file}} ) {
	$adeps{$dep} = 1;
	foreach my $idep ( alldeps($dep, $level+1) ) {
	    $adeps{$idep} = 1;
	}
    }
    return sort(keys(%adeps));
}

# This converts a filename from host syntax to target syntax
# This almost certainly works only on relative filenames...
sub convert_file($$) {
    my($file,$sep) = @_;

    my @fspec = (basename($file));
    while ( ($file = dirname($file)) ne File::Spec->curdir() &&
	    $file ne File::Spec->rootdir() ) {
	unshift(@fspec, basename($file));
    }

    if ( $sep eq '' ) {
	# This means kill path completely.  Used with Makes who do
	# path searches, but doesn't handle output files in subdirectories,
	# like OpenWatcom WMAKE.
	return $fspec[scalar(@fspec)-1];
    } else {
	return join($sep, @fspec);
    }
}

#
# Insert dependencies into a Makefile
#
sub _insert_deps($$) {
    my($file, $out) = @_;

    open(my $in, '<', $file)
	or die "$0: Cannot open input: $file\n";

    my $line, $parm, $val;
    my $obj = '.o';		# Defaults
    my $sep = '/';
    my $cont = "\\";
    my $include_command = 'include';
    my $selfrule = 0;
    my $do_external = 0;
    my $maxline = 78;		# Seems like a reasonable default
    my @exclude = ();		# Don't exclude anything
    my @genhdrs = ();
    my $external = undef;
    my $raw_output = 0;
    my @outfile = ();
    my $done = 0;

    while ( defined($line = <$in>) && !$done ) {
	if ( $line =~ /^([^\s\#\$\:]+\.h):/ ) {
	    # Note: we trust the first Makefile given best
	    my $fpath = $1;
	    my $fbase = basename($fpath);
	    if (!defined($dep_path{$fbase})) {
		$dep_path{$fbase} = $fpath;
		print STDERR "Makefile: $fbase -> $fpath\n";
	    }
	} elsif ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
	    $parm = $1;  $val = $2;
	    if ( $parm eq 'object-ending' ) {
		$obj = $val;
	    } elsif ( $parm eq 'path-separator' ) {
		$sep = $val;
	    } elsif ( $parm eq 'line-width' ) {
		$maxline = $val+0;
	    } elsif ( $parm eq 'continuation' ) {
		$cont = $val;
	    } elsif ( $parm eq 'exclude' ) {
		@exclude = split(/\,/, $val);
	    } elsif ( $parm eq 'include-command' ) {
		$include_command = $val;
	    } elsif ( $parm eq 'external' ) {
		# Keep dependencies in an external file
		$external = $val;
	    } elsif ( $parm eq 'selfrule' ) {
		$selfrule = !!$val;
	    }
	} elsif ( $line =~ /^(\s*\#?\s*EXTERNAL_DEPENDENCIES\s*=\s*)([01])\s*$/ ) {
	    $is_external = $externalize ? 1 : $force_inline ? 0 : $2+0;
	    $line = $1.$is_external."\n";
	} elsif ( $line eq $barrier ) {
	    $done = 1;		# Stop reading input at barrier line
	}

	push @outfile, $line;
    }
    close($in);

    $is_external = $is_external && defined($external);

    if ( !$is_external || $externalize ) {
	print $out @outfile;
    } else {
	print $out $barrier;	# Start generated file with barrier
    }

    if ( $externalize ) {
	if ( $is_external ) {
	    print $out "$include_command $external\n";
	}
	return undef;
    }

    my $e;
    my %do_exclude = ();
    foreach $e (@exclude) {
	$do_exclude{$e} = 1;
    }

    foreach my $dfile ($external, sort(keys(%deps)) ) {
	my $ofile;
	my @deps;

	if ( $selfrule && $dfile eq $external ) {
	    $ofile = convert_file($dfile, $sep).':';
	    @deps = sort(keys(%deps));
	} elsif ( $dfile =~ /^(.*)\.[Cc]$/ ) {
	    $ofile = convert_file($1, $sep).$obj.':';
	    @deps = ($dfile,alldeps($dfile,1));
	}

	if (defined($ofile)) {
	    my $len = length($ofile);
	    print $out $ofile;
	    foreach my $dep (@deps) {
		unless ($do_exclude{$dep}) {
		    my $str = convert_file($dep, $sep);
		    my $sl = length($str)+1;
		    if ( $len+$sl > $maxline-2 ) {
			print $out ' ', $cont, "\n ", $str;
			$len = $sl;
		    } else {
			print $out ' ', $str;
			$len += $sl;
		    }
		}
	    }
	    print $out "\n";
	}
    }

    return $external;
}

sub insert_deps($)
{
    my($mkfile) = @_;
    my $tmp = File::Temp->new(DIR => dirname($mkfile));
    my $tmpname = $tmp->filename;

    my $newname = _insert_deps($mkfile, $tmp);
    close($tmp);

    $newname = $mkfile unless(defined($newname));

    move($tmpname, $newname);
}

#
# Main program
#

my %deps = ();
my @files = ();
my @mkfiles = ();
my $mkmode = 0;
$force_inline = 0;
$externalize = 0;
$debug = 0;

while ( defined(my $arg = shift(@ARGV)) ) {
    if ( $arg eq '-m' ) {
	$arg = shift(@ARGV);
	push(@mkfiles, $arg);
    } elsif ( $arg eq '-i' ) {
	$force_inline = 1;
    } elsif ( $arg eq '-e' ) {
	$externalize = 1;
    } elsif ( $arg eq '-d' ) {
	$debug++;
    } elsif ( $arg eq '-M' ) {
	$mkmode = 1;		# Futher filenames are output Makefile names
    } elsif ( $arg eq '--' && $mkmode ) {
	$mkmode = 0;
    } elsif ( $arg =~ /^-/ ) {
	die "Unknown option: $arg\n";
    } else {
	if ( $mkmode ) {
	    push(@mkfiles, $arg);
	} else {
	    push(@files, $arg);
	}
    }
}

my @cfiles = ();

foreach my $dir ( @files ) {
    opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";

    while ( my $file = readdir(DIR) ) {
	$path = ($dir eq File::Spec->curdir())
	    ? $file : File::Spec->catfile($dir,$file);
	if ( $file =~ /\.[Cc]$/ ) {
	    push(@cfiles, $path);
	} elsif ( $file =~ /\.[Hh]$/ ) {
	    print STDERR "Filesystem: $file -> $path\n" if ( $debug );
	    $dep_path{$file} = $path; # Allow the blank filename
	    $dep_path{$path} = $path; # Also allow the full pathname
	}
    }
    closedir(DIR);
}

foreach my $cfile ( @cfiles ) {
    scandeps($cfile);
}

foreach my $mkfile ( @mkfiles ) {
    insert_deps($mkfile);
}