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";
26 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
27 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
29 $Tmp_prefix ="${DEFAULT_TMPDIR}/mkdependC$$";
30 $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
35 sub quit_upon_signal {
37 print STDERR "Deleting $tempfile .. \n";
41 $SIG{'INT'} = 'quit_upon_signal';
42 $SIG{'QUIT'} = 'quit_upon_signal';
44 &mangle_command_line_args();
46 if ( ! $Makefile && -f 'makefile' ) {
47 $Makefile = 'makefile';
48 } elsif ( ! $Makefile && -f 'Makefile') {
49 $Makefile = 'Makefile';
50 } elsif ( ! $Makefile) {
51 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 ($baseName = $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";
76 binmode(OMKF); # Do not add stupid ^M's to the output on Win32
77 binmode(NMKF); # Do not add stupid ^M's to the output on Win32
81 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
85 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
89 print $Begin_magic_str;
92 while (<OMKF>) { # copy the rest through
99 sub mangle_command_line_args {
100 while($_ = $ARGV[0]) {
106 } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
108 } elsif ( /^(-optc)?(-I.*)/ ) {
109 $Include_dirs .= " $2";
111 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
115 $Makefile = &grab_arg_arg($_);
117 $Obj_suffix = &grab_arg_arg($_);
119 local($suff) = &grab_arg_arg($_);
120 push(@File_suffix, $suff);
122 $Begin_magic_str = &grab_arg_arg($_) . "\n";
124 $End_magic_str = &grab_arg_arg($_) . "\n";
126 $Width = &grab_arg_arg($_);
128 print STDERR "$Pgm: unknown option ignored: $_\n";
130 push(@Src_files, $_);
133 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
134 push(@Src_files,$_) if ! /^-/;
143 ($rest_of_arg = $option) =~ s/^-.//;
146 return($rest_of_arg);
147 } elsif ($#ARGV >= 0) {
148 local($temp) = $ARGV[0]; shift(@ARGV);
151 die "$Pgm: no argument following $option option\n";
155 sub slurp_file { # follows an example in the `open' item in perl man page
156 local($fname,$fhandle) = @_;
157 local($depend,$dep); # tmp
160 $fhandle++; # a string increment
162 $fname = &tidy_dir_names($fname);
164 ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
165 $tempfile =~ s|.*/([^/]+)$|$1|g;
167 # ${CPP} better be 'gcc -E', or the -x option will fail...
168 # ..and the -MM & -MMD.
169 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname > /dev/null");
172 # On the cheesy side..we do want to know what went wrong, so
173 # re-run the command. (This also has the beneficial effect
174 # of fixing a redirection problem with gcc-2.95 builds used with
176 $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname ");
183 local($dep_contents)='';
185 open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
191 ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
194 @Deps = split(/ +/, $deps);
196 $depend = "$baseName.$Obj_suffix";
197 foreach $suff (@File_suffix) {
198 $depend .= " $baseName.${suff}_$Obj_suffix";
201 foreach $dep (@Deps) {
202 push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
207 $tempfile = ''; # for quit_upon_signal
210 sub tidy_dir_names { # rm various pernicious dir-name combinations...
213 $str =~ s|/[^/.][^/]*/\.\.||g; # nuke: /<dir>/..
214 $str =~ s|/\.[^.][^/]*/\.\.||g; # nuke: /./.. (and others)