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