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";
91 $ghc_version_info = ${ProjectVersionInt};
95 %LibIfaces = (); # known prelude/syslib ifaces; read from a file
97 # directories to considered stable.
100 $Include_dirs = '-I.';
103 $Include_prelude = 0;
105 &mangle_command_line_args();
113 ( #OLD: "-D__HASKELL1__=$Haskell_1",
114 "-D__GLASGOW_HASKELL__=$ghc_version_info"));
116 @Import_dirs = split(/:/,$Import_dirs);
117 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
119 # set up array of ignored modules
120 local(@dirs) = ($INSTALLING) ?
121 ("$InstLibDirGhc/imports/std")
122 : ("$TopPwd/ghc/lib/std");
123 if (!$Include_prelude) {
124 push(@Ignore_dirs, @dirs);
126 push(@Import_dirs, @dirs);
129 foreach $lib ( @Syslibs ) {
132 if ( $lib eq 'win32' && ! $INSTALLING ) {
133 $dir = "${TopPwd}/hslibs/${lib}/src";
135 $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}"
136 : "${TopPwd}/ghc/lib/${lib}";
138 if (!$Include_prelude) {
139 push(@Ignore_dirs,$dir);
141 push(@Import_dirs, $dir);
146 # NB: We keep the scalar-variable equivalents to use in error messages
148 if ( ! $Makefile && -f 'makefile' ) {
149 $Makefile = 'makefile';
150 } elsif ( ! $Makefile && -f 'Makefile') {
151 $Makefile = 'Makefile';
152 } elsif ( ! $Makefile) {
153 die "$Pgm: no makefile or Makefile found\n";
156 print STDERR "CPP defines=@Defines\n" if $Verbose;
157 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
158 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
160 &preprocess_import_dirs();
164 # Delete temp. file if script is halted.
165 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
166 $SIG{'INT'} = 'quit_upon_signal';
167 $SIG{'QUIT'} = 'quit_upon_signal';
169 foreach $sf (@Src_files) {
171 # just like lit-inputter
172 # except it puts each file through CPP and
173 # a de-commenter (not implemented);
174 # builds up @Depend_lines
175 print STDERR "Here we go for source file: $sf\n" if $Verbose;
176 ($bf = $sf) =~ s/\.l?(hs|y)$//;
179 foreach $obj (@Obj_suffix) {
181 foreach $suff (@File_suffix) {
182 $str .= "$bf.${suff}_$obj ";
185 push(@Depend_lines, "$str: $sf\n");
186 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
187 if ( $sf !~ /\.l(hs|y)$/ ) {
190 $file_to_read = "$Tmp_prefix.hs";
191 local($to_do) = "$Unlit $sf $file_to_read";
192 &run_something($to_do, 'unlit');
194 &slurp_file_for_imports($file_to_read, $sf);
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/$1.$2 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 &process_dependency('import',0,'Prelude') if ($Include_prelude);
379 # we mangle #include's so they will also leave something
380 # behind to indicate the dependency on _them_
384 # $open_cmd = "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |";
385 &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", 'Running cpp');
386 $read_from_file="${file_to_read}.i";
389 $read_from_file="${file_to_read}";
390 $open_cmd = $file_to_read;
392 print STDERR "$open_cmd\n" if $Verbose;
394 open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";
398 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
399 # the compiler will deal with the absence of Foo.hi by consulting the
400 # source for Foo directly. (for dealing with recursive modules).
402 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
404 $source = ( $2 ne '') ? 1 : 0;
407 next if $modname eq '';
408 &process_dependency($todo,$source,$modname);
410 close(SRCFILE) || exit(1);
411 # remove temporary file, if any.
413 # truncate() may not be implemented, so we
416 open(TRUNC,"> $read_from_file") && close(TRUNC);
422 sub process_dependency {
423 local($todo,$source,$modname) = @_;
425 if ($todo eq 'import') {
426 if ( $ModuleIn{$modname} ) {
427 $follow_file = "$ModuleIn{$modname}/$modname.hi";
430 &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
434 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
437 if (! $follow_file) { # it didnae find anything
438 die "$orig_src_file: Couldn't handle: $_\n";
440 } else { # it found something
441 if ($follow_file ne '__ignore__') {
442 local($int_file) = $follow_file;
444 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
446 foreach $obj (@Obj_suffix) {
448 foreach $suff (@File_suffix) {
449 $str .= "$bf.${suff}_$obj ";
452 push(@Depend_lines, "$str: $int_file\n");
454 $int_file =~ s/\.l?hs$//;
455 $int_file =~ s/\.l?y$//;
456 $int_file =~ s/\.hi$//;
460 $source_dep = "$int_file.hi-boot";
463 foreach $obj (@Obj_suffix) {
466 push(@Depend_lines, "$str: $int_file.hi\n");
470 foreach $suff (@File_suffix) {
472 foreach $obj (@Obj_suffix) {
473 $str .= "$bf.${suff}_$obj ";
475 push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
479 foreach $obj (@Obj_suffix) {
481 foreach $suff (@File_suffix) {
482 $str .= "$bf.${suff}_$obj ";
485 push(@Depend_lines, "$str: $source_dep\n");
492 # when we see something, we cache that fact ('y').
493 # also, when we get a miss, we cache that (so we don't try later); ('n')
496 sub find_in_Import_dirs {
497 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
499 local($do_magical_check) = 0;
500 local($name_to_check);
502 # do it the old hard way: hop along Import_dir list
503 foreach $import_dir (@Import_dirs) {
505 if ($import_dir eq '.') {
506 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
507 $do_magical_check = 1;
510 $name_to_check = "$import_dir/$modname.hi";
511 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
512 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
513 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
514 if (-f $name_to_check) {
515 $FileExists{$name_to_check} = 'y';
516 return($name_to_check) ;
518 $FileExists{$name_to_check} = 'n';
522 for $suff ('hs', 'lhs', 'ly', 'y') {
523 $name_to_check = "$import_dir/$modname.$suff";
524 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
525 return($name_to_check) if -f $name_to_check;
529 $name_to_check = "$import_dir/$modname.hi-boot";
530 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
531 return($name_to_check) if -f $name_to_check;
534 if ($do_magical_check == 1) {
535 $name_to_check = "$last_seen_dir/$modname.hi";
537 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
538 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
539 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
540 if (-f $name_to_check) {
541 $FileExists{$name_to_check} = 'y';
542 return($name_to_check) ;
544 $FileExists{$name_to_check} = 'n';
548 for $suff ('lhs', 'hs', 'ly', 'y') {
549 $name_to_check = "$last_seen_dir/$modname.$suff";
550 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
551 return($name_to_check) if -f $name_to_check;
555 # OK, maybe it's referring to something in a system library
556 #foreach $lib ( @Syslibs ) {
557 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
560 # Last hope: referring to a Prelude interface
561 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
563 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
566 sub find_in_Include_dirs {
567 local($orig_src_file, $name, $last_seen_dir) = @_;
569 local($do_magical_check) = 0;
571 # no funny name guessing here
573 # hop along Include_dir list
574 foreach $include_dir (@Include_dirs) {
575 $include_dir =~ s/^-I//;
578 if ($include_dir eq '.') {
579 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
580 $do_magical_check = 1;
582 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
583 if (-f "$include_dir/$name") {
584 return("$include_dir/$name");
587 if ($do_magical_check == 1) {
588 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
589 if (-f "$last_seen_dir/$name") {
590 return("$last_seen_dir/$name");
593 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
596 # out of the driver, actually
598 local($str_to_do, $tidy_name) = @_;
600 print STDERR "\n$tidy_name:\n\t" if $Verbose;
601 print STDERR "$str_to_do\n" if $Verbose;
603 local($return_val) = system($str_to_do) >> 8;
605 if ($return_val != 0) {
606 local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
607 $die_msg .= " (program not found)" if $return_val == 255;
608 $die_msg .= " ($!)" if $Verbose && $! != 0;
610 print STDERR $die_msg;