[project @ 1998-10-08 15:11:10 by simonm]
[ghc-hetmet.git] / glafp-utils / mkdependC / mkdependC.prl
index be3d42f..7c524bd 100644 (file)
@@ -2,10 +2,7 @@
 # This stub of perl assumes that the following two
 # variables are prepended:
 #
-#  RAWCPP
-#  HOSTPLATFORM
-#
-# tries to work like mkdependC
+# TMPDIR CPP
 #
 # ToDo: strip out all the .h junk
 #
@@ -18,29 +15,30 @@ $Dashdashes_seen = 0;
 
 $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 = ();
 
-# 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 ( $HostPlatform =~ /linux/ ) {
-    push(@Defines, '-D__linux__');
-}
-if ( $HostPlatform =~ /^sparc-/ ) {
-    push(@Defines, '-D__sparc__');
+if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
+    $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependC$$";
+} else {
+    $Tmp_prefix ="${TMPDIR}/mkdependC$$";
+    $ENV{'TMPDIR'} = "${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();
 
@@ -55,7 +53,6 @@ if ( ! $Makefile && -f 'makefile' ) {
 @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) {
@@ -64,7 +61,7 @@ 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/;
+    ($bf = $sf) =~ s/\.(c|hc)$//;
 
     &slurp_file($sf, 'fh00');
 }
@@ -115,6 +112,9 @@ sub mangle_command_line_args {
                $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/ ) {
@@ -156,9 +156,17 @@ sub slurp_file { # follows an example in the `open' item in perl man page
 
     $fname = &tidy_dir_names($fname);
 
-    unless (open($fhandle, "${RAWCPP} $Include_dirs @Defines $fname |")) {
-         die "$Pgm: Can't open $fname: $!\n";
-    }
+    $tempfile = "$Tmp_prefix.i";
+
+    # ${CPP} better be 'gcc -E', or the -x option will fail...
+    $result = system("${CPP} $Include_dirs @Defines -x c $fname -o $tempfile");
+    if ($result != 0) {
+       unlink($tempfile);
+       exit($result);
+    };
+
+    open($fhandle, $tempfile) || die "$Pgm: Can't open $tempfile: $!\n";
+
     line: while (<$fhandle>) {
        next line if ! /^#/;
        next line if /^#(ident|pragma)/;
@@ -174,32 +182,37 @@ sub slurp_file { # follows an example in the `open' item in perl man page
        # 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/;
-       # Hack - the cygwin32 dir structure is odd!
+       # Hack - the cygwin32 dir structupre is odd!
        next line if /H-i386-cygwin32\/i386-cygwin32/;
        next line if /H-i386-cygwin32\/lib\/gcc-lib\/i386-cygwin32/;
        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...
+       $int_file = $_;
+       $depend = "$bf.$Obj_suffix ";
+       foreach $suff (@File_suffix) {
+          $depend .= "$bf.${suff}_$Obj_suffix ";
+        }
+       $depend .= " : $int_file\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;
+
+       push (@Depend_lines, $depend);
     }
     close($fhandle);
+    unlink($tempfile);
+    $tempfile = '';  # for quit_upon_signal
 }
 
 sub tidy_dir_names { # rm various pernicious dir-name combinations...