3 # Script to create Makefile-style dependencies.
5 # Usage: perl [-s path-separator] [-o obj-ext] mkdep.pl 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) ) {
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));
70 return join($sep, @fspec);
74 # Insert dependencies into a Makefile
78 $nexttemp++; # Unique serial number for each temp file
79 my($tmp) = File::Spec->catfile(dirname($file), 'tmp.'.$nexttemp);
81 sysopen(IN, $file, O_RDONLY)
82 or die "$0: Cannot open input: $file\n";
83 sysopen(OUT, $tmp, O_WRONLY|O_CREAT|O_TRUNC, 0666)
84 or die "$0: Cannot open output: $tmp\n";
87 my($obj) = '.o'; # Defaults
90 while ( defined($line = <IN>) ) {
91 if ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
92 $parm = $1; $val = $2;
93 if ( $parm eq 'object-ending' ) {
95 } elsif ( $parm eq 'path-separator' ) {
98 } elsif ( $line eq $barrier ) {
99 last; # Stop reading input at barrier line
110 foreach $dfile ( sort(keys(%deps)) ) {
111 if ( $dfile =~ /\.[Cc]$/ ) {
112 $ofile = $dfile; $ofile =~ s/\.[Cc]$//;
113 print OUT convert_file($ofile,$sep), $obj, ':';
114 foreach $dep ($dfile, alldeps($dfile)) {
115 print OUT ' ', convert_file($dep,$sep);
122 (unlink($file) && rename($tmp, $file))
123 or die "$0: Failed to change $tmp -> $file\n";
134 while ( defined($arg = shift(@ARGV)) ) {
135 if ( $arg eq '-m' ) {
137 push(@mkfiles, $arg);
138 } elsif ( $arg =~ /^-/ ) {
139 die "Unknown option: $arg\n";
145 foreach $dir ( @files ) {
146 opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
148 while ( $file = readdir(DIR) ) {
149 $path = ($dir eq File::Spec->curdir())
150 ? $file : File::Spec->catfile($dir,$file);
151 if ( $file =~ /\.[Cc]$/ ) {
158 foreach $mkfile ( @mkfiles ) {
159 insert_deps($mkfile);