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";
25 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
26 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
28 $Tmp_prefix ="${TMPDIR}/mkdependC$$";
29 $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
34 sub quit_upon_signal {
36 print STDERR "Deleting $tempfile .. \n";
40 $SIG{'INT'} = 'quit_upon_signal';
41 $SIG{'QUIT'} = 'quit_upon_signal';
43 &mangle_command_line_args();
45 if ( ! $Makefile && -f 'makefile' ) {
46 $Makefile = 'makefile';
47 } elsif ( ! $Makefile && -f 'Makefile') {
48 $Makefile = 'Makefile';
49 } elsif ( ! $Makefile) {
50 die "$Pgm: no makefile or Makefile found\n";
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 ($bf = $sf) =~ s/\.(c|hc)$//;
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 local($suff) = &grab_arg_arg($_);
117 push(@File_suffix, $suff);
119 $Begin_magic_str = &grab_arg_arg($_) . "\n";
121 $End_magic_str = &grab_arg_arg($_) . "\n";
123 $Width = &grab_arg_arg($_);
125 print STDERR "$Pgm: unknown option ignored: $_\n";
127 push(@Src_files, $_);
130 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
131 push(@Src_files,$_) if ! /^-/;
140 ($rest_of_arg = $option) =~ s/^-.//;
143 return($rest_of_arg);
144 } elsif ($#ARGV >= 0) {
145 local($temp) = $ARGV[0]; shift(@ARGV);
148 die "$Pgm: no argument following $option option\n";
152 sub slurp_file { # follows an example in the `open' item in perl man page
153 local($fname,$fhandle) = @_;
154 local($depend); # tmp
155 $fhandle++; # a string increment
157 $fname = &tidy_dir_names($fname);
159 $tempfile = "$Tmp_prefix.i";
161 # ${CPP} better be 'gcc -E', or the -x option will fail...
162 $result = system("${CPP} $Include_dirs @Defines -x c $fname -o $tempfile");
168 open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
170 line: while (<$fhandle>) {
172 next line if /^#(ident|pragma)/;
173 chop; # rm trailing newline
175 $_ = &tidy_dir_names($_);
177 # strip junk off the front and back
178 $_ =~ s/^#\s+\d+\s+//;
181 # a little bit of ad-hoc fiddling now:
182 # don't bother w/ dependencies on /usr/include stuff
183 # don't bother if it looks like a GCC built-in hdr file
184 # don't bother with funny yacc-ish files
185 # don't let a file depend on itself
186 next line if /^\/usr\/include/;
187 # Hack - the cygwin32 dir structupre is odd!
188 next line if /H-i386-cygwin32\/i386-cygwin32/;
189 next line if /H-i386-cygwin32\/lib\/gcc-lib\/i386-cygwin32/;
190 next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
191 next line if /\/gnu\/[^-\/]+-[^-\/]+-[^-\/]+\/include\//;
192 next line if /\/yaccpar/;
193 next line if /\/bison\.(simple|hairy)/;
194 next line if $_ eq $fname;
196 print STDERR "$fname :: $_\n" if $Verbose;
198 # ToDo: some sanity checks that we still have something reasonable?
201 $depend = "$bf.$Obj_suffix ";
202 foreach $suff (@File_suffix) {
203 $depend .= "$bf.${suff}_$Obj_suffix ";
205 $depend .= " : $int_file\n";
207 next line if $Depend_seen{$depend}; # already seen this one...
208 # OK, it's a new one.
209 $Depend_seen{$depend} = 1;
211 push (@Depend_lines, $depend);
215 $tempfile = ''; # for quit_upon_signal
218 sub tidy_dir_names { # rm various pernicious dir-name combinations...
221 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
222 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)