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