#
-# This stub of perl assumes that the following two
-# variables are prepended:
+# This perl script template assumes that definitions for
+# the following variables are prepended:
#
-# RAWCPP
-# HOSTPLATFORM
-#
-# tries to work like mkdependC
+# DEFAULT_TMPDIR CPP BUILDPLATFORM
#
# ToDo: strip out all the .h junk
#
$Begin_magic_str = "# DO NOT DELETE: Beginning of C dependencies\n";
$End_magic_str = "# DO NOT DELETE: End of C dependencies\n";
-$Obj_suffix = '.o';
+$Obj_suffix = 'o';
@Defines = ();
$Include_dirs = '';
-$Col_width = 78; # ignored
$Makefile = '';
@Src_files = ();
+@File_suffix = ();
+$baseName='';
+$ignore_output='> /dev/null';
-# the following is a hack, so we can use RAWCPP, but there you go;
-# put in just enough #defines that mkdependC will not barf.
-$HostPlatform = ${HOSTPLATFORM};
-
-if ( $HostPlatform =~ /^i386-/ ) {
- push(@Defines, '-D__i386__');
+if ( ${BUILDPLATFORM} eq "i386-unknown-mingw32" ) {
+ # Assuming the underlying perl uses cmd to exec system() calls.
+ $ignore_output = ">nul";
}
-if ( $HostPlatform =~ /^sparc-/ ) {
- push(@Defines, '-D__sparc__');
+
+if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
+ $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
+} else {
+ $Tmp_prefix ="${DEFAULT_TMPDIR}/mkdependC$$";
+ $ENV{'TMPDIR'} = "${DEFAULT_TMPDIR}"; # set the env var as well
}
-if ( $HostPlatform =~ /-solaris2$/ ) {
- push(@Defines, '-D__svr4__');
+
+$tempfile = '';
+
+sub quit_upon_signal {
+ if (-f $tempfile) {
+ print STDERR "Deleting $tempfile .. \n";
+ unlink $tempfile;
+ }
}
+$SIG{'INT'} = 'quit_upon_signal';
+$SIG{'QUIT'} = 'quit_upon_signal';
&mangle_command_line_args();
}
@Depend_lines = ();
-%Depend_seen = ();
-print STDERR "CPP defines=@Defines\n" if $Verbose;
print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
foreach $sf (@Src_files) {
# a de-commenter (not implemented);
# builds up @Depend_lines
print STDERR "Here we go for source file: $sf\n" if $Verbose;
- ($of = $sf) =~ s/\.(c|hc)$/$Obj_suffix/;
+ ($baseName = $sf) =~ s/\.(c|hc)$//;
&slurp_file($sf, 'fh00');
}
# and adding the new
open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
+binmode(OMKF); # Do not add stupid ^M's to the output on Win32
+binmode(NMKF); # Do not add stupid ^M's to the output on Win32
+
select(NMKF);
$_ = <OMKF>;
while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
if ( /^--$/ ) {
$Dashdashes_seen++;
- } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
- push(@Defines, $_);
- } elsif ( /^-I/ ) {
- $Include_dirs .= " $_";
+ } elsif ( /^(-optc)?(-D.*)/ ) { # recognized wherever they occur
+ push(@Defines, $2);
+ } elsif ( /^(-optc)?(-I.*)/ ) {
+ $Include_dirs .= " $2";
} elsif ($Dashdashes_seen != 1) { # not between -- ... --
if ( /^-v$/ ) {
$Makefile = &grab_arg_arg($_);
} elsif ( /^-o/ ) {
$Obj_suffix = &grab_arg_arg($_);
+ } elsif ( /^-s/ ) {
+ local($suff) = &grab_arg_arg($_);
+ push(@File_suffix, $suff);
} elsif ( /^-bs/ ) {
$Begin_magic_str = &grab_arg_arg($_) . "\n";
} elsif ( /^-es/ ) {
sub slurp_file { # follows an example in the `open' item in perl man page
local($fname,$fhandle) = @_;
- local($depend); # tmp
+ local($depend,$dep); # tmp
+ local(@Deps);
+
$fhandle++; # a string increment
$fname = &tidy_dir_names($fname);
- unless (open($fhandle, "${RAWCPP} $Include_dirs @Defines $fname |")) {
- die "$Pgm: Can't open $fname: $!\n";
+ ($tempfile = $fname) =~ s/\.[^\.]*$/\.d/;
+ $tempfile =~ s|.*/([^/]+)$|$1|g;
+
+ # ${CPP} better be 'gcc -E', or the -x option will fail...
+ # ..and the -MM & -MMD.
+ $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname $ignore_output");
+
+ if ($result != 0) {
+ # On the cheesy side..we do want to know what went wrong, so
+ # re-run the command.
+ $result = system("${CPP} -MM -MMD $Include_dirs @Defines -x c $fname ");
+ if ($result != 0) {
+ unlink($tempfile);
+ exit($result);
+ }
+ };
+
+ local($dep_contents)='';
+ local($deps)='';
+ open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
+
+ while (<$fhandle>) {
+ chop;
+ $dep_contents .= $_;
+ }
+ ($deps = $dep_contents) =~ s|^[^:]+:(.*)$|$1|g;
+ $deps =~ s| \\| |g;
+
+ @Deps = split(/ +/, $deps);
+
+ $depend = "$baseName.$Obj_suffix";
+ foreach $suff (@File_suffix) {
+ $depend .= " $baseName.${suff}_$Obj_suffix";
}
- line: while (<$fhandle>) {
- next line if ! /^#/;
- next line if /^#(ident|pragma)/;
- chop; # rm trailing newline
-
- $_ = &tidy_dir_names($_);
-
- # strip junk off the front and back
- $_ =~ s/^#\s+\d+\s+//;
- $_ =~ s/[ 0-9]*$//;
-
- # a little bit of ad-hoc fiddling now:
- # don't bother w/ dependencies on /usr/include stuff
- # don't bother if it looks like a GCC built-in hdr file
- # don't bother with funny yacc-ish files
- # don't bother with "literate" .h files (.lh); we'll just
- # depend on the de-litified versions (which have better info)
- # don't let a file depend on itself
- next line if /^\/usr\/include/;
- next line if /\/gcc-lib\/[^\/\n]+\/[\.0-9]+\/include\//;
- next line if /\/gnu\/[^-\/]+-[^-\/]+-[^-\/]+\/include\//;
- next line if /\/yaccpar/;
- next line if /\/bison\.(simple|hairy)/;
- next line if /\.lh$/;
- next line if $_ eq $fname;
-
- print STDERR "$fname :: $_\n" if $Verbose;
-
- # ToDo: some sanity checks that we still have something reasonable?
-
- $depend = "$of : $_\n";
- next line if $Depend_seen{$depend}; # already seen this one...
-
- # OK, it's a new one.
- push (@Depend_lines, $depend);
- $Depend_seen{$depend} = 1;
+
+ foreach $dep (@Deps) {
+ push(@Depend_lines, "$depend: $dep\n") if $dep ne '';
}
+
close($fhandle);
+ unlink($tempfile);
+ $tempfile = ''; # for quit_upon_signal
}
sub tidy_dir_names { # rm various pernicious dir-name combinations...