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