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