summaryrefslogtreecommitdiff
path: root/ghc/utils/mkdependHS/mkdependHS.prl
blob: 47ec6770928fc63c76509a94d27ed7b7c20c4729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#
# perl script expect the following variables to be prepended:
#
#  RAWCPP TMPDIR TOP_PWD HscIfaceFileVersion
#  libdir libexecdir datadir INSTALLING
#  SED 
#
# tries to work like mkdependC - capable of dealing with:
#
#  -literate Haskell code (Bird tracks or Glasgow literate style) (.lhs)
#  -straight Haskell source (.hs)
#  -literate or straight Happy specs    (.ly)
#  
# ToDo: strip out all the .h junk
#
($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
$Usage  = <<EOUSAGE;
Usage: $Pgm [mkdependHS options] [-- GHC options --] srcfile1 [srcfile2 ...]

Options recognised wherever they occur (mkdependHS or GHC):

    -D<blah>	A cpp #define; usual meaning
    -i<dirs>	Add <dirs> (colon-separated) to list of directories
		to search for "import"ed modules
    -I<dir>	Add <dir> to list of directories to search for
		.h files (i.e., usual meaning)
    -package <blah> This program uses this GHC system library; take
		appropriate action (e.g., recognise when they are
		"import"ing a module from that library).
    -osuf <osuf> Use <osuf> as the "object file" suffix ( default: o)

mkdependHS-specific options (not between --'s):

    -v		Be verbose.
    -v -v	Be very verbose.
    -w          No warnings
    -f blah	Use "blah" as the makefile, rather than "makefile"
		or "Makefile".
    -cpp	Wash the input files through cpp(1).
    -s <suf>	Make extra dependencies for files with
		suffix <suf><osuf>; thus, "-o hc -s _a" will
		make dependencies both for .hc files and for .a_hc
		files.  (Useful in conjunction with NoFib "ways".)
    --exclude-module=<file> 
                Regard <file> as "stable"; i.e., eXclude it from having
		dependencies on it.
    -x          same as --exclude-module
    --exclude-directory=<dirs> 
                Regard : separated list of directories as containing stable,
                don't generate any dependencies on modules therein.
    -Xdirs      same as --exclude-directory
    --include-prelude
		Regard prelude libraries as unstable, i.e., generate dependencies
		on prelude modules. This option is normally only used by the
                various system libraries. If a -package option is used, dependencies
                will also be generated on the library's interfaces.
    --include-module=<file> 
                Regard <file> as not "stable"; i.e., generate dependencies
                on it (if any). This option is normally used in conjunction 
                with the --exclude-directory option.
		
EOUSAGE

$Status     = 0; # just used for exit() status
$Verbose    = 0; # 1 => verbose, 2 => very verbose
$Warnings   = 1; # 1 => warn about duplicate interface files
$Dashdashes_seen = 0;

$Cpp = ${RAWCPP};
$Cpp_flag_set = 0; # 1 => run cpp over input files.

if ( $ENV{'TMPDIR'} ) { # where to make tmp file names
    $Tmp_prefix = $ENV{'TMPDIR'} . "/mkdependHS$$";
} else {
    $Tmp_prefix ="${TMPDIR}/mkdependHS$$";
    $ENV{'TMPDIR'} = "${TMPDIR}"; # set the env var as well
}

$TopPwd	           = "${TOP_PWD}";
$InstLibDirGhc     = "${libdir}";
$InstLibExecDirGhc = "${libexecdir}";
$InstDataDirGhc    = "${datadir}";

$Unlit = ($INSTALLING) ? "${InstLibExecDirGhc}/unlit" 
		       : "${TopPwd}/ghc/utils/unlit/unlit";

$HiBootVersion = $HscIfaceFileVersion;
$hi_boot_with_version = 0;

$Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
$End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
@Obj_suffix =  ("o");
@File_suffix = ();

$Import_dirs = '.';
%Syslibs = ();
%LibIfaces  = ();  # known prelude/syslib ifaces; read from a file
%IgnoreMe = ();
# directories to considered stable.
@Ignore_dirs = ();

%ModuleIn = ();

$Include_dirs = '-I.';
$Makefile = '';
@Src_files = ();
$Include_prelude = 0;
@Defines = ();
$ProcessingOptions=0;

# Delete temp. file if script is halted.
sub quit_upon_signal { print STDERR "Deleting $Tmp_prefix.hs .. \n"; unlink "$Tmp_prefix.hs"; }
$SIG{'INT'}  = 'quit_upon_signal';
$SIG{'QUIT'} = 'quit_upon_signal';

&mangle_command_line_args(@ARGV);

if ( $Status ) {
    print stderr $Usage;
    exit(1);
}

@Import_dirs  = split(/:/,$Import_dirs);
@Include_dirs = split(/\s+/,$Include_dirs); # still has -I's in it

# NB: We keep the scalar-variable equivalents to use in error messages

&gather_import_dirs();

if ( ! $Makefile && -f 'makefile' ) {
    $Makefile = 'makefile';
} elsif ( ! $Makefile && -f 'Makefile') {
    $Makefile = 'Makefile';
} elsif ( ! $Makefile) {
    die "$Pgm: no makefile or Makefile found\n";
}

print STDERR "CPP defines=@Defines\n" if $Verbose;
print STDERR "Import_dirs=@Import_dirs\n" if $Verbose;
print STDERR "Include_dirs=@Include_dirs\n" if $Verbose;

&preprocess_import_dirs();

@Depend_lines = ();

foreach $sf (@Src_files) {

    # just like lit-inputter
    # except it puts each file through CPP and
    # a de-commenter (not implemented);
    # builds up @Depend_lines
    print STDERR "Here we go for source file: $sf\n" if $Verbose;
    ($bf = $sf) =~ s/\.l?(hs|y)$//;

    local($str)="";
    foreach $obj  (@Obj_suffix) {
  	$str .= "$bf.$obj ";
        foreach $suff (@File_suffix) {
	   $str .= "$bf.${suff}_$obj ";
        }
    }
    push(@Depend_lines, "$str: $sf\n");
    # if it's a literate file, .lhs or .ly? (happy specs), then we de-literatize it: 
    if ( $sf !~ /\.l(hs|y)$/ ) {
	$file_to_read = $sf;
    } else {
    	$file_to_read = "$Tmp_prefix.hs";
	local($to_do) = "$Unlit $sf $file_to_read";
	&run_something($to_do, $sf, 'unlit');
    }
    &slurp_file_for_imports($file_to_read, $sf);

    # Delete the temporary.
    if ( $sf =~ /\.l(hs|y)$/ ) {
	unlink "$Tmp_prefix.hs";
    }
}

#
# print dependencies
#
if ( $Makefile eq "-" ) {
   #
   # print dependencies to STDOUT
   #
   &print_deps();
} elsif ( ! -f $Makefile ) {
   #
   # $Makefile does not exist, just print the deps.
   #
   open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
   select(NMKF);
   &print_deps();
   close(NMKF);
} else {
   #
   # Create backup version of output file, then generate a new one.
   # 
   rename($Makefile,"$Makefile.bak") || die "$Pgm: can't rename $Makefile: $!\n";
   
   # now copy Makefile.bak into Makefile, rm'ing old dependencies
   # and adding the new
   open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
   open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
   select(NMKF);

   $_ = <OMKF>;
   while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
       print $_;
       $_ = <OMKF>;
   }
   while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
       $_ = <OMKF>;
   }

   &print_deps();
   
   while (<OMKF>) { # copy the rest through
       print $_ || die "Failed to write out dependencies ($!)";
   }
   close(NMKF) || exit(1);
   close(OMKF) || exit(1);
}

exit 0;


sub print_deps {
    # insert dependencies
    print "$Begin_magic_str", @Depend_lines, $End_magic_str || die "Failed to write out dependencies ($!)";
}

sub mangle_command_line_args {
    local(@Args) = @_;
        
    while($_ = $Args[0]) {
	shift(@Args);

	if ( /^--$/ ) {
	    $Dashdashes_seen++;

	} elsif ( /^-D(.*)/ ) { # recognized wherever they occur
	    push(@Defines, $_);
	} elsif ( /^-cpp$/ ) { # recognized wherever they occur
	    $Cpp_flag_set =1; 
	} elsif ( /^-i$/ ) {
	    $Import_dirs = ''; # import path cleared!
	} elsif ( /^-i(.*)/ ) {
	    $Import_dirs = "$1:$Import_dirs";
	} elsif ( /^-I/ ) {
	    $Include_dirs .= " $_";
	} elsif ( /^-syslib$/ ) { # deprecated, use -syslib
	    push(@Syslibs, &grab_arg_arg(*Args,$_,''));
	} elsif ( /^-package$/ ) {
	    push(@Syslibs, &grab_arg_arg(*Args,$_,''));
	} elsif ( /^-fglasgow-exts$/ ) {
	    push(@Syslibs, 'lang');
	} elsif ( /^-concurrent$/ ) {
	    push(@Syslibs, 'concurrent');
	} elsif (/^-#include(.*)/) {
	    &grab_arg_arg(*Args,'-#include',$1);
	} elsif ( /^-osuf(.*)/ ) {
	    local($suff)	= &grab_arg_arg(*Args,'-osuf',$1);
	    # Weed out existing entry .. there must be a better way of doing this
	    # with arrays (non-assoc) ! -- sof
	    @Obj_suffix	= grep(!/$suff/,@Obj_suffix);
	    push(@Obj_suffix, $suff);

	} elsif ($Dashdashes_seen != 1) { # not between -- ... --
	    if ( /^-v$/ ) {
		$Verbose++;
	    } elsif ( /^-w$/ ) {
		$Warnings     = 0;
	    } elsif ( /^-f(.*)/ && !$ProcessingOptions ) {
		$Makefile	= &grab_arg_arg(*Args,'-f',$1);
            #
	    # --exclude-module=mod => it's stable, trust me!
	    } elsif ( /^-(x|-exclude-module=)(.*)/ ) { 
		local($thing) = &grab_arg_arg(*Args,$1,$2);
		$IgnoreMe{$thing} = 'Y';
	    } elsif ( /^-(X|-exclude-directory=)(.*)/ ) { 
		foreach $d ( split(/:/,&grab_arg_arg(*Args,$1, $2)) ) {
                   push(@Ignore_dirs,"$d");
                }
	    } elsif ( /^--include-module=(.*)/ ) { 
		local($thing) = &grab_arg_arg(*Args,$1,$2);
		$IgnoreMe{$thing} = 'n';
	    } elsif ( /^--include-prelude$/ ) { 
		$Include_prelude = 1;
	    } elsif ( /^-s(.*)/ ) {
		local($suff)	=  &grab_arg_arg(*Args,'-s',$1);
		push(@File_suffix, $suff);
	    } elsif ( /^-/ ) {
		if (!$ProcessingOptions) {
		   print STDERR "$Pgm: unknown option ignored: $_\n";
		   $Status++;
	        }
	    } else {
		push(@Src_files, $_);
	    }
        }
        # Removed support for picking up plausible source file 
        # names inside the ghc options chunk of the command
        # line. It failed to deal with `option-value' stuff present
        # on some invocations of ghc (e.g., "-ohi foo.baz").
        #  -- sof 12/97
	#
	# } elsif ($Dashdashes_seen == 1) {
	#     push(@Src_files, $_) if ! /^-/;
	# }
    }
    @File_suffix = sort (@File_suffix);
}

sub grab_arg_arg {
    local(*Args, $option, $rest_of_arg) = @_;
    
    if ($rest_of_arg) {
	return($rest_of_arg);
    } elsif ($#Args >= 0) {
	local($temp) = $Args[0]; shift(@Args); 
	return($temp);
    } else {
	print STDERR "$Pgm: no argument following $option option\n";
	$Status++;
    }
}

sub gather_import_dirs {

	# set up array of ignored modules
	local(@dirs) = ($INSTALLING) ? 
    		       ("$InstLibDirGhc/imports/std")
	     	     : ("$TopPwd/ghc/lib/std");

	if (!$Include_prelude) {
    	   push(@Ignore_dirs, @dirs);
	} else {
    	   push(@Import_dirs, @dirs);
        }

	foreach $lib ( @Syslibs ) {
	   local($dir);

           # Yuck ^ 2
           if ( $lib eq 'lang' && ! $INSTALLING ) {
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/monads");
	   }
           if ( $lib eq 'text' && ! $INSTALLING ) {
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/html");
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/haxml/lib");
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/parsec");
	   }
           if ( $lib eq 'data' && ! $INSTALLING ) {
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison");
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Assoc");
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Coll");
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/edison/Seq");
	   }
           if ( $lib eq 'util' && ! $INSTALLING ) {
              push(@Import_dirs, "${TopPwd}/hslibs/${lib}/check");
	   }

           # Yuck ^ 3
           if ( $lib eq 'win32' && ! $INSTALLING ) {
              $dir = "${TopPwd}/hslibs/${lib}/src";
           } elsif ( $lib eq 'greencard' && ! $INSTALLING ) {
              $dir = "${TopPwd}/green-card/lib/ghc";
           } elsif ( $lib eq 'com' && ! $INSTALLING ) {
              $dir = "${TopPwd}/hdirect/lib";
           } else {
              $dir = ($INSTALLING) ? "${InstLibDirGhc}/imports/${lib}" 
	                           : "${TopPwd}/hslibs/${lib}";
           }

    	   if (!$Include_prelude) {
              push(@Ignore_dirs,$dir);
           } else {
              push(@Import_dirs, $dir);
           }
	}
}



sub preprocess_import_dirs {
    # it's probably cheaper to find out what's in all
    # the @Import_dirs before we start processing.
    local($d, $thing);
    local($_);

    foreach $d ( @Import_dirs ) {
        # Check to see if it can be ignored
        #print STDERR "Ignore imports from $d\n" if $Verbose && $Ignore_dirs{$d};
        #next if $Ignore_dirs{$d};

	opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";

	for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
	    next unless /(.*)\.hi$/;
	    $thing = $1;
	    if ($ModuleIn{$thing} && $ModuleIn{$thing} ne $d) {
		print STDERR "$Pgm: warning: $thing.hi appears in both $ModuleIn{$thing} and $d!\n" if ($Warnings);
	    } else {
		$ModuleIn{$thing} = $d;
	    }
	}
	closedir(DIR); # No, don't check the error code
    }

    # Add all the modules
    # to the IgnoreMe array before we start scanning for imports.
    foreach $d (@Ignore_dirs) {

	opendir(DIR, $d) || die "$Pgm: can't open directory $d\n";

        for ($_ = readdir(DIR); $_; $_ = readdir(DIR)) {
            next unless /(.*)\.(hi|l?hs|l?y)$/;
            #don't tag it twice or overwrite it with a diff. value
            next if $IgnoreMe{$1};
            print STDERR "Module $d will be ignored\n" if $Verbose;

            $IgnoreMe{$1} = 'y';
        }
	closedir(DIR); # No, don't check the error code
    }
}

sub slurp_file_for_imports {
    local($file_to_read, $orig_src_file) = @_;
    local($follow_file,$read_from_file);
    local($cleanup)=0;

    local(@Old_Syslibs, $options);
    local(@Old_Ignore_dirs);
    local($found_options)=0;

    local($last_seen_dir) = $orig_src_file;
    $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
    $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);

    local($mod_name) = $orig_src_file;

    $mod_name =~ s/.*\/([^\/]+)$/$1/g;
    $mod_name =~ s/^([^.]+)\.(.*)$/$1/;

    print STDERR "Warning: processing module $mod_name, which I was supposed to ignore.\n"
    	if ( $IgnoreMe{$mod_name} eq 'Y' && $Warnings );

    &process_dependency('import',0,'Prelude') if ($Include_prelude);

    #
    # Check for {-# OPTIONS in the file-to-be-processed. If any syslib related options
    # *only* are found, add them to the module search list (and take them off once
    # we're through with this module.)
    #

    $options = &check_for_source_options($file_to_read);
    if ($options ne "") {
      @Old_Syslibs=@Syslibs;
      $ProcessingOptions=1;
      &mangle_command_line_args(split(/\s+/,$options));
      if (@Old_Syslibs ne @Syslibs) {
        $found_options=1;
	@Old_Ignore_dirs = @Ignore_dirs;
        &gather_import_dirs();
	&preprocess_import_dirs();
      }
    }
    
    # we mangle #include's so they will also leave something
    # behind to indicate the dependency on _them_
    
    local ($open_cmd);
    if ($Cpp_flag_set) {
       &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');
       $read_from_file="${file_to_read}.i";
       $cleanup=1;
    } else {
       $read_from_file="${file_to_read}";
       $open_cmd = $file_to_read;
    }
    print STDERR "$open_cmd\n" if $Verbose;

    open(SRCFILE, $read_from_file) || die "$Pgm: Can't open $file_to_read: $!\n";

    while (<SRCFILE>) {
        #
        # import {-# SOURCE #-} Foo (bar) generates dependencies on the source file only,
        # the compiler will deal with the absence of Foo.hi by consulting the
        # source for Foo directly. (for dealing with recursive modules).
        #
	next unless (/^>?\s*(import)(\s+{-#\s*SOURCE\s*#-})?(\s+qualified)?\s+([A-Z][A-Za-z0-9_']*)/ || /^!(include)(\s+)"(\S+)"/);
	$todo    = $1;
        $source  = ( $2 ne '') ? 1 : 0;
	$modname = $4;

        next if $modname eq '';
	&process_dependency($todo,$source,$modname);
    }

    if ($found_options) {
      @Ignore_dirs = @Old_Ignore_dirs;
    }

    close(SRCFILE) || exit(1);
    # remove temporary file, if any.
    if ($cleanup) {
	   unlink("$read_from_file");
    }
}

#
# Handle
sub process_dependency {
  local($todo,$source,$modname) = @_;

  if ($todo eq 'import') {
    if ( $IgnoreMe{$modname} eq 'Y' ) {
         # user specifically asked for this module
	 # to be ignored.
	 $follow_file = '__ignore__';
    } elsif ( $ModuleIn{$modname} ) {
        $follow_file = "$ModuleIn{$modname}/$modname.hi";
    } else { # hard way
	$follow_file =
	  &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir, $source );
    }
  } else {
	$follow_file
	  = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
  }

  if (! $follow_file) { # it didnae find anything
      die "$orig_src_file: Couldn't handle: $_\n";

  } else { # it found something
    if ($follow_file ne '__ignore__') {
	local($int_file) = $follow_file;

	if ( $int_file !~ /\.(l?hs|hi|l?y)$/ ) {
	    local($str) = "";
	    foreach $obj  (@Obj_suffix) {
		$str .= "$bf.$obj ";
		foreach $suff (@File_suffix) {
		   $str .= "$bf.${suff}_$obj ";
	        }
	    }
            push(@Depend_lines, "$str: $int_file\n");
	} else {
	    $int_file =~ s/\.l?hs$//;
	    $int_file =~ s/\.l?y$//;
	    $int_file =~ s/\.hi$//;
            local($source_dep);

            if ( $source ) {
		# if a version specific .hi-boot file exist, use it.
		if ( -f "$int_file.hi-boot-${HiBootVersion}" ) {
               	   $source_dep = "$int_file.hi-boot-${HiBootVersion}";
		} else {
		   $source_dep = "$int_file.hi-boot";
		}
            } else {
	        local($str)="";
		foreach $obj (@Obj_suffix) {
	           $str .= "$bf.$obj ";
                }
	        push(@Depend_lines, "$str: $int_file.hi\n");
            }

            if ( ! $source ) {
	       foreach $suff  (@File_suffix) {
	          local($str) = "";
		  foreach $obj (@Obj_suffix) {
		    $str .= "$bf.${suff}_$obj ";
		  }
		  push(@Depend_lines, "$str: $int_file.${suff}_hi\n");
	       }
            } else {
	       local($str) = "";
	       foreach $obj  (@Obj_suffix) {
		   $str .= "$bf.$obj ";
		   foreach $suff (@File_suffix) {
		     $str .= "$bf.${suff}_$obj ";
	           }
	       }
	       push(@Depend_lines, "$str: $source_dep\n");
            }
	}
     }
   }
}

# when we see something, we cache that fact ('y').
# also, when we get a miss, we cache that (so we don't try later); ('n')
%FileExists = ();

sub find_in_Import_dirs {
    local($orig_src_file, $modname, $last_seen_dir, $source) = @_;
    local($import_dir);
    local($do_magical_check) = 0;
    local($name_to_check);

    # do it the old hard way: hop along Import_dir list
    foreach $import_dir (@Import_dirs) {
	# handle . magically
	if ($import_dir eq '.') {
	    # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
	    $do_magical_check = 1;
	}

	$name_to_check = "$import_dir/$modname.hi";
	if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
	    print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
	    return($name_to_check) if $FileExists{$name_to_check} eq 'y';
	    if (-f $name_to_check) {
		$FileExists{$name_to_check} = 'y';
		return($name_to_check) ;
	    } else {
		$FileExists{$name_to_check} = 'n';
	    }
    	}

	for $suff ('hs', 'lhs', 'ly', 'y') {
	    $name_to_check = "$import_dir/$modname.$suff";
	    print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
	    return($name_to_check) if -f $name_to_check;
        }

	if ( $source ) {
	    $name_to_check = "$import_dir/$modname.hi-boot-${HiBootVersion}";
	    print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
	    return($name_to_check) if -f $name_to_check;

	    $name_to_check = "$import_dir/$modname.hi-boot";
	    print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
	    return($name_to_check) if -f $name_to_check;
	}		
    }
    if ($do_magical_check == 1) {
	$name_to_check = "$last_seen_dir/$modname.hi";

	if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
	    print STDERR "trying $name_to_check...\n" if $Verbose >= 2; # very verbose
	    return($name_to_check) if $FileExists{$name_to_check} eq 'y';
	    if (-f $name_to_check) {
		$FileExists{$name_to_check} = 'y';
		return($name_to_check) ;
	    } else {
		$FileExists{$name_to_check} = 'n';
	    }
    	}

	for $suff ('lhs', 'hs', 'ly', 'y') {
	    $name_to_check = "$last_seen_dir/$modname.$suff";
	    print STDERR "trying... $name_to_check\n" if $Verbose >= 2; # very verbose
	    return($name_to_check) if -f $name_to_check;
        }
    }

    # OK, maybe it's referring to something in a system library
    #foreach $lib ( @Syslibs ) {
    #	return('__ignore__') if $LibIfaces{"$lib:$modname"};
    #}

    # Last hope: referring to a Prelude interface
    return('__ignore__') if ( $IgnoreMe{$modname} eq 'y' );

    die "No file `$modname.hi', `$modname.lhs', `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
}

sub find_in_Include_dirs {
    local($orig_src_file, $name, $last_seen_dir) = @_;
    local($include_dir);
    local($do_magical_check) = 0;

    # no funny name guessing here

    # hop along Include_dir list
    foreach $include_dir (@Include_dirs) {
	$include_dir =~ s/^-I//;

	# handle . magically
	if ($include_dir eq '.') {
	    # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
	    $do_magical_check = 1;
	}
	print STDERR "trying $include_dir/$name...\n" if $Verbose >= 2; # very verbose
	if (-f "$include_dir/$name") {
	    return("$include_dir/$name");
	}
    }
    if ($do_magical_check == 1) {
	print STDERR "trying $last_seen_dir/$name...\n" if $Verbose >= 2; # very verbose
	if (-f "$last_seen_dir/$name") {
	    return("$last_seen_dir/$name");
	}
    }
    die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
}

# out of the driver, actually
sub run_something {
    local($str_to_do, $file_to_read, $tidy_name) = @_;

    print STDERR "\n$tidy_name:\n\t" if $Verbose;
    print STDERR "$str_to_do\n" if $Verbose;

    local($return_val) = system($str_to_do) >> 8;

    if ($return_val != 0) {
	local($die_msg) = "$Pgm: Running $tidy_name ( on $file_to_read ) failed";
        $die_msg .= " program not found: $str_to_do " if $return_val == 255;
	$die_msg .= " ($!)" if $Verbose && $! != 0;
	$die_msg .= "\n";
	print STDERR $die_msg;
	exit $return_val;
    }
}

# out of the driver too.
sub check_for_source_options {
    local($file) = @_;
    local($comment_start,$comment_end);

    # Assume it is a file containing Haskell source
    $comment_start = "{-#";
    $comment_end   = "#-}";

    open(FILE,$file) || return ""; # No big loss
    
    while (<FILE>) {
	if ( /^${comment_start} OPTIONS (.*)${comment_end}/ ) {
           # return the options.
	   local($stuff) = $1;
	   print STDERR "Found OPTIONS $stuff in $file\n" if $Verbose;
	   close(FILE);
	   return $stuff;
	}
        elsif ( /^$/ ) { # ignore empty lines
           ;
        }
        elsif ( /^#line.+$/ ) { # ignore comment lines (unused..ToDo: rm )
           ;
        }
        elsif ( /^{-# LINE.+$/ ) { # ignore line pragmas
           ;
        }
        else { # stop looking, something non-empty / not
	       # ${comment_start} OPTIONS .. ${comment_end} encountered.
	    close(FILE);return "";
        }
    }
    close(FILE);
    return "";
}