2 # perl script expect the following variables to be prepended:
4 # RAWCPP TMPDIR TOP_PWD
5 # libdir libexecdir datadir INSTALLING
6 # ProjectVersionInt SED
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 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
88 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
94 %LibIfaces = (); # known prelude/syslib ifaces; read from a file
96 # directories to considered stable.
99 $Include_dirs = '-I.';
102 $Include_prelude = 0;
104 # Delete temp. file if script is halted.
105 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
106 $SIG{'INT'} = 'quit_upon_signal';
107 $SIG{'QUIT'} = 'quit_upon_signal';
109 &mangle_command_line_args();
117 ( #OLD: "-D__HASKELL1__=$Haskell_1",
118 "-D__GLASGOW_HASKELL__=$ProjectVersionInt"));
120 @Import_dirs = split(/:/,$Import_dirs);
121 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
123 # set up array of ignored modules
124 local(@dirs) = ($INSTALLING) ?
125 ("$InstLibDirGhc/imports/std")
126 : ("$TopPwd/ghc/lib/std");
127 if (!$Include_prelude) {
128 push(@Ignore_dirs, @dirs);
130 push(@Import_dirs, @dirs);
133 foreach $lib ( @Syslibs ) {
136 if ( $lib eq 'win32' && ! $INSTALLING ) {
137 $dir = "${TopPwd}/hslibs/${lib}/src";
139 $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}"
140 : "${TopPwd}/ghc/lib/${lib}";
142 if (!$Include_prelude) {
143 push(@Ignore_dirs,$dir);
145 push(@Import_dirs, $dir);
150 # NB: We keep the scalar-variable equivalents to use in error messages
152 if ( ! $Makefile && -f 'makefile' ) {
153 $Makefile = 'makefile';
154 } elsif ( ! $Makefile && -f 'Makefile') {
155 $Makefile = 'Makefile';
156 } elsif ( ! $Makefile) {
157 die "$Pgm: no makefile or Makefile found\n";
160 print STDERR "CPP defines=@Defines\n" if $Verbose;
161 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
162 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
164 &preprocess_import_dirs();
168 foreach $sf (@Src_files) {
170 # just like lit-inputter
171 # except it puts each file through CPP and
172 # a de-commenter (not implemented);
173 # builds up @Depend_lines
174 print STDERR "Here we go for source file: $sf\n" if $Verbose;
175 ($bf = $sf) =~ s/\.l?(hs|y)$//;
178 foreach $obj (@Obj_suffix) {
180 foreach $suff (@File_suffix) {
181 $str .= "$bf.${suff}_$obj ";
184 push(@Depend_lines, "$str: $sf\n");
185 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
186 if ( $sf !~ /\.l(hs|y)$/ ) {
189 $file_to_read = "$Tmp_prefix.hs";
190 local($to_do) = "$Unlit $sf $file_to_read";
191 &run_something($to_do, $sf, 'unlit');
193 &slurp_file_for_imports($file_to_read, $sf);
195 # Delete the temporary.
196 if ( $sf =~ /\.l(hs|y)$/ ) {
197 unlink "$Tmp_prefix.hs";
203 # Create backup version of output file.
205 if ( ! -f $Makefile ) {
206 # truncate() may not be implemented, so we
209 open(TRUNC,"> $Makefile.bak") && close(TRUNC);
211 rename($Makefile,"$Makefile.bak");
213 # now copy Makefile.bak into Makefile, rm'ing old dependencies
215 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
216 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
219 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
223 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
226 # insert dependencies
227 print "\n$Begin_magic_str", @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
229 while (<OMKF>) { # copy the rest through
230 print $_ || die "Failed to write out dependencies ($!)";
232 close(NMKF) || exit(1);
233 close(OMKF) || exit(1);
236 sub mangle_command_line_args {
237 while($_ = $ARGV[0]) {
243 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
245 } elsif ( /^-cpp$/ ) { # recognized wherever they occur
248 $Import_dirs = ''; # import path cleared!
249 } elsif ( /^-i(.*)/ ) {
250 $Import_dirs = "$1:$Import_dirs";
252 $Include_dirs .= " $_";
253 } elsif ( /^-syslib$/ ) {
254 push(@Syslibs, &grab_arg_arg($_,''));
255 } elsif ( /^-fglasgow-exts$/ ) {
256 push(@Syslibs, 'exts');
257 } elsif ( /^-concurrent$/ ) {
258 push(@Syslibs, 'concurrent');
259 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
264 } elsif ( /^-f(.*)/ ) {
265 $Makefile = &grab_arg_arg('-f',$1);
266 } elsif ( /^-o(.*)/ ) {
267 local($suff) = &grab_arg_arg('-o',$1);
268 # Weed out existing entry .. there must be a better way of doing this
269 # with arrays (non-assoc) ! -- sof
270 @Obj_suffix = grep(!/$suff/,@Obj_suffix);
271 push(@Obj_suffix, $suff);
273 # --exclude-module=mod => it's stable, trust me!
275 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
276 local($thing) = &grab_arg_arg($1,$2);
277 $IgnoreMe{$thing} = 'Y';
278 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
279 foreach $d ( split(/:/,&grab_arg_arg($1, $2)) ) {
280 push(@Ignore_dirs,"$d");
282 } elsif ( /^--include-module=(.*)/ ) {
283 local($thing) = &grab_arg_arg($1,$2);
284 $IgnoreMe{$thing} = 'n';
285 } elsif ( /^--include-prelude$/ ) {
286 $Include_prelude = 1;
287 } elsif ( /^-s(.*)/ ) {
288 local($suff) = &grab_arg_arg('-s',$1);
289 push(@File_suffix, $suff);
291 print STDERR "$Pgm: unknown option ignored: $_\n";
294 push(@Src_files, $_);
297 # Removed support for picking up plausible source file
298 # names inside the ghc options chunk of the command
299 # line. It failed to deal with `option-value' stuff present
300 # on some invocations of ghc (e.g., "-ohi foo.baz").
303 # } elsif ($Dashdashes_seen == 1) {
304 # push(@Src_files, $_) if ! /^-/;
307 @File_suffix = sort (@File_suffix);
311 local($option, $rest_of_arg) = @_;
314 return($rest_of_arg);
315 } elsif ($#ARGV >= 0) {
316 local($temp) = $ARGV[0]; shift(@ARGV);
319 print STDERR "$Pgm: no argument following $option option\n";
324 sub preprocess_import_dirs {
325 # it's probably cheaper to find out what's in all
326 # the @Import_dirs before we start processing.
331 foreach $d ( @Import_dirs ) {
332 # Check to see if it can be ignored
333 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
334 #next if $Ignore_dirs{$d};
336 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
338 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
339 next unless /(.*)\.hi$/;
341 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
342 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
344 $ModuleIn{$thing} = $d;
347 closedir(DIR); # No, don't check the error code
350 # Add all the modules
351 # to the IgnoreMe array before we start scanning for imports.
352 foreach $d (@Ignore_dirs) {
354 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
356 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
357 next unless /(.*)\.(hi|l?hs|l?y)$/;
358 #don't tag it twice or overwrite it with a diff. value
359 next if $IgnoreMe{$1};
360 print STDERR "Module $d will be ignored\n" if $Verbose;
364 closedir(DIR); # No, don't check the error code
368 sub slurp_file_for_imports {
369 local($file_to_read, $orig_src_file) = @_;
370 local($follow_file,$read_from_file);
373 local($last_seen_dir) = $orig_src_file;
374 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
375 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
377 local($mod_name) = $orig_src_file;
379 $mod_name =~ s/.*\/([^\/]+)$/$1/g;
380 $mod_name =~ s/^([^.]+)\.(.*)$/$1/;
382 print STDERR "Warning: processing module $mod_name, which I was supposed to ignore.\n"
383 if ( $IgnoreMe{$mod_name} eq 'Y' && $Warnings );
385 &process_dependency('import',0,'Prelude') if ($Include_prelude);
387 # we mangle #include's so they will also leave something
388 # behind to indicate the dependency on _them_
392 # $open_cmd = "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |";
393 &run_something("${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines 2>&1 > ${file_to_read}.i", $orig_src_file, 'cpp');
394 $read_from_file="${file_to_read}.i";
397 $read_from_file="${file_to_read}";
398 $open_cmd = $file_to_read;
400 print STDERR "$open_cmd\n" if $Verbose;
402 open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";
406 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
407 # the compiler will deal with the absence of Foo.hi by consulting the
408 # source for Foo directly. (for dealing with recursive modules).
410 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
412 $source = ( $2 ne '') ? 1 : 0;
415 next if $modname eq '';
416 &process_dependency($todo,$source,$modname);
418 close(SRCFILE) || exit(1);
419 # remove temporary file, if any.
421 # truncate() may not be implemented, so we
424 open(TRUNC,"> $read_from_file") && close(TRUNC);
430 sub process_dependency {
431 local($todo,$source,$modname) = @_;
433 if ($todo eq 'import') {
434 if ( $IgnoreMe{$modname} eq 'Y' ) {
435 # user specifically asked for this module
437 $follow_file = '__ignore__';
438 } elsif ( $ModuleIn{$modname} ) {
439 $follow_file = "$ModuleIn{$modname}/$modname.hi";
442 &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
446 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
449 if (! $follow_file) { # it didnae find anything
450 die "$orig_src_file: Couldn't handle: $_\n";
452 } else { # it found something
453 if ($follow_file ne '__ignore__') {
454 local($int_file) = $follow_file;
456 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
458 foreach $obj (@Obj_suffix) {
460 foreach $suff (@File_suffix) {
461 $str .= "$bf.${suff}_$obj ";
464 push(@Depend_lines, "$str: $int_file\n");
466 $int_file =~ s/\.l?hs$//;
467 $int_file =~ s/\.l?y$//;
468 $int_file =~ s/\.hi$//;
472 $source_dep = "$int_file.hi-boot";
475 foreach $obj (@Obj_suffix) {
478 push(@Depend_lines, "$str: $int_file.hi\n");
482 foreach $suff (@File_suffix) {
484 foreach $obj (@Obj_suffix) {
485 $str .= "$bf.${suff}_$obj ";
487 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
491 foreach $obj (@Obj_suffix) {
493 foreach $suff (@File_suffix) {
494 $str .= "$bf.${suff}_$obj ";
497 push(@Depend_lines, "$str: $source_dep\n");
504 # when we see something, we cache that fact ('y').
505 # also, when we get a miss, we cache that (so we don't try later); ('n')
508 sub find_in_Import_dirs {
509 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
511 local($do_magical_check) = 0;
512 local($name_to_check);
514 # do it the old hard way: hop along Import_dir list
515 foreach $import_dir (@Import_dirs) {
517 if ($import_dir eq '.') {
518 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
519 $do_magical_check = 1;
522 $name_to_check = "$import_dir/$modname.hi";
523 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
524 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
525 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
526 if (-f $name_to_check) {
527 $FileExists{$name_to_check} = 'y';
528 return($name_to_check) ;
530 $FileExists{$name_to_check} = 'n';
534 for $suff ('hs', 'lhs', 'ly', 'y') {
535 $name_to_check = "$import_dir/$modname.$suff";
536 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
537 return($name_to_check) if -f $name_to_check;
541 $name_to_check = "$import_dir/$modname.hi-boot";
542 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
543 return($name_to_check) if -f $name_to_check;
546 if ($do_magical_check == 1) {
547 $name_to_check = "$last_seen_dir/$modname.hi";
549 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
550 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
551 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
552 if (-f $name_to_check) {
553 $FileExists{$name_to_check} = 'y';
554 return($name_to_check) ;
556 $FileExists{$name_to_check} = 'n';
560 for $suff ('lhs', 'hs', 'ly', 'y') {
561 $name_to_check = "$last_seen_dir/$modname.$suff";
562 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
563 return($name_to_check) if -f $name_to_check;
567 # OK, maybe it's referring to something in a system library
568 #foreach $lib ( @Syslibs ) {
569 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
572 # Last hope: referring to a Prelude interface
573 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
575 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
578 sub find_in_Include_dirs {
579 local($orig_src_file, $name, $last_seen_dir) = @_;
581 local($do_magical_check) = 0;
583 # no funny name guessing here
585 # hop along Include_dir list
586 foreach $include_dir (@Include_dirs) {
587 $include_dir =~ s/^-I//;
590 if ($include_dir eq '.') {
591 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
592 $do_magical_check = 1;
594 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
595 if (-f "$include_dir/$name") {
596 return("$include_dir/$name");
599 if ($do_magical_check == 1) {
600 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
601 if (-f "$last_seen_dir/$name") {
602 return("$last_seen_dir/$name");
605 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
608 # out of the driver, actually
610 local($str_to_do, $file_to_read, $tidy_name) = @_;
612 print STDERR "\n$tidy_name:\n\t" if $Verbose;
613 print STDERR "$str_to_do\n" if $Verbose;
615 local($return_val) = system($str_to_do) >> 8;
617 if ($return_val != 0) {
618 local($die_msg) = "$Pgm: Running $tidy_name ( on $file_to_read ) failed";
619 $die_msg .= " program not found: $str_to_do " if $return_val == 255;
620 $die_msg .= " ($!)" if $Verbose && $! != 0;
622 print STDERR $die_msg;