2 # perl script expect the following variables to be prepended:
4 # RAWCPP TMPDIR TOP_PWD HscIfaceFileVersion
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 -package <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.
36 -f blah Use "blah" as the makefile, rather than "makefile"
38 -cpp Wash the input files through cpp(1).
39 -o <osuf> Use <osuf> as the "object file" suffix ( default: o)
40 -s <suf> Make extra dependencies for files with
41 suffix <suf><osuf>; thus, "-o hc -s _a" will
42 make dependencies both for .hc files and for .a_hc
43 files. (Useful in conjunction with NoFib "ways".)
44 --exclude-module=<file>
45 Regard <file> as "stable"; i.e., eXclude it from having
47 -x same as --exclude-module
48 --exclude-directory=<dirs>
49 Regard : separated list of directories as containing stable,
50 don't generate any dependencies on modules therein.
51 -Xdirs same as --exclude-directory
53 Regard prelude libraries as unstable, i.e., generate dependencies
54 on prelude modules. This option is normally only used by the
55 various system libraries. If a -package option is used, dependencies
56 will also be generated on the library's interfaces.
57 --include-module=<file>
58 Regard <file> as not "stable"; i.e., generate dependencies
59 on it (if any). This option is normally used in conjunction
60 with the --exclude-directory option.
64 $Status = 0; # just used for exit() status
65 $Verbose = 0; # 1 => verbose, 2 => very verbose
66 $Warnings = 1; # 1 => warn about duplicate interface files
70 $Cpp_flag_set = 0; # 1 => run cpp over input files.
72 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
73 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependHS$$";
75 $Tmp_prefix ="${TMPDIR}/mkdependHS$$";
76 $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
79 $TopPwd = "${TOP_PWD}";
80 $InstLibDirGhc = "${libdir}";
81 $InstLibExecDirGhc = "${libexecdir}";
82 $InstDataDirGhc = "${datadir}";
84 $Unlit = ($INSTALLING) ? "${InstLibExecDirGhc}/unlit"
85 : "${TopPwd}/ghc/utils/unlit/unlit";
87 $HiBootVersion = $HscIfaceFileVersion;
88 $hi_boot_with_version = 0;
90 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
91 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
97 %LibIfaces = (); # known prelude/syslib ifaces; read from a file
99 # directories to considered stable.
104 $Include_dirs = '-I.';
107 $Include_prelude = 0;
109 $ProcessingOptions=0;
111 # Delete temp. file if script is halted.
112 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
113 $SIG{'INT'} = 'quit_upon_signal';
114 $SIG{'QUIT'} = 'quit_upon_signal';
116 &mangle_command_line_args(@ARGV);
123 @Import_dirs = split(/:/,$Import_dirs);
124 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
126 # NB: We keep the scalar-variable equivalents to use in error messages
128 &gather_import_dirs();
130 if ( ! $Makefile && -f 'makefile' ) {
131 $Makefile = 'makefile';
132 } elsif ( ! $Makefile && -f 'Makefile') {
133 $Makefile = 'Makefile';
134 } elsif ( ! $Makefile) {
135 die "$Pgm: no makefile or Makefile found\n";
138 print STDERR "CPP defines=@Defines\n" if $Verbose;
139 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
140 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
142 &preprocess_import_dirs();
146 foreach $sf (@Src_files) {
148 # just like lit-inputter
149 # except it puts each file through CPP and
150 # a de-commenter (not implemented);
151 # builds up @Depend_lines
152 print STDERR "Here we go for source file: $sf\n" if $Verbose;
153 ($bf = $sf) =~ s/\.l?(hs|y)$//;
156 foreach $obj (@Obj_suffix) {
158 foreach $suff (@File_suffix) {
159 $str .= "$bf.${suff}_$obj ";
162 push(@Depend_lines, "$str: $sf\n");
163 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
164 if ( $sf !~ /\.l(hs|y)$/ ) {
167 $file_to_read = "$Tmp_prefix.hs";
168 local($to_do) = "$Unlit $sf $file_to_read";
169 &run_something($to_do, $sf, 'unlit');
171 &slurp_file_for_imports($file_to_read, $sf);
173 # Delete the temporary.
174 if ( $sf =~ /\.l(hs|y)$/ ) {
175 unlink "$Tmp_prefix.hs";
181 # Create backup version of output file.
183 if ( ! -f $Makefile ) {
184 # truncate() may not be implemented, so we
187 open(TRUNC,"> $Makefile.bak") && close(TRUNC);
189 rename($Makefile,"$Makefile.bak");
191 # now copy Makefile.bak into Makefile, rm'ing old dependencies
193 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
194 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
197 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
201 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
204 # insert dependencies
205 print "$Begin_magic_str", @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
207 while (<OMKF>) { # copy the rest through
208 print $_ || die "Failed to write out dependencies ($!)";
210 close(NMKF) || exit(1);
211 close(OMKF) || exit(1);
214 sub mangle_command_line_args {
217 while($_ = $Args[0]) {
223 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
225 } elsif ( /^-cpp$/ ) { # recognized wherever they occur
228 $Import_dirs = ''; # import path cleared!
229 } elsif ( /^-i(.*)/ ) {
230 $Import_dirs = "$1:$Import_dirs";
232 $Include_dirs .= " $_";
233 } elsif ( /^-syslib$/ ) { # deprecated, use -syslib
234 push(@Syslibs, &grab_arg_arg(*Args,$_,''));
235 } elsif ( /^-package$/ ) {
236 push(@Syslibs, &grab_arg_arg(*Args,$_,''));
237 } elsif ( /^-fglasgow-exts$/ ) {
238 push(@Syslibs, 'lang');
239 } elsif ( /^-concurrent$/ ) {
240 push(@Syslibs, 'concurrent');
241 } elsif (/^-#include(.*)/) {
242 &grab_arg_arg(*Args,'-#include',$1);
243 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
248 } elsif ( /^-f(.*)/ && !$ProcessingOptions ) {
249 $Makefile = &grab_arg_arg(*Args,'-f',$1);
250 } elsif ( /^-o(.*)/ ) {
251 local($suff) = &grab_arg_arg(*Args,'-o',$1);
252 # Weed out existing entry .. there must be a better way of doing this
253 # with arrays (non-assoc) ! -- sof
254 @Obj_suffix = grep(!/$suff/,@Obj_suffix);
255 push(@Obj_suffix, $suff);
257 # --exclude-module=mod => it's stable, trust me!
259 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
260 local($thing) = &grab_arg_arg(*Args,$1,$2);
261 $IgnoreMe{$thing} = 'Y';
262 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
263 foreach $d ( split(/:/,&grab_arg_arg(*Args,$1, $2)) ) {
264 push(@Ignore_dirs,"$d");
266 } elsif ( /^--include-module=(.*)/ ) {
267 local($thing) = &grab_arg_arg(*Args,$1,$2);
268 $IgnoreMe{$thing} = 'n';
269 } elsif ( /^--include-prelude$/ ) {
270 $Include_prelude = 1;
271 } elsif ( /^-s(.*)/ ) {
272 local($suff) = &grab_arg_arg(*Args,'-s',$1);
273 push(@File_suffix, $suff);
275 if (!$ProcessingOptions) {
276 print STDERR "$Pgm: unknown option ignored: $_\n";
280 push(@Src_files, $_);
283 # Removed support for picking up plausible source file
284 # names inside the ghc options chunk of the command
285 # line. It failed to deal with `option-value' stuff present
286 # on some invocations of ghc (e.g., "-ohi foo.baz").
289 # } elsif ($Dashdashes_seen == 1) {
290 # push(@Src_files, $_) if ! /^-/;
293 @File_suffix = sort (@File_suffix);
297 local(*Args, $option, $rest_of_arg) = @_;
300 return($rest_of_arg);
301 } elsif ($#Args >= 0) {
302 local($temp) = $Args[0]; shift(@Args);
305 print STDERR "$Pgm: no argument following $option option\n";
310 sub gather_import_dirs {
312 # set up array of ignored modules
313 local(@dirs) = ($INSTALLING) ?
314 ("$InstLibDirGhc/imports/std")
315 : ("$TopPwd/ghc/lib/std");
317 if (!$Include_prelude) {
318 push(@Ignore_dirs, @dirs);
320 push(@Import_dirs, @dirs);
323 foreach $lib ( @Syslibs ) {
327 if ( $lib eq 'lang' && ! $INSTALLING ) {
328 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/monads");
330 if ( $lib eq 'text' && ! $INSTALLING ) {
331 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/html");
332 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/haxml/lib");
333 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/parsec");
335 if ( $lib eq 'data' && ! $INSTALLING ) {
336 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison");
337 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Assoc");
338 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Coll");
339 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Seq");
341 if ( $lib eq 'util' && ! $INSTALLING ) {
342 push(@Import_dirs, "${TopPwd}/hslibs/${lib}/check");
346 if ( $lib eq 'win32' && ! $INSTALLING ) {
347 $dir = "${TopPwd}/hslibs/${lib}/src";
348 } elsif ( $lib eq 'com' && ! $INSTALLING ) {
349 $dir = "${TopPwd}/hdirect/lib";
351 $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}"
352 : "${TopPwd}/hslibs/${lib}";
354 if (!$Include_prelude) {
355 push(@Ignore_dirs,$dir);
357 push(@Import_dirs, $dir);
364 sub preprocess_import_dirs {
365 # it's probably cheaper to find out what's in all
366 # the @Import_dirs before we start processing.
370 foreach $d ( @Import_dirs ) {
371 # Check to see if it can be ignored
372 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
373 #next if $Ignore_dirs{$d};
375 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
377 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
378 next unless /(.*)\.hi$/;
380 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
381 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
383 $ModuleIn{$thing} = $d;
386 closedir(DIR); # No, don't check the error code
389 # Add all the modules
390 # to the IgnoreMe array before we start scanning for imports.
391 foreach $d (@Ignore_dirs) {
393 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
395 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
396 next unless /(.*)\.(hi|l?hs|l?y)$/;
397 #don't tag it twice or overwrite it with a diff. value
398 next if $IgnoreMe{$1};
399 print STDERR "Module $d will be ignored\n" if $Verbose;
403 closedir(DIR); # No, don't check the error code
407 sub slurp_file_for_imports {
408 local($file_to_read, $orig_src_file) = @_;
409 local($follow_file,$read_from_file);
412 local(@Old_Syslibs, $options);
413 local(@Old_Ignore_dirs);
414 local($found_options)=0;
416 local($last_seen_dir) = $orig_src_file;
417 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
418 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
420 local($mod_name) = $orig_src_file;
422 $mod_name =~ s/.*\/([^\/]+)$/$1/g;
423 $mod_name =~ s/^([^.]+)\.(.*)$/$1/;
425 print STDERR "Warning: processing module $mod_name, which I was supposed to ignore.\n"
426 if ( $IgnoreMe{$mod_name} eq 'Y' && $Warnings );
428 &process_dependency('import',0,'Prelude') if ($Include_prelude);
431 # Check for {-# OPTIONS in the file-to-be-processed. If any syslib related options
432 # *only* are found, add them to the module search list (and take them off once
433 # we're through with this module.)
436 $options = &check_for_source_options($file_to_read);
437 if ($options ne "") {
438 @Old_Syslibs=@Syslibs;
439 $ProcessingOptions=1;
440 &mangle_command_line_args(split(/\s+/,$options));
441 if (@Old_Syslibs ne @Syslibs) {
443 @Old_Ignore_dirs = @Ignore_dirs;
444 &gather_import_dirs();
445 &preprocess_import_dirs();
449 # we mangle #include's so they will also leave something
450 # behind to indicate the dependency on _them_
454 &run_something("${SED} -e '/^# *include/{p;s/^# *include/!include/;}' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines - 2>&1 > ${file_to_read}.i", $orig_src_file, 'cpp');
455 $read_from_file="${file_to_read}.i";
458 $read_from_file="${file_to_read}";
459 $open_cmd = $file_to_read;
461 print STDERR "$open_cmd\n" if $Verbose;
463 open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";
467 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
468 # the compiler will deal with the absence of Foo.hi by consulting the
469 # source for Foo directly. (for dealing with recursive modules).
471 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
473 $source = ( $2 ne '') ? 1 : 0;
476 next if $modname eq '';
477 &process_dependency($todo,$source,$modname);
480 if ($found_options) {
481 @Ignore_dirs = @Old_Ignore_dirs;
484 close(SRCFILE) || exit(1);
485 # remove temporary file, if any.
487 unlink("$read_from_file");
493 sub process_dependency {
494 local($todo,$source,$modname) = @_;
496 if ($todo eq 'import') {
497 if ( $IgnoreMe{$modname} eq 'Y' ) {
498 # user specifically asked for this module
500 $follow_file = '__ignore__';
501 } elsif ( $ModuleIn{$modname} ) {
502 $follow_file = "$ModuleIn{$modname}/$modname.hi";
505 &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
509 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
512 if (! $follow_file) { # it didnae find anything
513 die "$orig_src_file: Couldn't handle: $_\n";
515 } else { # it found something
516 if ($follow_file ne '__ignore__') {
517 local($int_file) = $follow_file;
519 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
521 foreach $obj (@Obj_suffix) {
523 foreach $suff (@File_suffix) {
524 $str .= "$bf.${suff}_$obj ";
527 push(@Depend_lines, "$str: $int_file\n");
529 $int_file =~ s/\.l?hs$//;
530 $int_file =~ s/\.l?y$//;
531 $int_file =~ s/\.hi$//;
535 # if a version specific .hi-boot file exist, use it.
536 if ( -f "$int_file.hi-boot-${HiBootVersion}" ) {
537 $source_dep = "$int_file.hi-boot-${HiBootVersion}";
539 $source_dep = "$int_file.hi-boot";
543 foreach $obj (@Obj_suffix) {
546 push(@Depend_lines, "$str: $int_file.hi\n");
550 foreach $suff (@File_suffix) {
552 foreach $obj (@Obj_suffix) {
553 $str .= "$bf.${suff}_$obj ";
555 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
559 foreach $obj (@Obj_suffix) {
561 foreach $suff (@File_suffix) {
562 $str .= "$bf.${suff}_$obj ";
565 push(@Depend_lines, "$str: $source_dep\n");
572 # when we see something, we cache that fact ('y').
573 # also, when we get a miss, we cache that (so we don't try later); ('n')
576 sub find_in_Import_dirs {
577 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
579 local($do_magical_check) = 0;
580 local($name_to_check);
582 # do it the old hard way: hop along Import_dir list
583 foreach $import_dir (@Import_dirs) {
585 if ($import_dir eq '.') {
586 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
587 $do_magical_check = 1;
590 $name_to_check = "$import_dir/$modname.hi";
591 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
592 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
593 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
594 if (-f $name_to_check) {
595 $FileExists{$name_to_check} = 'y';
596 return($name_to_check) ;
598 $FileExists{$name_to_check} = 'n';
602 for $suff ('hs', 'lhs', 'ly', 'y') {
603 $name_to_check = "$import_dir/$modname.$suff";
604 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
605 return($name_to_check) if -f $name_to_check;
609 $name_to_check = "$import_dir/$modname.hi-boot-${HiBootVersion}";
610 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
611 return($name_to_check) if -f $name_to_check;
613 $name_to_check = "$import_dir/$modname.hi-boot";
614 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
615 return($name_to_check) if -f $name_to_check;
618 if ($do_magical_check == 1) {
619 $name_to_check = "$last_seen_dir/$modname.hi";
621 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
622 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
623 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
624 if (-f $name_to_check) {
625 $FileExists{$name_to_check} = 'y';
626 return($name_to_check) ;
628 $FileExists{$name_to_check} = 'n';
632 for $suff ('lhs', 'hs', 'ly', 'y') {
633 $name_to_check = "$last_seen_dir/$modname.$suff";
634 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
635 return($name_to_check) if -f $name_to_check;
639 # OK, maybe it's referring to something in a system library
640 #foreach $lib ( @Syslibs ) {
641 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
644 # Last hope: referring to a Prelude interface
645 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
647 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
650 sub find_in_Include_dirs {
651 local($orig_src_file, $name, $last_seen_dir) = @_;
653 local($do_magical_check) = 0;
655 # no funny name guessing here
657 # hop along Include_dir list
658 foreach $include_dir (@Include_dirs) {
659 $include_dir =~ s/^-I//;
662 if ($include_dir eq '.') {
663 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
664 $do_magical_check = 1;
666 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
667 if (-f "$include_dir/$name") {
668 return("$include_dir/$name");
671 if ($do_magical_check == 1) {
672 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
673 if (-f "$last_seen_dir/$name") {
674 return("$last_seen_dir/$name");
677 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
680 # out of the driver, actually
682 local($str_to_do, $file_to_read, $tidy_name) = @_;
684 print STDERR "\n$tidy_name:\n\t" if $Verbose;
685 print STDERR "$str_to_do\n" if $Verbose;
687 local($return_val) = system($str_to_do) >> 8;
689 if ($return_val != 0) {
690 local($die_msg) = "$Pgm: Running $tidy_name ( on $file_to_read ) failed";
691 $die_msg .= " program not found: $str_to_do " if $return_val == 255;
692 $die_msg .= " ($!)" if $Verbose && $! != 0;
694 print STDERR $die_msg;
699 # out of the driver too.
700 sub check_for_source_options {
702 local($comment_start,$comment_end);
704 # Assume it is a file containing Haskell source
705 $comment_start = "{-#";
706 $comment_end = "#-}";
708 open(FILE,$file) || return ""; # No big loss
711 if ( /^${comment_start} OPTIONS (.*)${comment_end}/ ) {
712 # return the options.
714 print STDERR "Found OPTIONS $stuff in $file\n" if $Verbose;
718 elsif ( /^$/ ) { # ignore empty lines
721 elsif ( /^#line.+$/ ) { # ignore comment lines (unused..ToDo: rm )
724 elsif ( /^{-# LINE.+$/ ) { # ignore line pragmas
727 else { # stop looking, something non-empty / not
728 # ${comment_start} OPTIONS .. ${comment_end} encountered.
729 close(FILE);return "";