Dependency-generation script
[platform/upstream/nasm.git] / mkdep.pl
1 #!/usr/bin/perl
2 #
3 # Script to create Makefile-style dependencies.
4 #
5 # Usage: perl [-s path-separator] [-o obj-ext] mkdep.pl dir... > deps
6 #
7
8 sub scandeps($) {
9     my($file) = @_;
10     my($line, $nf);
11     my(@xdeps) = ();
12     my(@mdeps) = ();
13
14     open(FILE, "< $file\0") or return; # If not openable, assume generated
15     while ( defined($line = <FILE>) ) {
16         chomp $line;
17         $line =~ s:/\*.*\*/::g;
18         $line =~ s://.*$::;
19         if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) {
20             $nf = $1;
21             push(@mdeps, $nf);
22             push(@xdeps, $nf) unless ( defined($deps{$nf}) );
23         }
24     }
25     close(FILE);
26     $deps{$file} = [@mdeps];
27
28     foreach $file ( @xdeps ) {
29         scandeps($file);
30     }
31 }
32
33 # %deps contains direct dependencies.  This subroutine resolves
34 # indirect dependencies that result.
35 sub alldeps($) {
36     my($file) = @_;
37     my(%adeps);
38     my($dep,$idep);
39
40     foreach $dep ( @{$deps{$file}} ) {
41         $adeps{$dep} = 1;
42         foreach $idep ( alldeps($dep) ) {
43             $adeps{$idep} = 1;
44         }
45     }
46     return keys(%adeps);
47 }
48
49 %deps = ();
50 @files = ();
51 $sep = '/';                     # Default, and works on most systems
52 $obj = 'o';                     # Object file extension
53
54 while ( defined($arg = shift(@ARGV)) ) {
55     if ( $arg =~ /^\-s$/ ) {
56         $sep = shift(@ARGV);
57     } elsif ( $arg =~ /^\-o$/ ) {
58         $obj = shift(@ARGV);
59     } elsif ( $arg =~ /^-/ ) {
60         die "Unknown option: $arg\n";
61     } else {
62         push(@files, $arg);
63     }
64 }
65
66 foreach $dir ( @files ) {
67     opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
68
69     while ( $file = readdir(DIR) ) {
70         $path = ($dir eq '.') ? $file : $dir.$sep.$file;
71         if ( $file =~ /\.[Cc]$/ ) {
72             scandeps($path);
73         }
74     }
75     closedir(DIR);
76 }
77
78 foreach $file ( sort(keys(%deps)) ) {
79     if ( $file =~ /\.[Cc]$/ && scalar(@{$deps{$file}}) ) {
80         $ofile = $file; $ofile =~ s/\.[Cc]$/\./; $ofile .= $obj;
81         print $ofile, ': ';
82         print join(' ', alldeps($file));
83         print "\n";
84     }
85 }
86