7c524bd4d85117019c274ee9f292f9122b09f347
[ghc-hetmet.git] / glafp-utils / mkdependC / mkdependC.prl
1 #
2 # This stub of perl assumes that the following two
3 # variables are prepended:
4 #
5 # TMPDIR CPP
6 #
7 # ToDo: strip out all the .h junk
8 #
9 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
10 $Usage  = "usage: $Pgm: not done yet\n";
11
12 $Status  = 0; # just used for exit() status
13 $Verbose = 0;
14 $Dashdashes_seen = 0;
15
16 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
17 $End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
18 $Obj_suffix = 'o';
19 @Defines = ();
20 $Include_dirs = '';
21 $Makefile = '';
22 @Src_files = ();
23 @File_suffix = ();
24
25 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
26     $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
27 } else {
28     $Tmp_prefix ="${TMPDIR}/mkdependC$$";
29     $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
30 }
31
32 $tempfile = '';
33
34 sub quit_upon_signal { 
35   if (-f $tempfile) {
36         print STDERR "Deleting $tempfile .. \n"; 
37         unlink $tempfile;
38    }
39 }
40 $SIG{'INT'}  = 'quit_upon_signal';
41 $SIG{'QUIT'} = 'quit_upon_signal';
42
43 &mangle_command_line_args();
44
45 if ( ! $Makefile && -f 'makefile' ) {
46     $Makefile = 'makefile';
47 } elsif ( ! $Makefile && -f 'Makefile') {
48     $Makefile = 'Makefile';
49 } elsif ( ! $Makefile) {
50     die "$Pgm: no makefile or Makefile found\n";
51 }
52
53 @Depend_lines = ();
54 %Depend_seen = ();
55
56 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
57
58 foreach $sf (@Src_files) {
59     # just like lit-inputter
60     # except it puts each file through CPP and
61     # a de-commenter (not implemented);
62     # builds up @Depend_lines
63     print STDERR "Here we go for source file: $sf\n" if $Verbose;
64     ($bf = $sf) =~ s/\.(c|hc)$//;
65
66     &slurp_file($sf, 'fh00');
67 }
68
69 # OK, mangle the Makefile
70 unlink("$Makefile.bak");
71 rename($Makefile,"$Makefile.bak");
72 # now copy Makefile.bak into Makefile, rm'ing old dependencies
73 # and adding the new
74 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
75 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
76 select(NMKF);
77 $_ = <OMKF>;
78 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
79     print $_;
80     $_ = <OMKF>;
81 }
82 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
83     $_ = <OMKF>;
84 }
85 # insert dependencies
86 print $Begin_magic_str;
87 print @Depend_lines;
88 print $End_magic_str;
89 while (<OMKF>) { # copy the rest through
90     print $_;
91 }
92 close(NMKF);
93 close(OMKF);
94 exit 0;
95
96 sub mangle_command_line_args {
97     while($_ = $ARGV[0]) {
98         shift(@ARGV);
99
100         if ( /^--$/ ) {
101             $Dashdashes_seen++;
102
103         } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
104             push(@Defines, $_);
105         } elsif ( /^-I/ ) {
106             $Include_dirs .= " $_";
107
108         } elsif ($Dashdashes_seen != 1) { # not between -- ... --
109             if ( /^-v$/ ) {
110                 $Verbose++;
111             } elsif ( /^-f/ ) {
112                 $Makefile       = &grab_arg_arg($_);
113             } elsif ( /^-o/ ) {
114                 $Obj_suffix     = &grab_arg_arg($_);
115             } elsif ( /^-s/ ) {
116                 local($suff)    =  &grab_arg_arg($_);
117                 push(@File_suffix, $suff);
118             } elsif ( /^-bs/ ) {
119                 $Begin_magic_str = &grab_arg_arg($_) . "\n";
120             } elsif ( /^-es/ ) {
121                 $End_magic_str = &grab_arg_arg($_) . "\n";
122             } elsif ( /^-w/ ) {
123                 $Width  = &grab_arg_arg($_);
124             } elsif ( /^-/ ) {
125                 print STDERR "$Pgm: unknown option ignored: $_\n";
126             } else {
127                 push(@Src_files, $_);
128             }
129
130         } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
131             push(@Src_files,$_) if ! /^-/;
132         }
133     }
134 }
135
136 sub grab_arg_arg {
137     local($option) = @_;
138     local($rest_of_arg);
139     
140     ($rest_of_arg = $option) =~ s/^-.//;
141
142     if ($rest_of_arg) {
143         return($rest_of_arg);
144     } elsif ($#ARGV >= 0) {
145         local($temp) = $ARGV[0]; shift(@ARGV); 
146         return($temp);
147     } else {
148         die "$Pgm: no argument following $option option\n";
149     }
150 }
151
152 sub slurp_file { # follows an example in the `open' item in perl man page
153     local($fname,$fhandle) = @_;
154     local($depend); # tmp
155     $fhandle++; # a string increment
156
157     $fname = &tidy_dir_names($fname);
158
159     $tempfile = "$Tmp_prefix.i";
160
161     # ${CPP} better be 'gcc -E', or the -x option will fail...
162     $result = system("${CPP} $Include_dirs @Defines -x c $fname -o $tempfile");
163     if ($result != 0) {
164         unlink($tempfile);
165         exit($result);
166     };
167
168     open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
169
170     line: while (<$fhandle>) {
171         next line if ! /^#/;
172         next line if /^#(ident|pragma)/;
173         chop; # rm trailing newline
174
175         $_ = &tidy_dir_names($_);
176
177         # strip junk off the front and back
178         $_ =~ s/^#\s+\d+\s+//;
179         $_ =~ s/[ 0-9]*$//;
180         
181         # a little bit of ad-hoc fiddling now:
182         # don't bother w/ dependencies on /usr/include stuff
183         # don't bother if it looks like a GCC built-in hdr file
184         # don't bother with funny yacc-ish files
185         # don't let a file depend on itself
186         next line if /^\/usr\/include/;
187         # Hack - the cygwin32 dir structupre is odd!
188         next line if /H-i386-cygwin32\/i386-cygwin32/;
189         next line if /H-i386-cygwin32\/lib\/gcc-lib\/i386-cygwin32/;
190         next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
191         next line if /\/gnu\/[^-\/]+-[^-\/]+-[^-\/]+\/include\//;
192         next line if /\/yaccpar/;
193         next line if /\/bison\.(simple|hairy)/;
194         next line if $_ eq $fname;
195
196         print STDERR "$fname :: $_\n" if $Verbose;
197
198         # ToDo: some sanity checks that we still have something reasonable?
199
200         $int_file = $_;
201         $depend = "$bf.$Obj_suffix ";
202         foreach $suff (@File_suffix) {
203            $depend .= "$bf.${suff}_$Obj_suffix ";
204         }
205         $depend .= " : $int_file\n";
206
207         next line if $Depend_seen{$depend};  # already seen this one...
208         # OK, it's a new one.
209         $Depend_seen{$depend} = 1;
210
211         push (@Depend_lines, $depend);
212     }
213     close($fhandle);
214     unlink($tempfile);
215     $tempfile = '';  # for quit_upon_signal
216 }
217
218 sub tidy_dir_names { # rm various pernicious dir-name combinations...
219     local($str) = @_;
220
221     $str =~ s|/[^/.][^/]*/\.\.||g;      # nuke: /<dir>/..
222     $str =~ s|/\.[^.][^/]*/\.\.||g;     # nuke: /./.. (and others)
223     $str =~ s|"||g;
224     $str =~ s| \./| |;
225     $str;
226 }