chiark / gitweb /
165f796beb4024227880d1a5fc87ee4b397283cd
[sgt-puzzles.git] / mkfiles.pl
1 #!/usr/bin/env perl
2 #
3 # Cross-platform Makefile generator.
4 #
5 # Reads the file `Recipe' to determine the list of generated
6 # executables and their component objects. Then reads the source
7 # files to compute #include dependencies. Finally, writes out the
8 # various target Makefiles.
9
10 # PuTTY specifics which could still do with removing:
11 #  - Mac makefile is not portabilised at all. Include directories
12 #    are hardwired, and also the libraries are fixed. This is
13 #    mainly because I was too scared to go anywhere near it.
14 #  - sbcsgen.pl is still run at startup.
15
16 # Other things undone:
17 #  - special-define objects (foo.o[PREPROCSYMBOL]) are not
18 #    supported in the mac or vcproj makefiles.
19
20 use IO::Handle;
21 use Cwd;
22
23 @filestack = ();
24 $in = new IO::Handle;
25 open $in, "Recipe" or do {
26     # We want to deal correctly with being run from one of the
27     # subdirs in the source tree. So if we can't find Recipe here,
28     # try one level up.
29     chdir "..";
30     open $in, "Recipe" or die "unable to open Recipe file\n";
31 };
32 push @filestack, $in;
33
34 # HACK: One of the source files in `charset' is auto-generated by
35 # sbcsgen.pl. We need to generate that _now_, before attempting
36 # dependency analysis.
37 eval 'chdir "charset"; require "sbcsgen.pl"; chdir ".."';
38
39 @srcdirs = ("./");
40
41 $divert = undef; # ref to scalar in which text is currently being put
42 $help = ""; # list of newline-free lines of help text
43 $project_name = "project"; # this is a good enough default
44 %makefiles = (); # maps makefile types to output makefile pathnames
45 %makefile_extra = (); # maps makefile types to extra Makefile text
46 %programs = (); # maps prog name + type letter to listref of objects/resources
47 %groups = (); # maps group name to listref of objects/resources
48
49 @allobjs = (); # all object file names
50
51 readinput: while (1) {
52   while (not defined ($_ = <$in>)) {
53     close $in;
54     last readinput if 0 == scalar @filestack;
55     $in = pop @filestack;
56   }
57
58   chomp;
59   split;
60
61   # Skip comments (unless the comments belong, for example because
62   # they're part of a diversion).
63   next if /^\s*#/ and !defined $divert;
64
65   if ($_[0] eq "!begin" and $_[1] eq "help") { $divert = \$help; next; }
66   if ($_[0] eq "!end") { $divert = undef; next; }
67   if ($_[0] eq "!name") { $project_name = $_[1]; next; }
68   if ($_[0] eq "!srcdir") { push @srcdirs, $_[1]; next; }
69   if ($_[0] eq "!makefile" and &mfval($_[1])) { $makefiles{$_[1]}=$_[2]; next;}
70   if ($_[0] eq "!specialobj" and &mfval($_[1])) { $specialobj{$_[1]}->{$_[2]} = 1; next;}
71   if ($_[0] eq "!begin") {
72       if ($_[1] =~ /^>(.*)/) {
73           $divert = \$auxfiles{$1};
74       } elsif (&mfval($_[1])) {
75           $divert = \$makefile_extra{$_[1]};
76       }
77       next;
78   }
79   if ($_[0] eq "!include") {
80       @newfiles = ();
81       for ($i = 1; $i <= $#_; $i++) {
82           push @newfiles, (sort glob $_[$i]);
83       }
84       for ($i = $#newfiles; $i >= 0; $i--) {
85           $file = $newfiles[$i];
86           $f = new IO::Handle;
87           open $f, "<$file" or die "unable to open include file '$file'\n";
88           push @filestack, $f;
89       }
90       $in = $filestack[$#filestack];
91       next;
92   }
93   # If we're gathering help text, keep doing so.
94   if (defined $divert) { ${$divert} .= "$_\n"; next; }
95   # Ignore blank lines.
96   next if scalar @_ == 0;
97
98   # Now we have an ordinary line. See if it's an = line, a : line
99   # or a + line.
100   @objs = @_;
101
102   if ($_[0] eq "+") {
103     $listref = $lastlistref;
104     $prog = undef;
105     die "$.: unexpected + line\n" if !defined $lastlistref;
106   } elsif ($_[1] eq "=") {
107     $groups{$_[0]} = [];
108     $listref = $groups{$_[0]};
109     $prog = undef;
110     shift @objs; # eat the group name
111   } elsif ($_[1] eq "+=") {
112     $groups{$_[0]} = [] if !defined $groups{$_[0]};
113     $listref = $groups{$_[0]};
114     $prog = undef;
115     shift @objs; # eat the group name
116   } elsif ($_[1] eq ":") {
117     $listref = [];
118     $prog = $_[0];
119     shift @objs; # eat the program name
120   } else {
121     die "$.: unrecognised line type: '$_'\n";
122   }
123   shift @objs; # eat the +, the = or the :
124
125   while (scalar @objs > 0) {
126     $i = shift @objs;
127     if ($groups{$i}) {
128       foreach $j (@{$groups{$i}}) { unshift @objs, $j; }
129     } elsif (($i eq "[G]" or $i eq "[C]" or $i eq "[M]" or
130               $i eq "[X]" or $i eq "[U]" or $i eq "[MX]") and defined $prog) {
131       $type = substr($i,1,(length $i)-2);
132     } else {
133       if ($i =~ /\?$/) {
134         # Object files with a trailing question mark are optional:
135         # the build can proceed fine without them, so we only use
136         # them if their primary source files are present.
137         $i =~ s/\?$//;
138         $i = undef unless defined &finddep($i);
139       } elsif ($i =~ /\|/) {
140         # Object file descriptions containing a vertical bar are
141         # lists of choices: we use the _first_ one whose primary
142         # source file is present.
143         @options = split /\|/, $i;
144         $j = undef;
145         foreach $k (@options) {
146           $j=$k, last if defined &finddep($k);
147         }
148         die "no alternative found for $i\n" unless defined $j;
149         $i = $j;
150       }
151       if (defined $i) {
152         push @$listref, $i;
153         push @allobjs, $i;
154       }
155     }
156   }
157   if ($prog and $type) {
158     die "multiple program entries for $prog [$type]\n"
159         if defined $programs{$prog . "," . $type};
160     $programs{$prog . "," . $type} = $listref;
161   }
162   $lastlistref = $listref;
163 }
164
165 foreach $aux (sort keys %auxfiles) {
166     open AUX, ">$aux";
167     print AUX $auxfiles{$aux};
168     close AUX;
169 }
170
171 # Find object file names with predefines (in square brackets after
172 # the module name), and decide on actual object names for them.
173 foreach $i (@allobjs) {
174   if ($i !~ /\[/) {
175     $objname{$i} = $i;
176     $srcname{$i} = $i;
177     $usedobjname{$i} = 1;
178   }
179 }
180 foreach $i (@allobjs) {
181   if ($i =~ /^(.*)\[([^\]]*)/) {
182     $defs{$i} = [ split ",",$2 ];
183     $srcname{$i} = $s = $1;
184     $index = 1;
185     while (1) {
186       $maxlen = length $s;
187       $maxlen = 8 if $maxlen < 8;
188       $chop = $maxlen - length $index;
189       $chop = length $s if $chop > length $s;
190       $chop = 0 if $chop < 0;
191       $name = substr($s, 0, $chop) . $index;
192       $index++, next if $usedobjname{$name};
193       $objname{$i} = $name;
194       $usedobjname{$name} = 1;
195       last;
196     }
197   }
198 }
199
200 # Now retrieve the complete list of objects and resource files, and
201 # construct dependency data for them. While we're here, expand the
202 # object list for each program, and complain if its type isn't set.
203 @prognames = sort keys %programs;
204 %depends = ();
205 @scanlist = ();
206 foreach $i (@prognames) {
207   ($prog, $type) = split ",", $i;
208   # Strip duplicate object names.
209   $prev = undef;
210   @list = grep { $status = ($prev ne $_); $prev=$_; $status }
211           sort @{$programs{$i}};
212   $programs{$i} = [@list];
213   foreach $jj (@list) {
214     $j = $srcname{$jj};
215     $file = &finddep($j);
216     if (defined $file) {
217       $depends{$jj} = [$file];
218       push @scanlist, $file;
219     }
220   }
221 }
222
223 # Scan each file on @scanlist and find further inclusions.
224 # Inclusions are given by lines of the form `#include "otherfile"'
225 # (system headers are automatically ignored by this because they'll
226 # be given in angle brackets). Files included by this method are
227 # added back on to @scanlist to be scanned in turn (if not already
228 # done).
229 #
230 # Resource scripts (.rc) can also include a file by means of a line
231 # ending `ICON "filename"'. Files included by this method are not
232 # added to @scanlist because they can never include further files.
233 #
234 # In this pass we write out a hash %further which maps a source
235 # file name into a listref containing further source file names.
236
237 %further = ();
238 while (scalar @scanlist > 0) {
239   $file = shift @scanlist;
240   next if defined $further{$file}; # skip if we've already done it
241   $resource = ($file =~ /\.rc$/ ? 1 : 0);
242   $further{$file} = [];
243   $dirfile = &findfile($file);
244   open IN, "$dirfile" or die "unable to open source file $file\n";
245   while (<IN>) {
246     chomp;
247     /^\s*#include\s+\"([^\"]+)\"/ and do {
248       push @{$further{$file}}, $1;
249       push @scanlist, $1;
250       next;
251     };
252     /ICON\s+\"([^\"]+)\"\s*$/ and do {
253       push @{$further{$file}}, $1;
254       next;
255     }
256   }
257   close IN;
258 }
259
260 # Now we're ready to generate the final dependencies section. For
261 # each key in %depends, we must expand the dependencies list by
262 # iteratively adding entries from %further.
263 foreach $i (keys %depends) {
264   %dep = ();
265   @scanlist = @{$depends{$i}};
266   foreach $i (@scanlist) { $dep{$i} = 1; }
267   while (scalar @scanlist > 0) {
268     $file = shift @scanlist;
269     foreach $j (@{$further{$file}}) {
270       if ($dep{$j} != 1) {
271         $dep{$j} = 1;
272         push @{$depends{$i}}, $j;
273         push @scanlist, $j;
274       }
275     }
276   }
277 #  printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
278 }
279
280 # Validation of input.
281
282 sub mfval($) {
283     my ($type) = @_;
284     # Returns true if the argument is a known makefile type. Otherwise,
285     # prints a warning and returns false;
286     if (grep { $type eq $_ }
287         ("vc","vcproj","cygwin","borland","lcc","gtk","mpw","osx")) {
288             return 1;
289         }
290     warn "$.:unknown makefile type '$type'\n";
291     return 0;
292 }
293
294 # Utility routines while writing out the Makefiles.
295
296 sub dirpfx {
297     my ($path) = shift @_;
298     my ($sep) = shift @_;
299     my $ret = "", $i;
300     while (($i = index $path, $sep) >= 0) {
301         $path = substr $path, ($i + length $sep);
302         $ret .= "..$sep";
303     }
304     return $ret;
305 }
306
307 sub findfile {
308   my ($name) = @_;
309   my $dir;
310   my $i;
311   my $outdir = undef;
312   unless (defined $findfilecache{$name}) {
313     $i = 0;
314     foreach $dir (@srcdirs) {
315       $outdir = $dir, $i++ if -f "$dir$name";
316     }
317     die "multiple instances of source file $name\n" if $i > 1;
318     $findfilecache{$name} = (defined $outdir ? $outdir . $name : undef);
319   }
320   return $findfilecache{$name};
321 }
322
323 sub finddep {
324   my $j = shift @_;
325   my $file;
326   # Find the first dependency of an object.
327
328   # Dependencies for "x" start with "x.c" or "x.m" (depending on
329   # which one exists).
330   # Dependencies for "x.res" start with "x.rc".
331   # Dependencies for "x.rsrc" start with "x.r".
332   # Both types of file are pushed on the list of files to scan.
333   # Libraries (.lib) don't have dependencies at all.
334   if ($j =~ /^(.*)\.res$/) {
335     $file = "$1.rc";
336   } elsif ($j =~ /^(.*)\.rsrc$/) {
337     $file = "$1.r";
338   } elsif ($j !~ /\./) {
339     $file = "$j.c";
340     $file = "$j.m" unless &findfile($file);
341   } else {
342     # For everything else, we assume it's its own dependency.
343     $file = $j;
344   }
345   $file = undef unless &findfile($file);
346   return $file;
347 }
348
349 sub objects {
350   my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
351   my @ret;
352   my ($i, $x, $y);
353   @ret = ();
354   foreach $ii (@{$programs{$prog}}) {
355     $i = $objname{$ii};
356     $x = "";
357     if ($i =~ /^(.*)\.(res|rsrc)/) {
358       $y = $1;
359       ($x = $rtmpl) =~ s/X/$y/;
360     } elsif ($i =~ /^(.*)\.lib/) {
361       $y = $1;
362       ($x = $ltmpl) =~ s/X/$y/;
363     } elsif ($i !~ /\./) {
364       ($x = $otmpl) =~ s/X/$i/;
365     }
366     push @ret, $x if $x ne "";
367   }
368   return join " ", @ret;
369 }
370
371 sub special {
372   my ($prog, $suffix) = @_;
373   my @ret;
374   my ($i, $x, $y);
375   @ret = ();
376   foreach $ii (@{$programs{$prog}}) {
377     $i = $objname{$ii};
378     if (substr($i, (length $i) - (length $suffix)) eq $suffix) {
379       push @ret, $i;
380     }
381   }
382   return join " ", @ret;
383 }
384
385 sub splitline {
386   my ($line, $width, $splitchar) = @_;
387   my ($result, $len);
388   $len = (defined $width ? $width : 76);
389   $splitchar = (defined $splitchar ? $splitchar : '\\');
390   while (length $line > $len) {
391     $line =~ /^(.{0,$len})\s(.*)$/ or $line =~ /^(.{$len,}?\s(.*)$/;
392     $result .= $1;
393     $result .= " ${splitchar}\n\t\t" if $2 ne '';
394     $line = $2;
395     $len = 60;
396   }
397   return $result . $line;
398 }
399
400 sub deps {
401   my ($otmpl, $rtmpl, $prefix, $dirsep, $depchar, $splitchar) = @_;
402   my ($i, $x, $y);
403   my @deps, @ret;
404   @ret = ();
405   $depchar ||= ':';
406   foreach $ii (sort keys %depends) {
407     $i = $objname{$ii};
408     next if $specialobj{$mftyp}->{$i};
409     if ($i =~ /^(.*)\.(res|rsrc)/) {
410       next if !defined $rtmpl;
411       $y = $1;
412       ($x = $rtmpl) =~ s/X/$y/;
413     } else {
414       ($x = $otmpl) =~ s/X/$i/;
415     }
416     @deps = @{$depends{$ii}};
417     @deps = map {
418       $_ = &findfile($_);
419       s/\//$dirsep/g;
420       $_ = $prefix . $_;
421     } @deps;
422     push @ret, {obj => $x, deps => [@deps], defs => $defs{$ii}};
423   }
424   return @ret;
425 }
426
427 sub prognames {
428   my ($types) = @_;
429   my ($n, $prog, $type);
430   my @ret;
431   @ret = ();
432   foreach $n (@prognames) {
433     ($prog, $type) = split ",", $n;
434     push @ret, $n if index(":$types:", ":$type:") >= 0;
435   }
436   return @ret;
437 }
438
439 sub progrealnames {
440   my ($types) = @_;
441   my ($n, $prog, $type);
442   my @ret;
443   @ret = ();
444   foreach $n (@prognames) {
445     ($prog, $type) = split ",", $n;
446     push @ret, $prog if index(":$types:", ":$type:") >= 0;
447   }
448   return @ret;
449 }
450
451 sub manpages {
452   my ($types,$suffix) = @_;
453
454   # assume that all UNIX programs have a man page
455   if($suffix eq "1" && $types =~ /:X:/) {
456     return map("$_.1", &progrealnames($types));
457   }
458   return ();
459 }
460
461 # Now we're ready to output the actual Makefiles.
462
463 if (defined $makefiles{'cygwin'}) {
464     $mftyp = 'cygwin';
465     $dirpfx = &dirpfx($makefiles{'cygwin'}, "/");
466
467     ##-- CygWin makefile
468     open OUT, ">$makefiles{'cygwin'}"; select OUT;
469     print
470     "# Makefile for $project_name under cygwin.\n".
471     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
472     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
473     # gcc command line option is -D not /D
474     ($_ = $help) =~ s/=\/D/=-D/gs;
475     print $_;
476     print
477     "\n".
478     "# You can define this path to point at your tools if you need to\n".
479     "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
480     "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
481     "CC = \$(TOOLPATH)gcc\n".
482     "RC = \$(TOOLPATH)windres\n".
483     "# Uncomment the following two lines to compile under Winelib\n".
484     "# CC = winegcc\n".
485     "# RC = wrc\n".
486     "# You may also need to tell windres where to find include files:\n".
487     "# RCINC = --include-dir c:\\cygwin\\include\\\n".
488     "\n".
489     &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
490       " -D_NO_OLDNAMES -DNO_MULTIMON " .
491                (join " ", map {"-I$dirpfx$_"} @srcdirs)) .
492                "\n".
493     "LDFLAGS = -mno-cygwin -s\n".
494     &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
495       " --define WINVER=0x0400 --define MINGW32_FIX=1")."\n".
496     "\n";
497     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
498     print "\n\n";
499     foreach $p (&prognames("G:C")) {
500       ($prog, $type) = split ",", $p;
501       $objstr = &objects($p, "X.o", "X.res.o", undef);
502       print &splitline($prog . ".exe: " . $objstr), "\n";
503       my $mw = $type eq "G" ? " -mwindows" : "";
504       $libstr = &objects($p, undef, undef, "-lX");
505       print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
506                        "-Wl,-Map,$prog.map " .
507                        $objstr . " $libstr", 69), "\n\n";
508     }
509     foreach $d (&deps("X.o", "X.res.o", $dirpfx, "/")) {
510       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
511         "\n";
512       if ($d->{obj} =~ /\.res\.o$/) {
513         print "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n";
514       } else {
515         $deflist = join "", map { " -D$_" } @{$d->{defs}};
516         print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS)" .
517             " \$(XFLAGS)$deflist -c \$< -o \$\@\n";
518       }
519     }
520     print "\n";
521     print $makefile_extra{'cygwin'};
522     print "\nclean:\n".
523     "\trm -f *.o *.exe *.res.o *.map\n".
524     "\n";
525     select STDOUT; close OUT;
526
527 }
528
529 ##-- Borland makefile
530 if (defined $makefiles{'borland'}) {
531     $mftyp = 'borland';
532     $dirpfx = &dirpfx($makefiles{'borland'}, "\\");
533
534     %stdlibs = (  # Borland provides many Win32 API libraries intrinsically
535       "advapi32" => 1,
536       "comctl32" => 1,
537       "comdlg32" => 1,
538       "gdi32" => 1,
539       "imm32" => 1,
540       "shell32" => 1,
541       "user32" => 1,
542       "winmm" => 1,
543       "winspool" => 1,
544       "wsock32" => 1,
545     );
546     open OUT, ">$makefiles{'borland'}"; select OUT;
547     print
548     "# Makefile for $project_name under Borland C.\n".
549     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
550     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
551     # bcc32 command line option is -D not /D
552     ($_ = $help) =~ s/=\/D/=-D/gs;
553     print $_;
554     print
555     "\n".
556     "# If you rename this file to `Makefile', you should change this line,\n".
557     "# so that the .rsp files still depend on the correct makefile.\n".
558     "MAKEFILE = Makefile.bor\n".
559     "\n".
560     "# C compilation flags\n".
561     "CFLAGS = -D_WINDOWS -DWINVER=0x0401\n".
562     "\n".
563     "# Get include directory for resource compiler\n".
564     "!if !\$d(BCB)\n".
565     "BCB = \$(MAKEDIR)\\..\n".
566     "!endif\n".
567     "\n";
568     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
569     print "\n\n";
570     foreach $p (&prognames("G:C")) {
571       ($prog, $type) = split ",", $p;
572       $objstr = &objects($p, "X.obj", "X.res", undef);
573       print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
574       my $ap = ($type eq "G") ? "-aa" : "-ap";
575       print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
576     }
577     foreach $p (&prognames("G:C")) {
578       ($prog, $type) = split ",", $p;
579       print $prog, ".rsp: \$(MAKEFILE)\n";
580       $objstr = &objects($p, "X.obj", undef, undef);
581       @objlist = split " ", $objstr;
582       @objlines = ("");
583       foreach $i (@objlist) {
584         if (length($objlines[$#objlines] . " $i") > 50) {
585           push @objlines, "";
586         }
587         $objlines[$#objlines] .= " $i";
588       }
589       $c0w = ($type eq "G") ? "c0w32" : "c0x32";
590       print "\techo $c0w + > $prog.rsp\n";
591       for ($i=0; $i<=$#objlines; $i++) {
592         $plus = ($i < $#objlines ? " +" : "");
593         print "\techo$objlines[$i]$plus >> $prog.rsp\n";
594       }
595       print "\techo $prog.exe >> $prog.rsp\n";
596       $objstr = &objects($p, "X.obj", "X.res", undef);
597       @libs = split " ", &objects($p, undef, undef, "X");
598       @libs = grep { !$stdlibs{$_} } @libs;
599       unshift @libs, "cw32", "import32";
600       $libstr = join ' ', @libs;
601       print "\techo nul,$libstr, >> $prog.rsp\n";
602       print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
603       print "\n";
604     }
605     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\")) {
606       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
607         "\n";
608       if ($d->{obj} =~ /\.res$/) {
609         print &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) " .
610                          "-i \$(BCB)\\include -r -DNO_WINRESRC_H -DWIN32".
611                          " -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n";
612       } else {
613         $deflist = join "", map { " -D$_" } @{$d->{defs}};
614         print &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT)" .
615                          " \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist ".
616                          (join " ", map {"-I$dirpfx$_"} @srcdirs) .
617                          " /o$d->{obj} /c ".$d->{deps}->[0],69)."\n";
618       }
619     }
620     print "\n";
621     print $makefile_extra{'borland'};
622     print "\nclean:\n".
623     "\t-del *.obj\n".
624     "\t-del *.exe\n".
625     "\t-del *.res\n".
626     "\t-del *.pch\n".
627     "\t-del *.aps\n".
628     "\t-del *.il*\n".
629     "\t-del *.pdb\n".
630     "\t-del *.rsp\n".
631     "\t-del *.tds\n".
632     "\t-del *.\$\$\$\$\$\$\n";
633     select STDOUT; close OUT;
634 }
635
636 if (defined $makefiles{'vc'}) {
637     $mftyp = 'vc';
638     $dirpfx = &dirpfx($makefiles{'vc'}, "\\");
639
640     ##-- Visual C++ makefile
641     open OUT, ">$makefiles{'vc'}"; select OUT;
642     print
643       "# Makefile for $project_name under Visual C.\n".
644       "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
645       "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
646     print $help;
647     print
648       "\n".
649       "# If you rename this file to `Makefile', you should change this line,\n".
650       "# so that the .rsp files still depend on the correct makefile.\n".
651       "MAKEFILE = Makefile.vc\n".
652       "\n".
653       "# C compilation flags\n".
654       "CFLAGS = /nologo /W3 /O1 /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401\n".
655       "LFLAGS = /incremental:no /fixed\n".
656       "\n";
657     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
658     print "\n\n";
659     foreach $p (&prognames("G:C")) {
660         ($prog, $type) = split ",", $p;
661         $objstr = &objects($p, "X.obj", "X.res", undef);
662         print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
663         print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
664     }
665     foreach $p (&prognames("G:C")) {
666         ($prog, $type) = split ",", $p;
667         print $prog, ".rsp: \$(MAKEFILE)\n";
668         $objstr = &objects($p, "X.obj", "X.res", "X.lib");
669         @objlist = split " ", $objstr;
670         @objlines = ("");
671         foreach $i (@objlist) {
672             if (length($objlines[$#objlines] . " $i") > 50) {
673                 push @objlines, "";
674             }
675             $objlines[$#objlines] .= " $i";
676         }
677         $subsys = ($type eq "G") ? "windows" : "console";
678         print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
679         for ($i=0; $i<=$#objlines; $i++) {
680             print "\techo$objlines[$i] >> $prog.rsp\n";
681         }
682         print "\n";
683     }
684     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\")) {
685         print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
686           "\n";
687         if ($d->{obj} =~ /\.res$/) {
688             print "\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 ".
689               "-DWINVER=0x0400 -fo".$d->{obj}." ".$d->{deps}->[0]."\n";
690         } else {
691             $deflist = join "", map { " /D$_" } @{$d->{defs}};
692             print "\tcl \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist".
693               " /c ".$d->{deps}->[0]." /Fo$d->{obj}\n";
694         }
695     }
696     print "\n";
697     print $makefile_extra{'vc'};
698     print "\nclean: tidy\n".
699       "\t-del *.exe\n\n".
700       "tidy:\n".
701       "\t-del *.obj\n".
702       "\t-del *.res\n".
703       "\t-del *.pch\n".
704       "\t-del *.aps\n".
705       "\t-del *.ilk\n".
706       "\t-del *.pdb\n".
707       "\t-del *.rsp\n".
708       "\t-del *.dsp\n".
709       "\t-del *.dsw\n".
710       "\t-del *.ncb\n".
711       "\t-del *.opt\n".
712       "\t-del *.plg\n".
713       "\t-del *.map\n".
714       "\t-del *.idb\n".
715       "\t-del debug.log\n";
716     select STDOUT; close OUT;
717 }
718
719 if (defined $makefiles{'vcproj'}) {
720     $mftyp = 'vcproj';
721
722     $orig_dir = cwd;
723
724     ##-- MSVC 6 Workspace and projects
725     #
726     # Note: All files created in this section are written in binary
727     # mode, because although MSVC's command-line make can deal with
728     # LF-only line endings, MSVC project files really _need_ to be
729     # CRLF. Hence, in order for mkfiles.pl to generate usable project
730     # files even when run from Unix, I make sure all files are binary
731     # and explicitly write the CRLFs.
732     #
733     # Create directories if necessary
734     mkdir $makefiles{'vcproj'}
735         if(! -d $makefiles{'vcproj'});
736     chdir $makefiles{'vcproj'};
737     @deps = &deps("X.obj", "X.res", "", "\\");
738     %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
739     # Create the project files
740     # Get names of all Windows projects (GUI and console)
741     my @prognames = &prognames("G:C");
742     foreach $progname (@prognames) {
743         create_project(\%all_object_deps, $progname);
744     }
745     # Create the workspace file
746     open OUT, ">$project_name.dsw"; binmode OUT; select OUT;
747     print
748     "Microsoft Developer Studio Workspace File, Format Version 6.00\r\n".
749     "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n".
750     "\r\n".
751     "###############################################################################\r\n".
752     "\r\n";
753     # List projects
754     foreach $progname (@prognames) {
755       ($windows_project, $type) = split ",", $progname;
756         print "Project: \"$windows_project\"=\".\\$windows_project\\$windows_project.dsp\" - Package Owner=<4>\r\n";
757     }
758     print
759     "\r\n".
760     "Package=<5>\r\n".
761     "{{{\r\n".
762     "}}}\r\n".
763     "\r\n".
764     "Package=<4>\r\n".
765     "{{{\r\n".
766     "}}}\r\n".
767     "\r\n".
768     "###############################################################################\r\n".
769     "\r\n".
770     "Global:\r\n".
771     "\r\n".
772     "Package=<5>\r\n".
773     "{{{\r\n".
774     "}}}\r\n".
775     "\r\n".
776     "Package=<3>\r\n".
777     "{{{\r\n".
778     "}}}\r\n".
779     "\r\n".
780     "###############################################################################\r\n".
781     "\r\n";
782     select STDOUT; close OUT;
783     chdir $orig_dir;
784
785     sub create_project {
786         my ($all_object_deps, $progname) = @_;
787         # Construct program's dependency info
788         %seen_objects = ();
789         %lib_files = ();
790         %source_files = ();
791         %header_files = ();
792         %resource_files = ();
793         @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
794         foreach $object_file (@object_files) {
795             next if defined $seen_objects{$object_file};
796             $seen_objects{$object_file} = 1;
797             if($object_file =~ /\.lib$/io) {
798                 $lib_files{$object_file} = 1;
799                 next;
800             }
801             $object_deps = $all_object_deps{$object_file};
802             foreach $object_dep (@$object_deps) {
803                 if($object_dep =~ /\.c$/io) {
804                     $source_files{$object_dep} = 1;
805                     next;
806                 }
807                 if($object_dep =~ /\.h$/io) {
808                     $header_files{$object_dep} = 1;
809                     next;
810                 }
811                 if($object_dep =~ /\.(rc|ico)$/io) {
812                     $resource_files{$object_dep} = 1;
813                     next;
814                 }
815             }
816         }
817         $libs = join " ", sort keys %lib_files;
818         @source_files = sort keys %source_files;
819         @header_files = sort keys %header_files;
820         @resources = sort keys %resource_files;
821         ($windows_project, $type) = split ",", $progname;
822         mkdir $windows_project
823             if(! -d $windows_project);
824         chdir $windows_project;
825         $subsys = ($type eq "G") ? "windows" : "console";
826         open OUT, ">$windows_project.dsp"; binmode OUT; select OUT;
827         print
828         "# Microsoft Developer Studio Project File - Name=\"$windows_project\" - Package Owner=<4>\r\n".
829         "# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n".
830         "# ** DO NOT EDIT **\r\n".
831         "\r\n".
832         "# TARGTYPE \"Win32 (x86) Application\" 0x0101\r\n".
833         "\r\n".
834         "CFG=$windows_project - Win32 Debug\r\n".
835         "!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n".
836         "!MESSAGE use the Export Makefile command and run\r\n".
837         "!MESSAGE \r\n".
838         "!MESSAGE NMAKE /f \"$windows_project.mak\".\r\n".
839         "!MESSAGE \r\n".
840         "!MESSAGE You can specify a configuration when running NMAKE\r\n".
841         "!MESSAGE by defining the macro CFG on the command line. For example:\r\n".
842         "!MESSAGE \r\n".
843         "!MESSAGE NMAKE /f \"$windows_project.mak\" CFG=\"$windows_project - Win32 Debug\"\r\n".
844         "!MESSAGE \r\n".
845         "!MESSAGE Possible choices for configuration are:\r\n".
846         "!MESSAGE \r\n".
847         "!MESSAGE \"$windows_project - Win32 Release\" (based on \"Win32 (x86) Application\")\r\n".
848         "!MESSAGE \"$windows_project - Win32 Debug\" (based on \"Win32 (x86) Application\")\r\n".
849         "!MESSAGE \r\n".
850         "\r\n".
851         "# Begin Project\r\n".
852         "# PROP AllowPerConfigDependencies 0\r\n".
853         "# PROP Scc_ProjName \"\"\r\n".
854         "# PROP Scc_LocalPath \"\"\r\n".
855         "CPP=cl.exe\r\n".
856         "MTL=midl.exe\r\n".
857         "RSC=rc.exe\r\n".
858         "\r\n".
859         "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
860         "\r\n".
861         "# PROP BASE Use_MFC 0\r\n".
862         "# PROP BASE Use_Debug_Libraries 0\r\n".
863         "# PROP BASE Output_Dir \"Release\"\r\n".
864         "# PROP BASE Intermediate_Dir \"Release\"\r\n".
865         "# PROP BASE Target_Dir \"\"\r\n".
866         "# PROP Use_MFC 0\r\n".
867         "# PROP Use_Debug_Libraries 0\r\n".
868         "# PROP Output_Dir \"Release\"\r\n".
869         "# PROP Intermediate_Dir \"Release\"\r\n".
870         "# PROP Ignore_Export_Lib 0\r\n".
871         "# PROP Target_Dir \"\"\r\n".
872         "# ADD BASE CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
873         "# ADD CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
874         "# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
875         "# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
876         "# ADD BASE RSC /l 0x809 /d \"NDEBUG\"\r\n".
877         "# ADD RSC /l 0x809 /d \"NDEBUG\"\r\n".
878         "BSC32=bscmake.exe\r\n".
879         "# ADD BASE BSC32 /nologo\r\n".
880         "# ADD BSC32 /nologo\r\n".
881         "LINK32=link.exe\r\n".
882         "# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:$subsys /machine:I386\r\n".
883         "# ADD LINK32 $libs /nologo /subsystem:$subsys /machine:I386\r\n".
884         "# SUBTRACT LINK32 /pdb:none\r\n".
885         "\r\n".
886         "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
887         "\r\n".
888         "# PROP BASE Use_MFC 0\r\n".
889         "# PROP BASE Use_Debug_Libraries 1\r\n".
890         "# PROP BASE Output_Dir \"Debug\"\r\n".
891         "# PROP BASE Intermediate_Dir \"Debug\"\r\n".
892         "# PROP BASE Target_Dir \"\"\r\n".
893         "# PROP Use_MFC 0\r\n".
894         "# PROP Use_Debug_Libraries 1\r\n".
895         "# PROP Output_Dir \"Debug\"\r\n".
896         "# PROP Intermediate_Dir \"Debug\"\r\n".
897         "# PROP Ignore_Export_Lib 0\r\n".
898         "# PROP Target_Dir \"\"\r\n".
899         "# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
900         "# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
901         "# ADD BASE MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
902         "# ADD MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
903         "# ADD BASE RSC /l 0x809 /d \"_DEBUG\"\r\n".
904         "# ADD RSC /l 0x809 /d \"_DEBUG\"\r\n".
905         "BSC32=bscmake.exe\r\n".
906         "# ADD BASE BSC32 /nologo\r\n".
907         "# ADD BSC32 /nologo\r\n".
908         "LINK32=link.exe\r\n".
909         "# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:$subsys /debug /machine:I386 /pdbtype:sept\r\n".
910         "# ADD LINK32 $libs /nologo /subsystem:$subsys /debug /machine:I386 /pdbtype:sept\r\n".
911         "# SUBTRACT LINK32 /pdb:none\r\n".
912         "\r\n".
913         "!ENDIF \r\n".
914         "\r\n".
915         "# Begin Target\r\n".
916         "\r\n".
917         "# Name \"$windows_project - Win32 Release\"\r\n".
918         "# Name \"$windows_project - Win32 Debug\"\r\n".
919         "# Begin Group \"Source Files\"\r\n".
920         "\r\n".
921         "# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\"\r\n";
922         foreach $source_file (@source_files) {
923             print
924               "# Begin Source File\r\n".
925               "\r\n".
926               "SOURCE=..\\..\\$source_file\r\n";
927             if($source_file =~ /ssh\.c/io) {
928                 # Disable 'Edit and continue' as Visual Studio can't handle the macros
929                 print
930                   "\r\n".
931                   "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
932                   "\r\n".
933                   "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
934                   "\r\n".
935                   "# ADD CPP /Zi\r\n".
936                   "\r\n".
937                   "!ENDIF \r\n".
938                   "\r\n";
939             }
940             print "# End Source File\r\n";
941         }
942         print
943         "# End Group\r\n".
944         "# Begin Group \"Header Files\"\r\n".
945         "\r\n".
946         "# PROP Default_Filter \"h;hpp;hxx;hm;inl\"\r\n";
947         foreach $header_file (@header_files) {
948             print
949               "# Begin Source File\r\n".
950               "\r\n".
951               "SOURCE=..\\..\\$header_file\r\n".
952               "# End Source File\r\n";
953         }
954         print
955         "# End Group\r\n".
956         "# Begin Group \"Resource Files\"\r\n".
957         "\r\n".
958         "# PROP Default_Filter \"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\"\r\n";
959         foreach $resource_file (@resources) {
960             print
961               "# Begin Source File\r\n".
962               "\r\n".
963               "SOURCE=..\\..\\$resource_file\r\n".
964               "# End Source File\r\n";
965         }
966         print
967         "# End Group\r\n".
968         "# End Target\r\n".
969         "# End Project\r\n";
970         select STDOUT; close OUT;
971         chdir "..";
972     }
973 }
974
975 if (defined $makefiles{'gtk'}) {
976     $mftyp = 'gtk';
977     $dirpfx = &dirpfx($makefiles{'gtk'}, "/");
978
979     ##-- X/GTK/Unix makefile
980     open OUT, ">$makefiles{'gtk'}"; select OUT;
981     print
982     "# Makefile for $project_name under X/GTK and Unix.\n".
983     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
984     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
985     # gcc command line option is -D not /D
986     ($_ = $help) =~ s/=\/D/=-D/gs;
987     print $_;
988     print
989     "\n".
990     "# You can define this path to point at your tools if you need to\n".
991     "# TOOLPATH = /opt/gcc/bin\n".
992     "CC = \$(TOOLPATH)cc\n".
993     "# You can manually set this to `gtk-config' or `pkg-config gtk+-1.2'\n".
994     "# (depending on what works on your system) if you want to enforce\n".
995     "# building with GTK 1.2, or you can set it to `pkg-config gtk+-2.0'\n".
996     "# if you want to enforce 2.0. The default is to try 2.0 and fall back\n".
997     "# to 1.2 if it isn't found.\n".
998     "GTK_CONFIG = sh -c 'pkg-config gtk+-2.0 \$\$0 2>/dev/null || gtk-config \$\$0'\n".
999     "\n".
1000     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1001                (join " ", map {"-I$dirpfx$_"} @srcdirs) .
1002                " `\$(GTK_CONFIG) --cflags`")."\n".
1003     "XLDFLAGS = `\$(GTK_CONFIG) --libs`\n".
1004     "ULDFLAGS =#\n".
1005     "INSTALL=install\n",
1006     "INSTALL_PROGRAM=\$(INSTALL)\n",
1007     "INSTALL_DATA=\$(INSTALL)\n",
1008     "prefix=/usr/local\n",
1009     "exec_prefix=\$(prefix)\n",
1010     "bindir=\$(exec_prefix)/bin\n",
1011     "gamesdir=\$(exec_prefix)/games\n",
1012     "mandir=\$(prefix)/man\n",
1013     "man1dir=\$(mandir)/man1\n",
1014     "\n";
1015     print &splitline("all:" . join "", map { " $_" } &progrealnames("X:U"));
1016     print "\n\n";
1017     foreach $p (&prognames("X:U")) {
1018       ($prog, $type) = split ",", $p;
1019       $objstr = &objects($p, "X.o", undef, undef);
1020       print &splitline($prog . ": " . $objstr), "\n";
1021       $libstr = &objects($p, undef, undef, "-lX");
1022       print &splitline("\t\$(CC)" . $mw . " \$(${type}LDFLAGS) -o \$@ " .
1023                        $objstr . " $libstr", 69), "\n\n";
1024     }
1025     foreach $d (&deps("X.o", undef, $dirpfx, "/")) {
1026       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1027           "\n";
1028       $deflist = join "", map { " -D$_" } @{$d->{defs}};
1029       print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist" .
1030           " -c \$< -o \$\@\n";
1031     }
1032     print "\n";
1033     print $makefile_extra{'gtk'};
1034     print "\nclean:\n".
1035     "\trm -f *.o". (join "", map { " $_" } &progrealnames("X:U")) . "\n";
1036     select STDOUT; close OUT;
1037 }
1038
1039 if (defined $makefiles{'mpw'}) {
1040     $mftyp = 'mpw';
1041     ##-- MPW Makefile
1042     open OUT, ">$makefiles{'mpw'}"; select OUT;
1043     print
1044     "# Makefile for $project_name under MPW.\n#\n".
1045     "# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1046     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1047     # MPW command line option is -d not /D
1048     ($_ = $help) =~ s/=\/D/=-d /gs;
1049     print $_;
1050     print "\n\n".
1051     "ROptions     = `Echo \"{VER}\" | StreamEdit -e \"1,\$ replace /=(\xc5)\xa81\xb0/ 'STR=\xb6\xb6\xb6\xb6\xb6\"' \xa81 '\xb6\xb6\xb6\xb6\xb6\"'\"`".
1052     "\n".
1053     "C_68K = {C}\n".
1054     "C_CFM68K = {C}\n".
1055     "C_PPC = {PPCC}\n".
1056     "C_Carbon = {PPCC}\n".
1057     "\n".
1058     "# -w 35 disables \"unused parameter\" warnings\n".
1059     "COptions     = -i : -i :: -i ::charset -w 35 -w err -proto strict -ansi on \xb6\n".
1060     "          -notOnce\n".
1061     "COptions_68K = {COptions} -model far -opt time\n".
1062     "# Enabling \"-opt space\" for CFM-68K gives me undefined references to\n".
1063     "# _\$LDIVT and _\$LMODT.\n".
1064     "COptions_CFM68K = {COptions} -model cfmSeg -opt time\n".
1065     "COptions_PPC = {COptions} -opt size -traceback\n".
1066     "COptions_Carbon = {COptions} -opt size -traceback -d TARGET_API_MAC_CARBON\n".
1067     "\n".
1068     "Link_68K = ILink\n".
1069     "Link_CFM68K = ILink\n".
1070     "Link_PPC = PPCLink\n".
1071     "Link_Carbon = PPCLink\n".
1072     "\n".
1073     "LinkOptions = -c 'pTTY'\n".
1074     "LinkOptions_68K = {LinkOptions} -br 68k -model far -compact\n".
1075     "LinkOptions_CFM68K = {LinkOptions} -br 020 -model cfmseg -compact\n".
1076     "LinkOptions_PPC = {LinkOptions}\n".
1077     "LinkOptions_Carbon = -m __appstart -w {LinkOptions}\n".
1078     "\n".
1079     "Libs_68K = \"{CLibraries}StdCLib.far.o\" \xb6\n".
1080     "           \"{Libraries}MacRuntime.o\" \xb6\n".
1081     "           \"{Libraries}MathLib.far.o\" \xb6\n".
1082     "           \"{Libraries}IntEnv.far.o\" \xb6\n".
1083     "           \"{Libraries}Interface.o\" \xb6\n".
1084     "           \"{Libraries}Navigation.far.o\" \xb6\n".
1085     "           \"{Libraries}OpenTransport.o\" \xb6\n".
1086     "           \"{Libraries}OpenTransportApp.o\" \xb6\n".
1087     "           \"{Libraries}OpenTptInet.o\" \xb6\n".
1088     "           \"{Libraries}UnicodeConverterLib.far.o\"\n".
1089     "\n".
1090     "Libs_CFM = \"{SharedLibraries}InterfaceLib\" \xb6\n".
1091     "           \"{SharedLibraries}StdCLib\" \xb6\n".
1092     "           \"{SharedLibraries}AppearanceLib\" \xb6\n".
1093     "                   -weaklib AppearanceLib \xb6\n".
1094     "           \"{SharedLibraries}NavigationLib\" \xb6\n".
1095     "                   -weaklib NavigationLib \xb6\n".
1096     "           \"{SharedLibraries}TextCommon\" \xb6\n".
1097     "                   -weaklib TextCommon \xb6\n".
1098     "           \"{SharedLibraries}UnicodeConverter\" \xb6\n".
1099     "                   -weaklib UnicodeConverter\n".
1100     "\n".
1101     "Libs_CFM68K =      {Libs_CFM} \xb6\n".
1102     "           \"{CFM68KLibraries}NuMacRuntime.o\"\n".
1103     "\n".
1104     "Libs_PPC = {Libs_CFM} \xb6\n".
1105     "           \"{SharedLibraries}ControlsLib\" \xb6\n".
1106     "                   -weaklib ControlsLib \xb6\n".
1107     "           \"{SharedLibraries}WindowsLib\" \xb6\n".
1108     "                   -weaklib WindowsLib \xb6\n".
1109     "           \"{SharedLibraries}OpenTransportLib\" \xb6\n".
1110     "                   -weaklib OTClientLib \xb6\n".
1111     "                   -weaklib OTClientUtilLib \xb6\n".
1112     "           \"{SharedLibraries}OpenTptInternetLib\" \xb6\n".
1113     "                   -weaklib OTInetClientLib \xb6\n".
1114     "           \"{PPCLibraries}StdCRuntime.o\" \xb6\n".
1115     "           \"{PPCLibraries}PPCCRuntime.o\" \xb6\n".
1116     "           \"{PPCLibraries}CarbonAccessors.o\" \xb6\n".
1117     "           \"{PPCLibraries}OpenTransportAppPPC.o\" \xb6\n".
1118     "           \"{PPCLibraries}OpenTptInetPPC.o\"\n".
1119     "\n".
1120     "Libs_Carbon =      \"{PPCLibraries}CarbonStdCLib.o\" \xb6\n".
1121     "           \"{PPCLibraries}StdCRuntime.o\" \xb6\n".
1122     "           \"{PPCLibraries}PPCCRuntime.o\" \xb6\n".
1123     "           \"{SharedLibraries}CarbonLib\" \xb6\n".
1124     "           \"{SharedLibraries}StdCLib\"\n".
1125     "\n";
1126     print &splitline("all \xc4 " . join(" ", &progrealnames("M")), undef, "\xb6");
1127     print "\n\n";
1128     foreach $p (&prognames("M")) {
1129       ($prog, $type) = split ",", $p;
1130
1131       print &splitline("$prog \xc4 $prog.68k $prog.ppc $prog.carbon",
1132                    undef, "\xb6"), "\n\n";
1133
1134       $rsrc = &objects($p, "", "X.rsrc", undef);
1135
1136       foreach $arch (qw(68K CFM68K PPC Carbon)) {
1137           $objstr = &objects($p, "X.\L$arch\E.o", "", undef);
1138           print &splitline("$prog.\L$arch\E \xc4 $objstr $rsrc", undef, "\xb6");
1139           print "\n";
1140           print &splitline("\tDuplicate -y $rsrc {Targ}", 69, "\xb6"), "\n";
1141           print &splitline("\t{Link_$arch} -o {Targ} -fragname $prog " .
1142                        "{LinkOptions_$arch} " .
1143                        $objstr . " {Libs_$arch}", 69, "\xb6"), "\n";
1144           print &splitline("\tSetFile -a BMi {Targ}", 69, "\xb6"), "\n\n";
1145       }
1146
1147     }
1148     foreach $d (&deps("", "X.rsrc", "::", ":")) {
1149       next unless $d->{obj};
1150       print &splitline(sprintf("%s \xc4 %s", $d->{obj}, join " ", @{$d->{deps}}),
1151                    undef, "\xb6"), "\n";
1152       print "\tRez ", $d->{deps}->[0], " -o {Targ} {ROptions}\n\n";
1153     }
1154     foreach $arch (qw(68K CFM68K)) {
1155         foreach $d (&deps("X.\L$arch\E.o", "", "::", ":")) {
1156          next unless $d->{obj};
1157         print &splitline(sprintf("%s \xc4 %s", $d->{obj},
1158                                  join " ", @{$d->{deps}}),
1159                          undef, "\xb6"), "\n";
1160          print "\t{C_$arch} ", $d->{deps}->[0],
1161                " -o {Targ} {COptions_$arch}\n\n";
1162          }
1163     }
1164     foreach $arch (qw(PPC Carbon)) {
1165         foreach $d (&deps("X.\L$arch\E.o", "", "::", ":")) {
1166          next unless $d->{obj};
1167         print &splitline(sprintf("%s \xc4 %s", $d->{obj},
1168                                  join " ", @{$d->{deps}}),
1169                          undef, "\xb6"), "\n";
1170          # The odd stuff here seems to stop afpd getting confused.
1171          print "\techo -n > {Targ}\n";
1172          print "\tsetfile -t XCOF {Targ}\n";
1173          print "\t{C_$arch} ", $d->{deps}->[0],
1174                " -o {Targ} {COptions_$arch}\n\n";
1175          }
1176     }
1177     select STDOUT; close OUT;
1178 }
1179
1180 if (defined $makefiles{'lcc'}) {
1181     $mftyp = 'lcc';
1182     $dirpfx = &dirpfx($makefiles{'lcc'}, "\\");
1183
1184     ##-- lcc makefile
1185     open OUT, ">$makefiles{'lcc'}"; select OUT;
1186     print
1187     "# Makefile for $project_name under lcc.\n".
1188     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1189     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1190     # lcc command line option is -D not /D
1191     ($_ = $help) =~ s/=\/D/=-D/gs;
1192     print $_;
1193     print
1194     "\n".
1195     "# If you rename this file to `Makefile', you should change this line,\n".
1196     "# so that the .rsp files still depend on the correct makefile.\n".
1197     "MAKEFILE = Makefile.lcc\n".
1198     "\n".
1199     "# C compilation flags\n".
1200     "CFLAGS = -D_WINDOWS " .
1201       (join " ", map {"-I$dirpfx$_"} @srcdirs) .
1202       "\n".
1203     "\n".
1204     "# Get include directory for resource compiler\n".
1205     "\n";
1206     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
1207     print "\n\n";
1208     foreach $p (&prognames("G:C")) {
1209       ($prog, $type) = split ",", $p;
1210       $objstr = &objects($p, "X.obj", "X.res", undef);
1211       print &splitline("$prog.exe: " . $objstr ), "\n";
1212       $subsystemtype = undef;
1213       if ($type eq "G") { $subsystemtype = "-subsystem  windows"; }
1214       my $libss = "shell32.lib wsock32.lib ws2_32.lib winspool.lib winmm.lib imm32.lib";
1215       print &splitline("\tlcclnk $subsystemtype -o $prog.exe $objstr $libss");
1216       print "\n\n";
1217     }
1218
1219     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\")) {
1220       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1221         "\n";
1222       if ($d->{obj} =~ /\.res$/) {
1223         print &splitline("\tlrc \$(FWHACK) \$(RCFL) -r \$*.rc",69)."\n";
1224       } else {
1225         $deflist = join "", map { " -D$_" } @{$d->{defs}};
1226         print &splitline("\tlcc -O -p6 \$(COMPAT) \$(FWHACK) \$(CFLAGS)".
1227                          " \$(XFLAGS)$deflist ".$d->{deps}->[0]." -o \$\@",69)."\n";
1228       }
1229     }
1230     print "\n";
1231     print $makefile_extra{'lcc'};
1232     print "\nclean:\n".
1233     "\t-del *.obj\n".
1234     "\t-del *.exe\n".
1235     "\t-del *.res\n";
1236
1237     select STDOUT; close OUT;
1238 }
1239
1240 if (defined $makefiles{'osx'}) {
1241     $mftyp = 'osx';
1242     $dirpfx = &dirpfx($makefiles{'osx'}, "/");
1243
1244     ##-- Mac OS X makefile
1245     open OUT, ">$makefiles{'osx'}"; select OUT;
1246     print
1247     "# Makefile for $project_name under Mac OS X.\n".
1248     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1249     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1250     # gcc command line option is -D not /D
1251     ($_ = $help) =~ s/=\/D/=-D/gs;
1252     print $_;
1253     print
1254     "CC = \$(TOOLPATH)gcc\n".
1255     "\n".
1256     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1257                (join " ", map {"-I$dirpfx$_"} @srcdirs))."\n".
1258     "LDFLAGS = -framework Cocoa\n".
1259     &splitline("all:" . join "", map { " $_" } &progrealnames("MX:U")) .
1260     "\n" .
1261     $makefile_extra{'osx'} .
1262     "\n".
1263     ".SUFFIXES: .o .c .m\n".
1264     "\n";
1265     print "\n\n";
1266     foreach $p (&prognames("MX")) {
1267       ($prog, $type) = split ",", $p;
1268       $objstr = &objects($p, "X.o", undef, undef);
1269       $icon = &special($p, ".icns");
1270       $infoplist = &special($p, "info.plist");
1271       print "${prog}.app:\n\tmkdir -p \$\@\n";
1272       print "${prog}.app/Contents: ${prog}.app\n\tmkdir -p \$\@\n";
1273       print "${prog}.app/Contents/MacOS: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1274       $targets = "${prog}.app/Contents/MacOS/$prog";
1275       if (defined $icon) {
1276         print "${prog}.app/Contents/Resources: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1277         print "${prog}.app/Contents/Resources/${prog}.icns: ${prog}.app/Contents/Resources $icon\n\tcp $icon \$\@\n";
1278         $targets .= " ${prog}.app/Contents/Resources/${prog}.icns";
1279       }
1280       if (defined $infoplist) {
1281         print "${prog}.app/Contents/Info.plist: ${prog}.app/Contents/Resources $infoplist\n\tcp $infoplist \$\@\n";
1282         $targets .= " ${prog}.app/Contents/Info.plist";
1283       }
1284       $targets .= " \$(${prog}_extra)";
1285       print &splitline("${prog}: $targets", 69) . "\n\n";
1286       print &splitline("${prog}.app/Contents/MacOS/$prog: ".
1287                        "${prog}.app/Contents/MacOS " . $objstr), "\n";
1288       $libstr = &objects($p, undef, undef, "-lX");
1289       print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
1290                        $objstr . " $libstr", 69), "\n\n";
1291     }
1292     foreach $p (&prognames("U")) {
1293       ($prog, $type) = split ",", $p;
1294       $objstr = &objects($p, "X.o", undef, undef);
1295       print &splitline($prog . ": " . $objstr), "\n";
1296       $libstr = &objects($p, undef, undef, "-lX");
1297       print &splitline("\t\$(CC)" . $mw . " \$(ULDFLAGS) -o \$@ " .
1298                        $objstr . " $libstr", 69), "\n\n";
1299     }
1300     foreach $d (&deps("X.o", undef, $dirpfx, "/")) {
1301       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1302           "\n";
1303       $deflist = join "", map { " -D$_" } @{$d->{defs}};
1304       if ($d->{deps}->[0] =~ /\.m$/) {
1305         print "\t\$(CC) -x objective-c \$(COMPAT) \$(FWHACK) \$(CFLAGS)".
1306             " \$(XFLAGS)$deflist -c \$< -o \$\@\n";
1307       } else {
1308         print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist" .
1309             " -c \$< -o \$\@\n";
1310       }
1311     }
1312     print "\nclean:\n".
1313     "\trm -f *.o *.dmg". (join "", map { " $_" } &progrealnames("U")) . "\n".
1314     "\trm -rf *.app\n";
1315     select STDOUT; close OUT;
1316 }