2 # This stub of perl assumes that the following two
3 # variables are prepended:
8 # tries to work like mkdependC
10 # ToDo: strip out all the .h junk
12 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
13 $Usage = "usage: $Pgm: not done yet\n";
15 $Status = 0; # just used for exit() status
19 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
20 $End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
24 $Col_width = 78; # ignored
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};
32 if ( $HostPlatform =~ /^i386-/ ) {
33 push(@Defines, '-D__i386__');
35 if ( $HostPlatform =~ /^sparc-/ ) {
36 push(@Defines, '-D__sparc__');
38 if ( $HostPlatform =~ /-solaris2$/ ) {
39 push(@Defines, '-D__svr4__');
42 &mangle_command_line_args();
44 if ( ! $Makefile && -f 'makefile' ) {
45 $Makefile = 'makefile';
46 } elsif ( ! $Makefile && -f 'Makefile') {
47 $Makefile = 'Makefile';
48 } elsif ( ! $Makefile) {
49 die "$Pgm: no makefile or Makefile found\n";
55 print STDERR "CPP defines=@Defines\n" if $Verbose;
56 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
58 foreach $sf (@Src_files) {
59 # just like lit-inputter
60 # except it puts each file through CPP and
61 # a de-commenter (not implemented);
62 # builds up @Depend_lines
63 print STDERR "Here we go for source file: $sf\n" if $Verbose;
64 ($of = $sf) =~ s/\.(c|hc)$/$Obj_suffix/;
66 &slurp_file($sf, 'fh00');
69 # OK, mangle the Makefile
70 unlink("$Makefile.bak");
71 rename($Makefile,"$Makefile.bak");
72 # now copy Makefile.bak into Makefile, rm'ing old dependencies
74 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
75 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
78 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
82 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
86 print $Begin_magic_str;
89 while (<OMKF>) { # copy the rest through
96 sub mangle_command_line_args {
97 while($_ = $ARGV[0]) {
103 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
106 $Include_dirs .= " $_";
108 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
112 $Makefile = &grab_arg_arg($_);
114 $Obj_suffix = &grab_arg_arg($_);
116 $Begin_magic_str = &grab_arg_arg($_) . "\n";
118 $End_magic_str = &grab_arg_arg($_) . "\n";
120 $Width = &grab_arg_arg($_);
122 print STDERR "$Pgm: unknown option ignored: $_\n";
124 push(@Src_files, $_);
127 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
128 push(@Src_files,$_) if ! /^-/;
137 ($rest_of_arg = $option) =~ s/^-.//;
140 return($rest_of_arg);
141 } elsif ($#ARGV >= 0) {
142 local($temp) = $ARGV[0]; shift(@ARGV);
145 die "$Pgm: no argument following $option option\n";
149 sub slurp_file { # follows an example in the `open' item in perl man page
150 local($fname,$fhandle) = @_;
151 local($depend); # tmp
152 $fhandle++; # a string increment
154 $fname = &tidy_dir_names($fname);
156 unless (open($fhandle, "${RAWCPP} $Include_dirs @Defines $fname |")) {
157 die "$Pgm: Can't open $fname: $!\n";
159 line: while (<$fhandle>) {
161 next line if /^#(ident|pragma)/;
162 chop; # rm trailing newline
164 $_ = &tidy_dir_names($_);
166 # strip junk off the front and back
167 $_ =~ s/^#\s+\d+\s+//;
170 # a little bit of ad-hoc fiddling now:
171 # don't bother w/ dependencies on /usr/include stuff
172 # don't bother if it looks like a GCC built-in hdr file
173 # don't bother with funny yacc-ish files
174 # don't bother with "literate" .h files (.lh); we'll just
175 # depend on the de-litified versions (which have better info)
176 # don't let a file depend on itself
177 next line if /^\/usr\/include/;
178 next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
179 next line if /\/gnu\/[^-\/]+-[^-\/]+-[^-\/]+\/include\//;
180 next line if /\/yaccpar/;
181 next line if /\/bison\.(simple|hairy)/;
182 next line if /\.lh$/;
183 next line if $_ eq $fname;
185 print STDERR "$fname :: $_\n" if $Verbose;
187 # ToDo: some sanity checks that we still have something reasonable?
189 $depend = "$of : $_\n";
190 next line if $Depend_seen{$depend}; # already seen this one...
192 # OK, it's a new one.
193 push (@Depend_lines, $depend);
194 $Depend_seen{$depend} = 1;
199 sub tidy_dir_names { # rm various pernicious dir-name combinations...
202 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
203 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)