1 # *** MSUB does some substitutions here ***
4 # tries to work like mkdependC
6 # ToDo: strip out all the .h junk
8 ($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
9 $Usage = "usage: $Pgm: not done yet\n";
11 $Status = 0; # just used for exit() status
15 $OrigCpp = '$(RAWCPP)';
16 if ( $OrigCpp =~ /(\S+)\s+(.*)/ ) {
19 if ( -x $cmd ) { # cool
21 } else { # oops; try to guess
22 $GccV = `gcc -v 2>&1`;
23 if ( $GccV =~ /Reading specs from (.*)\/specs/ ) {
24 $Cpp = "$1/cpp $rest";
26 die "hscpp: don't know how to run cpp: $OrigCpp\n";
33 $Tmp_prefix = (( $ENV{'TMPDIR'} ) # to make tmp file names
34 ? ($ENV{'TMPDIR'} . "/mkdependHS$$")
35 : "$(TMPDIR)/mkdependHS$$" );
37 #------------------------------------------------------------------------
38 # If you are adjusting paths by hand for a binary GHC distribution,
39 # de-commenting the line to set GLASGOW_HASKELL_ROOT should do.
40 # Or you can leave it as is, and set the environment variable externally.
41 #------------------------------------------------------------------------
42 # $ENV{'GLASGOW_HASKELL_ROOT'} = '/some/absolute/path/name';
44 if (! $ENV{'GLASGOW_HASKELL_ROOT'}) { # good -- death to environment variables
45 $TopPwd = '$(TOP_PWD)';
46 $InstLibDirGhc = '$(INSTLIBDIR_GHC)';
47 $InstDataDirGhc = '$(INSTDATADIR_GHC)';
49 $TopPwd = $ENV{'GLASGOW_HASKELL_ROOT'};
51 if ( '$(INSTLIBDIR_GHC)' =~ /\/local\/fp(\/.*)/ ) {
52 $InstLibDirGhc = $ENV{'GLASGOW_HASKELL_ROOT'} . $1;
54 print STDERR "GLASGOW_HASKELL_ROOT environment variable is set;\nBut can't untangle $(INSTLIBDIR_GHC).\n(Installation error)\n";
58 if ( '$(INSTDATADIR_GHC)' =~ /\/local\/fp(\/.*)/ ) {
59 $InstDataDirGhc = $ENV{'GLASGOW_HASKELL_ROOT'} . $1;
61 print STDERR "GLASGOW_HASKELL_ROOT environment variable is set;\nBut can't untangle $(INSTDATADIR_GHC).\n(Installation error)\n";
66 $Unlit = ( $(INSTALLING) ) ? "$InstLibDirGhc/unlit"
67 : "$TopPwd/$(CURRENT_DIR)/$(GHC_UNLIT)";
69 $Begin_magic_str = "# DO NOT DELETE: Beginning of Haskell dependencies\n";
70 $End_magic_str = "# DO NOT DELETE: End of Haskell dependencies\n";
72 $ghc_version_info = $(PROJECTVERSION) * 100;
73 @Defines = ('-D__HASKELL1__=2', "-D__GLASGOW_HASKELL__=$ghc_version_info");
78 %PreludeIfaces = ( 'Prelude', '1',
80 'PreludeGlaMisc', '1',
83 %GhcLibIfaces = ( 'Bag', '1',
85 # CharSeq not supposed to be used by user (I think. WDP)
99 %HbcLibIfaces = ( 'Algebra', '1',
115 %IO13Ifaces = ( 'LibSystem', '1',
121 $Haskell_1_3 = 0; # assume Haskell 1.2, still. Changed by -fhaskell-1.3
122 $Include_dirs = '-I.';
123 $Col_width = 78; # ignored
127 &mangle_command_line_args();
129 if ( ! $Makefile && -f 'makefile' ) {
130 $Makefile = 'makefile';
131 } elsif ( ! $Makefile && -f 'Makefile') {
132 $Makefile = 'Makefile';
134 die "$Pgm: no makefile or Makefile found\n";
139 print STDERR "CPP defines=@Defines\n" if $Verbose;
140 print STDERR "Import_dirs=$Import_dirs\n" if $Verbose;
141 print STDERR "Include_dirs=$Include_dirs\n" if $Verbose;
143 foreach $sf (@Src_files) {
144 # just like lit-inputter
145 # except it puts each file through CPP and
146 # a de-commenter (not implemented);
147 # builds up @Depend_lines
148 print STDERR "Here we go for source file: $sf\n" if $Verbose;
149 ($of = $sf) =~ s/\.l?hs$/$Obj_suffix/;
150 push(@Depend_lines, "$of : $sf\n");
152 # if it's a literate file, .lhs, then we de-literatize it:
153 if ( $sf !~ /\.lhs$/ ) {
156 $file_to_read = "$Tmp_prefix.hs";
157 local($to_do) = "$Unlit $sf $file_to_read";
158 &run_something($to_do, 'unlit');
160 &slurp_file_for_imports($file_to_read, $sf);
162 if ( $sf =~ /\.lhs$/ ) {
163 unlink "$Tmp_prefix.hs";
167 # OK, mangle the Makefile
168 unlink("$Makefile.bak");
169 rename($Makefile,"$Makefile.bak");
170 # now copy Makefile.bak into Makefile, rm'ing old dependencies
172 open(OMKF,"< $Makefile.bak") || die "$Pgm: can't open $Makefile.bak: $!\n";
173 open(NMKF,"> $Makefile") || die "$Pgm: can't open $Makefile: $!\n";
176 while ($_ && $_ ne $Begin_magic_str) { # copy through, 'til Begin_magic_str
180 while ($_ && $_ ne $End_magic_str) { # delete 'til End_magic_str
183 # insert dependencies
184 print $Begin_magic_str;
186 print $End_magic_str;
187 while (<OMKF>) { # copy the rest through
190 close(NMKF) || exit(1);
191 close(OMKF) || exit(1);
192 chmod 0444, 'Makefile';
195 sub mangle_command_line_args {
196 while($_ = $ARGV[0]) {
202 } elsif ( /^-D(.*)/ ) { # recognized wherever they occur
204 } elsif ( /^-i(.*)/ ) {
205 $Import_dirs .= ":$1";
207 $Include_dirs .= " $_";
208 } elsif ( /^-syslib$/ ) {
209 push(@Syslibs, &grab_arg_arg($_,''));
210 } elsif ( /^-fhaskell-1\.3/ ) {
212 } elsif ( /^-stable$/ ) {
213 # user-defined syslibs that she believes are stable.
214 push(@StableLibs, &grab_arg_arg($_,''));
216 } elsif ($Dashdashes_seen != 1) { # not between -- ... --
219 } elsif ( /^-f(.*)/ ) {
220 $Makefile = &grab_arg_arg('-f',$1);
221 } elsif ( /^-o(.*)/ ) {
222 $Obj_suffix = &grab_arg_arg('-o',$1);
223 } elsif ( /^-bs(.*)/ ) {
224 $Begin_magic_str = &grab_arg_arg('-bs',$1) . "\n";
225 } elsif ( /^-es(.*)/ ) {
226 $End_magic_str = &grab_arg_arg('-es',$1) . "\n";
227 } elsif ( /^-w(.*)/ ) {
228 $Width = &grab_arg_arg('-w',$1);
230 print STDERR "$Pgm: unknown option ignored: $_\n";
232 push(@Src_files, $_);
235 } elsif ($Dashdashes_seen == 1) { # where we ignore unknown options
236 push(@Src_files,$_) if ! /^-/;
242 local($option, $rest_of_arg) = @_;
245 return($rest_of_arg);
246 } elsif ($#ARGV >= 0) {
247 local($temp) = $ARGV[0]; shift(@ARGV);
250 print STDERR "$Pgm: no argument following $option option\n";
255 sub slurp_file_for_imports {
256 local($file_to_read, $orig_src_file) = @_;
259 local($last_seen_dir) = $orig_src_file;
260 $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
261 $last_seen_dir = '.' if ($last_seen_dir eq $orig_src_file);
263 # we mangle #include's so they will also leave something
264 # behind to indicate the dependency on _them_
266 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;
268 open(SRCFILE, "sed -e '/^# *include/{p;s/^# *include/!include/;};s/'\\''//g;s/\"//g' $file_to_read | $Cpp $Include_dirs -I$last_seen_dir @Defines |")
269 || die "$Pgm: Can't open $file_to_read: $!\n";
272 if (/^>?\s*import\s+([A-Z][A-Za-z0-9_']*)/ || /^!include\s+"(\S+)"/) {
274 if (/^>?\s*import/) {
275 $follow_file = &find_in_Import_dirs($orig_src_file, $modname, $last_seen_dir);
277 $follow_file = &find_in_Include_dirs($orig_src_file, $modname, $last_seen_dir);
280 if ($follow_file) { # it found something
282 if ($follow_file ne '__syslib__') {
284 ($int_file = $follow_file) =~ s/\.l?hs$/\.hi/;
286 push(@Depend_lines, "$of : $int_file\n");
289 die "$orig_src_file: Couldn't handle: $_\n";
293 close(SRCFILE) || exit(1);
296 # when we see something, we cache that fact ('y').
297 # also, when we get a miss, we cache that (so we don't try later); ('n')
300 sub find_in_Import_dirs {
301 local($orig_src_file, $modname, $last_seen_dir) = @_;
303 local($do_magical_check) = 0;
304 local($name_to_check);
306 # hop along Import_dir list
307 foreach $import_dir (split(/:/,$Import_dirs)) {
309 if ($import_dir eq '.') {
310 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
311 $do_magical_check = 1;
314 $name_to_check = "$import_dir/$modname.hi";
315 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
316 print STDERR "trying $name_to_check...\n" if $Verbose;
317 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
318 if (-f $name_to_check) {
319 $FileExists{$name_to_check} = 'y';
320 return($name_to_check) ;
322 $FileExists{$name_to_check} = 'n';
326 $name_to_check = "$import_dir/$modname.hs";
327 print STDERR "trying... $name_to_check\n" if $Verbose;
328 return($name_to_check) if -f $name_to_check;
330 $name_to_check = "$import_dir/$modname.lhs";
331 print STDERR "trying... $name_to_check\n" if $Verbose;
332 return($name_to_check) if -f $name_to_check;
334 if ($do_magical_check == 1) {
335 $name_to_check = "$last_seen_dir/$modname.hi";
337 if ( $FileExists{$name_to_check} ne 'n' ) { # either 'y' or nothing
338 print STDERR "trying $name_to_check...\n" if $Verbose;
339 return($name_to_check) if $FileExists{$name_to_check} eq 'y';
340 if (-f $name_to_check) {
341 $FileExists{$name_to_check} = 'y';
342 return($name_to_check) ;
344 $FileExists{$name_to_check} = 'n';
348 $name_to_check = "$last_seen_dir/$modname.lhs";
349 print STDERR "trying... $name_to_check\n" if $Verbose;
350 return($name_to_check) if -f $name_to_check;
352 $name_to_check = "$last_seen_dir/$modname.hs";
353 print STDERR "trying... $name_to_check\n" if $Verbose;
354 return($name_to_check) if -f $name_to_check;
356 # OK, maybe it's referring to something in a system library
357 foreach $lib ( @Syslibs ) {
358 if ( $lib eq 'ghc' ) {
359 return('__syslib__') if $GhcLibIfaces{$modname};
360 } elsif ( $lib eq 'hbc' ) {
361 return('__syslib__') if $HbcLibIfaces{$modname};
363 die "Unrecognised syslib: $lib\n";
367 # HACK HACK: Let the user define his own "stable" modules.
368 foreach $stableLib ( @StableLibs ) {
369 return('__syslib__') if ( $stableLib eq $modname );
372 # Might be a Haskell 1.3 Module (but only if we've said -fhaskell-1.3)
373 if ( $Haskell_1_3 == 1 ) {
374 return('__syslib__') if $IO13Ifaces{$modname};
377 # Last hope: referring to a Prelude interface
378 return('__syslib__') if $PreludeIfaces{$modname};
380 die "No file `$modname.hi', `$modname.lhs' or `$modname.hs' (reqd from file `$orig_src_file')\namong import directories:\n\t$Import_dirs\n";
383 sub find_in_Include_dirs {
384 local($orig_src_file, $name, $last_seen_dir) = @_;
386 local($do_magical_check) = 0;
388 # no funny name guessing here
390 # hop along Include_dir list
391 foreach $include_dir (split(/\s+/,$Include_dirs)) {
392 $include_dir =~ s/^-I//;
395 if ($include_dir eq '.') {
396 # record that we should do a SPECIAL try for a file in last_seen_dir (LAST)
397 $do_magical_check = 1;
399 print STDERR "trying $include_dir/$name...\n" if $Verbose;
400 if (-f "$include_dir/$name") {
401 return("$include_dir/$name");
404 if ($do_magical_check == 1) {
405 print STDERR "trying $last_seen_dir/$name...\n" if $Verbose;
406 if (-f "$last_seen_dir/$name") {
407 return("$last_seen_dir/$name");
410 die "No file `$name' (reqd from file `$orig_src_file') among include directories: $Include_dirs\n";
413 # out of the driver, actually
415 local($str_to_do, $tidy_name) = @_;
417 print STDERR "\n$tidy_name:\n\t" if $Verbose;
418 print STDERR "$str_to_do\n" if $Verbose;
420 local($return_val) = system($str_to_do) >> 8;
422 if ($return_val != 0) {
423 local($die_msg) = "$Pgm: execution of the $tidy_name had trouble";
424 $die_msg .= " (program not found)" if $return_val == 255;
425 $die_msg .= " ($!)" if $Verbose && $! != 0;
427 print STDERR $die_msg;