2 # perl script expect the following variables to be prepended:
4 # RAWCPP TMPDIR TOP_PWD
5 # libdir libexecdir datadir INSTALLING
8 # tries to work like mkdependC - capable of dealing with:
10 # -literate Haskell code (Bird tracks or Glasgow literate style) (.lhs)
11 # -straight Haskell source (.hs)
12 # -literate or straight Happy specs (.ly)
14 # ToDo: strip out all the .h junk
16 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
18 Usage: $Pgm [mkdependHS options] [-- GHC options --] srcfile1 [srcfile2 ...]
20 Options recognised wherever they occur (mkdependHS or GHC):
22 -D<blah> A cpp #define; usual meaning
23 -i<dirs> Add <dirs> (colon-separated) to list of directories
24 to search for "import"ed modules
25 -I<dir> Add <dir> to list of directories to search for
26 .h files (i.e., usual meaning)
27 -syslib <blah> This program uses this GHC system library; take
28 appropriate action (e.g., recognise when they are
29 "import"ing a module from that library).
31 mkdependHS-specific options (not between --'s):
34 -v -v Be very verbose.
35 -f blah Use "blah" as the makefile, rather than "makefile"
37 -o <osuf> Use <osuf> as the "object file" suffix ( default: o)
38 -s <suf> Make extra dependencies for files with
39 suffix <suf><osuf>; thus, "-o hc -s _a" will
40 make dependencies both for .hc files and for .a_hc
41 files. (Useful in conjunction with NoFib "ways".)
42 --exclude-module=<file>
43 Regard <file> as "stable"; i.e., eXclude it from having
45 -x same as --exclude-module
46 --exclude-directory=<dirs>
47 Regard : separated list of directories as containing stable,
48 don't generate any dependencies on modules therein.
49 -Xdirs same as --exclude-directory
50 --include-module=<file>
51 Regard <file> as not "stable"; i.e., generate dependencies
52 on it (if any). This option is normally used in conjunction
53 with the --exclude-directory option.
57 $Status = 0; # just used for exit() status
58 $Verbose = 0; # 1 => verbose, 2 => very verbose
61 # Try to guess how to run gcc's CPP directly -------------
64 if ( $OrigCpp !~ /(\S+)\s+(.*)/ ) {
69 if ( -x $cmd ) { # cool
71 } else { # oops; try to guess
72 $GccV = `gcc -v 2>&1`;
73 if ( $GccV =~ /Reading specs from (.*)\/specs/ ) {
74 $Cpp = "$1/cpp $rest";
76 die "hscpp: don't know how to run cpp: $OrigCpp\n";
81 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
82 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependHS$$";
84 $Tmp_prefix ="${TMPDIR}/mkdependHS$$";
85 $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
88 $TopPwd = "${TOP_PWD}";
89 $InstLibDirGhc = "${libdir}";
90 $InstLibExecDirGhc = "${libexecdir}";
91 $InstHsLibDirGhc = "${libdir}/hslibs";
92 $InstDataDirGhc = "${datadir}";
94 $Unlit = ($INSTALLING) ? "${InstLibExecDirGhc}/unlit"
95 : "${TopPwd}/ghc/utils/unlit/unlit";
97 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
98 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
101 $ghc_version_info = int ( ${PROJECTVERSION} * 100 );
105 %LibIfaces = (); # known prelude/syslib ifaces; read from a file
108 # directories to considered stable.
109 @Ignore_dirs = ($INSTALLING) ?
110 ("$InstLibDirGhc/imports")
111 : ("$TopPwd/ghc/lib/ghc",
112 "$TopPwd/ghc/lib/required",
113 "$TopPwd/ghc/lib/glaExts",
114 "$TopPwd/ghc/lib/concurrent");
116 $Include_dirs = '-I.';
120 &mangle_command_line_args();
122 # set up array of ignored modules
123 foreach $lib ( @Syslibs ) {
125 ($INSTALLING) ? "${InstHsLibDirGhc}/${lib}/imports"
126 : "${TopPwd}/hslibs/${lib}/src";
127 push(@Ignore_dirs,$dir);
136 ( #OLD: "-D__HASKELL1__=$Haskell_1",
137 "-D__GLASGOW_HASKELL__=$ghc_version_info"));
139 @Import_dirs = split(/:/,$Import_dirs);
140 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
141 # NB: We keep the scalar-variable equivalents to use in error messages
143 if ( ! $Makefile && -f 'makefile' ) {
144 $Makefile = 'makefile';
145 } elsif ( ! $Makefile && -f 'Makefile') {
146 $Makefile = 'Makefile';
147 } elsif ( ! $Makefile) {
148 die "$Pgm: no makefile or Makefile found\n";
151 print STDERR "CPP defines=@Defines\n" if $Verbose;
152 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
153 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
155 &preprocess_import_dirs();
159 foreach $sf (@Src_files) {
160 # just like lit-inputter
161 # except it puts each file through CPP and
162 # a de-commenter (not implemented);
163 # builds up @Depend_lines
164 print STDERR "Here we go for source file: $sf\n" if $Verbose;
165 ($bf = $sf) =~ s/\.l?(hs|y)$//;
166 #push(@Depend_lines, "$bf.$Obj_suffix $bf.hi : $sf\n");
167 #push(@Depend_lines, "$bf.$Obj_suffix : $sf\n");
169 foreach $obj (@Obj_suffix) {
171 foreach $suff (@File_suffix) {
172 $str .= "$bf.${suff}_$obj ";
175 push(@Depend_lines, "$str: $sf\n");
176 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
177 if ( $sf !~ /\.l(hs|y)$/ ) {
180 $file_to_read = "$Tmp_prefix.hs";
181 local($to_do) = "$Unlit $sf $file_to_read";
182 &run_something($to_do, 'unlit');
184 &slurp_file_for_imports($file_to_read, $sf);
186 if ( $sf =~ /\.l(hs|y)$/ ) {
187 unlink "$Tmp_prefix.hs";
191 # OK, mangle the Makefile
192 unlink("$Makefile.bak");
195 # If no output file exist as yet, create one.
197 if ( ! -f $Makefile.bak ) {
198 system("touch $Makefile.bak");
200 if ( -f $Makefile ) {
201 rename($Makefile,"$Makefile.bak");
203 # now copy Makefile.bak into Makefile, rm'ing old dependencies
205 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
206 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
209 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
213 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
216 # insert dependencies
217 print $Begin_magic_str;
219 print $End_magic_str;
220 while (<OMKF>) { # copy the rest through
223 close(NMKF) || exit(1);
224 close(OMKF) || exit(1);
227 sub mangle_command_line_args {
228 while($_ = $ARGV[0]) {
234 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
236 } elsif ( /^-i(.*)/ ) {
237 $Import_dirs .= ":$1";
239 $Include_dirs .= " $_";
240 } elsif ( /^-syslib$/ ) {
241 push(@Syslibs, &grab_arg_arg($_,''));
242 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
245 } elsif ( /^-f(.*)/ ) {
246 $Makefile = &grab_arg_arg('-f',$1);
247 } elsif ( /^-o(.*)/ ) {
248 local($suff) = &grab_arg_arg('-o',$1);
249 # Weed out existing entry .. there must be a better way of doing this
250 # with arrays (non-assoc) ! -- sof
251 @Obj_suffix = grep(!/$suff/,@Obj_suffix);
252 push(@Obj_suffix, $suff);
254 # --exclude-module=mod => it's stable, trust me!
256 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
257 local($thing) = &grab_arg_arg($1,$2);
258 $IgnoreMe{$thing} = 'y';
259 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
260 foreach $d ( split(/:/,&grab_arg_arg($1, $2)) ) {
261 push(@Ignore_dirs,"$d");
263 } elsif ( /^--include-module=(.*)/ ) {
264 local($thing) = &grab_arg_arg($1,$2);
265 $IgnoreMe{$thing} = 'n';
266 } elsif ( /^-s(.*)/ ) {
267 local($suff) = &grab_arg_arg('-s',$1);
268 push(@File_suffix, $suff);
270 print STDERR "$Pgm: unknown option ignored: $_\n";
273 push(@Src_files, $_);
276 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
277 push(@Src_files, $_) if ! /^-/;
280 @File_suffix = sort (@File_suffix);
284 local($option, $rest_of_arg) = @_;
287 return($rest_of_arg);
288 } elsif ($#ARGV >= 0) {
289 local($temp) = $ARGV[0]; shift(@ARGV);
292 print STDERR "$Pgm: no argument following $option option\n";
297 sub preprocess_import_dirs {
298 # it's probably cheaper to find out what's in all
299 # the @Import_dirs before we start processing.
304 foreach $d ( @Import_dirs ) {
305 # Check to see if it can be ignored
306 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
307 #next if $Ignore_dirs{$d};
309 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
311 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
312 next unless /(.*)\.hi$/;
314 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
315 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n";
317 $ModuleIn{$thing} = $d;
320 closedir(DIR); # No, don't check the error code
323 # Add all the modules
324 # to the IgnoreMe array before we start scanning for imports.
325 foreach $d (@Ignore_dirs) {
327 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
329 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
330 next unless /(.*)\.(hi|l?hs|l?y)$/;
331 #don't tag it twice or overwrite it with a diff. value
332 next if $IgnoreMe{$1};
333 print STDERR "Module $d/$1.$2 will be ignored\n" if $Verbose;
337 closedir(DIR); # No, don't check the error code
341 sub slurp_file_for_imports {
342 local($file_to_read, $orig_src_file) = @_;
345 local($last_seen_dir) = $orig_src_file;
346 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
347 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
349 # we mangle #include's so they will also leave something
350 # behind to indicate the dependency on _them_
352 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;
354 open(SRCFILE, "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |")
355 || die "$Pgm: Can't open $file_to_read: $!\n";
359 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
360 # the compiler will deal with the absence of Foo.hi by consulting the
361 # source for Foo directly. (for dealing with recursive modules).
363 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
365 $source = ( $2 ne '') ? 1 : 0;
368 next if $modname eq '';
369 if ($todo eq 'import') {
370 if ( $ModuleIn{$modname} ) {
371 $follow_file = "$ModuleIn{$modname}/$modname.hi";
374 = &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
378 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
381 if (! $follow_file) { # it didnae find anything
382 die "$orig_src_file: Couldn't handle: $_\n";
384 } else { # it found something
385 if ($follow_file ne '__ignore__') {
387 $int_file = $follow_file;
389 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
391 foreach $obj (@Obj_suffix) {
393 foreach $suff (@File_suffix) {
394 $str .= "$bf.${suff}_$obj ";
397 push(@Depend_lines, "$str: $int_file\n");
399 $int_file =~ s/\.l?hs$//;
400 $int_file =~ s/\.l?y$//;
401 $int_file =~ s/\.hi$//;
405 $source_dep = "$int_file.hi-boot";
408 foreach $obj (@Obj_suffix) {
411 push(@Depend_lines, "$str: $int_file.hi\n");
415 foreach $suff (@File_suffix) {
417 foreach $obj (@Obj_suffix) {
418 $str .= "$bf.${suff}_$obj ";
420 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
424 foreach $obj (@Obj_suffix) {
426 foreach $suff (@File_suffix) {
427 $str .= "$bf.${suff}_$obj ";
430 push(@Depend_lines, "$str: $source_dep\n");
436 close(SRCFILE) || exit(1);
439 # when we see something, we cache that fact ('y').
440 # also, when we get a miss, we cache that (so we don't try later); ('n')
443 sub find_in_Import_dirs {
444 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
446 local($do_magical_check) = 0;
447 local($name_to_check);
449 # do it the old hard way: hop along Import_dir list
450 foreach $import_dir (@Import_dirs) {
452 if ($import_dir eq '.') {
453 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
454 $do_magical_check = 1;
457 $name_to_check = "$import_dir/$modname.hi";
458 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
459 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
460 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
461 if (-f $name_to_check) {
462 $FileExists{$name_to_check} = 'y';
463 return($name_to_check) ;
465 $FileExists{$name_to_check} = 'n';
469 for $suff ('hs', 'lhs', 'ly', 'y') {
470 $name_to_check = "$import_dir/$modname.$suff";
471 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
472 return($name_to_check) if -f $name_to_check;
476 $name_to_check = "$import_dir/$modname.hi-boot";
477 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
478 return($name_to_check) if -f $name_to_check;
481 if ($do_magical_check == 1) {
482 $name_to_check = "$last_seen_dir/$modname.hi";
484 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
485 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
486 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
487 if (-f $name_to_check) {
488 $FileExists{$name_to_check} = 'y';
489 return($name_to_check) ;
491 $FileExists{$name_to_check} = 'n';
495 for $suff ('lhs', 'hs', 'ly', 'y') {
496 $name_to_check = "$last_seen_dir/$modname.$suff";
497 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
498 return($name_to_check) if -f $name_to_check;
502 # OK, maybe it's referring to something in a system library
503 #foreach $lib ( @Syslibs ) {
504 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
507 # Last hope: referring to a Prelude interface
508 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
510 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
513 sub find_in_Include_dirs {
514 local($orig_src_file, $name, $last_seen_dir) = @_;
516 local($do_magical_check) = 0;
518 # no funny name guessing here
520 # hop along Include_dir list
521 foreach $include_dir (@Include_dirs) {
522 $include_dir =~ s/^-I//;
525 if ($include_dir eq '.') {
526 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
527 $do_magical_check = 1;
529 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
530 if (-f "$include_dir/$name") {
531 return("$include_dir/$name");
534 if ($do_magical_check == 1) {
535 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
536 if (-f "$last_seen_dir/$name") {
537 return("$last_seen_dir/$name");
540 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
543 # out of the driver, actually
545 local($str_to_do, $tidy_name) = @_;
547 print STDERR "\n$tidy_name:\n\t" if $Verbose;
548 print STDERR "$str_to_do\n" if $Verbose;
550 local($return_val) = system($str_to_do) >> 8;
552 if ($return_val != 0) {
553 local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
554 $die_msg .= " (program not found)" if $return_val == 255;
555 $die_msg .= " ($!)" if $Verbose && $! != 0;
557 print STDERR $die_msg;