2 # This stub of perl assumes that the following two
3 # variables are prepended:
7 # ToDo: strip out all the .h junk
9 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
10 $Usage = "usage: $Pgm: not done yet\n";
12 $Status = 0; # just used for exit() status
16 $Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
17 $End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
24 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
25 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
27 $Tmp_prefix ="${TMPDIR}/mkdependC$$";
28 $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
31 sub quit_upon_signal {
32 if (-f "$Tmp_prefix.i") {
33 print STDERR "Deleting $Tmp_prefix.i .. \n";
34 unlink "$Tmp_prefix.i";
37 $SIG{'INT'} = 'quit_upon_signal';
38 $SIG{'QUIT'} = 'quit_upon_signal';
40 &mangle_command_line_args();
42 if ( ! $Makefile && -f 'makefile' ) {
43 $Makefile = 'makefile';
44 } elsif ( ! $Makefile && -f 'Makefile') {
45 $Makefile = 'Makefile';
46 } elsif ( ! $Makefile) {
47 die "$Pgm: no makefile or Makefile found\n";
53 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
55 foreach $sf (@Src_files) {
56 # just like lit-inputter
57 # except it puts each file through CPP and
58 # a de-commenter (not implemented);
59 # builds up @Depend_lines
60 print STDERR "Here we go for source file: $sf\n" if $Verbose;
61 ($of = $sf) =~ s/\.(c|hc)$/$Obj_suffix/;
63 &slurp_file($sf, 'fh00');
66 # OK, mangle the Makefile
67 unlink("$Makefile.bak");
68 rename($Makefile,"$Makefile.bak");
69 # now copy Makefile.bak into Makefile, rm'ing old dependencies
71 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
72 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
75 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
79 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
83 print $Begin_magic_str;
86 while (<OMKF>) { # copy the rest through
93 sub mangle_command_line_args {
94 while($_ = $ARGV[0]) {
100 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
103 $Include_dirs .= " $_";
105 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
109 $Makefile = &grab_arg_arg($_);
111 $Obj_suffix = &grab_arg_arg($_);
113 $Begin_magic_str = &grab_arg_arg($_) . "\n";
115 $End_magic_str = &grab_arg_arg($_) . "\n";
117 $Width = &grab_arg_arg($_);
119 print STDERR "$Pgm: unknown option ignored: $_\n";
121 push(@Src_files, $_);
124 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
125 push(@Src_files,$_) if ! /^-/;
134 ($rest_of_arg = $option) =~ s/^-.//;
137 return($rest_of_arg);
138 } elsif ($#ARGV >= 0) {
139 local($temp) = $ARGV[0]; shift(@ARGV);
142 die "$Pgm: no argument following $option option\n";
146 sub slurp_file { # follows an example in the `open' item in perl man page
147 local($fname,$fhandle) = @_;
148 local($depend); # tmp
149 $fhandle++; # a string increment
151 $fname = &tidy_dir_names($fname);
153 $tempfile = "$Tmp_prefix.i";
155 # ${CPP} better be 'gcc -E', or the -x option will fail...
156 $result = system("${CPP} $Include_dirs @Defines -x c $fname -o $tempfile");
162 open($fhandle, $tempfile) || die "$Pgm: Can't open $fname: $!\n";
164 line: while (<$fhandle>) {
166 next line if /^#(ident|pragma)/;
167 chop; # rm trailing newline
169 $_ = &tidy_dir_names($_);
171 # strip junk off the front and back
172 $_ =~ s/^#\s+\d+\s+//;
175 # a little bit of ad-hoc fiddling now:
176 # don't bother w/ dependencies on /usr/include stuff
177 # don't bother if it looks like a GCC built-in hdr file
178 # don't bother with funny yacc-ish files
179 # don't bother with "literate" .h files (.lh); we'll just
180 # depend on the de-litified versions (which have better info)
181 # don't let a file depend on itself
182 next line if /^\/usr\/include/;
183 # Hack - the cygwin32 dir structure is odd!
184 next line if /H-i386-cygwin32\/i386-cygwin32/;
185 next line if /H-i386-cygwin32\/lib\/gcc-lib\/i386-cygwin32/;
186 next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
187 next line if /\/gnu\/[^-\/]+-[^-\/]+-[^-\/]+\/include\//;
188 next line if /\/yaccpar/;
189 next line if /\/bison\.(simple|hairy)/;
190 next line if /\.lh$/;
191 next line if $_ eq $fname;
193 print STDERR "$fname :: $_\n" if $Verbose;
195 # ToDo: some sanity checks that we still have something reasonable?
197 $depend = "$of : $_\n";
198 next line if $Depend_seen{$depend}; # already seen this one...
200 # OK, it's a new one.
201 push (@Depend_lines, $depend);
202 $Depend_seen{$depend} = 1;
206 $tempname = ''; # for quit_upon_signal
209 sub tidy_dir_names { # rm various pernicious dir-name combinations...
212 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
213 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)