[project @ 1997-05-18 04:58:22 by sof]
[ghc-hetmet.git] / ghc / utils / mkdependHS / mkdependHS.prl
index c216394..d0e2baa 100644 (file)
@@ -1,8 +1,16 @@
-# *** MSUB does some substitutions here ***
-# *** grep for $( ***
 #
-# tries to work like mkdependC
+# perl script expect the following variables to be prepended:
 #
+#  RAWCPP TMPDIR TOP_PWD
+#  libdir datadir INSTALLING
+#  PROJECTVERSION SED 
+#
+# tries to work like mkdependC - capable of dealing with:
+#
+#  -literate Haskell code (Bird tracks or Glasgow literate style) (.lhs)
+#  -straight Haskell source (.hs)
+#  -literate or straight Happy specs    (.ly)
+#  
 # ToDo: strip out all the .h junk
 #
 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
@@ -21,7 +29,6 @@ Options recognised wherever they occur (mkdependHS or GHC):
                "import"ing a module from that library).
     -fhaskell1.[2-9] Deal with the oddities associated with a
                particular version of Haskell 1.
-    -ignore <mod>
 
 mkdependHS-specific options (not between --'s):
 
@@ -34,8 +41,19 @@ mkdependHS-specific options (not between --'s):
                suffix <suf><osuf>; thus, "-o .hc -s _a" will
                make dependencies both for .hc files and for _a.hc
                files.  (Useful in conjunction with NoFib "ways".)
-    -x <file>   Regard <file> as "stable"; i.e., eXclude it from having
+    --exclude-module=<file> 
+                Regard <file> as "stable"; i.e., eXclude it from having
                dependencies on it.
+    -x          same as --exclude-module
+    --exclude-directory=<dirs> 
+                Regard : separated list of directories as containing stable,
+                don't generate any dependencies on modules therein.
+    -Xdirs      same as --exclude-directory
+    --include-module=<file> 
+                Regard <file> as not "stable"; i.e., generate dependencies
+                on it (if any). This option is normally used in conjunction 
+                with the --exclude-directory option.
+
 EOUSAGE
 
 $Status  = 0; # just used for exit() status
@@ -44,7 +62,7 @@ $Dashdashes_seen = 0;
 
 # Try to guess how to run gcc's CPP directly -------------
 
-$OrigCpp = '$(RAWCPP)';
+$OrigCpp = ${RAWCPP};
 if ( $OrigCpp !~ /(\S+)\s+(.*)/ ) {
     $Cpp = $OrigCpp;
 } else {
@@ -65,111 +83,50 @@ if ( $OrigCpp !~ /(\S+)\s+(.*)/ ) {
 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
     $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependHS$$";
 } else {
-    $Tmp_prefix ="$(TMPDIR)/mkdependHS$$";
-    $ENV{'TMPDIR'} = '$(TMPDIR)'; # set the env var as well
+    $Tmp_prefix ="${TMPDIR}/mkdependHS$$";
+    $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
 }
 
-#------------------------------------------------------------------------
-# If you are adjusting paths by hand for a binary GHC distribution,
-# de-commenting the line to set GLASGOW_HASKELL_ROOT should do.
-# Or you can leave it as is, and set the environment variable externally.
-#------------------------------------------------------------------------
-# $ENV{'GLASGOW_HASKELL_ROOT'} = '/some/absolute/path/name';
-
-if (! $ENV{'GLASGOW_HASKELL_ROOT'}) { # good -- death to environment variables
-    $TopPwd        = '$(TOP_PWD)';
-    $InstLibDirGhc  = '$(INSTLIBDIR_GHC)';
-    $InstDataDirGhc = '$(INSTDATADIR_GHC)';
-} else {
-    $TopPwd = $ENV{'GLASGOW_HASKELL_ROOT'};
-
-    if ( '$(INSTLIBDIR_GHC)' =~ /\/local\/fp(\/.*)/ ) {
-       $InstLibDirGhc  = $ENV{'GLASGOW_HASKELL_ROOT'} . $1;
-    } else {
-       print STDERR "GLASGOW_HASKELL_ROOT environment variable is set;\nBut can't untangle $(INSTLIBDIR_GHC).\n(Installation error)\n";
-       exit(1);
-    }
-
-    if ( '$(INSTDATADIR_GHC)' =~ /\/local\/fp(\/.*)/ ) {
-       $InstDataDirGhc = $ENV{'GLASGOW_HASKELL_ROOT'} . $1;
-    } else {
-       print STDERR "GLASGOW_HASKELL_ROOT environment variable is set;\nBut can't untangle $(INSTDATADIR_GHC).\n(Installation error)\n";
-       exit(1);
-    }
-}
+$TopPwd                 = "${TOP_PWD}";
+$InstLibDirGhc   = "${libdir}";
+$InstHsLibDirGhc = "${libdir}/hslibs";
+$InstDataDirGhc  = "${datadir}";
 
-$Unlit = ( $(INSTALLING) ) ? "$InstLibDirGhc/unlit"
-                            : "$TopPwd/$(CURRENT_DIR)/$(GHC_UNLIT)";
+$Unlit = ($INSTALLING) ? "${InstLibDirGhc}/unlit" : "${TopPwd}/ghc/utils/unlit/unlit";
 
 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
-$Obj_suffix = '.o';
-$ghc_version_info = $(PROJECTVERSION) * 100;
+$Obj_suffix = 'o';
+$ghc_version_info = int ( ${PROJECTVERSION} * 100 );
 
 $Import_dirs = '.';
 %Syslibs = ();
+%LibIfaces  = ();  # known prelude/syslib ifaces; read from a file
+
 %IgnoreMe = ();
-%PreludeIfaces = ( 'Prelude',      '1',
-                , 'Array',         '1'
-                , 'Char',          '1'
-                , 'Complex',       '1'
-                , 'Directory',     '1'
-                , 'IO',            '1'
-                , 'Ix',            '1'
-                , 'List',          '1'
-                , 'Maybe',         '1'
-                , 'Monad',         '1'
-                , 'Ratio',         '1'
-                , 'System',        '1'
-                , 'PreludeGlaST',  '1'
-                , 'PreludeGlaMisc','1'
-                , 'Concurrent',    '1'
-                , 'Parallel',      '1');
-%GhcLibIfaces = (  'Bag', '1',
-                  'BitSet', '1',
-                  # CharSeq not supposed to be used by user (I think. WDP)
-                  'FiniteMap', '1',
-                  'ListSetOps', '1',
-                  'Maybes', '1',
-                  'PackedString', '1',
-                  'Regex', '1',
-                  'MatchPS', '1',
-                  'Readline', '1',
-                  'Socket', '1',
-                  'SocketPrim', '1',
-                  'BSD', '1',
-                  'Pretty', '1',
-                  'Set', '1',
-                  'Util', '1' );
-%HbcLibIfaces = (  'Algebra', '1',
-                  'Hash', '1',
-                  'ListUtil', '1',
-                  'Miranda', '1',
-                  'NameSupply', '1',
-                  'Native', '1',
-                  'Number', '1',
-                  'Parse', '1',
-                  'Pretty', '1',
-                  'Printf', '1',
-                  'QSort', '1',
-                  'Random', '1',
-                  'SimpleLex', '1',
-                  'Time', '1',
-                  'Trace', '1',
-                  'Word', '1' );
-%IO13Ifaces = (    'LibSystem', '1',
-                  'LibCPUTime', '1',
-                  'LibDirectory', '1',
-                  'LibPosix', '1',
-                  'LibTime', '1' );
-
-$Haskell_1 = 2; # assume Haskell 1.2, still. Changed by -fhaskell-1.3
+# directories to considered stable.
+@Ignore_dirs = ($INSTALLING) ? 
+                   ("$InstLibDirGhc/imports")
+                : ("$TopPwd/ghc/lib/ghc",
+                   "$TopPwd/ghc/lib/required",
+                   "$TopPwd/ghc/lib/glaExts",
+                   "$TopPwd/ghc/lib/concurrent");
+
+$Haskell_1 = 3; # assume Haskell 1.3. Changed by -fhaskell-1.?
 $Include_dirs = '-I.';
 $Makefile = '';
 @Src_files = ();
 
 &mangle_command_line_args();
 
+# set up array of ignored modules
+foreach $lib ( @Syslibs ) {
+    local($dir) = 
+      ($INSTALLING) ? "${InstHsLibDirGhc}/${lib}/imports" 
+                   : "${TopPwd}/hslibs/${lib}/src";
+    push(@Ignore_dirs,$dir);
+}
+
 if ( $Status ) {
     print stderr $Usage;
     exit(1);
@@ -205,14 +162,15 @@ 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;
-    ($bf = $sf) =~ s/\.l?hs$//;
-    push(@Depend_lines, "$bf$Obj_suffix : $sf\n");
+    ($bf = $sf) =~ s/\.l?(hs|y)$//;
+    #push(@Depend_lines, "$bf.$Obj_suffix $bf.hi : $sf\n");
+    push(@Depend_lines, "$bf.$Obj_suffix : $sf\n");
     foreach $suff (@File_suffix) {
-        push(@Depend_lines, "$bf$suff$Obj_suffix : $sf\n");
+        push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $sf\n");
     }
 
-    # if it's a literate file, .lhs, then we de-literatize it:
-    if ( $sf !~ /\.lhs$/ ) {
+    # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it: 
+    if ( $sf !~ /\.l(hs|y)$/ ) {
        $file_to_read = $sf;
     } else {
        $file_to_read = "$Tmp_prefix.hs";
@@ -221,14 +179,23 @@ foreach $sf (@Src_files) {
     }
     &slurp_file_for_imports($file_to_read, $sf);
 
-    if ( $sf =~ /\.lhs$/ ) {
+    if ( $sf =~ /\.l(hs|y)$/ ) {
        unlink "$Tmp_prefix.hs";
     }
 }
 
 # OK, mangle the Makefile
 unlink("$Makefile.bak");
-rename($Makefile,"$Makefile.bak");
+
+#
+# If no output file exist as yet, create one.
+# 
+if ( ! -f $Makefile.bak ) {
+   system("touch $Makefile.bak");
+} 
+if ( -f $Makefile ) {
+   rename($Makefile,"$Makefile.bak");
+}
 # now copy Makefile.bak into Makefile, rm'ing old dependencies
 # and adding the new
 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
@@ -277,9 +244,19 @@ sub mangle_command_line_args {
                $Makefile       = &grab_arg_arg('-f',$1);
            } elsif ( /^-o(.*)/ ) {
                $Obj_suffix     = &grab_arg_arg('-o',$1);
-           } elsif ( /^-x(.*)/ ) { 
-               local($thing) = &grab_arg_arg($_,$1);
+            #
+           # --exclude-module=mod => it's stable, trust me!
+            
+           } elsif ( /^-(x|-exclude-module=)(.*)/ ) { 
+               local($thing) = &grab_arg_arg($1,$2);
                $IgnoreMe{$thing} = 'y';
+           } elsif ( /^-(X|-exclude-directory=)(.*)/ ) { 
+               foreach $d ( split(/:/,&grab_arg_arg($1, $2)) ) {
+                   push(@Ignore_dirs,"$d");
+                }
+           } elsif ( /^--include-module=(.*)/ ) { 
+               local($thing) = &grab_arg_arg($1,$2);
+               $IgnoreMe{$thing} = 'n';
            } elsif ( /^-s(.*)/ ) {
                local($suff)    =  &grab_arg_arg('-s',$1);
                push(@File_suffix, $suff);
@@ -319,6 +296,10 @@ sub preprocess_import_dirs {
     %ModuleIn = ();
 
     foreach $d ( @Import_dirs ) {
+        # Check to see if it can be ignored
+        #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
+        #next if $Ignore_dirs{$d};
+
        opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
 
        for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
@@ -332,6 +313,23 @@ sub preprocess_import_dirs {
        }
        closedir(DIR); # No, don't check the error code
     }
+
+    # Add all the modules
+    # to the IgnoreMe array before we start scanning for imports.
+    foreach $d (@Ignore_dirs) {
+
+       opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
+
+        for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
+            next unless /(.*)\.(hi|l?hs|l?y)$/;
+            #don't tag it twice or overwrite it with a diff. value
+            next if $IgnoreMe{$1};
+            print STDERR "Module $d/$1.$2 will be ignored\n" if $Verbose;
+
+            $IgnoreMe{$1} = 'y';
+        }
+       closedir(DIR); # No, don't check the error code
+    }
 }
 
 sub slurp_file_for_imports {
@@ -344,33 +342,34 @@ sub slurp_file_for_imports {
 
     # we mangle #include's so they will also leave something
     # behind to indicate the dependency on _them_
+    
+    print STDERR "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |\n" if $Verbose;
 
-    print STDERR "/usr/bin/sed -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |\n" if $Verbose;
-
-    open(SRCFILE, "/usr/bin/sed -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |")
+    open(SRCFILE, "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |")
        || die "$Pgm: Can't open $file_to_read: $!\n";
 
     while (<SRCFILE>) {
-       next unless (/^>?\s*(import)\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)\s+"(\S+)"/);
+        #
+        # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
+        # the compiler will deal with the absence of Foo.hi by consulting the
+        # source for Foo directly. (for dealing with recursive modules).
+        #
+       next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
        $todo    = $1;
-       $modname = $2;
+        $source  = ( $2 ne '') ? 1 : 0;
+       $modname = $4;
 
+        next if $modname eq '';
        if ($todo eq 'import') {
-           if ( $IgnoreMe{$modname} eq 'y' ) {
-               $follow_file = '__ignore__';
-           } elsif ( $ModuleIn{$modname} ) {
-               $follow_file = "$ModuleIn{$modname}/$modname.hi";
+           if ( $ModuleIn{$modname} ) {
+                  $follow_file = "$ModuleIn{$modname}/$modname.hi";
            } else { # hard way
                $follow_file
-                 = &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir);
+                 = &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir );
            }
        } else {
-           if ( $IgnoreMe{$modname} eq 'y' ) {
-               $follow_file = '__ignore__';
-           } else {
                $follow_file
                  = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
-           }
        }
 
        if (! $follow_file) { # it didnae find anything
@@ -381,20 +380,43 @@ sub slurp_file_for_imports {
                local($int_file);
                $int_file = $follow_file;
 
-               if ( $int_file !~ /\.(l?hs|hi)$/ ) {
-                   push(@Depend_lines, "$bf$Obj_suffix : $int_file\n");
+               if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
+                   push(@Depend_lines, "$bf.$Obj_suffix : $int_file\n");
                    foreach $suff (@File_suffix) {
-                       push(@Depend_lines, "$bf$suff$Obj_suffix : $int_file\n");
+                       push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $int_file\n");
                    }
 
                } else {
                    $int_file =~ s/\.l?hs$//;
+                   $int_file =~ s/\.l?y$//;
                    $int_file =~ s/\.hi$//;
-
-                   push(@Depend_lines, "$bf$Obj_suffix : $int_file.hi\n");
-                   foreach $suff (@File_suffix) {
-                       push(@Depend_lines, "$bf$suff$Obj_suffix : $int_file$suff.hi\n");
-                   }
+                    local($source_dep);
+
+                    if ( $source && -f "$int_file.hs" ) {
+                        $source_dep = "$int_file.hs";
+                       push(@Depend_lines, "$bf.$Obj_suffix : $source_dep\n");
+                    } elsif ( $source && -f "$int_file.lhs" ) {
+                        $source_dep = "$int_file.lhs";
+                       push(@Depend_lines, "$bf.$Obj_suffix : $source_dep\n");
+                    } elsif ( $source && ( -f "$int_file.ly" || -f "$int_file.y" ) ) {
+                        $source_dep = "$int_file.hs";
+                       push(@Depend_lines, "$bf.$Obj_suffix : $source_dep\n");
+                    } else {
+                        if ( $source ) {
+                           print STDERR "Warning: could not find source file dependency $int_file.(hs|lhs|y|ly)\n";
+                        }
+                       push(@Depend_lines, "$bf.$Obj_suffix : $int_file.hi\n");
+                   }
+
+                    if ( ! $source ) {
+                      foreach $suff (@File_suffix) {
+                         push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $int_file.${suff}_hi\n");
+                      }
+                    } else {
+                      foreach $suff (@File_suffix) {
+                         push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $source_dep\n");
+                      }
+                   }
                }
            }
        }
@@ -407,7 +429,7 @@ sub slurp_file_for_imports {
 %FileExists = ();
 
 sub find_in_Import_dirs {
-    local($orig_src_file, $modname, $last_seen_dir) = @_;
+    local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
     local($import_dir);
     local($do_magical_check) = 0;
     local($name_to_check);
@@ -432,13 +454,11 @@ sub find_in_Import_dirs {
            }
        }
 
-       $name_to_check = "$import_dir/$modname.hs";
-       print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
-       return($name_to_check) if -f $name_to_check;
-
-       $name_to_check = "$import_dir/$modname.lhs";
-       print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
-       return($name_to_check) if -f $name_to_check;
+       for $suff ('hs', 'lhs', 'ly', 'y') {
+           $name_to_check = "$import_dir/$modname.$suff";
+           print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
+           return($name_to_check) if -f $name_to_check;
+        }
     }
     if ($do_magical_check == 1) {
        $name_to_check = "$last_seen_dir/$modname.hi";
@@ -454,34 +474,22 @@ sub find_in_Import_dirs {
            }
        }
 
-       $name_to_check = "$last_seen_dir/$modname.lhs";
-       print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
-       return($name_to_check) if -f $name_to_check;
-
-       $name_to_check = "$last_seen_dir/$modname.hs";
-       print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
-       return($name_to_check) if -f $name_to_check;
-    }
-    # OK, maybe it's referring to something in a system library
-    foreach $lib ( @Syslibs ) {
-       if ( $lib eq 'ghc' ) {
-           return('__ignore__') if $GhcLibIfaces{$modname};
-       } elsif ( $lib eq 'hbc' ) {
-           return('__ignore__') if $HbcLibIfaces{$modname};
-       } else {
-           die "Unrecognised syslib: $lib\n";
-       }
+       for $suff ('lhs', 'hs', 'ly', 'y') {
+           $name_to_check = "$last_seen_dir/$modname.$suff";
+           print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
+           return($name_to_check) if -f $name_to_check;
+        }
     }
 
-    # Might be a Haskell 1.3 Module (but only if we've said -fhaskell-1.3)
-    if ( $Haskell_1 >= 3 ) {
-       return('__ignore__') if $IO13Ifaces{$modname};
-    }
+    # OK, maybe it's referring to something in a system library
+    #foreach $lib ( @Syslibs ) {
+    #  return('__ignore__') if $LibIfaces{"$lib:$modname"};
+    #}
 
     # Last hope: referring to a Prelude interface
-    return('__ignore__') if $PreludeIfaces{$modname};
+    return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
 
-    die "No file `$modname.hi', `$modname.lhs' or `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
+    die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
 }
 
 sub find_in_Include_dirs {