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