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.
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;
105 # Delete temp. file if script is halted.
106 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
107 $SIG{'INT'} = 'quit_upon_signal';
108 $SIG{'QUIT'} = 'quit_upon_signal';
110 &mangle_command_line_args();
117 @Import_dirs = split(/:/,$Import_dirs);
118 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
120 # set up array of ignored modules
121 local(@dirs) = ($INSTALLING) ?
122 ("$InstLibDirGhc/imports/std")
123 : ("$TopPwd/ghc/lib/std");
124 if (!$Include_prelude) {
125 push(@Ignore_dirs, @dirs);
127 push(@Import_dirs, @dirs);
130 foreach $lib ( @Syslibs ) {
133 if ( $lib eq 'win32' && ! $INSTALLING ) {
134 $dir = "${TopPwd}/hslibs/${lib}/src";
136 $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}"
137 : "${TopPwd}/ghc/lib/${lib}";
139 if (!$Include_prelude) {
140 push(@Ignore_dirs,$dir);
142 push(@Import_dirs, $dir);
147 # NB: We keep the scalar-variable equivalents to use in error messages
149 if ( ! $Makefile && -f 'makefile' ) {
150 $Makefile = 'makefile';
151 } elsif ( ! $Makefile && -f 'Makefile') {
152 $Makefile = 'Makefile';
153 } elsif ( ! $Makefile) {
154 die "$Pgm: no makefile or Makefile found\n";
157 print STDERR "CPP defines=@Defines\n" if $Verbose;
158 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
159 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
161 &preprocess_import_dirs();
165 foreach $sf (@Src_files) {
167 # just like lit-inputter
168 # except it puts each file through CPP and
169 # a de-commenter (not implemented);
170 # builds up @Depend_lines
171 print STDERR "Here we go for source file: $sf\n" if $Verbose;
172 ($bf = $sf) =~ s/\.l?(hs|y)$//;
175 foreach $obj (@Obj_suffix) {
177 foreach $suff (@File_suffix) {
178 $str .= "$bf.${suff}_$obj ";
181 push(@Depend_lines, "$str: $sf\n");
182 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
183 if ( $sf !~ /\.l(hs|y)$/ ) {
186 $file_to_read = "$Tmp_prefix.hs";
187 local($to_do) = "$Unlit $sf $file_to_read";
188 &run_something($to_do, $sf, 'unlit');
190 &slurp_file_for_imports($file_to_read, $sf);
192 # Delete the temporary.
193 if ( $sf =~ /\.l(hs|y)$/ ) {
194 unlink "$Tmp_prefix.hs";
200 # Create backup version of output file.
202 if ( ! -f $Makefile ) {
203 # truncate() may not be implemented, so we
206 open(TRUNC,"> $Makefile.bak") && close(TRUNC);
208 rename($Makefile,"$Makefile.bak");
210 # now copy Makefile.bak into Makefile, rm'ing old dependencies
212 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
213 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
216 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
220 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
223 # insert dependencies
224 print "\n$Begin_magic_str", @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
226 while (<OMKF>) { # copy the rest through
227 print $_ || die "Failed to write out dependencies ($!)";
229 close(NMKF) || exit(1);
230 close(OMKF) || exit(1);
233 sub mangle_command_line_args {
234 while($_ = $ARGV[0]) {
240 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
242 } elsif ( /^-cpp$/ ) { # recognized wherever they occur
245 $Import_dirs = ''; # import path cleared!
246 } elsif ( /^-i(.*)/ ) {
247 $Import_dirs = "$1:$Import_dirs";
249 $Include_dirs .= " $_";
250 } elsif ( /^-syslib$/ ) {
251 push(@Syslibs, &grab_arg_arg($_,''));
252 } elsif ( /^-fglasgow-exts$/ ) {
253 push(@Syslibs, 'exts');
254 } elsif ( /^-concurrent$/ ) {
255 push(@Syslibs, 'concurrent');
256 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
261 } elsif ( /^-f(.*)/ ) {
262 $Makefile = &grab_arg_arg('-f',$1);
263 } elsif ( /^-o(.*)/ ) {
264 local($suff) = &grab_arg_arg('-o',$1);
265 # Weed out existing entry .. there must be a better way of doing this
266 # with arrays (non-assoc) ! -- sof
267 @Obj_suffix = grep(!/$suff/,@Obj_suffix);
268 push(@Obj_suffix, $suff);
270 # --exclude-module=mod => it's stable, trust me!
272 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
273 local($thing) = &grab_arg_arg($1,$2);
274 $IgnoreMe{$thing} = 'Y';
275 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
276 foreach $d ( split(/:/,&grab_arg_arg($1, $2)) ) {
277 push(@Ignore_dirs,"$d");
279 } elsif ( /^--include-module=(.*)/ ) {
280 local($thing) = &grab_arg_arg($1,$2);
281 $IgnoreMe{$thing} = 'n';
282 } elsif ( /^--include-prelude$/ ) {
283 $Include_prelude = 1;
284 } elsif ( /^-s(.*)/ ) {
285 local($suff) = &grab_arg_arg('-s',$1);
286 push(@File_suffix, $suff);
288 print STDERR "$Pgm: unknown option ignored: $_\n";
291 push(@Src_files, $_);
294 # Removed support for picking up plausible source file
295 # names inside the ghc options chunk of the command
296 # line. It failed to deal with `option-value' stuff present
297 # on some invocations of ghc (e.g., "-ohi foo.baz").
300 # } elsif ($Dashdashes_seen == 1) {
301 # push(@Src_files, $_) if ! /^-/;
304 @File_suffix = sort (@File_suffix);
308 local($option, $rest_of_arg) = @_;
311 return($rest_of_arg);
312 } elsif ($#ARGV >= 0) {
313 local($temp) = $ARGV[0]; shift(@ARGV);
316 print STDERR "$Pgm: no argument following $option option\n";
321 sub preprocess_import_dirs {
322 # it's probably cheaper to find out what's in all
323 # the @Import_dirs before we start processing.
328 foreach $d ( @Import_dirs ) {
329 # Check to see if it can be ignored
330 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
331 #next if $Ignore_dirs{$d};
333 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
335 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
336 next unless /(.*)\.hi$/;
338 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
339 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
341 $ModuleIn{$thing} = $d;
344 closedir(DIR); # No, don't check the error code
347 # Add all the modules
348 # to the IgnoreMe array before we start scanning for imports.
349 foreach $d (@Ignore_dirs) {
351 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
353 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
354 next unless /(.*)\.(hi|l?hs|l?y)$/;
355 #don't tag it twice or overwrite it with a diff. value
356 next if $IgnoreMe{$1};
357 print STDERR "Module $d will be ignored\n" if $Verbose;
361 closedir(DIR); # No, don't check the error code
365 sub slurp_file_for_imports {
366 local($file_to_read, $orig_src_file) = @_;
367 local($follow_file,$read_from_file);
370 local($last_seen_dir) = $orig_src_file;
371 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
372 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
374 local($mod_name) = $orig_src_file;
376 $mod_name =~ s/.*\/([^\/]+)$/$1/g;
377 $mod_name =~ s/^([^.]+)\.(.*)$/$1/;
379 print STDERR "Warning: processing module $mod_name, which I was supposed to ignore.\n"
380 if ( $IgnoreMe{$mod_name} eq 'Y' && $Warnings );
382 &process_dependency('import',0,'Prelude') if ($Include_prelude);
384 # we mangle #include's so they will also leave something
385 # behind to indicate the dependency on _them_
389 # $open_cmd = "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |";
390 &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');
391 $read_from_file="${file_to_read}.i";
394 $read_from_file="${file_to_read}";
395 $open_cmd = $file_to_read;
397 print STDERR "$open_cmd\n" if $Verbose;
399 open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";
403 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
404 # the compiler will deal with the absence of Foo.hi by consulting the
405 # source for Foo directly. (for dealing with recursive modules).
407 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
409 $source = ( $2 ne '') ? 1 : 0;
412 next if $modname eq '';
413 &process_dependency($todo,$source,$modname);
415 close(SRCFILE) || exit(1);
416 # remove temporary file, if any.
418 # truncate() may not be implemented, so we
421 open(TRUNC,"> $read_from_file") && close(TRUNC);
427 sub process_dependency {
428 local($todo,$source,$modname) = @_;
430 if ($todo eq 'import') {
431 if ( $IgnoreMe{$modname} eq 'Y' ) {
432 # user specifically asked for this module
434 $follow_file = '__ignore__';
435 } elsif ( $ModuleIn{$modname} ) {
436 $follow_file = "$ModuleIn{$modname}/$modname.hi";
439 &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
443 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
446 if (! $follow_file) { # it didnae find anything
447 die "$orig_src_file: Couldn't handle: $_\n";
449 } else { # it found something
450 if ($follow_file ne '__ignore__') {
451 local($int_file) = $follow_file;
453 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
455 foreach $obj (@Obj_suffix) {
457 foreach $suff (@File_suffix) {
458 $str .= "$bf.${suff}_$obj ";
461 push(@Depend_lines, "$str: $int_file\n");
463 $int_file =~ s/\.l?hs$//;
464 $int_file =~ s/\.l?y$//;
465 $int_file =~ s/\.hi$//;
469 $source_dep = "$int_file.hi-boot";
472 foreach $obj (@Obj_suffix) {
475 push(@Depend_lines, "$str: $int_file.hi\n");
479 foreach $suff (@File_suffix) {
481 foreach $obj (@Obj_suffix) {
482 $str .= "$bf.${suff}_$obj ";
484 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
488 foreach $obj (@Obj_suffix) {
490 foreach $suff (@File_suffix) {
491 $str .= "$bf.${suff}_$obj ";
494 push(@Depend_lines, "$str: $source_dep\n");
501 # when we see something, we cache that fact ('y').
502 # also, when we get a miss, we cache that (so we don't try later); ('n')
505 sub find_in_Import_dirs {
506 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
508 local($do_magical_check) = 0;
509 local($name_to_check);
511 # do it the old hard way: hop along Import_dir list
512 foreach $import_dir (@Import_dirs) {
514 if ($import_dir eq '.') {
515 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
516 $do_magical_check = 1;
519 $name_to_check = "$import_dir/$modname.hi";
520 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
521 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
522 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
523 if (-f $name_to_check) {
524 $FileExists{$name_to_check} = 'y';
525 return($name_to_check) ;
527 $FileExists{$name_to_check} = 'n';
531 for $suff ('hs', 'lhs', 'ly', 'y') {
532 $name_to_check = "$import_dir/$modname.$suff";
533 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
534 return($name_to_check) if -f $name_to_check;
538 $name_to_check = "$import_dir/$modname.hi-boot";
539 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
540 return($name_to_check) if -f $name_to_check;
543 if ($do_magical_check == 1) {
544 $name_to_check = "$last_seen_dir/$modname.hi";
546 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
547 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
548 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
549 if (-f $name_to_check) {
550 $FileExists{$name_to_check} = 'y';
551 return($name_to_check) ;
553 $FileExists{$name_to_check} = 'n';
557 for $suff ('lhs', 'hs', 'ly', 'y') {
558 $name_to_check = "$last_seen_dir/$modname.$suff";
559 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
560 return($name_to_check) if -f $name_to_check;
564 # OK, maybe it's referring to something in a system library
565 #foreach $lib ( @Syslibs ) {
566 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
569 # Last hope: referring to a Prelude interface
570 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
572 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
575 sub find_in_Include_dirs {
576 local($orig_src_file, $name, $last_seen_dir) = @_;
578 local($do_magical_check) = 0;
580 # no funny name guessing here
582 # hop along Include_dir list
583 foreach $include_dir (@Include_dirs) {
584 $include_dir =~ s/^-I//;
587 if ($include_dir eq '.') {
588 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
589 $do_magical_check = 1;
591 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
592 if (-f "$include_dir/$name") {
593 return("$include_dir/$name");
596 if ($do_magical_check == 1) {
597 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
598 if (-f "$last_seen_dir/$name") {
599 return("$last_seen_dir/$name");
602 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
605 # out of the driver, actually
607 local($str_to_do, $file_to_read, $tidy_name) = @_;
609 print STDERR "\n$tidy_name:\n\t" if $Verbose;
610 print STDERR "$str_to_do\n" if $Verbose;
612 local($return_val) = system($str_to_do) >> 8;
614 if ($return_val != 0) {
615 local($die_msg) = "$Pgm: Running $tidy_name ( on $file_to_read ) failed";
616 $die_msg .= " program not found: $str_to_do " if $return_val == 255;
617 $die_msg .= " ($!)" if $Verbose && $! != 0;
619 print STDERR $die_msg;