1 # *** MSUB does some substitutions here ***
4 # tries to work like mkdependC
6 # ToDo: strip out all the .h junk
8 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
9 $Usage = "usage: $Pgm: not done yet\n";
11 $Status = 0; # just used for exit() status
15 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
16 $End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
20 $Col_width = 78; # ignored
24 &mangle_command_line_args();
26 if ( ! $Makefile && -f 'makefile' ) {
27 $Makefile = 'makefile';
28 } elsif ( ! $Makefile && -f 'Makefile') {
29 $Makefile = 'Makefile';
30 } elsif ( ! $Makefile) {
31 die "$Pgm: no makefile or Makefile found\n";
37 print STDERR "CPP defines=@Defines\n" if $Verbose;
38 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
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/;
48 &slurp_file($sf, 'fh00');
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
56 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
57 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
60 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
64 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
68 print $Begin_magic_str;
71 while (<OMKF>) { # copy the rest through
78 sub mangle_command_line_args {
79 while($_ = $ARGV[0]) {
85 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
88 $Include_dirs .= " $_";
90 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
94 $Makefile = &grab_arg_arg($_);
96 $Obj_suffix = &grab_arg_arg($_);
98 $Begin_magic_str = &grab_arg_arg($_) . "\n";
100 $End_magic_str = &grab_arg_arg($_) . "\n";
102 $Width = &grab_arg_arg($_);
104 print STDERR "$Pgm: unknown option ignored: $_\n";
106 push(@Src_files, $_);
109 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
110 push(@Src_files,$_) if ! /^-/;
119 ($rest_of_arg = $option) =~ s/^-.//;
122 return($rest_of_arg);
123 } elsif ($#ARGV >= 0) {
124 local($temp) = $ARGV[0]; shift(@ARGV);
127 die "$Pgm: no argument following $option option\n";
131 sub slurp_file { # follows an example in the `open' item in perl man page
132 local($fname,$fhandle) = @_;
133 local($depend); # tmp
134 $fhandle++; # a string increment
136 $fname = &tidy_dir_names($fname);
138 unless (open($fhandle, "$(GNUCPP) $Include_dirs @Defines $fname |")) {
139 die "$Pgm: Can't open $fname: $!\n";
141 line: while (<$fhandle>) {
143 next line if /^#(ident|pragma)/;
144 chop; # rm trailing newline
146 $_ = &tidy_dir_names($_);
148 # strip junk off the front and back
149 $_ =~ s/^#\s+\d+\s+//;
152 # a little bit of ad-hoc fiddling now:
153 # don't bother w/ dependencies on /usr/include stuff
154 # don't bother if it looks like a GCC built-in hdr file
155 # don't bother with funny yacc-ish files
156 # don't bother with "literate" .h files (.lh); we'll just
157 # depend on the de-litified versions (which have better info)
158 # don't let a file depend on itself
159 next line if /^\/usr\/include/;
160 next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
161 next line if /\/yaccpar/;
162 next line if /\/bison\.(simple|hairy)/;
163 next line if /\.lh$/;
164 next line if $_ eq $fname;
166 print STDERR "$fname :: $_\n" if $Verbose;
168 # ToDo: some sanity checks that we still have something reasonable?
170 $depend = "$of : $_\n";
171 next line if $Depend_seen{$depend}; # already seen this one...
173 # OK, it's a new one.
174 push (@Depend_lines, $depend);
175 $Depend_seen{$depend} = 1;
180 sub tidy_dir_names { # rm various pernicious dir-name combinations...
183 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
184 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)