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