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 -o <osuf> Use <osuf> as the "object file" suffix ( default: o)
39 -s <suf> Make extra dependencies for files with
40 suffix <suf><osuf>; thus, "-o hc -s _a" will
41 make dependencies both for .hc files and for .a_hc
42 files. (Useful in conjunction with NoFib "ways".)
43 --exclude-module=<file>
44 Regard <file> as "stable"; i.e., eXclude it from having
46 -x same as --exclude-module
47 --exclude-directory=<dirs>
48 Regard : separated list of directories as containing stable,
49 don't generate any dependencies on modules therein.
50 -Xdirs same as --exclude-directory
52 Regard prelude libraries as unstable, i.e., generate dependencies
53 on prelude modules. This option is normally only used by the
54 various system libraries. If a -syslib option is used, dependencies
55 will also be generated on the library's interfaces.
56 --include-module=<file>
57 Regard <file> as not "stable"; i.e., generate dependencies
58 on it (if any). This option is normally used in conjunction
59 with the --exclude-directory option.
63 $Status = 0; # just used for exit() status
64 $Verbose = 0; # 1 => verbose, 2 => very verbose
65 $Warnings = 1; # 1 => warn about duplicate interface files
70 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
71 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependHS$$";
73 $Tmp_prefix ="${TMPDIR}/mkdependHS$$";
74 $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
77 $TopPwd = "${TOP_PWD}";
78 $InstLibDirGhc = "${libdir}";
79 $InstLibExecDirGhc = "${libexecdir}";
80 $InstHsLibDirGhc = "${libdir}/hslibs";
81 $InstDataDirGhc = "${datadir}";
83 $Unlit = ($INSTALLING) ? "${InstLibExecDirGhc}/unlit"
84 : "${TopPwd}/ghc/utils/unlit/unlit";
86 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
87 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
90 $ghc_version_info = ${ProjectVersionInt};
94 %LibIfaces = (); # known prelude/syslib ifaces; read from a file
96 # directories to considered stable.
99 $Include_dirs = '-I.';
102 $Include_prelude = 0;
104 &mangle_command_line_args();
112 ( #OLD: "-D__HASKELL1__=$Haskell_1",
113 "-D__GLASGOW_HASKELL__=$ghc_version_info"));
115 @Import_dirs = split(/:/,$Import_dirs);
116 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
118 # set up array of ignored modules
119 local(@dirs) = ($INSTALLING) ?
120 ("$InstLibDirGhc/imports/std")
121 : ("$TopPwd/ghc/lib/std");
122 if (!$Include_prelude) {
123 push(@Ignore_dirs, @dirs);
125 push(@Import_dirs, @dirs);
128 foreach $lib ( @Syslibs ) {
130 ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}"
131 : "${TopPwd}/ghc/lib/${lib}";
132 if (!$Include_prelude) {
133 push(@Ignore_dirs,$dir);
135 push(@Import_dirs, @dirs);
140 # NB: We keep the scalar-variable equivalents to use in error messages
142 if ( ! $Makefile && -f 'makefile' ) {
143 $Makefile = 'makefile';
144 } elsif ( ! $Makefile && -f 'Makefile') {
145 $Makefile = 'Makefile';
146 } elsif ( ! $Makefile) {
147 die "$Pgm: no makefile or Makefile found\n";
150 print STDERR "CPP defines=@Defines\n" if $Verbose;
151 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
152 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
154 &preprocess_import_dirs();
158 # Delete temp. file if script is halted.
159 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
160 $SIG{'INT'} = 'quit_upon_signal';
161 $SIG{'QUIT'} = 'quit_upon_signal';
163 foreach $sf (@Src_files) {
165 # just like lit-inputter
166 # except it puts each file through CPP and
167 # a de-commenter (not implemented);
168 # builds up @Depend_lines
169 print STDERR "Here we go for source file: $sf\n" if $Verbose;
170 ($bf = $sf) =~ s/\.l?(hs|y)$//;
173 foreach $obj (@Obj_suffix) {
175 foreach $suff (@File_suffix) {
176 $str .= "$bf.${suff}_$obj ";
179 push(@Depend_lines, "$str: $sf\n");
180 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
181 if ( $sf !~ /\.l(hs|y)$/ ) {
184 $file_to_read = "$Tmp_prefix.hs";
185 local($to_do) = "$Unlit $sf $file_to_read";
186 &run_something($to_do, 'unlit');
188 &slurp_file_for_imports($file_to_read, $sf);
190 if ( $sf =~ /\.l(hs|y)$/ ) {
191 unlink "$Tmp_prefix.hs";
197 # Create backup version of output file.
199 if ( ! -f $Makefile ) {
200 # truncate() may not be implemented, so we
203 open(TRUNC,"> $Makefile.bak") && close(TRUNC);
205 rename($Makefile,"$Makefile.bak");
207 # now copy Makefile.bak into Makefile, rm'ing old dependencies
209 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
210 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
213 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
217 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
220 # insert dependencies
221 print $Begin_magic_str, @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
223 while (<OMKF>) { # copy the rest through
224 print $_ || die "Failed to write out dependencies ($!)";
226 close(NMKF) || exit(1);
227 close(OMKF) || exit(1);
230 sub mangle_command_line_args {
231 while($_ = $ARGV[0]) {
237 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
240 $Import_dirs = ''; # import path cleared!
241 } elsif ( /^-i(.*)/ ) {
242 $Import_dirs = "$1:$Import_dirs";
244 $Include_dirs .= " $_";
245 } elsif ( /^-syslib$/ ) {
246 push(@Syslibs, &grab_arg_arg($_,''));
247 } elsif ( /^-fglasgow-exts$/ ) {
248 push(@Syslibs, 'exts');
249 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
254 } elsif ( /^-f(.*)/ ) {
255 $Makefile = &grab_arg_arg('-f',$1);
256 } elsif ( /^-o(.*)/ ) {
257 local($suff) = &grab_arg_arg('-o',$1);
258 # Weed out existing entry .. there must be a better way of doing this
259 # with arrays (non-assoc) ! -- sof
260 @Obj_suffix = grep(!/$suff/,@Obj_suffix);
261 push(@Obj_suffix, $suff);
263 # --exclude-module=mod => it's stable, trust me!
265 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
266 local($thing) = &grab_arg_arg($1,$2);
267 $IgnoreMe{$thing} = 'y';
268 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
269 foreach $d ( split(/:/,&grab_arg_arg($1, $2)) ) {
270 push(@Ignore_dirs,"$d");
272 } elsif ( /^--include-module=(.*)/ ) {
273 local($thing) = &grab_arg_arg($1,$2);
274 $IgnoreMe{$thing} = 'n';
275 } elsif ( /^--include-prelude$/ ) {
276 $Include_prelude = 1;
277 } elsif ( /^-s(.*)/ ) {
278 local($suff) = &grab_arg_arg('-s',$1);
279 push(@File_suffix, $suff);
281 print STDERR "$Pgm: unknown option ignored: $_\n";
284 push(@Src_files, $_);
287 # Removed support for picking up plausible source file
288 # names inside the ghc options chunk of the command
289 # line. It failed to deal with `option-value' stuff present
290 # on some invocations of ghc (e.g., "-ohi foo.baz").
293 # } elsif ($Dashdashes_seen == 1) {
294 # push(@Src_files, $_) if ! /^-/;
297 @File_suffix = sort (@File_suffix);
301 local($option, $rest_of_arg) = @_;
304 return($rest_of_arg);
305 } elsif ($#ARGV >= 0) {
306 local($temp) = $ARGV[0]; shift(@ARGV);
309 print STDERR "$Pgm: no argument following $option option\n";
314 sub preprocess_import_dirs {
315 # it's probably cheaper to find out what's in all
316 # the @Import_dirs before we start processing.
321 foreach $d ( @Import_dirs ) {
322 # Check to see if it can be ignored
323 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
324 #next if $Ignore_dirs{$d};
326 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
328 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
329 next unless /(.*)\.hi$/;
331 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
332 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
334 $ModuleIn{$thing} = $d;
337 closedir(DIR); # No, don't check the error code
340 # Add all the modules
341 # to the IgnoreMe array before we start scanning for imports.
342 foreach $d (@Ignore_dirs) {
344 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
346 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
347 next unless /(.*)\.(hi|l?hs|l?y)$/;
348 #don't tag it twice or overwrite it with a diff. value
349 next if $IgnoreMe{$1};
350 print STDERR "Module $d/$1.$2 will be ignored\n" if $Verbose;
354 closedir(DIR); # No, don't check the error code
358 sub slurp_file_for_imports {
359 local($file_to_read, $orig_src_file) = @_;
362 local($last_seen_dir) = $orig_src_file;
363 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
364 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
366 &process_dependency('import',0,'Prelude') if ($Include_prelude);
368 # we mangle #include's so they will also leave something
369 # behind to indicate the dependency on _them_
371 print STDERR "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |\n" if $Verbose;
373 open(SRCFILE, "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |")
374 || die "$Pgm: Can't open $file_to_read: $!\n";
378 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
379 # the compiler will deal with the absence of Foo.hi by consulting the
380 # source for Foo directly. (for dealing with recursive modules).
382 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
384 $source = ( $2 ne '') ? 1 : 0;
387 next if $modname eq '';
388 &process_dependency($todo,$source,$modname);
390 close(SRCFILE) || exit(1);
395 sub process_dependency {
396 local($todo,$source,$modname) = @_;
398 if ($todo eq 'import') {
399 if ( $ModuleIn{$modname} ) {
400 $follow_file = "$ModuleIn{$modname}/$modname.hi";
403 &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
407 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
410 if (! $follow_file) { # it didnae find anything
411 die "$orig_src_file: Couldn't handle: $_\n";
413 } else { # it found something
414 if ($follow_file ne '__ignore__') {
415 local($int_file) = $follow_file;
417 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
419 foreach $obj (@Obj_suffix) {
421 foreach $suff (@File_suffix) {
422 $str .= "$bf.${suff}_$obj ";
425 push(@Depend_lines, "$str: $int_file\n");
427 $int_file =~ s/\.l?hs$//;
428 $int_file =~ s/\.l?y$//;
429 $int_file =~ s/\.hi$//;
433 $source_dep = "$int_file.hi-boot";
436 foreach $obj (@Obj_suffix) {
439 push(@Depend_lines, "$str: $int_file.hi\n");
443 foreach $suff (@File_suffix) {
445 foreach $obj (@Obj_suffix) {
446 $str .= "$bf.${suff}_$obj ";
448 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
452 foreach $obj (@Obj_suffix) {
454 foreach $suff (@File_suffix) {
455 $str .= "$bf.${suff}_$obj ";
458 push(@Depend_lines, "$str: $source_dep\n");
465 # when we see something, we cache that fact ('y').
466 # also, when we get a miss, we cache that (so we don't try later); ('n')
469 sub find_in_Import_dirs {
470 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
472 local($do_magical_check) = 0;
473 local($name_to_check);
475 # do it the old hard way: hop along Import_dir list
476 foreach $import_dir (@Import_dirs) {
478 if ($import_dir eq '.') {
479 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
480 $do_magical_check = 1;
483 $name_to_check = "$import_dir/$modname.hi";
484 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
485 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
486 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
487 if (-f $name_to_check) {
488 $FileExists{$name_to_check} = 'y';
489 return($name_to_check) ;
491 $FileExists{$name_to_check} = 'n';
495 for $suff ('hs', 'lhs', 'ly', 'y') {
496 $name_to_check = "$import_dir/$modname.$suff";
497 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
498 return($name_to_check) if -f $name_to_check;
502 $name_to_check = "$import_dir/$modname.hi-boot";
503 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
504 return($name_to_check) if -f $name_to_check;
507 if ($do_magical_check == 1) {
508 $name_to_check = "$last_seen_dir/$modname.hi";
510 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
511 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
512 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
513 if (-f $name_to_check) {
514 $FileExists{$name_to_check} = 'y';
515 return($name_to_check) ;
517 $FileExists{$name_to_check} = 'n';
521 for $suff ('lhs', 'hs', 'ly', 'y') {
522 $name_to_check = "$last_seen_dir/$modname.$suff";
523 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
524 return($name_to_check) if -f $name_to_check;
528 # OK, maybe it's referring to something in a system library
529 #foreach $lib ( @Syslibs ) {
530 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
533 # Last hope: referring to a Prelude interface
534 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
536 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
539 sub find_in_Include_dirs {
540 local($orig_src_file, $name, $last_seen_dir) = @_;
542 local($do_magical_check) = 0;
544 # no funny name guessing here
546 # hop along Include_dir list
547 foreach $include_dir (@Include_dirs) {
548 $include_dir =~ s/^-I//;
551 if ($include_dir eq '.') {
552 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
553 $do_magical_check = 1;
555 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
556 if (-f "$include_dir/$name") {
557 return("$include_dir/$name");
560 if ($do_magical_check == 1) {
561 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
562 if (-f "$last_seen_dir/$name") {
563 return("$last_seen_dir/$name");
566 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
569 # out of the driver, actually
571 local($str_to_do, $tidy_name) = @_;
573 print STDERR "\n$tidy_name:\n\t" if $Verbose;
574 print STDERR "$str_to_do\n" if $Verbose;
576 local($return_val) = system($str_to_do) >> 8;
578 if ($return_val != 0) {
579 local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
580 $die_msg .= " (program not found)" if $return_val == 255;
581 $die_msg .= " ($!)" if $Verbose && $! != 0;
583 print STDERR $die_msg;