Update for newer RH releases
[nasm/avx512.git] / mkdep.pl
blob113197141c1251c5c0df8c62d3f560d7a54b6080
1 #!/usr/bin/perl
3 # Script to create Makefile-style dependencies.
5 # Usage: perl [-s path-separator] [-o obj-ext] mkdep.pl 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 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 return join($sep, @fspec);
74 # Insert dependencies into a Makefile
76 sub insert_deps($) {
77 my($file) = @_;
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";
86 my($line,$parm,$val);
87 my($obj) = '.o'; # Defaults
88 my($sep) = '/';
89 my($maxline) = 78; # Seems like a reasonable default
91 while ( defined($line = <IN>) ) {
92 if ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
93 $parm = $1; $val = $2;
94 if ( $parm eq 'object-ending' ) {
95 $obj = $val;
96 } elsif ( $parm eq 'path-separator' ) {
97 $sep = $val;
98 } elsif ( $parm eq 'line-width' ) {
99 $maxline = $val+0;
101 } elsif ( $line eq $barrier ) {
102 last; # Stop reading input at barrier line
104 print OUT $line;
106 close(IN);
108 my $dfile, $ofile, $str, $sl, $len;
109 my @deps, $dep;
111 print OUT $barrier;
113 foreach $dfile ( sort(keys(%deps)) ) {
114 if ( $dfile =~ /\.[Cc]$/ ) {
115 $ofile = $dfile; $ofile =~ s/\.[Cc]$//;
116 $str = convert_file($ofile,$sep).$obj.':';
117 $len = length($str);
118 print OUT $str;
119 foreach $dep ($dfile, alldeps($dfile)) {
120 $str = convert_file($dep,$sep);
121 $sl = length($str)+1;
122 if ( $len+$sl > $maxline-2 ) {
123 print OUT " \\\n ", $str;
124 $len = $sl;
125 } else {
126 print OUT ' ', $str;
127 $len += $sl;
130 print OUT "\n";
133 close(OUT);
135 (unlink($file) && rename($tmp, $file))
136 or die "$0: Failed to change $tmp -> $file\n";
140 # Main program
143 %deps = ();
144 @files = ();
145 @mkfiles = ();
146 $mkmode = 0;
148 while ( defined($arg = shift(@ARGV)) ) {
149 if ( $arg eq '-m' ) {
150 $arg = shift(@ARGV);
151 push(@mkfiles, $arg);
152 } elsif ( $arg eq '-M' ) {
153 $mkmode = 1; # Futher filenames are output Makefile names
154 } elsif ( $arg eq '--' && $mkmode ) {
155 $mkmode = 0;
156 } elsif ( $arg =~ /^-/ ) {
157 die "Unknown option: $arg\n";
158 } else {
159 if ( $mkmode ) {
160 push(@mkfiles, $arg);
161 } else {
162 push(@files, $arg);
167 foreach $dir ( @files ) {
168 opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
170 while ( $file = readdir(DIR) ) {
171 $path = ($dir eq File::Spec->curdir())
172 ? $file : File::Spec->catfile($dir,$file);
173 if ( $file =~ /\.[Cc]$/ ) {
174 scandeps($path);
177 closedir(DIR);
180 foreach $mkfile ( @mkfiles ) {
181 insert_deps($mkfile);