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).
30 -fhaskell1.[2-9] Deal with the oddities associated with a
31 particular version of Haskell 1.
33 mkdependHS-specific options (not between --'s):
36 -v -v Be very verbose.
37 -f blah Use "blah" as the makefile, rather than "makefile"
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
52 --include-module=<file>
53 Regard <file> as not "stable"; i.e., generate dependencies
54 on it (if any). This option is normally used in conjunction
55 with the --exclude-directory option.
59 $Status = 0; # just used for exit() status
60 $Verbose = 0; # 1 => verbose, 2 => very verbose
63 # Try to guess how to run gcc's CPP directly -------------
66 if ( $OrigCpp !~ /(\S+)\s+(.*)/ ) {
71 if ( -x $cmd ) { # cool
73 } else { # oops; try to guess
74 $GccV = `gcc -v 2>&1`;
75 if ( $GccV =~ /Reading specs from (.*)\/specs/ ) {
76 $Cpp = "$1/cpp $rest";
78 die "hscpp: don't know how to run cpp: $OrigCpp\n";
83 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
84 $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependHS$$";
86 $Tmp_prefix ="${TMPDIR}/mkdependHS$$";
87 $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
90 $TopPwd = "${TOP_PWD}";
91 $InstLibDirGhc = "${libdir}";
92 $InstLibExecDirGhc = "${libexecdir}";
93 $InstHsLibDirGhc = "${libdir}/hslibs";
94 $InstDataDirGhc = "${datadir}";
96 $Unlit = ($INSTALLING) ? "${InstLibExecDirGhc}/unlit"
97 : "${TopPwd}/ghc/utils/unlit/unlit";
99 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
100 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
102 $ghc_version_info = int ( ${PROJECTVERSION} * 100 );
106 %LibIfaces = (); # known prelude/syslib ifaces; read from a file
109 # directories to considered stable.
110 @Ignore_dirs = ($INSTALLING) ?
111 ("$InstLibDirGhc/imports")
112 : ("$TopPwd/ghc/lib/ghc",
113 "$TopPwd/ghc/lib/required",
114 "$TopPwd/ghc/lib/glaExts",
115 "$TopPwd/ghc/lib/concurrent");
117 $Haskell_1 = 3; # assume Haskell 1.3. Changed by -fhaskell-1.?
118 $Include_dirs = '-I.';
122 &mangle_command_line_args();
124 # set up array of ignored modules
125 foreach $lib ( @Syslibs ) {
127 ($INSTALLING) ? "${InstHsLibDirGhc}/${lib}/imports"
128 : "${TopPwd}/hslibs/${lib}/src";
129 push(@Ignore_dirs,$dir);
138 ("-D__HASKELL1__=$Haskell_1",
139 "-D__GLASGOW_HASKELL__=$ghc_version_info"));
141 @Import_dirs = split(/:/,$Import_dirs);
142 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
143 # NB: We keep the scalar-variable equivalents to use in error messages
145 if ( ! $Makefile && -f 'makefile' ) {
146 $Makefile = 'makefile';
147 } elsif ( ! $Makefile && -f 'Makefile') {
148 $Makefile = 'Makefile';
149 } elsif ( ! $Makefile) {
150 die "$Pgm: no makefile or Makefile found\n";
153 print STDERR "CPP defines=@Defines\n" if $Verbose;
154 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
155 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
157 &preprocess_import_dirs();
161 foreach $sf (@Src_files) {
162 # just like lit-inputter
163 # except it puts each file through CPP and
164 # a de-commenter (not implemented);
165 # builds up @Depend_lines
166 print STDERR "Here we go for source file: $sf\n" if $Verbose;
167 ($bf = $sf) =~ s/\.l?(hs|y)$//;
168 #push(@Depend_lines, "$bf.$Obj_suffix $bf.hi : $sf\n");
169 push(@Depend_lines, "$bf.$Obj_suffix : $sf\n");
170 foreach $suff (@File_suffix) {
171 push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $sf\n");
174 # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it:
175 if ( $sf !~ /\.l(hs|y)$/ ) {
178 $file_to_read = "$Tmp_prefix.hs";
179 local($to_do) = "$Unlit $sf $file_to_read";
180 &run_something($to_do, 'unlit');
182 &slurp_file_for_imports($file_to_read, $sf);
184 if ( $sf =~ /\.l(hs|y)$/ ) {
185 unlink "$Tmp_prefix.hs";
189 # OK, mangle the Makefile
190 unlink("$Makefile.bak");
193 # If no output file exist as yet, create one.
195 if ( ! -f $Makefile.bak ) {
196 system("touch $Makefile.bak");
198 if ( -f $Makefile ) {
199 rename($Makefile,"$Makefile.bak");
201 # now copy Makefile.bak into Makefile, rm'ing old dependencies
203 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
204 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
207 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
211 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
214 # insert dependencies
215 print $Begin_magic_str;
217 print $End_magic_str;
218 while (<OMKF>) { # copy the rest through
221 close(NMKF) || exit(1);
222 close(OMKF) || exit(1);
225 sub mangle_command_line_args {
226 while($_ = $ARGV[0]) {
232 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
234 } elsif ( /^-i(.*)/ ) {
235 $Import_dirs .= ":$1";
237 $Include_dirs .= " $_";
238 } elsif ( /^-syslib$/ ) {
239 push(@Syslibs, &grab_arg_arg($_,''));
240 } elsif ( /^-fhaskell-1\.([2-9])/ ) {
242 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
245 } elsif ( /^-f(.*)/ ) {
246 $Makefile = &grab_arg_arg('-f',$1);
247 } elsif ( /^-o(.*)/ ) {
248 $Obj_suffix = &grab_arg_arg('-o',$1);
250 # --exclude-module=mod => it's stable, trust me!
252 } elsif ( /^-(x|-exclude-module=)(.*)/ ) {
253 local($thing) = &grab_arg_arg($1,$2);
254 $IgnoreMe{$thing} = 'y';
255 } elsif ( /^-(X|-exclude-directory=)(.*)/ ) {
256 foreach $d ( split(/:/,&grab_arg_arg($1, $2)) ) {
257 push(@Ignore_dirs,"$d");
259 } elsif ( /^--include-module=(.*)/ ) {
260 local($thing) = &grab_arg_arg($1,$2);
261 $IgnoreMe{$thing} = 'n';
262 } elsif ( /^-s(.*)/ ) {
263 local($suff) = &grab_arg_arg('-s',$1);
264 push(@File_suffix, $suff);
266 print STDERR "$Pgm: unknown option ignored: $_\n";
269 push(@Src_files, $_);
272 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
273 push(@Src_files, $_) if ! /^-/;
276 @File_suffix = sort (@File_suffix);
280 local($option, $rest_of_arg) = @_;
283 return($rest_of_arg);
284 } elsif ($#ARGV >= 0) {
285 local($temp) = $ARGV[0]; shift(@ARGV);
288 print STDERR "$Pgm: no argument following $option option\n";
293 sub preprocess_import_dirs {
294 # it's probably cheaper to find out what's in all
295 # the @Import_dirs before we start processing.
300 foreach $d ( @Import_dirs ) {
301 # Check to see if it can be ignored
302 #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
303 #next if $Ignore_dirs{$d};
305 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
307 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
308 next unless /(.*)\.hi$/;
310 if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
311 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n";
313 $ModuleIn{$thing} = $d;
316 closedir(DIR); # No, don't check the error code
319 # Add all the modules
320 # to the IgnoreMe array before we start scanning for imports.
321 foreach $d (@Ignore_dirs) {
323 opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
325 for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
326 next unless /(.*)\.(hi|l?hs|l?y)$/;
327 #don't tag it twice or overwrite it with a diff. value
328 next if $IgnoreMe{$1};
329 print STDERR "Module $d/$1.$2 will be ignored\n" if $Verbose;
333 closedir(DIR); # No, don't check the error code
337 sub slurp_file_for_imports {
338 local($file_to_read, $orig_src_file) = @_;
341 local($last_seen_dir) = $orig_src_file;
342 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
343 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
345 # we mangle #include's so they will also leave something
346 # behind to indicate the dependency on _them_
348 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;
350 open(SRCFILE, "${SED} -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |")
351 || die "$Pgm: Can't open $file_to_read: $!\n";
355 # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
356 # the compiler will deal with the absence of Foo.hi by consulting the
357 # source for Foo directly. (for dealing with recursive modules).
359 next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
361 $source = ( $2 ne '') ? 1 : 0;
364 next if $modname eq '';
365 if ($todo eq 'import') {
366 if ( $ModuleIn{$modname} ) {
367 $follow_file = "$ModuleIn{$modname}/$modname.hi";
370 = &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir );
374 = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
377 if (! $follow_file) { # it didnae find anything
378 die "$orig_src_file: Couldn't handle: $_\n";
380 } else { # it found something
381 if ($follow_file ne '__ignore__') {
383 $int_file = $follow_file;
385 if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
386 push(@Depend_lines, "$bf.$Obj_suffix : $int_file\n");
387 foreach $suff (@File_suffix) {
388 push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $int_file\n");
392 $int_file =~ s/\.l?hs$//;
393 $int_file =~ s/\.l?y$//;
394 $int_file =~ s/\.hi$//;
397 if ( $source && -f "$int_file.hs" ) {
398 $source_dep = "$int_file.hs";
399 push(@Depend_lines, "$bf.$Obj_suffix : $source_dep\n");
400 } elsif ( $source && -f "$int_file.lhs" ) {
401 $source_dep = "$int_file.lhs";
402 push(@Depend_lines, "$bf.$Obj_suffix : $source_dep\n");
403 } elsif ( $source && ( -f "$int_file.ly" || -f "$int_file.y" ) ) {
404 $source_dep = "$int_file.hs";
405 push(@Depend_lines, "$bf.$Obj_suffix : $source_dep\n");
408 print STDERR "Warning: could not find source file dependency $int_file.(hs|lhs|y|ly)\n";
410 push(@Depend_lines, "$bf.$Obj_suffix : $int_file.hi\n");
414 foreach $suff (@File_suffix) {
415 push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $int_file.${suff}_hi\n");
418 foreach $suff (@File_suffix) {
419 push(@Depend_lines, "$bf.${suff}_$Obj_suffix : $source_dep\n");
426 close(SRCFILE) || exit(1);
429 # when we see something, we cache that fact ('y').
430 # also, when we get a miss, we cache that (so we don't try later); ('n')
433 sub find_in_Import_dirs {
434 local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
436 local($do_magical_check) = 0;
437 local($name_to_check);
439 # do it the old hard way: hop along Import_dir list
440 foreach $import_dir (@Import_dirs) {
442 if ($import_dir eq '.') {
443 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
444 $do_magical_check = 1;
447 $name_to_check = "$import_dir/$modname.hi";
448 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
449 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
450 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
451 if (-f $name_to_check) {
452 $FileExists{$name_to_check} = 'y';
453 return($name_to_check) ;
455 $FileExists{$name_to_check} = 'n';
459 for $suff ('hs', 'lhs', 'ly', 'y') {
460 $name_to_check = "$import_dir/$modname.$suff";
461 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
462 return($name_to_check) if -f $name_to_check;
465 if ($do_magical_check == 1) {
466 $name_to_check = "$last_seen_dir/$modname.hi";
468 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
469 print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
470 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
471 if (-f $name_to_check) {
472 $FileExists{$name_to_check} = 'y';
473 return($name_to_check) ;
475 $FileExists{$name_to_check} = 'n';
479 for $suff ('lhs', 'hs', 'ly', 'y') {
480 $name_to_check = "$last_seen_dir/$modname.$suff";
481 print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
482 return($name_to_check) if -f $name_to_check;
486 # OK, maybe it's referring to something in a system library
487 #foreach $lib ( @Syslibs ) {
488 # return('__ignore__') if $LibIfaces{"$lib:$modname"};
491 # Last hope: referring to a Prelude interface
492 return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
494 die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
497 sub find_in_Include_dirs {
498 local($orig_src_file, $name, $last_seen_dir) = @_;
500 local($do_magical_check) = 0;
502 # no funny name guessing here
504 # hop along Include_dir list
505 foreach $include_dir (@Include_dirs) {
506 $include_dir =~ s/^-I//;
509 if ($include_dir eq '.') {
510 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
511 $do_magical_check = 1;
513 print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
514 if (-f "$include_dir/$name") {
515 return("$include_dir/$name");
518 if ($do_magical_check == 1) {
519 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
520 if (-f "$last_seen_dir/$name") {
521 return("$last_seen_dir/$name");
524 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
527 # out of the driver, actually
529 local($str_to_do, $tidy_name) = @_;
531 print STDERR "\n$tidy_name:\n\t" if $Verbose;
532 print STDERR "$str_to_do\n" if $Verbose;
534 local($return_val) = system($str_to_do) >> 8;
536 if ($return_val != 0) {
537 local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
538 $die_msg .= " (program not found)" if $return_val == 255;
539 $die_msg .= " ($!)" if $Verbose && $! != 0;
541 print STDERR $die_msg;