3 # Script to create Makefile-style dependencies.
5 # Usage: perl mkdep.pl [-s path-separator] [-o obj-ext] dir... > deps
12 $barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n";
15 # Scan files for dependencies
23 sysopen(FILE
, $file, O_RDONLY
)
24 or return; # If not openable, assume generated
26 while ( defined($line = <FILE
>) ) {
28 $line =~ s
:/\*.*\*/::g
;
30 if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
33 push(@xdeps, $nf) unless ( defined($deps{$nf}) );
37 $deps{$file} = [@mdeps];
39 foreach $file ( @xdeps ) {
44 # %deps contains direct dependencies. This subroutine resolves
45 # indirect dependencies that result.
51 foreach $dep ( @
{$deps{$file}} ) {
53 foreach $idep ( alldeps
($dep) ) {
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
($$) {
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));
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];
76 return join($sep, @fspec);
81 # Insert dependencies into a Makefile
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";
94 my($obj) = '.o'; # Defaults
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' ) {
105 } elsif ( $parm eq 'path-separator' ) {
107 } elsif ( $parm eq 'line-width' ) {
109 } elsif ( $parm eq 'continuation' ) {
111 } elsif ( $parm eq 'exclude' ) {
112 @exclude = split(/\,/, $val);
114 } elsif ( $line eq $barrier ) {
115 last; # Stop reading input at barrier line
122 foreach $e (@exclude) {
126 my $dfile, $ofile, $str, $sl, $len;
131 foreach $dfile ( sort(keys(%deps)) ) {
132 if ( $dfile =~ /\.[Cc]$/ ) {
133 $ofile = $dfile; $ofile =~ s/\.[Cc]$//;
134 $str = convert_file
($ofile,$sep).$obj.':';
137 foreach $dep ($dfile, alldeps
($dfile)) {
138 unless ($do_exclude{$dep}) {
139 $str = convert_file
($dep,$sep);
140 $sl = length($str)+1;
141 if ( $len+$sl > $maxline-2 ) {
142 print OUT
' ', $cont, "\n ", $str;
155 (unlink($file) && rename($tmp, $file))
156 or die "$0: Failed to change $tmp -> $file\n";
168 while ( defined($arg = shift(@ARGV)) ) {
169 if ( $arg eq '-m' ) {
171 push(@mkfiles, $arg);
172 } elsif ( $arg eq '-M' ) {
173 $mkmode = 1; # Futher filenames are output Makefile names
174 } elsif ( $arg eq '--' && $mkmode ) {
176 } elsif ( $arg =~ /^-/ ) {
177 die "Unknown option: $arg\n";
180 push(@mkfiles, $arg);
187 foreach $dir ( @files ) {
188 opendir(DIR
, $dir) or die "$0: Cannot open directory: $dir";
190 while ( $file = readdir(DIR
) ) {
191 $path = ($dir eq File
::Spec
->curdir())
192 ?
$file : File
::Spec
->catfile($dir,$file);
193 if ( $file =~ /\.[Cc]$/ ) {
200 foreach $mkfile ( @mkfiles ) {
201 insert_deps
($mkfile);