# # perl script expect the following variables to be prepended: # # RAWCPP TMPDIR TOP_PWD # libdir libexecdir 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/; $Usage = < A cpp #define; usual meaning -i Add (colon-separated) to list of directories to search for "import"ed modules -I Add to list of directories to search for .h files (i.e., usual meaning) -syslib This program uses this GHC system library; take appropriate action (e.g., recognise when they are "import"ing a module from that library). mkdependHS-specific options (not between --'s): -v Be verbose. -v -v Be very verbose. -w No warnings -f blah Use "blah" as the makefile, rather than "makefile" or "Makefile". -o Use as the "object file" suffix ( default: o) -s Make extra dependencies for files with suffix ; thus, "-o hc -s _a" will make dependencies both for .hc files and for .a_hc files. (Useful in conjunction with NoFib "ways".) --exclude-module= Regard as "stable"; i.e., eXclude it from having dependencies on it. -x same as --exclude-module --exclude-directory= Regard : separated list of directories as containing stable, don't generate any dependencies on modules therein. -Xdirs same as --exclude-directory --include-prelude Regard prelude library as unstable, i.e., generate dependencies on prelude modules. This option is normally only used by the various system libraries. If a -syslib option is used, dependencies will also be generated on the library's interfaces. --include-module= Regard 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 $Verbose = 0; # 1 => verbose, 2 => very verbose $Warnings = 1; # 1 => warn about duplicate interface files $Dashdashes_seen = 0; $Cpp = ${RAWCPP}; 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 } $TopPwd = "${TOP_PWD}"; $InstLibDirGhc = "${libdir}"; $InstLibExecDirGhc = "${libexecdir}"; $InstHsLibDirGhc = "${libdir}/hslibs"; $InstDataDirGhc = "${datadir}"; $Unlit = ($INSTALLING) ? "${InstLibExecDirGhc}/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"); @File_suffix = (); $ghc_version_info = int ( ${PROJECTVERSION} * 100 ); $Import_dirs = '.'; %Syslibs = (); %LibIfaces = (); # known prelude/syslib ifaces; read from a file %IgnoreMe = (); # directories to considered stable. @Ignore_dirs = (); $Include_dirs = '-I.'; $Makefile = ''; @Src_files = (); $Include_prelude = 0; &mangle_command_line_args(); if ( $Status ) { print stderr $Usage; exit(1); } push(@Defines, ( #OLD: "-D__HASKELL1__=$Haskell_1", "-D__GLASGOW_HASKELL__=$ghc_version_info")); @Import_dirs = split(/:/,$Import_dirs); @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it # set up array of ignored modules local(@dirs) = ($INSTALLING) ? ("$InstLibDirGhc/imports") : ("$TopPwd/ghc/lib/ghc", "$TopPwd/ghc/lib/required", "$TopPwd/ghc/lib/glaExts", "$TopPwd/ghc/lib/concurrent"); if (!$Include_prelude) { push(@Ignore_dirs, @dirs); } else { push(@Import_dirs, @dirs); } foreach $lib ( @Syslibs ) { local($dir) = ($INSTALLING) ? "${InstHsLibDirGhc}/${lib}/imports" : "${TopPwd}/hslibs/${lib}/src"; if (!$Include_prelude) { push(@Ignore_dirs,$dir); } else { push(@Import_dirs, @dirs); } } # NB: We keep the scalar-variable equivalents to use in error messages if ( ! $Makefile && -f 'makefile' ) { $Makefile = 'makefile'; } elsif ( ! $Makefile && -f 'Makefile') { $Makefile = 'Makefile'; } elsif ( ! $Makefile) { die "$Pgm: no makefile or Makefile found\n"; } print STDERR "CPP defines=@Defines\n" if $Verbose; print STDERR "Import_dirs=@Import_dirs\n" if $Verbose; print STDERR "Include_dirs=@Include_dirs\n" if $Verbose; &preprocess_import_dirs(); @Depend_lines = (); # Delete temp. file if script is halted. sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; } $SIG{'INT'} = 'quit_upon_signal'; $SIG{'QUIT'} = 'quit_upon_signal'; foreach $sf (@Src_files) { # just like lit-inputter # except it puts each file through CPP and # 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|y)$//; local($str)=""; foreach $obj (@Obj_suffix) { $str .= "$bf.$obj "; foreach $suff (@File_suffix) { $str .= "$bf.${suff}_$obj "; } } push(@Depend_lines, "$str: $sf\n"); # 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"; local($to_do) = "$Unlit $sf $file_to_read"; &run_something($to_do, 'unlit'); } &slurp_file_for_imports($file_to_read, $sf); if ( $sf =~ /\.l(hs|y)$/ ) { unlink "$Tmp_prefix.hs"; } } # OK, mangle the Makefile unlink("$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"; open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n"; select(NMKF); $_ = ; while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str print $_; $_ = ; } while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str $_ = ; } # insert dependencies print $Begin_magic_str, @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)"; while () { # copy the rest through print $_ || die "Failed to write out dependencies ($!)"; } close(NMKF) || exit(1); close(OMKF) || exit(1); exit 0; sub mangle_command_line_args { while($_ = $ARGV[0]) { shift(@ARGV); if ( /^--$/ ) { $Dashdashes_seen++; } elsif ( /^-D(.*)/ ) { # recognized wherever they occur push(@Defines, $_); } elsif ( /^-i(.*)/ ) { # ditto $Import_dirs .= ":$1"; } elsif ( /^-I/ ) { $Include_dirs .= " $_"; } elsif ( /^-syslib$/ ) { push(@Syslibs, &grab_arg_arg($_,'')); } elsif ($Dashdashes_seen != 1) { # not between -- ... -- if ( /^-v$/ ) { $Verbose++; } elsif ( /^-w$/ ) { $Warnings = 0; } elsif ( /^-f(.*)/ ) { $Makefile = &grab_arg_arg('-f',$1); } elsif ( /^-o(.*)/ ) { local($suff) = &grab_arg_arg('-o',$1); # Weed out existing entry .. there must be a better way of doing this # with arrays (non-assoc) ! -- sof @Obj_suffix = grep(!/$suff/,@Obj_suffix); push(@Obj_suffix, $suff); # # --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 ( /^--include-prelude$/ ) { $Include_prelude = 1; } elsif ( /^-s(.*)/ ) { local($suff) = &grab_arg_arg('-s',$1); push(@File_suffix, $suff); } elsif ( /^-/ ) { print STDERR "$Pgm: unknown option ignored: $_\n"; $Status++; } else { push(@Src_files, $_); } } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options push(@Src_files, $_) if ! /^-/; } } @File_suffix = sort (@File_suffix); } sub grab_arg_arg { local($option, $rest_of_arg) = @_; if ($rest_of_arg) { return($rest_of_arg); } elsif ($#ARGV >= 0) { local($temp) = $ARGV[0]; shift(@ARGV); return($temp); } else { print STDERR "$Pgm: no argument following $option option\n"; $Status++; } } sub preprocess_import_dirs { # it's probably cheaper to find out what's in all # the @Import_dirs before we start processing. local($d, $thing); local($_); %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)) { next unless /(.*)\.hi$/; $thing = $1; if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) { print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings); } else { $ModuleIn{$thing} = $d; } } 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 { local($file_to_read, $orig_src_file) = @_; local($follow_file); local($last_seen_dir) = $orig_src_file; $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file); &process_dependency('import',0,'Prelude') if ($Include_prelude); # 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; 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 () { # # 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; $source = ( $2 ne '') ? 1 : 0; $modname = $4; next if $modname eq ''; &process_dependency($todo,$source,$modname); } close(SRCFILE) || exit(1); } # # Handle sub process_dependency { local($todo,$source,$modname) = @_; if ($todo eq 'import') { 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, $source ); } } else { $follow_file = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir); } if (! $follow_file) { # it didnae find anything die "$orig_src_file: Couldn't handle: $_\n"; } else { # it found something if ($follow_file ne '__ignore__') { local($int_file) = $follow_file; if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) { local($str) = ""; foreach $obj (@Obj_suffix) { $str .= "$bf.$obj "; foreach $suff (@File_suffix) { $str .= "$bf.${suff}_$obj "; } } push(@Depend_lines, "$str: $int_file\n"); } else { $int_file =~ s/\.l?hs$//; $int_file =~ s/\.l?y$//; $int_file =~ s/\.hi$//; local($source_dep); if ( $source ) { $source_dep = "$int_file.hi-boot"; } else { local($str)=""; foreach $obj (@Obj_suffix) { $str .= "$bf.$obj "; } push(@Depend_lines, "$str: $int_file.hi\n"); } if ( ! $source ) { foreach $suff (@File_suffix) { local($str) = ""; foreach $obj (@Obj_suffix) { $str .= "$bf.${suff}_$obj "; } push(@Depend_lines, "$str: $int_file.${suff}_hi\n"); } } else { local($str) = ""; foreach $obj (@Obj_suffix) { $str .= "$bf.$obj "; foreach $suff (@File_suffix) { $str .= "$bf.${suff}_$obj "; } } push(@Depend_lines, "$str: $source_dep\n"); } } } } } # when we see something, we cache that fact ('y'). # also, when we get a miss, we cache that (so we don't try later); ('n') %FileExists = (); sub find_in_Import_dirs { local($orig_src_file, $modname, $last_seen_dir, $source) = @_; local($import_dir); local($do_magical_check) = 0; local($name_to_check); # do it the old hard way: hop along Import_dir list foreach $import_dir (@Import_dirs) { # handle . magically if ($import_dir eq '.') { # record that we should do a SPECIAL try for a file in last_seen_dir (LAST) $do_magical_check = 1; } $name_to_check = "$import_dir/$modname.hi"; if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose return($name_to_check) if $FileExists{$name_to_check} eq 'y'; if (-f $name_to_check) { $FileExists{$name_to_check} = 'y'; return($name_to_check) ; } else { $FileExists{$name_to_check} = 'n'; } } 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 ( $source ) { $name_to_check = "$import_dir/$modname.hi-boot"; 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"; if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose return($name_to_check) if $FileExists{$name_to_check} eq 'y'; if (-f $name_to_check) { $FileExists{$name_to_check} = 'y'; return($name_to_check) ; } else { $FileExists{$name_to_check} = '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; } } # 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 ( $IgnoreMe{$modname} eq 'y' ); 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 { local($orig_src_file, $name, $last_seen_dir) = @_; local($include_dir); local($do_magical_check) = 0; # no funny name guessing here # hop along Include_dir list foreach $include_dir (@Include_dirs) { $include_dir =~ s/^-I//; # handle . magically if ($include_dir eq '.') { # record that we should do a SPECIAL try for a file in last_seen_dir (LAST) $do_magical_check = 1; } print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose if (-f "$include_dir/$name") { return("$include_dir/$name"); } } if ($do_magical_check == 1) { print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose if (-f "$last_seen_dir/$name") { return("$last_seen_dir/$name"); } } die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n"; } # out of the driver, actually sub run_something { local($str_to_do, $tidy_name) = @_; print STDERR "\n$tidy_name:\n\t" if $Verbose; print STDERR "$str_to_do\n" if $Verbose; local($return_val) = system($str_to_do) >> 8; if ($return_val != 0) { local($die_msg) = "$Pgm: execution of the $tidy_name had trouble"; $die_msg .= " (program not found)" if $return_val == 255; $die_msg .= " ($!)" if $Verbose && $! != 0; $die_msg .= "\n"; print STDERR $die_msg; exit $return_val; } }