[project @ 2000-07-26 20:47:21 by michaelw]
[ghc-hetmet.git] / ghc / utils / mkdependHS / mkdependHS.prl
1 #
2 # perl script expect the following variables to be prepended:
3 #
4 #  RAWCPP TMPDIR TOP_PWD HscIfaceFileVersion
5 #  libdir libexecdir datadir INSTALLING
6 #  SED 
7 #
8 # tries to work like mkdependC - capable of dealing with:
9 #
10 #  -literate Haskell code (Bird tracks or Glasgow literate style) (.lhs)
11 #  -straight Haskell source (.hs)
12 #  -literate or straight Happy specs    (.ly)
13 #  
14 # ToDo: strip out all the .h junk
15 #
16 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
17 $Usage  = <<EOUSAGE;
18 Usage: $Pgm [mkdependHS options] [-- GHC options --] srcfile1 [srcfile2 ...]
19
20 Options recognised wherever they occur (mkdependHS or GHC):
21
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     -package <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     -osuf <osuf> Use <osuf> as the "object file" suffix ( default: o)
31
32 mkdependHS-specific options (not between --'s):
33
34     -v          Be verbose.
35     -v -v       Be very verbose.
36     -w          No warnings
37     -f blah     Use "blah" as the makefile, rather than "makefile"
38                 or "Makefile".
39     -cpp        Wash the input files through cpp(1).
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
46                 dependencies on it.
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-prelude
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 -package 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.
61                 
62 EOUSAGE
63
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
67 $Dashdashes_seen = 0;
68
69 $Cpp = ${RAWCPP};
70 $Cpp_flag_set = 0; # 1 => run cpp over input files.
71
72 if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
73     $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependHS$$";
74 } else {
75     $Tmp_prefix ="${TMPDIR}/mkdependHS$$";
76     $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
77 }
78
79 $TopPwd            = "${TOP_PWD}";
80 $InstLibDirGhc     = "${libdir}";
81 $InstLibExecDirGhc = "${libexecdir}";
82 $InstDataDirGhc    = "${datadir}";
83
84 $Unlit = ($INSTALLING) ? "${InstLibExecDirGhc}/unlit" 
85                        : "${TopPwd}/ghc/utils/unlit/unlit";
86
87 $HiBootVersion = $HscIfaceFileVersion;
88 $hi_boot_with_version = 0;
89
90 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
91 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
92 @Obj_suffix =  ("o");
93 @File_suffix = ();
94
95 $Import_dirs = '.';
96 %Syslibs = ();
97 %LibIfaces  = ();  # known prelude/syslib ifaces; read from a file
98 %IgnoreMe = ();
99 # directories to considered stable.
100 @Ignore_dirs = ();
101
102 %ModuleIn = ();
103
104 $Include_dirs = '-I.';
105 $Makefile = '';
106 @Src_files = ();
107 $Include_prelude = 0;
108 @Defines = ();
109 $ProcessingOptions=0;
110
111 # Delete temp. file if script is halted.
112 sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
113 $SIG{'INT'}  = 'quit_upon_signal';
114 $SIG{'QUIT'} = 'quit_upon_signal';
115
116 &mangle_command_line_args(@ARGV);
117
118 if ( $Status ) {
119     print stderr $Usage;
120     exit(1);
121 }
122
123 @Import_dirs  = split(/:/,$Import_dirs);
124 @Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it
125
126 # NB: We keep the scalar-variable equivalents to use in error messages
127
128 &gather_import_dirs();
129
130 if ( ! $Makefile && -f 'makefile' ) {
131     $Makefile = 'makefile';
132 } elsif ( ! $Makefile && -f 'Makefile') {
133     $Makefile = 'Makefile';
134 } elsif ( ! $Makefile) {
135     die "$Pgm: no makefile or Makefile found\n";
136 }
137
138 print STDERR "CPP defines=@Defines\n" if $Verbose;
139 print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
140 print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;
141
142 &preprocess_import_dirs();
143
144 @Depend_lines = ();
145
146 foreach $sf (@Src_files) {
147
148     # just like lit-inputter
149     # except it puts each file through CPP and
150     # a de-commenter (not implemented);
151     # builds up @Depend_lines
152     print STDERR "Here we go for source file: $sf\n" if $Verbose;
153     ($bf = $sf) =~ s/\.l?(hs|y)$//;
154
155     local($str)="";
156     foreach $obj  (@Obj_suffix) {
157         $str .= "$bf.$obj ";
158         foreach $suff (@File_suffix) {
159            $str .= "$bf.${suff}_$obj ";
160         }
161     }
162     push(@Depend_lines, "$str: $sf\n");
163     # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it: 
164     if ( $sf !~ /\.l(hs|y)$/ ) {
165         $file_to_read = $sf;
166     } else {
167         $file_to_read = "$Tmp_prefix.hs";
168         local($to_do) = "$Unlit $sf $file_to_read";
169         &run_something($to_do, $sf, 'unlit');
170     }
171     &slurp_file_for_imports($file_to_read, $sf);
172
173     # Delete the temporary.
174     if ( $sf =~ /\.l(hs|y)$/ ) {
175         unlink "$Tmp_prefix.hs";
176     }
177 }
178
179 #
180 # print dependencies
181 #
182 if ( $Makefile eq "-" ) {
183    #
184    # print dependencies to STDOUT
185    #
186    &print_deps();
187 } elsif ( ! -f $Makefile ) {
188    #
189    # $Makefile does not exist, just print the deps.
190    #
191    open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
192    select(NMKF);
193    &print_deps();
194    close(NMKF);
195 } else {
196    #
197    # Create backup version of output file, then generate a new one.
198    # 
199    rename($Makefile,"$Makefile.bak") || die "$Pgm: can't rename $Makefile: $!\n";
200    
201    # now copy Makefile.bak into Makefile, rm'ing old dependencies
202    # and adding the new
203    open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
204    open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
205    select(NMKF);
206
207    $_ = <OMKF>;
208    while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
209        print $_;
210        $_ = <OMKF>;
211    }
212    while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
213        $_ = <OMKF>;
214    }
215
216    &print_deps();
217    
218    while (<OMKF>) { # copy the rest through
219        print $_ || die "Failed to write out dependencies ($!)";
220    }
221    close(NMKF) || exit(1);
222    close(OMKF) || exit(1);
223 }
224
225 exit 0;
226
227
228 sub print_deps {
229     # insert dependencies
230     print "$Begin_magic_str", @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
231 }
232
233 sub mangle_command_line_args {
234     local(@Args) = @_;
235         
236     while($_ = $Args[0]) {
237         shift(@Args);
238
239         if ( /^--$/ ) {
240             $Dashdashes_seen++;
241
242         } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
243             push(@Defines, $_);
244         } elsif ( /^-cpp$/ ) { # recognized wherever they occur
245             $Cpp_flag_set =1; 
246         } elsif ( /^-i$/ ) {
247             $Import_dirs = ''; # import path cleared!
248         } elsif ( /^-i(.*)/ ) {
249             $Import_dirs = "$1:$Import_dirs";
250         } elsif ( /^-I/ ) {
251             $Include_dirs .= " $_";
252         } elsif ( /^-syslib$/ ) { # deprecated, use -syslib
253             push(@Syslibs, &grab_arg_arg(*Args,$_,''));
254         } elsif ( /^-package$/ ) {
255             push(@Syslibs, &grab_arg_arg(*Args,$_,''));
256         } elsif ( /^-fglasgow-exts$/ ) {
257             push(@Syslibs, 'lang');
258         } elsif ( /^-concurrent$/ ) {
259             push(@Syslibs, 'concurrent');
260         } elsif (/^-#include(.*)/) {
261             &grab_arg_arg(*Args,'-#include',$1);
262         } elsif ( /^-osuf(.*)/ ) {
263             local($suff)        = &grab_arg_arg(*Args,'-osuf',$1);
264             # Weed out existing entry .. there must be a better way of doing this
265             # with arrays (non-assoc) ! -- sof
266             @Obj_suffix = grep(!/$suff/,@Obj_suffix);
267             push(@Obj_suffix, $suff);
268
269         } elsif ($Dashdashes_seen != 1) { # not between -- ... --
270             if ( /^-v$/ ) {
271                 $Verbose++;
272             } elsif ( /^-w$/ ) {
273                 $Warnings     = 0;
274             } elsif ( /^-f(.*)/ && !$ProcessingOptions ) {
275                 $Makefile       = &grab_arg_arg(*Args,'-f',$1);
276             #
277             # --exclude-module=mod => it's stable, trust me!
278             } elsif ( /^-(x|-exclude-module=)(.*)/ ) { 
279                 local($thing) = &grab_arg_arg(*Args,$1,$2);
280                 $IgnoreMe{$thing} = 'Y';
281             } elsif ( /^-(X|-exclude-directory=)(.*)/ ) { 
282                 foreach $d ( split(/:/,&grab_arg_arg(*Args,$1, $2)) ) {
283                    push(@Ignore_dirs,"$d");
284                 }
285             } elsif ( /^--include-module=(.*)/ ) { 
286                 local($thing) = &grab_arg_arg(*Args,$1,$2);
287                 $IgnoreMe{$thing} = 'n';
288             } elsif ( /^--include-prelude$/ ) { 
289                 $Include_prelude = 1;
290             } elsif ( /^-s(.*)/ ) {
291                 local($suff)    =  &grab_arg_arg(*Args,'-s',$1);
292                 push(@File_suffix, $suff);
293             } elsif ( /^-/ ) {
294                 if (!$ProcessingOptions) {
295                    print STDERR "$Pgm: unknown option ignored: $_\n";
296                    $Status++;
297                 }
298             } else {
299                 push(@Src_files, $_);
300             }
301         }
302         # Removed support for picking up plausible source file 
303         # names inside the ghc options chunk of the command
304         # line. It failed to deal with `option-value' stuff present
305         # on some invocations of ghc (e.g., "-ohi foo.baz").
306         #  -- sof 12/97
307         #
308         # } elsif ($Dashdashes_seen == 1) {
309         #     push(@Src_files, $_) if ! /^-/;
310         # }
311     }
312     @File_suffix = sort (@File_suffix);
313 }
314
315 sub grab_arg_arg {
316     local(*Args, $option, $rest_of_arg) = @_;
317     
318     if ($rest_of_arg) {
319         return($rest_of_arg);
320     } elsif ($#Args >= 0) {
321         local($temp) = $Args[0]; shift(@Args); 
322         return($temp);
323     } else {
324         print STDERR "$Pgm: no argument following $option option\n";
325         $Status++;
326     }
327 }
328
329 sub gather_import_dirs {
330
331         # set up array of ignored modules
332         local(@dirs) = ($INSTALLING) ? 
333                        ("$InstLibDirGhc/imports/std")
334                      : ("$TopPwd/ghc/lib/std");
335
336         if (!$Include_prelude) {
337            push(@Ignore_dirs, @dirs);
338         } else {
339            push(@Import_dirs, @dirs);
340         }
341
342         foreach $lib ( @Syslibs ) {
343            local($dir);
344
345            # Yuck ^ 2
346            if ( $lib eq 'lang' && ! $INSTALLING ) {
347               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/monads");
348            }
349            if ( $lib eq 'text' && ! $INSTALLING ) {
350               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/html");
351               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/haxml/lib");
352               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/parsec");
353            }
354            if ( $lib eq 'data' && ! $INSTALLING ) {
355               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison");
356               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Assoc");
357               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Coll");
358               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Seq");
359            }
360            if ( $lib eq 'util' && ! $INSTALLING ) {
361               push(@Import_dirs, "${TopPwd}/hslibs/${lib}/check");
362            }
363
364            # Yuck ^ 3
365            if ( $lib eq 'win32' && ! $INSTALLING ) {
366               $dir = "${TopPwd}/hslibs/${lib}/src";
367            } elsif ( $lib eq 'greencard' && ! $INSTALLING ) {
368               $dir = "${TopPwd}/green-card/lib/ghc";
369            } elsif ( $lib eq 'com' && ! $INSTALLING ) {
370               $dir = "${TopPwd}/hdirect/lib";
371            } else {
372               $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}" 
373                                    : "${TopPwd}/hslibs/${lib}";
374            }
375
376            if (!$Include_prelude) {
377               push(@Ignore_dirs,$dir);
378            } else {
379               push(@Import_dirs, $dir);
380            }
381         }
382 }
383
384
385
386 sub preprocess_import_dirs {
387     # it's probably cheaper to find out what's in all
388     # the @Import_dirs before we start processing.
389     local($d, $thing);
390     local($_);
391
392     foreach $d ( @Import_dirs ) {
393         # Check to see if it can be ignored
394         #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
395         #next if $Ignore_dirs{$d};
396
397         opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
398
399         for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
400             next unless /(.*)\.hi$/;
401             $thing = $1;
402             if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
403                 print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
404             } else {
405                 $ModuleIn{$thing} = $d;
406             }
407         }
408         closedir(DIR); # No, don't check the error code
409     }
410
411     # Add all the modules
412     # to the IgnoreMe array before we start scanning for imports.
413     foreach $d (@Ignore_dirs) {
414
415         opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";
416
417         for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
418             next unless /(.*)\.(hi|l?hs|l?y)$/;
419             #don't tag it twice or overwrite it with a diff. value
420             next if $IgnoreMe{$1};
421             print STDERR "Module $d will be ignored\n" if $Verbose;
422
423             $IgnoreMe{$1} = 'y';
424         }
425         closedir(DIR); # No, don't check the error code
426     }
427 }
428
429 sub slurp_file_for_imports {
430     local($file_to_read, $orig_src_file) = @_;
431     local($follow_file,$read_from_file);
432     local($cleanup)=0;
433
434     local(@Old_Syslibs, $options);
435     local(@Old_Ignore_dirs);
436     local($found_options)=0;
437
438     local($last_seen_dir) = $orig_src_file;
439     $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
440     $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
441
442     local($mod_name) = $orig_src_file;
443
444     $mod_name =~ s/.*\/([^\/]+)$/$1/g;
445     $mod_name =~ s/^([^.]+)\.(.*)$/$1/;
446
447     print STDERR "Warning: processing module $mod_name, which I was supposed to ignore.\n"
448         if ( $IgnoreMe{$mod_name} eq 'Y' && $Warnings );
449
450     &process_dependency('import',0,'Prelude') if ($Include_prelude);
451
452     #
453     # Check for {-# OPTIONS in the file-to-be-processed. If any syslib related options
454     # *only* are found, add them to the module search list (and take them off once
455     # we're through with this module.)
456     #
457
458     $options = &check_for_source_options($file_to_read);
459     if ($options ne "") {
460       @Old_Syslibs=@Syslibs;
461       $ProcessingOptions=1;
462       &mangle_command_line_args(split(/\s+/,$options));
463       if (@Old_Syslibs ne @Syslibs) {
464         $found_options=1;
465         @Old_Ignore_dirs = @Ignore_dirs;
466         &gather_import_dirs();
467         &preprocess_import_dirs();
468       }
469     }
470     
471     # we mangle #include's so they will also leave something
472     # behind to indicate the dependency on _them_
473     
474     local ($open_cmd);
475     if ($Cpp_flag_set) {
476        &run_something("${SED} -e '/^# *include/{p;s/^# *include/!include/;}' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines - 2>&1 > ${file_to_read}.i", $orig_src_file, 'cpp');
477        $read_from_file="${file_to_read}.i";
478        $cleanup=1;
479     } else {
480        $read_from_file="${file_to_read}";
481        $open_cmd = $file_to_read;
482     }
483     print STDERR "$open_cmd\n" if $Verbose;
484
485     open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";
486
487     while (<SRCFILE>) {
488         #
489         # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
490         # the compiler will deal with the absence of Foo.hi by consulting the
491         # source for Foo directly. (for dealing with recursive modules).
492         #
493         next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
494         $todo    = $1;
495         $source  = ( $2 ne '') ? 1 : 0;
496         $modname = $4;
497
498         next if $modname eq '';
499         &process_dependency($todo,$source,$modname);
500     }
501
502     if ($found_options) {
503       @Ignore_dirs = @Old_Ignore_dirs;
504     }
505
506     close(SRCFILE) || exit(1);
507     # remove temporary file, if any.
508     if ($cleanup) {
509            unlink("$read_from_file");
510     }
511 }
512
513 #
514 # Handle
515 sub process_dependency {
516   local($todo,$source,$modname) = @_;
517
518   if ($todo eq 'import') {
519     if ( $IgnoreMe{$modname} eq 'Y' ) {
520          # user specifically asked for this module
521          # to be ignored.
522          $follow_file = '__ignore__';
523     } elsif ( $ModuleIn{$modname} ) {
524         $follow_file = "$ModuleIn{$modname}/$modname.hi";
525     } else { # hard way
526         $follow_file =
527           &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
528     }
529   } else {
530         $follow_file
531           = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
532   }
533
534   if (! $follow_file) { # it didnae find anything
535       die "$orig_src_file: Couldn't handle: $_\n";
536
537   } else { # it found something
538     if ($follow_file ne '__ignore__') {
539         local($int_file) = $follow_file;
540
541         if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
542             local($str) = "";
543             foreach $obj  (@Obj_suffix) {
544                 $str .= "$bf.$obj ";
545                 foreach $suff (@File_suffix) {
546                    $str .= "$bf.${suff}_$obj ";
547                 }
548             }
549             push(@Depend_lines, "$str: $int_file\n");
550         } else {
551             $int_file =~ s/\.l?hs$//;
552             $int_file =~ s/\.l?y$//;
553             $int_file =~ s/\.hi$//;
554             local($source_dep);
555
556             if ( $source ) {
557                 # if a version specific .hi-boot file exist, use it.
558                 if ( -f "$int_file.hi-boot-${HiBootVersion}" ) {
559                    $source_dep = "$int_file.hi-boot-${HiBootVersion}";
560                 } else {
561                    $source_dep = "$int_file.hi-boot";
562                 }
563             } else {
564                 local($str)="";
565                 foreach $obj (@Obj_suffix) {
566                    $str .= "$bf.$obj ";
567                 }
568                 push(@Depend_lines, "$str: $int_file.hi\n");
569             }
570
571             if ( ! $source ) {
572                foreach $suff  (@File_suffix) {
573                   local($str) = "";
574                   foreach $obj (@Obj_suffix) {
575                     $str .= "$bf.${suff}_$obj ";
576                   }
577                   push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
578                }
579             } else {
580                local($str) = "";
581                foreach $obj  (@Obj_suffix) {
582                    $str .= "$bf.$obj ";
583                    foreach $suff (@File_suffix) {
584                      $str .= "$bf.${suff}_$obj ";
585                    }
586                }
587                push(@Depend_lines, "$str: $source_dep\n");
588             }
589         }
590      }
591    }
592 }
593
594 # when we see something, we cache that fact ('y').
595 # also, when we get a miss, we cache that (so we don't try later); ('n')
596 %FileExists = ();
597
598 sub find_in_Import_dirs {
599     local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
600     local($import_dir);
601     local($do_magical_check) = 0;
602     local($name_to_check);
603
604     # do it the old hard way: hop along Import_dir list
605     foreach $import_dir (@Import_dirs) {
606         # handle . magically
607         if ($import_dir eq '.') {
608             # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
609             $do_magical_check = 1;
610         }
611
612         $name_to_check = "$import_dir/$modname.hi";
613         if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
614             print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
615             return($name_to_check) if $FileExists{$name_to_check} eq 'y';
616             if (-f $name_to_check) {
617                 $FileExists{$name_to_check} = 'y';
618                 return($name_to_check) ;
619             } else {
620                 $FileExists{$name_to_check} = 'n';
621             }
622         }
623
624         for $suff ('hs', 'lhs', 'ly', 'y') {
625             $name_to_check = "$import_dir/$modname.$suff";
626             print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
627             return($name_to_check) if -f $name_to_check;
628         }
629
630         if ( $source ) {
631             $name_to_check = "$import_dir/$modname.hi-boot-${HiBootVersion}";
632             print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
633             return($name_to_check) if -f $name_to_check;
634
635             $name_to_check = "$import_dir/$modname.hi-boot";
636             print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
637             return($name_to_check) if -f $name_to_check;
638         }               
639     }
640     if ($do_magical_check == 1) {
641         $name_to_check = "$last_seen_dir/$modname.hi";
642
643         if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
644             print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
645             return($name_to_check) if $FileExists{$name_to_check} eq 'y';
646             if (-f $name_to_check) {
647                 $FileExists{$name_to_check} = 'y';
648                 return($name_to_check) ;
649             } else {
650                 $FileExists{$name_to_check} = 'n';
651             }
652         }
653
654         for $suff ('lhs', 'hs', 'ly', 'y') {
655             $name_to_check = "$last_seen_dir/$modname.$suff";
656             print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
657             return($name_to_check) if -f $name_to_check;
658         }
659     }
660
661     # OK, maybe it's referring to something in a system library
662     #foreach $lib ( @Syslibs ) {
663     #   return('__ignore__') if $LibIfaces{"$lib:$modname"};
664     #}
665
666     # Last hope: referring to a Prelude interface
667     return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );
668
669     die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
670 }
671
672 sub find_in_Include_dirs {
673     local($orig_src_file, $name, $last_seen_dir) = @_;
674     local($include_dir);
675     local($do_magical_check) = 0;
676
677     # no funny name guessing here
678
679     # hop along Include_dir list
680     foreach $include_dir (@Include_dirs) {
681         $include_dir =~ s/^-I//;
682
683         # handle . magically
684         if ($include_dir eq '.') {
685             # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
686             $do_magical_check = 1;
687         }
688         print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
689         if (-f "$include_dir/$name") {
690             return("$include_dir/$name");
691         }
692     }
693     if ($do_magical_check == 1) {
694         print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
695         if (-f "$last_seen_dir/$name") {
696             return("$last_seen_dir/$name");
697         }
698     }
699     die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
700 }
701
702 # out of the driver, actually
703 sub run_something {
704     local($str_to_do, $file_to_read, $tidy_name) = @_;
705
706     print STDERR "\n$tidy_name:\n\t" if $Verbose;
707     print STDERR "$str_to_do\n" if $Verbose;
708
709     local($return_val) = system($str_to_do) >> 8;
710
711     if ($return_val != 0) {
712         local($die_msg) = "$Pgm: Running $tidy_name ( on $file_to_read ) failed";
713         $die_msg .= " program not found: $str_to_do " if $return_val == 255;
714         $die_msg .= " ($!)" if $Verbose && $! != 0;
715         $die_msg .= "\n";
716         print STDERR $die_msg;
717         exit $return_val;
718     }
719 }
720
721 # out of the driver too.
722 sub check_for_source_options {
723     local($file) = @_;
724     local($comment_start,$comment_end);
725
726     # Assume it is a file containing Haskell source
727     $comment_start = "{-#";
728     $comment_end   = "#-}";
729
730     open(FILE,$file) || return ""; # No big loss
731     
732     while (<FILE>) {
733         if ( /^${comment_start} OPTIONS (.*)${comment_end}/ ) {
734            # return the options.
735            local($stuff) = $1;
736            print STDERR "Found OPTIONS $stuff in $file\n" if $Verbose;
737            close(FILE);
738            return $stuff;
739         }
740         elsif ( /^$/ ) { # ignore empty lines
741            ;
742         }
743         elsif ( /^#line.+$/ ) { # ignore comment lines (unused..ToDo: rm )
744            ;
745         }
746         elsif ( /^{-# LINE.+$/ ) { # ignore line pragmas
747            ;
748         }
749         else { # stop looking, something non-empty / not
750                # ${comment_start} OPTIONS .. ${comment_end} encountered.
751             close(FILE);return "";
752         }
753     }
754     close(FILE);
755     return "";
756 }