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 -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.
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 -syslib 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.
102 $Include_dirs = '-I.';
105 $Include_prelude = 0;
108 # Delete temp. file if script is halted.
109 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
110 $SIG{'INT'} = 'quit_upon_signal';
111 $SIG{'QUIT'} = 'quit_upon_signal';
113 &mangle_command_line_args(@ARGV);
120 @Import_dirs = split(/:/,$Import_dirs);
121 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
123 # NB: We keep the scalar-variable equivalents to use in error messages
125 &gather_import_dirs();
127 if ( ! $Makefile && -f 'makefile' ) {
128 $Makefile = 'makefile';
129 } elsif ( ! $Makefile && -f 'Makefile') {
130 $Makefile = 'Makefile';
131 } elsif ( ! $Makefile) {
132 die "$Pgm: no makefile or Makefile found\n";
135 print STDERR "CPP defines=@Defines\n" if $Verbose;
136 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
137 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
139 &preprocess_import_dirs();
143 foreach $sf (@Src_files) {
145 # just like lit-inputter
146 # except it puts each file through CPP and
147 # a de-commenter (not implemented);
148 # builds up @Depend_lines
149 print STDERR "Here we go for source file: $sf\n" if $Verbose;
150 ($bf = $sf) =~ s/\.l?(hs|y)$//;
153 foreach $obj (@Obj_suffix) {
155 foreach $suff (@File_suffix) {
156 $str .= "$bf.${suff}_$obj ";
159 push(@Depend_lines, "$str: $sf\n");
160 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
161 if ( $sf !~ /\.l(hs|y)$/ ) {
164 $file_to_read = "$Tmp_prefix.hs";
165 local($to_do) = "$Unlit $sf $file_to_read";
166 &run_something($to_do, $sf, 'unlit');
168 &slurp_file_for_imports($file_to_read, $sf);
170 # Delete the temporary.
171 if ( $sf =~ /\.l(hs|y)$/ ) {
172 unlink "$Tmp_prefix.hs";
178 # Create backup version of output file.
180 if ( ! -f $Makefile ) {
181 # truncate() may not be implemented, so we
184 open(TRUNC,"> $Makefile.bak") && close(TRUNC);
186 rename($Makefile,"$Makefile.bak");
188 # now copy Makefile.bak into Makefile, rm'ing old dependencies
190 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
191 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
194 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
198 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
201 # insert dependencies
202 print "$Begin_magic_str", @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
204 while (<OMKF>) { # copy the rest through
205 print $_ || die "Failed to write out dependencies ($!)";
207 close(NMKF) || exit(1);
208 close(OMKF) || exit(1);
211 sub mangle_command_line_args {
214 while($_ = $Args[0]) {
220 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
222 } elsif ( /^-cpp$/ ) { # recognized wherever they occur
225 $Import_dirs = ''; # import path cleared!
226 } elsif ( /^-i(.*)/ ) {
227 $Import_dirs = "$1:$Import_dirs";
229 $Include_dirs .= " $_";
230 } elsif ( /^-syslib$/ ) {
231 push(@Syslibs, &grab_arg_arg(*Args,$_,''));
232 } elsif ( /^-fglasgow-exts$/ ) {
233 push(@Syslibs, 'exts');
234 } elsif ( /^-concurrent$/ ) {
235 push(@Syslibs, 'concurrent');
236 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
241 } elsif ( /^-f(.*)/ ) {
242 $Makefile = &grab_arg_arg(*Args,'-f',$1);
243 } elsif ( /^-o(.*)/ ) {
244 local($suff) = &grab_arg_arg(*Args,'-o',$1);
245 # Weed out existing entry .. there must be a better way of doing this
246 # with arrays (non-assoc) ! -- sof
247 @Obj_suffix = grep(!/$suff/,@Obj_suffix);
248 push(@Obj_suffix, $suff);
250 # --exclude-module=mod => it's stable, trust me!
252 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
253 local($thing) = &grab_arg_arg(*Args,$1,$2);
254 $IgnoreMe{$thing} = 'Y';
255 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
256 foreach $d ( split(/:/,&grab_arg_arg(*Args,$1, $2)) ) {
257 push(@Ignore_dirs,"$d");
259 } elsif ( /^--include-module=(.*)/ ) {
260 local($thing) = &grab_arg_arg(*Args,$1,$2);
261 $IgnoreMe{$thing} = 'n';
262 } elsif ( /^--include-prelude$/ ) {
263 $Include_prelude = 1;
264 } elsif ( /^-s(.*)/ ) {
265 local($suff) = &grab_arg_arg(*Args,'-s',$1);
266 push(@File_suffix, $suff);
268 print STDERR "$Pgm: unknown option ignored: $_\n";
271 push(@Src_files, $_);
274 # Removed support for picking up plausible source file
275 # names inside the ghc options chunk of the command
276 # line. It failed to deal with `option-value' stuff present
277 # on some invocations of ghc (e.g., "-ohi foo.baz").
280 # } elsif ($Dashdashes_seen == 1) {
281 # push(@Src_files, $_) if ! /^-/;
284 @File_suffix = sort (@File_suffix);
288 local(*Args, $option, $rest_of_arg) = @_;
291 return($rest_of_arg);
292 } elsif ($#Args >= 0) {
293 local($temp) = $Args[0]; shift(@Args);
296 print STDERR "$Pgm: no argument following $option option\n";
301 sub gather_import_dirs {
303 # set up array of ignored modules
304 local(@dirs) = ($INSTALLING) ?
305 ("$InstLibDirGhc/imports/std")
306 : ("$TopPwd/ghc/lib/std");
308 if (!$Include_prelude) {
309 push(@Ignore_dirs, @dirs);
311 push(@Import_dirs, @dirs);
314 foreach $lib ( @Syslibs ) {
318 if ( $lib eq 'win32' && ! $INSTALLING ) {
319 $dir = "${TopPwd}/hslibs/${lib}/src";
320 } elsif ( $lib eq 'com' && ! $INSTALLING ) {
321 $dir = "${TopPwd}/hdirect/lib";
323 $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}"
324 : "${TopPwd}/ghc/lib/${lib}";
326 if (!$Include_prelude) {
327 push(@Ignore_dirs,$dir);
329 push(@Import_dirs, $dir);
336 sub preprocess_import_dirs {
337 # it's probably cheaper to find out what's in all
338 # the @Import_dirs before we start processing.
343 foreach $d ( @Import_dirs ) {
344 # Check to see if it can be ignored
345 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
346 #next if $Ignore_dirs{$d};
348 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
350 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
351 next unless /(.*)\.hi$/;
354 # dLL_ifs.hi is used to indicate whether a directory
355 # contains interface files whose object codes reside in a Win32 DLL.
357 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d && $thing ne 'dLL_ifs') {
358 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
360 $ModuleIn{$thing} = $d;
363 closedir(DIR); # No, don't check the error code
366 # Add all the modules
367 # to the IgnoreMe array before we start scanning for imports.
368 foreach $d (@Ignore_dirs) {
370 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
372 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
373 next unless /(.*)\.(hi|l?hs|l?y)$/;
374 #don't tag it twice or overwrite it with a diff. value
375 next if $IgnoreMe{$1};
376 print STDERR "Module $d will be ignored\n" if $Verbose;
380 closedir(DIR); # No, don't check the error code
384 sub slurp_file_for_imports {
385 local($file_to_read, $orig_src_file) = @_;
386 local($follow_file,$read_from_file);
389 local(@Old_Syslibs, $options);
390 local(@Old_Ignore_dirs);
391 local($found_options)=0;
393 local($last_seen_dir) = $orig_src_file;
394 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
395 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
397 local($mod_name) = $orig_src_file;
399 $mod_name =~ s/.*\/([^\/]+)$/$1/g;
400 $mod_name =~ s/^([^.]+)\.(.*)$/$1/;
402 print STDERR "Warning: processing module $mod_name, which I was supposed to ignore.\n"
403 if ( $IgnoreMe{$mod_name} eq 'Y' && $Warnings );
405 &process_dependency('import',0,'Prelude') if ($Include_prelude);
408 # Check for {-# OPTIONS in the file-to-be-processed. If any syslib related options
409 # *only* are found, add them to the module search list (and take them off once
410 # we're through with this module.)
413 $options = &check_for_source_options($file_to_read);
414 if ($options ne "") {
415 @Old_Syslibs=@Syslibs;
416 &mangle_command_line_args(split(/ /,$options));
417 if (@Old_Syslibs ne @Syslibs) {
419 @Old_Ignore_dirs = @Ignore_dirs;
420 &gather_import_dirs();
421 &preprocess_import_dirs();
425 # we mangle #include's so they will also leave something
426 # behind to indicate the dependency on _them_
430 &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');
431 $read_from_file="${file_to_read}.i";
434 $read_from_file="${file_to_read}";
435 $open_cmd = $file_to_read;
437 print STDERR "$open_cmd\n" if $Verbose;
439 open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";
443 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
444 # the compiler will deal with the absence of Foo.hi by consulting the
445 # source for Foo directly. (for dealing with recursive modules).
447 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
449 $source = ( $2 ne '') ? 1 : 0;
452 next if $modname eq '';
453 &process_dependency($todo,$source,$modname);
456 @Ignore_dirs = @Old_Ignore_dirs if ($found_options);
458 close(SRCFILE) || exit(1);
459 # remove temporary file, if any.
461 unlink("$read_from_file");
467 sub process_dependency {
468 local($todo,$source,$modname) = @_;
470 if ($todo eq 'import') {
471 if ( $IgnoreMe{$modname} eq 'Y' ) {
472 # user specifically asked for this module
474 $follow_file = '__ignore__';
475 } elsif ( $ModuleIn{$modname} ) {
476 $follow_file = "$ModuleIn{$modname}/$modname.hi";
479 &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
483 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
486 if (! $follow_file) { # it didnae find anything
487 die "$orig_src_file: Couldn't handle: $_\n";
489 } else { # it found something
490 if ($follow_file ne '__ignore__') {
491 local($int_file) = $follow_file;
493 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
495 foreach $obj (@Obj_suffix) {
497 foreach $suff (@File_suffix) {
498 $str .= "$bf.${suff}_$obj ";
501 push(@Depend_lines, "$str: $int_file\n");
503 $int_file =~ s/\.l?hs$//;
504 $int_file =~ s/\.l?y$//;
505 $int_file =~ s/\.hi$//;
509 # if a version specific .hi-boot file exist, use it.
510 if ( -f "$int_file.hi-boot-${HiBootVersion}" ) {
511 $source_dep = "$int_file.hi-boot-${HiBootVersion}";
513 $source_dep = "$int_file.hi-boot";
517 foreach $obj (@Obj_suffix) {
520 push(@Depend_lines, "$str: $int_file.hi\n");
524 foreach $suff (@File_suffix) {
526 foreach $obj (@Obj_suffix) {
527 $str .= "$bf.${suff}_$obj ";
529 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
533 foreach $obj (@Obj_suffix) {
535 foreach $suff (@File_suffix) {
536 $str .= "$bf.${suff}_$obj ";
539 push(@Depend_lines, "$str: $source_dep\n");
546 # when we see something, we cache that fact ('y').
547 # also, when we get a miss, we cache that (so we don't try later); ('n')
550 sub find_in_Import_dirs {
551 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
553 local($do_magical_check) = 0;
554 local($name_to_check);
556 # do it the old hard way: hop along Import_dir list
557 foreach $import_dir (@Import_dirs) {
559 if ($import_dir eq '.') {
560 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
561 $do_magical_check = 1;
564 $name_to_check = "$import_dir/$modname.hi";
565 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
566 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
567 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
568 if (-f $name_to_check) {
569 $FileExists{$name_to_check} = 'y';
570 return($name_to_check) ;
572 $FileExists{$name_to_check} = 'n';
576 for $suff ('hs', 'lhs', 'ly', 'y') {
577 $name_to_check = "$import_dir/$modname.$suff";
578 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
579 return($name_to_check) if -f $name_to_check;
583 $name_to_check = "$import_dir/$modname.hi-boot-${HiBootVersion}";
584 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
585 return($name_to_check) if -f $name_to_check;
587 $name_to_check = "$import_dir/$modname.hi-boot";
588 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
589 return($name_to_check) if -f $name_to_check;
592 if ($do_magical_check == 1) {
593 $name_to_check = "$last_seen_dir/$modname.hi";
595 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
596 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
597 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
598 if (-f $name_to_check) {
599 $FileExists{$name_to_check} = 'y';
600 return($name_to_check) ;
602 $FileExists{$name_to_check} = 'n';
606 for $suff ('lhs', 'hs', 'ly', 'y') {
607 $name_to_check = "$last_seen_dir/$modname.$suff";
608 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
609 return($name_to_check) if -f $name_to_check;
613 # OK, maybe it's referring to something in a system library
614 #foreach $lib ( @Syslibs ) {
615 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
618 # Last hope: referring to a Prelude interface
619 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
621 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
624 sub find_in_Include_dirs {
625 local($orig_src_file, $name, $last_seen_dir) = @_;
627 local($do_magical_check) = 0;
629 # no funny name guessing here
631 # hop along Include_dir list
632 foreach $include_dir (@Include_dirs) {
633 $include_dir =~ s/^-I//;
636 if ($include_dir eq '.') {
637 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
638 $do_magical_check = 1;
640 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
641 if (-f "$include_dir/$name") {
642 return("$include_dir/$name");
645 if ($do_magical_check == 1) {
646 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
647 if (-f "$last_seen_dir/$name") {
648 return("$last_seen_dir/$name");
651 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
654 # out of the driver, actually
656 local($str_to_do, $file_to_read, $tidy_name) = @_;
658 print STDERR "\n$tidy_name:\n\t" if $Verbose;
659 print STDERR "$str_to_do\n" if $Verbose;
661 local($return_val) = system($str_to_do) >> 8;
663 if ($return_val != 0) {
664 local($die_msg) = "$Pgm: Running $tidy_name ( on $file_to_read ) failed";
665 $die_msg .= " program not found: $str_to_do " if $return_val == 255;
666 $die_msg .= " ($!)" if $Verbose && $! != 0;
668 print STDERR $die_msg;
673 # out of the driver too.
674 sub check_for_source_options {
676 local($comment_start,$comment_end);
678 # Assume it is a file containing Haskell source
679 $comment_start = "{-#";
680 $comment_end = "#-}";
682 open(FILE,$file) || return ""; # No big loss
685 if ( /^${comment_start} OPTIONS (.*)${comment_end}/ ) {
686 # return the options.
688 print STDERR "Found OPTIONS $stuff in $file\n" if $Verbose;
692 elsif ( /^$/ ) { # ignore empty lines
695 elsif ( /^#line.+$/ ) { # ignore comment lines (unused..ToDo: rm )
698 elsif ( /^{-# LINE.+$/ ) { # ignore line pragmas
701 else { # stop looking, something non-empty / not
702 # ${comment_start} OPTIONS .. ${comment_end} encountered.
703 close(FILE);return "";