36
36
# Script to create Makefile-style dependencies.
37
37
#
38
38
# Usage:
39
- # perl mkdep.pl [-s path-separator][-o obj-ext] dir... > deps
40
39
# perl mkdep.pl [-i][-e][-m makefile]...[-M makefile... --] dir...
41
40
#
42
41
42
+ use strict;
43
+ use integer;
44
+
43
45
use File::Spec;
44
46
use File::Basename;
45
47
use File::Copy;
46
48
use File::Temp;
47
49
use Fcntl;
48
50
49
- $barrier = " #-- Everything below is generated by mkdep.pl - do not edit --#\n " ;
51
+ my $barrier =
52
+ " #-- Everything below is generated by mkdep.pl - do not edit --#\n " ;
50
53
51
54
# This converts from filenames to full pathnames for our dependencies
52
- %dep_path = ();
55
+ my %dep_path = ();
53
56
54
57
# List of files that cannot be found; these *must* be excluded
55
- @must_exclude = ();
58
+ my @must_exclude = ();
59
+
60
+ #
61
+ # Variables derived from the command line
62
+ #
63
+ my %deps ;
64
+ my %excludes ;
65
+ my @files ;
66
+ my @mkfiles ;
67
+ my $mkmode = 0;
68
+ my @searchdirs = (File::Spec-> curdir());
69
+ my %searchdirs = (File::Spec-> curdir() => 1);
70
+ my $force_inline = 0;
71
+ my $externalize = 0;
72
+ my $debug = 0;
56
73
57
74
#
58
75
# Scan files for dependencies
59
76
#
60
- sub scandeps ($) {
61
- my ($file ) = @_ ;
77
+ sub scandeps {
78
+ # path is the filesystem path, file what the output should call it
79
+ my ($path , $file ) = @_ ;
62
80
my $line ;
63
81
my %xdeps ;
64
82
my %mdeps ;
65
83
66
- open (my $fh , ' <' , $file )
84
+ open (my $fh , ' <' , $path )
67
85
or return ; # If not openable, assume generated
68
86
69
87
while ( defined ($line = <$fh >) ) {
128
146
#
129
147
# Insert dependencies into a Makefile
130
148
#
131
- sub _insert_deps ($ $) {
132
- my ($file , $out ) = @_ ;
149
+ sub insert_deps ( $) {
150
+ my ($file ) = @_ ;
133
151
134
152
open (my $in , ' <' , $file )
135
153
or die " $0 : Cannot open input: $file \n " ;
@@ -192,8 +210,13 @@ ($$)
192
210
$is_external = $is_external && defined ($external );
193
211
undef $external if ( !$is_external );
194
212
213
+ my $out ;
214
+ my $outpath ;
195
215
if ( !$is_external || $externalize ) {
216
+ $out = File::Temp-> new(DIR => dirname($outpath = $file ));
196
217
print $out @outfile ;
218
+ } else {
219
+ $out = File::Temp-> new(DIR => dirname($outpath = $external ));
197
220
}
198
221
199
222
print $out $barrier ;
@@ -204,78 +227,60 @@ ($$)
204
227
print $out " $include_command $external \n " ;
205
228
}
206
229
unlink ($external );
207
- return undef ;
208
- }
230
+ } else {
209
231
210
- my $e ;
232
+ my $e ;
211
233
212
- foreach my $dfile ($external , sort (keys (%deps )) ) {
213
- my $ofile ;
214
- my @deps ;
234
+ foreach my $dfile ($external , sort (keys (%deps )) ) {
235
+ my $ofile ;
236
+ my @deps ;
215
237
216
- next unless (defined ($dfile ));
238
+ next unless (defined ($dfile ));
217
239
218
- if ( $selfrule && $dfile eq $external ) {
219
- $ofile = convert_file($dfile , $sep ).' :' ;
220
- @deps = sort (keys (%deps ));
221
- } elsif ( $dfile =~ / ^(.*)\. [Cc]$ / ) {
222
- $ofile = convert_file($1 , $sep ).$obj .' :' ;
223
- @deps = ($dfile ,alldeps($dfile ,1));
224
- }
240
+ if ( $selfrule && $dfile eq $external ) {
241
+ $ofile = convert_file($dfile , $sep ).' :' ;
242
+ @deps = sort (keys (%deps ));
243
+ } elsif ( $dfile =~ / ^(.*)\. [Cc]$ / ) {
244
+ $ofile = convert_file($1 , $sep ).$obj .' :' ;
245
+ @deps = ($dfile ,alldeps($dfile ,1));
246
+ }
225
247
226
- if (defined ($ofile )) {
227
- my $len = length ($ofile );
228
- print $out $ofile ;
229
- foreach my $dep (@deps ) {
230
- unless ($excludes {$dep }) {
231
- my $str = convert_file($dep , $sep );
232
- my $sl = length ($str )+1;
233
- if ( $len +$sl > $maxline -2 ) {
234
- print $out ' ' , $cont , " \n " , $str ;
235
- $len = $sl ;
236
- } else {
237
- print $out ' ' , $str ;
238
- $len += $sl ;
248
+ if (defined ($ofile )) {
249
+ my $len = length ($ofile );
250
+ print $out $ofile ;
251
+ foreach my $dep (@deps ) {
252
+ unless ($excludes {$dep }) {
253
+ my $str = convert_file($dep , $sep );
254
+ my $sl = length ($str )+1;
255
+ if ( $len +$sl > $maxline -2 ) {
256
+ print $out ' ' , $cont , " \n " , $str ;
257
+ $len = $sl ;
258
+ } else {
259
+ print $out ' ' , $str ;
260
+ $len += $sl ;
261
+ }
239
262
}
240
263
}
264
+ print $out " \n " ;
241
265
}
242
- print $out " \n " ;
243
266
}
244
267
}
245
268
246
- return $external ;
247
- }
248
-
249
- sub insert_deps ($)
250
- {
251
- my ($mkfile ) = @_ ;
252
- my $tmp = File::Temp-> new(DIR => dirname($mkfile ));
253
- my $tmpname = $tmp -> filename;
254
-
255
- my $newname = _insert_deps($mkfile , $tmp );
256
- close ($tmp );
257
-
258
- $newname = $mkfile unless (defined ($newname ));
259
-
260
- move($tmpname , $newname );
269
+ close ($out );
270
+ move($out -> filename, $outpath );
261
271
}
262
272
263
273
#
264
274
# Main program
265
275
#
266
276
267
- my %deps = ();
268
- my @files = ();
269
- my @mkfiles = ();
270
- my $mkmode = 0;
271
- $force_inline = 0;
272
- $externalize = 0;
273
- $debug = 0;
274
-
275
277
while ( defined (my $arg = shift (@ARGV )) ) {
276
278
if ( $arg eq ' -m' ) {
277
279
$arg = shift (@ARGV );
278
280
push (@mkfiles , $arg );
281
+ } elsif ( $arg eq ' -s' ) {
282
+ $arg = shift (@ARGV );
283
+ push (@searchdirs , $arg );
279
284
} elsif ( $arg eq ' -i' ) {
280
285
$force_inline = 1;
281
286
} elsif ( $arg eq ' -e' ) {
@@ -297,27 +302,66 @@ ($)
297
302
}
298
303
}
299
304
305
+ sub mycatdir ($$) {
306
+ my ($a ,$b ) = @_ ;
307
+ return $b if ($a eq File::Spec-> curdir());
308
+ return $a if ($b eq File::Spec-> curdir());
309
+ return File::Spec-> catdir($a ,$b );
310
+ }
311
+
312
+ sub mycatfile ($$) {
313
+ my ($d ,$f ) = @_ ;
314
+ return $f if ($d eq File::Spec-> curdir());
315
+ return File::Spec-> catfile($d ,$f );
316
+ }
317
+
300
318
my @cfiles = ();
301
319
302
- foreach my $dir ( @files ) {
303
- opendir (DIR, $dir ) or die " $0 : Cannot open directory: $dir " ;
304
-
305
- while ( my $file = readdir (DIR) ) {
306
- $path = ($dir eq File::Spec-> curdir())
307
- ? $file : File::Spec-> catfile($dir ,$file );
308
- if ( $file =~ / \. [Cc]$ / ) {
309
- push (@cfiles , $path );
310
- } elsif ( $file =~ / \. [Hh]$ / ) {
311
- print STDERR " Filesystem: $file -> $path \n " if ( $debug );
312
- $dep_path {$file } = $path ; # Allow the blank filename
313
- $dep_path {$path } = $path ; # Also allow the full pathname
320
+ my $err = 0;
321
+ my %scanned ;
322
+
323
+ foreach my $fdir ( @files ) {
324
+ my $found = 0;
325
+
326
+ foreach my $sdir ( @searchdirs ) {
327
+ my $dir = mycatdir($sdir , $fdir );
328
+ if ($scanned {$dir }) {
329
+ # Have already been here
330
+ $found = 1;
331
+ next ;
332
+ }
333
+
334
+ opendir (DIR, $dir ) or next ;
335
+ $scanned {$dir }++;
336
+ $found ++;
337
+
338
+ while ( my $file = readdir (DIR) ) {
339
+ # $fdir is correct here, because we expect VPATH to do
340
+ # its job, and the output filename depends on that, not
341
+ # on the full source dir path.
342
+ my $path = mycatfile($fdir , $file );
343
+ my $fullpath = mycatfile($dir , $file );
344
+ if ( $file =~ / \. [Cc]$ / ) {
345
+ push (@cfiles , [$fullpath , $path ]);
346
+ } elsif ( $file =~ / \. [Hh]$ / ) {
347
+ print STDERR " Filesystem: $file -> $path \n " if ( $debug );
348
+ $dep_path {$file } = $path ; # Allow the blank filename
349
+ $dep_path {$path } = $path ; # Also allow the full pathname
350
+ }
314
351
}
352
+ closedir (DIR);
353
+ }
354
+
355
+ if (!$found ) {
356
+ print STDERR " $0 : cannot find directory: $fdir \n " ;
357
+ $err ++;
315
358
}
316
- closedir (DIR);
317
359
}
318
360
361
+ exit (1) if ($err );
362
+
319
363
foreach my $cfile ( @cfiles ) {
320
- scandeps($cfile );
364
+ scandeps(@ $cfile );
321
365
}
322
366
323
367
foreach my $mkfile ( @mkfiles ) {
0 commit comments