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
33 sub quit_upon_signal {
35 print STDERR "Deleting $tempfile .. \n";
39 $SIG{'INT'} = 'quit_upon_signal';
40 $SIG{'QUIT'} = 'quit_upon_signal';
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 "Include_dirs=$Include_dirs\n" if $Verbose;
57 foreach $sf (@Src_files) {
58 # just like lit-inputter
59 # except it puts each file through CPP and
60 # a de-commenter (not implemented);
61 # builds up @Depend_lines
62 print STDERR "Here we go for source file: $sf\n" if $Verbose;
63 ($of = $sf) =~ s/\.(c|hc)$/$Obj_suffix/;
65 &slurp_file($sf, 'fh00');
68 # OK, mangle the Makefile
69 unlink("$Makefile.bak");
70 rename($Makefile,"$Makefile.bak");
71 # now copy Makefile.bak into Makefile, rm'ing old dependencies
73 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
74 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
77 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
81 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
85 print $Begin_magic_str;
88 while (<OMKF>) { # copy the rest through
95 sub mangle_command_line_args {
96 while($_ = $ARGV[0]) {
102 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
105 $Include_dirs .= " $_";
107 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
111 $Makefile = &grab_arg_arg($_);
113 $Obj_suffix = &grab_arg_arg($_);
115 $Begin_magic_str = &grab_arg_arg($_) . "\n";
117 $End_magic_str = &grab_arg_arg($_) . "\n";
119 $Width = &grab_arg_arg($_);
121 print STDERR "$Pgm: unknown option ignored: $_\n";
123 push(@Src_files, $_);
126 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
127 push(@Src_files,$_) if ! /^-/;
136 ($rest_of_arg = $option) =~ s/^-.//;
139 return($rest_of_arg);
140 } elsif ($#ARGV >= 0) {
141 local($temp) = $ARGV[0]; shift(@ARGV);
144 die "$Pgm: no argument following $option option\n";
148 sub slurp_file { # follows an example in the `open' item in perl man page
149 local($fname,$fhandle) = @_;
150 local($depend); # tmp
151 $fhandle++; # a string increment
153 $fname = &tidy_dir_names($fname);
155 $tempfile = "$Tmp_prefix.i";
157 # ${CPP} better be 'gcc -E', or the -x option will fail...
158 $result = system("${CPP} $Include_dirs @Defines -x c $fname -o $tempfile");
164 open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
166 line: while (<$fhandle>) {
168 next line if /^#(ident|pragma)/;
169 chop; # rm trailing newline
171 $_ = &tidy_dir_names($_);
173 # strip junk off the front and back
174 $_ =~ s/^#\s+\d+\s+//;
177 # a little bit of ad-hoc fiddling now:
178 # don't bother w/ dependencies on /usr/include stuff
179 # don't bother if it looks like a GCC built-in hdr file
180 # don't bother with funny yacc-ish files
181 # don't bother with "literate" .h files (.lh); we'll just
182 # depend on the de-litified versions (which have better info)
183 # don't let a file depend on itself
184 next line if /^\/usr\/include/;
185 # Hack - the cygwin32 dir structure is odd!
186 next line if /H-i386-cygwin32\/i386-cygwin32/;
187 next line if /H-i386-cygwin32\/lib\/gcc-lib\/i386-cygwin32/;
188 next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
189 next line if /\/gnu\/[^-\/]+-[^-\/]+-[^-\/]+\/include\//;
190 next line if /\/yaccpar/;
191 next line if /\/bison\.(simple|hairy)/;
192 next line if /\.lh$/;
193 next line if $_ eq $fname;
195 print STDERR "$fname :: $_\n" if $Verbose;
197 # ToDo: some sanity checks that we still have something reasonable?
199 $depend = "$of : $_\n";
200 next line if $Depend_seen{$depend}; # already seen this one...
202 # OK, it's a new one.
203 push (@Depend_lines, $depend);
204 $Depend_seen{$depend} = 1;
208 $tempfile = ''; # for quit_upon_signal
211 sub tidy_dir_names { # rm various pernicious dir-name combinations...
214 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
215 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)