[project @ 1997-10-30 22:12:25 by sof]
[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 #  RAWCPP
6 #  HOSTPLATFORM
7 #
8 # tries to work like mkdependC
9 #
10 # ToDo: strip out all the .h junk
11 #
12 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
13 $Usage  = "usage: $Pgm: not done yet\n";
14
15 $Status  = 0; # just used for exit() status
16 $Verbose = 0;
17 $Dashdashes_seen = 0;
18
19 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
20 $End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
21 $Obj_suffix = '.o';
22 @Defines = ();
23 $Include_dirs = '';
24 $Col_width = 78; # ignored
25 $Makefile = '';
26 @Src_files = ();
27
28 # the following is a hack, so we can use RAWCPP, but there you go;
29 # put in just enough #defines that mkdependC will not barf.
30 $HostPlatform = ${HOSTPLATFORM};
31
32 if ( $HostPlatform =~ /^i386-/ ) {
33     push(@Defines, '-D__i386__');
34 }
35 if ( $HostPlatform =~ /linux/ ) {
36     push(@Defines, '-D__linux__');
37 }
38 if ( $HostPlatform =~ /^sparc-/ ) {
39     push(@Defines, '-D__sparc__');
40 }
41 if ( $HostPlatform =~ /-solaris2$/ ) {
42     push(@Defines, '-D__svr4__');
43 }
44
45 &mangle_command_line_args();
46
47 if ( ! $Makefile && -f 'makefile' ) {
48     $Makefile = 'makefile';
49 } elsif ( ! $Makefile && -f 'Makefile') {
50     $Makefile = 'Makefile';
51 } elsif ( ! $Makefile) {
52     die "$Pgm: no makefile or Makefile found\n";
53 }
54
55 @Depend_lines = ();
56 %Depend_seen = ();
57
58 print STDERR "CPP defines=@Defines\n" if $Verbose;
59 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
60
61 foreach $sf (@Src_files) {
62     # just like lit-inputter
63     # except it puts each file through CPP and
64     # a de-commenter (not implemented);
65     # builds up @Depend_lines
66     print STDERR "Here we go for source file: $sf\n" if $Verbose;
67     ($of = $sf) =~ s/\.(c|hc)$/$Obj_suffix/;
68
69     &slurp_file($sf, 'fh00');
70 }
71
72 # OK, mangle the Makefile
73 unlink("$Makefile.bak");
74 rename($Makefile,"$Makefile.bak");
75 # now copy Makefile.bak into Makefile, rm'ing old dependencies
76 # and adding the new
77 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
78 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
79 select(NMKF);
80 $_ = <OMKF>;
81 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
82     print $_;
83     $_ = <OMKF>;
84 }
85 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
86     $_ = <OMKF>;
87 }
88 # insert dependencies
89 print $Begin_magic_str;
90 print @Depend_lines;
91 print $End_magic_str;
92 while (<OMKF>) { # copy the rest through
93     print $_;
94 }
95 close(NMKF);
96 close(OMKF);
97 exit 0;
98
99 sub mangle_command_line_args {
100     while($_ = $ARGV[0]) {
101         shift(@ARGV);
102
103         if ( /^--$/ ) {
104             $Dashdashes_seen++;
105
106         } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
107             push(@Defines, $_);
108         } elsif ( /^-I/ ) {
109             $Include_dirs .= " $_";
110
111         } elsif ($Dashdashes_seen != 1) { # not between -- ... --
112             if ( /^-v$/ ) {
113                 $Verbose++;
114             } elsif ( /^-f/ ) {
115                 $Makefile       = &grab_arg_arg($_);
116             } elsif ( /^-o/ ) {
117                 $Obj_suffix     = &grab_arg_arg($_);
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     unless (open($fhandle, "${RAWCPP} $Include_dirs @Defines $fname |")) {
160          die "$Pgm: Can't open $fname: $!\n";
161     }
162     line: while (<$fhandle>) {
163         next line if ! /^#/;
164         next line if /^#(ident|pragma)/;
165         chop; # rm trailing newline
166
167         $_ = &tidy_dir_names($_);
168
169         # strip junk off the front and back
170         $_ =~ s/^#\s+\d+\s+//;
171         $_ =~ s/[ 0-9]*$//;
172         
173         # a little bit of ad-hoc fiddling now:
174         # don't bother w/ dependencies on /usr/include stuff
175         # don't bother if it looks like a GCC built-in hdr file
176         # don't bother with funny yacc-ish files
177         # don't bother with "literate" .h files (.lh); we'll just
178         # depend on the de-litified versions (which have better info)
179         # don't let a file depend on itself
180         next line if /^\/usr\/include/;
181         # Hack - the cygwin32 dir structure is odd!
182         next line if /H-i386-cygwin32\/i386-cygwin32/;
183         next line if /H-i386-cygwin32\/lib\/gcc-lib\/i386-cygwin32/;
184         next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
185         next line if /\/gnu\/[^-\/]+-[^-\/]+-[^-\/]+\/include\//;
186         next line if /\/yaccpar/;
187         next line if /\/bison\.(simple|hairy)/;
188         next line if /\.lh$/;
189         next line if $_ eq $fname;
190
191         print STDERR "$fname :: $_\n" if $Verbose;
192
193         # ToDo: some sanity checks that we still have something reasonable?
194
195         $depend = "$of : $_\n";
196         next line if $Depend_seen{$depend};  # already seen this one...
197
198         # OK, it's a new one.
199         push (@Depend_lines, $depend);
200         $Depend_seen{$depend} = 1;
201     }
202     close($fhandle);
203 }
204
205 sub tidy_dir_names { # rm various pernicious dir-name combinations...
206     local($str) = @_;
207
208     $str =~ s|/[^/.][^/]*/\.\.||g;      # nuke: /<dir>/..
209     $str =~ s|/\.[^.][^/]*/\.\.||g;     # nuke: /./.. (and others)
210     $str =~ s|"||g;
211     $str =~ s| \./| |;
212     $str;
213 }