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;
109 # Delete temp. file if script is halted.
110 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
111 $SIG{'INT'} = 'quit_upon_signal';
112 $SIG{'QUIT'} = 'quit_upon_signal';
114 &mangle_command_line_args(@ARGV);
121 @Import_dirs = split(/:/,$Import_dirs);
122 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
124 # NB: We keep the scalar-variable equivalents to use in error messages
126 &gather_import_dirs();
128 if ( ! $Makefile && -f 'makefile' ) {
129 $Makefile = 'makefile';
130 } elsif ( ! $Makefile && -f 'Makefile') {
131 $Makefile = 'Makefile';
132 } elsif ( ! $Makefile) {
133 die "$Pgm: no makefile or Makefile found\n";
136 print STDERR "CPP defines=@Defines\n" if $Verbose;
137 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
138 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
140 &preprocess_import_dirs();
144 foreach $sf (@Src_files) {
146 # just like lit-inputter
147 # except it puts each file through CPP and
148 # a de-commenter (not implemented);
149 # builds up @Depend_lines
150 print STDERR "Here we go for source file: $sf\n" if $Verbose;
151 ($bf = $sf) =~ s/\.l?(hs|y)$//;
154 foreach $obj (@Obj_suffix) {
156 foreach $suff (@File_suffix) {
157 $str .= "$bf.${suff}_$obj ";
160 push(@Depend_lines, "$str: $sf\n");
161 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
162 if ( $sf !~ /\.l(hs|y)$/ ) {
165 $file_to_read = "$Tmp_prefix.hs";
166 local($to_do) = "$Unlit $sf $file_to_read";
167 &run_something($to_do, $sf, 'unlit');
169 &slurp_file_for_imports($file_to_read, $sf);
171 # Delete the temporary.
172 if ( $sf =~ /\.l(hs|y)$/ ) {
173 unlink "$Tmp_prefix.hs";
179 # Create backup version of output file.
181 if ( ! -f $Makefile ) {
182 # truncate() may not be implemented, so we
185 open(TRUNC,"> $Makefile.bak") && close(TRUNC);
187 rename($Makefile,"$Makefile.bak");
189 # now copy Makefile.bak into Makefile, rm'ing old dependencies
191 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
192 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
195 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
199 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
202 # insert dependencies
203 print "$Begin_magic_str", @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
205 while (<OMKF>) { # copy the rest through
206 print $_ || die "Failed to write out dependencies ($!)";
208 close(NMKF) || exit(1);
209 close(OMKF) || exit(1);
212 sub mangle_command_line_args {
215 while($_ = $Args[0]) {
221 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
223 } elsif ( /^-cpp$/ ) { # recognized wherever they occur
226 $Import_dirs = ''; # import path cleared!
227 } elsif ( /^-i(.*)/ ) {
228 $Import_dirs = "$1:$Import_dirs";
230 $Include_dirs .= " $_";
231 } elsif ( /^-syslib$/ ) {
232 push(@Syslibs, &grab_arg_arg(*Args,$_,''));
233 } elsif ( /^-fglasgow-exts$/ ) {
234 push(@Syslibs, 'exts');
235 } elsif ( /^-concurrent$/ ) {
236 push(@Syslibs, 'concurrent');
237 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
242 } elsif ( /^-f(.*)/ ) {
243 $Makefile = &grab_arg_arg(*Args,'-f',$1);
244 } elsif ( /^-o(.*)/ ) {
245 local($suff) = &grab_arg_arg(*Args,'-o',$1);
246 # Weed out existing entry .. there must be a better way of doing this
247 # with arrays (non-assoc) ! -- sof
248 @Obj_suffix = grep(!/$suff/,@Obj_suffix);
249 push(@Obj_suffix, $suff);
251 # --exclude-module=mod => it's stable, trust me!
253 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
254 local($thing) = &grab_arg_arg(*Args,$1,$2);
255 $IgnoreMe{$thing} = 'Y';
256 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
257 foreach $d ( split(/:/,&grab_arg_arg(*Args,$1, $2)) ) {
258 push(@Ignore_dirs,"$d");
260 } elsif ( /^--include-module=(.*)/ ) {
261 local($thing) = &grab_arg_arg(*Args,$1,$2);
262 $IgnoreMe{$thing} = 'n';
263 } elsif ( /^--include-prelude$/ ) {
264 $Include_prelude = 1;
265 } elsif ( /^-s(.*)/ ) {
266 local($suff) = &grab_arg_arg(*Args,'-s',$1);
267 push(@File_suffix, $suff);
269 if ($WarnOddOptions) {
270 print STDERR "$Pgm: unknown option ignored: $_\n";
274 push(@Src_files, $_);
277 # Removed support for picking up plausible source file
278 # names inside the ghc options chunk of the command
279 # line. It failed to deal with `option-value' stuff present
280 # on some invocations of ghc (e.g., "-ohi foo.baz").
283 # } elsif ($Dashdashes_seen == 1) {
284 # push(@Src_files, $_) if ! /^-/;
287 @File_suffix = sort (@File_suffix);
291 local(*Args, $option, $rest_of_arg) = @_;
294 return($rest_of_arg);
295 } elsif ($#Args >= 0) {
296 local($temp) = $Args[0]; shift(@Args);
299 print STDERR "$Pgm: no argument following $option option\n";
304 sub gather_import_dirs {
306 # set up array of ignored modules
307 local(@dirs) = ($INSTALLING) ?
308 ("$InstLibDirGhc/imports/std")
309 : ("$TopPwd/ghc/lib/std");
311 if (!$Include_prelude) {
312 push(@Ignore_dirs, @dirs);
314 push(@Import_dirs, @dirs);
317 foreach $lib ( @Syslibs ) {
321 if ( $lib eq 'win32' && ! $INSTALLING ) {
322 $dir = "${TopPwd}/hslibs/${lib}/src";
323 } elsif ( $lib eq 'com' && ! $INSTALLING ) {
324 $dir = "${TopPwd}/hdirect/lib";
326 $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}"
327 : "${TopPwd}/ghc/lib/${lib}";
329 if (!$Include_prelude) {
330 push(@Ignore_dirs,$dir);
332 push(@Import_dirs, $dir);
339 sub preprocess_import_dirs {
340 # it's probably cheaper to find out what's in all
341 # the @Import_dirs before we start processing.
346 foreach $d ( @Import_dirs ) {
347 # Check to see if it can be ignored
348 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
349 #next if $Ignore_dirs{$d};
351 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
353 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
354 next unless /(.*)\.hi$/;
357 # dLL_ifs.hi is used to indicate whether a directory
358 # contains interface files whose object codes reside in a Win32 DLL.
360 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d && $thing ne 'dLL_ifs') {
361 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
363 $ModuleIn{$thing} = $d;
366 closedir(DIR); # No, don't check the error code
369 # Add all the modules
370 # to the IgnoreMe array before we start scanning for imports.
371 foreach $d (@Ignore_dirs) {
373 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
375 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
376 next unless /(.*)\.(hi|l?hs|l?y)$/;
377 #don't tag it twice or overwrite it with a diff. value
378 next if $IgnoreMe{$1};
379 print STDERR "Module $d will be ignored\n" if $Verbose;
383 closedir(DIR); # No, don't check the error code
387 sub slurp_file_for_imports {
388 local($file_to_read, $orig_src_file) = @_;
389 local($follow_file,$read_from_file);
392 local(@Old_Syslibs, $options);
393 local(@Old_Ignore_dirs);
394 local($found_options)=0;
396 local($last_seen_dir) = $orig_src_file;
397 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
398 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
400 local($mod_name) = $orig_src_file;
402 $mod_name =~ s/.*\/([^\/]+)$/$1/g;
403 $mod_name =~ s/^([^.]+)\.(.*)$/$1/;
405 print STDERR "Warning: processing module $mod_name, which I was supposed to ignore.\n"
406 if ( $IgnoreMe{$mod_name} eq 'Y' && $Warnings );
408 &process_dependency('import',0,'Prelude') if ($Include_prelude);
411 # Check for {-# OPTIONS in the file-to-be-processed. If any syslib related options
412 # *only* are found, add them to the module search list (and take them off once
413 # we're through with this module.)
416 $options = &check_for_source_options($file_to_read);
417 if ($options ne "") {
418 @Old_Syslibs=@Syslibs;
420 &mangle_command_line_args(split(/ /,$options));
421 if (@Old_Syslibs ne @Syslibs) {
423 @Old_Ignore_dirs = @Ignore_dirs;
424 &gather_import_dirs();
425 &preprocess_import_dirs();
429 # we mangle #include's so they will also leave something
430 # behind to indicate the dependency on _them_
434 &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');
435 $read_from_file="${file_to_read}.i";
438 $read_from_file="${file_to_read}";
439 $open_cmd = $file_to_read;
441 print STDERR "$open_cmd\n" if $Verbose;
443 open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";
447 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
448 # the compiler will deal with the absence of Foo.hi by consulting the
449 # source for Foo directly. (for dealing with recursive modules).
451 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
453 $source = ( $2 ne '') ? 1 : 0;
456 next if $modname eq '';
457 &process_dependency($todo,$source,$modname);
460 @Ignore_dirs = @Old_Ignore_dirs if ($found_options);
462 close(SRCFILE) || exit(1);
463 # remove temporary file, if any.
465 unlink("$read_from_file");
471 sub process_dependency {
472 local($todo,$source,$modname) = @_;
474 if ($todo eq 'import') {
475 if ( $IgnoreMe{$modname} eq 'Y' ) {
476 # user specifically asked for this module
478 $follow_file = '__ignore__';
479 } elsif ( $ModuleIn{$modname} ) {
480 $follow_file = "$ModuleIn{$modname}/$modname.hi";
483 &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
487 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
490 if (! $follow_file) { # it didnae find anything
491 die "$orig_src_file: Couldn't handle: $_\n";
493 } else { # it found something
494 if ($follow_file ne '__ignore__') {
495 local($int_file) = $follow_file;
497 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
499 foreach $obj (@Obj_suffix) {
501 foreach $suff (@File_suffix) {
502 $str .= "$bf.${suff}_$obj ";
505 push(@Depend_lines, "$str: $int_file\n");
507 $int_file =~ s/\.l?hs$//;
508 $int_file =~ s/\.l?y$//;
509 $int_file =~ s/\.hi$//;
513 # if a version specific .hi-boot file exist, use it.
514 if ( -f "$int_file.hi-boot-${HiBootVersion}" ) {
515 $source_dep = "$int_file.hi-boot-${HiBootVersion}";
517 $source_dep = "$int_file.hi-boot";
521 foreach $obj (@Obj_suffix) {
524 push(@Depend_lines, "$str: $int_file.hi\n");
528 foreach $suff (@File_suffix) {
530 foreach $obj (@Obj_suffix) {
531 $str .= "$bf.${suff}_$obj ";
533 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
537 foreach $obj (@Obj_suffix) {
539 foreach $suff (@File_suffix) {
540 $str .= "$bf.${suff}_$obj ";
543 push(@Depend_lines, "$str: $source_dep\n");
550 # when we see something, we cache that fact ('y').
551 # also, when we get a miss, we cache that (so we don't try later); ('n')
554 sub find_in_Import_dirs {
555 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
557 local($do_magical_check) = 0;
558 local($name_to_check);
560 # do it the old hard way: hop along Import_dir list
561 foreach $import_dir (@Import_dirs) {
563 if ($import_dir eq '.') {
564 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
565 $do_magical_check = 1;
568 $name_to_check = "$import_dir/$modname.hi";
569 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
570 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
571 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
572 if (-f $name_to_check) {
573 $FileExists{$name_to_check} = 'y';
574 return($name_to_check) ;
576 $FileExists{$name_to_check} = 'n';
580 for $suff ('hs', 'lhs', 'ly', 'y') {
581 $name_to_check = "$import_dir/$modname.$suff";
582 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
583 return($name_to_check) if -f $name_to_check;
587 $name_to_check = "$import_dir/$modname.hi-boot-${HiBootVersion}";
588 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
589 return($name_to_check) if -f $name_to_check;
591 $name_to_check = "$import_dir/$modname.hi-boot";
592 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
593 return($name_to_check) if -f $name_to_check;
596 if ($do_magical_check == 1) {
597 $name_to_check = "$last_seen_dir/$modname.hi";
599 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
600 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
601 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
602 if (-f $name_to_check) {
603 $FileExists{$name_to_check} = 'y';
604 return($name_to_check) ;
606 $FileExists{$name_to_check} = 'n';
610 for $suff ('lhs', 'hs', 'ly', 'y') {
611 $name_to_check = "$last_seen_dir/$modname.$suff";
612 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
613 return($name_to_check) if -f $name_to_check;
617 # OK, maybe it's referring to something in a system library
618 #foreach $lib ( @Syslibs ) {
619 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
622 # Last hope: referring to a Prelude interface
623 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
625 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
628 sub find_in_Include_dirs {
629 local($orig_src_file, $name, $last_seen_dir) = @_;
631 local($do_magical_check) = 0;
633 # no funny name guessing here
635 # hop along Include_dir list
636 foreach $include_dir (@Include_dirs) {
637 $include_dir =~ s/^-I//;
640 if ($include_dir eq '.') {
641 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
642 $do_magical_check = 1;
644 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
645 if (-f "$include_dir/$name") {
646 return("$include_dir/$name");
649 if ($do_magical_check == 1) {
650 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
651 if (-f "$last_seen_dir/$name") {
652 return("$last_seen_dir/$name");
655 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
658 # out of the driver, actually
660 local($str_to_do, $file_to_read, $tidy_name) = @_;
662 print STDERR "\n$tidy_name:\n\t" if $Verbose;
663 print STDERR "$str_to_do\n" if $Verbose;
665 local($return_val) = system($str_to_do) >> 8;
667 if ($return_val != 0) {
668 local($die_msg) = "$Pgm: Running $tidy_name ( on $file_to_read ) failed";
669 $die_msg .= " program not found: $str_to_do " if $return_val == 255;
670 $die_msg .= " ($!)" if $Verbose && $! != 0;
672 print STDERR $die_msg;
677 # out of the driver too.
678 sub check_for_source_options {
680 local($comment_start,$comment_end);
682 # Assume it is a file containing Haskell source
683 $comment_start = "{-#";
684 $comment_end = "#-}";
686 open(FILE,$file) || return ""; # No big loss
689 if ( /^${comment_start} OPTIONS (.*)${comment_end}/ ) {
690 # return the options.
692 print STDERR "Found OPTIONS $stuff in $file\n" if $Verbose;
696 elsif ( /^$/ ) { # ignore empty lines
699 elsif ( /^#line.+$/ ) { # ignore comment lines (unused..ToDo: rm )
702 elsif ( /^{-# LINE.+$/ ) { # ignore line pragmas
705 else { # stop looking, something non-empty / not
706 # ${comment_start} OPTIONS .. ${comment_end} encountered.
707 close(FILE);return "";