chiark / gitweb /
changelog: document last change
[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 warnings;
21 use IO::Handle;
22 use Cwd;
23 use File::Basename;
24
25 while ($#ARGV >= 0) {
26     if ($ARGV[0] eq "-U") {
27         # Convenience for Unix users: -U means that after we finish what
28         # we're doing here, we also run mkauto.sh and then 'configure'. So
29         # it's a one-stop shop for regenerating the actual end-product
30         # Unix makefile.
31         #
32         # Arguments supplied after -U go to configure.
33         $do_unix = 1;
34         shift @ARGV;
35         @confargs = @ARGV;
36         @ARGV = ();
37     } else {
38         die "unrecognised command-line argument '$ARGV[0]'\n";
39     }
40 }
41
42 @filestack = ();
43 $in = new IO::Handle;
44 open $in, "Recipe" or do {
45     # We want to deal correctly with being run from one of the
46     # subdirs in the source tree. So if we can't find Recipe here,
47     # try one level up.
48     chdir "..";
49     open $in, "Recipe" or die "unable to open Recipe file\n";
50 };
51 push @filestack, $in;
52
53 # HACK: One of the source files in `charset' is auto-generated by
54 # sbcsgen.pl. We need to generate that _now_, before attempting
55 # dependency analysis.
56 eval 'chdir "charset"; require "sbcsgen.pl"; chdir ".."';
57
58 @srcdirs = ("./");
59
60 $divert = undef; # ref to array of refs of scalars in which text is
61                  # currently being put
62 $help = ""; # list of newline-free lines of help text
63 $project_name = "project"; # this is a good enough default
64 %makefiles = (); # maps makefile types to output makefile pathnames
65 %makefile_extra = (); # maps makefile types to extra Makefile text
66 %programs = (); # maps prog name + type letter to listref of objects/resources
67 %groups = (); # maps group name to listref of objects/resources
68
69 @allobjs = (); # all object file names
70
71 readinput: while (1) {
72   $in = $filestack[$#filestack];
73   while (not defined ($_ = <$in>)) {
74     close $filestack[$#filestack];
75     pop @filestack;
76     last readinput if 0 == scalar @filestack;
77     $in = $filestack[$#filestack];
78   }
79   chomp;
80   @_ = split;
81
82   # If we're gathering help text, keep doing so.
83   if (defined $divert) {
84       if ((defined $_[0]) && $_[0] eq "!end") {
85           $divert = undef;
86       } else {
87           for my $ref (@$divert) {
88               ${$ref} .= "$_\n";
89           }
90       }
91       next;
92   }
93   # Skip comments and blank lines.
94   next if /^\s*#/ or scalar @_ == 0;
95
96   if ($_[0] eq "!begin" and $_[1] eq "help") { $divert = [\$help]; next; }
97   if ($_[0] eq "!name") { $project_name = $_[1]; next; }
98   if ($_[0] eq "!srcdir") { push @srcdirs, $_[1]; next; }
99   if ($_[0] eq "!makefile" and &mfval($_[1])) { $makefiles{$_[1]}=$_[2]; next;}
100   if ($_[0] eq "!specialobj" and &mfval($_[1])) { $specialobj{$_[1]}->{$_[2]} = 1; next;}
101   if ($_[0] eq "!cflags" and &mfval($_[1])) {
102       ($rest = $_) =~ s/^\s*\S+\s+\S+\s+\S+\s*//; # find rest of input line
103       $rest = 1 if $rest eq "";
104       $cflags{$_[1]}->{$_[2]} = $rest;
105       next;
106   }
107   if ($_[0] eq "!begin") {
108       my @args = @_;
109       shift @args;
110       $divert = [];
111       for my $component (@args) {
112           if ($component =~ /^>(.*)/) {
113               push @$divert, \$auxfiles{$1};
114           } elsif ($component =~ /^([^_]*)(_.*)?$/ and &mfval($1)) {
115               push @$divert, \$makefile_extra{$component};
116           }
117       }
118       next;
119   }
120   if ($_[0] eq "!include") {
121       @newfiles = ();
122       for ($i = 1; $i <= $#_; $i++) {
123           push @newfiles, (sort glob $_[$i]);
124       }
125       for ($i = $#newfiles; $i >= 0; $i--) {
126           $file = $newfiles[$i];
127           $f = new IO::Handle;
128           open $f, "<$file" or die "unable to open include file '$file'\n";
129           push @filestack, $f;
130       }
131       next;
132   }
133
134   # Now we have an ordinary line. See if it's an = line, a : line
135   # or a + line.
136   @objs = @_;
137
138   if ($_[0] eq "+") {
139     $listref = $lastlistref;
140     $prog = undef;
141     die "$.: unexpected + line\n" if !defined $lastlistref;
142   } elsif ($_[1] eq "=") {
143     $groups{$_[0]} = [];
144     $listref = $groups{$_[0]};
145     $prog = undef;
146     shift @objs; # eat the group name
147   } elsif ($_[1] eq "+=") {
148     $groups{$_[0]} = [] if !defined $groups{$_[0]};
149     $listref = $groups{$_[0]};
150     $prog = undef;
151     shift @objs; # eat the group name
152   } elsif ($_[1] eq ":") {
153     $listref = [];
154     $prog = $_[0];
155     shift @objs; # eat the program name
156   } else {
157     die "$.: unrecognised line type: '$_'\n";
158   }
159   shift @objs; # eat the +, the = or the :
160
161   while (scalar @objs > 0) {
162     $i = shift @objs;
163     if ($groups{$i}) {
164       foreach $j (@{$groups{$i}}) { unshift @objs, $j; }
165     } elsif (($i eq "[G]" or $i eq "[C]" or $i eq "[M]" or
166               $i eq "[X]" or $i eq "[U]" or $i eq "[MX]") and defined $prog) {
167       $type = substr($i,1,(length $i)-2);
168     } else {
169       if ($i =~ /\?$/) {
170         # Object files with a trailing question mark are optional:
171         # the build can proceed fine without them, so we only use
172         # them if their primary source files are present.
173         $i =~ s/\?$//;
174         $i = undef unless defined &finddep($i);
175       } elsif ($i =~ /\|/) {
176         # Object file descriptions containing a vertical bar are
177         # lists of choices: we use the _first_ one whose primary
178         # source file is present.
179         @options = split /\|/, $i;
180         $j = undef;
181         foreach $k (@options) {
182           $j=$k, last if defined &finddep($k);
183         }
184         die "no alternative found for $i\n" unless defined $j;
185         $i = $j;
186       }
187       if (defined $i) {
188         push @$listref, $i;
189         push @allobjs, $i;
190       }
191     }
192   }
193   if ($prog and $type) {
194     die "multiple program entries for $prog [$type]\n"
195         if defined $programs{$prog . "," . $type};
196     $programs{$prog . "," . $type} = $listref;
197   }
198   $lastlistref = $listref;
199 }
200
201 foreach $aux (sort keys %auxfiles) {
202     open AUX, ">$aux";
203     print AUX $auxfiles{$aux};
204     close AUX;
205 }
206
207 # Find object file names with predefines (in square brackets after
208 # the module name), and decide on actual object names for them.
209 foreach $i (@allobjs) {
210   if ($i !~ /\[/) {
211     $objname{$i} = $i;
212     $srcname{$i} = $i;
213     $usedobjname{$i} = 1;
214   }
215 }
216 foreach $i (@allobjs) {
217   if ($i =~ /^(.*)\[([^\]]*)/) {
218     $defs{$i} = [ split ",",$2 ];
219     $srcname{$i} = $s = $1;
220     $index = 1;
221     while (1) {
222       $maxlen = length $s;
223       $maxlen = 8 if $maxlen < 8;
224       $chop = $maxlen - length $index;
225       $chop = length $s if $chop > length $s;
226       $chop = 0 if $chop < 0;
227       $name = substr($s, 0, $chop) . $index;
228       $index++, next if $usedobjname{$name};
229       $objname{$i} = $name;
230       $usedobjname{$name} = 1;
231       last;
232     }
233   }
234 }
235
236 # Now retrieve the complete list of objects and resource files, and
237 # construct dependency data for them. While we're here, expand the
238 # object list for each program, and complain if its type isn't set.
239 @prognames = sort keys %programs;
240 %depends = ();
241 @scanlist = ();
242 foreach $i (@prognames) {
243   ($prog, $type) = split ",", $i;
244   # Strip duplicate object names.
245   $prev = '';
246   @list = grep { $status = ($prev ne $_); $prev=$_; $status }
247           sort @{$programs{$i}};
248   $programs{$i} = [@list];
249   foreach $jj (@list) {
250     $j = $srcname{$jj};
251     $file = &finddep($j);
252     if (defined $file) {
253       $depends{$jj} = [$file];
254       push @scanlist, $file;
255     }
256   }
257 }
258
259 # Scan each file on @scanlist and find further inclusions.
260 # Inclusions are given by lines of the form `#include "otherfile"'
261 # (system headers are automatically ignored by this because they'll
262 # be given in angle brackets). Files included by this method are
263 # added back on to @scanlist to be scanned in turn (if not already
264 # done).
265 #
266 # Resource scripts (.rc) can also include a file by means of a line
267 # ending `ICON "filename"'. Files included by this method are not
268 # added to @scanlist because they can never include further files.
269 #
270 # In this pass we write out a hash %further which maps a source
271 # file name into a listref containing further source file names.
272
273 %further = ();
274 while (scalar @scanlist > 0) {
275   $file = shift @scanlist;
276   next if defined $further{$file}; # skip if we've already done it
277   $further{$file} = [];
278   $dirfile = &findfile($file);
279   open IN, "$dirfile" or die "unable to open source file $file\n";
280   while (<IN>) {
281     chomp;
282     /^\s*#include\s+\"([^\"]+)\"/ and do {
283       push @{$further{$file}}, $1;
284       push @scanlist, $1;
285       next;
286     };
287     /ICON\s+\"([^\"]+)\"\s*$/ and do {
288       push @{$further{$file}}, $1;
289       next;
290     }
291   }
292   close IN;
293 }
294
295 # Now we're ready to generate the final dependencies section. For
296 # each key in %depends, we must expand the dependencies list by
297 # iteratively adding entries from %further.
298 foreach $i (keys %depends) {
299   %dep = ();
300   @scanlist = @{$depends{$i}};
301   foreach $i (@scanlist) { $dep{$i} = 1; }
302   while (scalar @scanlist > 0) {
303     $file = shift @scanlist;
304     foreach $j (@{$further{$file}}) {
305       if (!$dep{$j}) {
306         $dep{$j} = 1;
307         push @{$depends{$i}}, $j;
308         push @scanlist, $j;
309       }
310     }
311   }
312 #  printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
313 }
314
315 # Validation of input.
316
317 sub mfval($) {
318     my ($type) = @_;
319     # Returns true if the argument is a known makefile type. Otherwise,
320     # prints a warning and returns false;
321     if (grep { $type eq $_ }
322         ("vc","vcproj","cygwin","borland","lcc","gtk","am","mpw","nestedvm","osx","wce","gnustep","emcc","clangcl")) {
323             return 1;
324         }
325     warn "$.:unknown makefile type '$type'\n";
326     return 0;
327 }
328
329 # Utility routines while writing out the Makefiles.
330
331 sub dirpfx {
332     my ($path) = shift @_;
333     my ($sep) = shift @_;
334     my $ret = "";
335     my $i;
336     while (($i = index $path, $sep) >= 0) {
337         $path = substr $path, ($i + length $sep);
338         $ret .= "..$sep";
339     }
340     return $ret;
341 }
342
343 sub findfile {
344   my ($name) = @_;
345   my $dir;
346   my $i;
347   my $outdir = undef;
348   unless (defined $findfilecache{$name}) {
349     $i = 0;
350     foreach $dir (@srcdirs) {
351       $outdir = $dir, $i++ if -f "$dir$name";
352     }
353     die "multiple instances of source file $name\n" if $i > 1;
354     $findfilecache{$name} = (defined $outdir ? $outdir . $name : undef);
355   }
356   return $findfilecache{$name};
357 }
358
359 sub finddep {
360   my $j = shift @_;
361   my $file;
362   # Find the first dependency of an object.
363
364   # Dependencies for "x" start with "x.c" or "x.m" (depending on
365   # which one exists).
366   # Dependencies for "x.res" start with "x.rc".
367   # Dependencies for "x.rsrc" start with "x.r".
368   # Both types of file are pushed on the list of files to scan.
369   # Libraries (.lib) don't have dependencies at all.
370   if ($j =~ /^(.*)\.res$/) {
371     $file = "$1.rc";
372   } elsif ($j =~ /^(.*)\.rsrc$/) {
373     $file = "$1.r";
374   } elsif ($j !~ /\./) {
375     $file = "$j.c";
376     $file = "$j.m" unless &findfile($file);
377   } else {
378     # For everything else, we assume it's its own dependency.
379     $file = $j;
380   }
381   $file = undef unless &findfile($file);
382   return $file;
383 }
384
385 sub objects {
386   my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
387   my @ret;
388   my ($i, $x, $y);
389   ($otmpl, $rtmpl, $ltmpl) = map { defined $_ ? $_ : "" } ($otmpl, $rtmpl, $ltmpl);
390   @ret = ();
391   foreach $ii (@{$programs{$prog}}) {
392     $i = $objname{$ii};
393     $x = "";
394     if ($i =~ /^(.*)\.(res|rsrc)/) {
395       $y = $1;
396       ($x = $rtmpl) =~ s/X/$y/;
397     } elsif ($i =~ /^(.*)\.lib/) {
398       $y = $1;
399       ($x = $ltmpl) =~ s/X/$y/;
400     } elsif ($i !~ /\./) {
401       ($x = $otmpl) =~ s/X/$i/;
402     }
403     push @ret, $x if $x ne "";
404   }
405   return join " ", @ret;
406 }
407
408 sub special {
409   my ($prog, $suffix) = @_;
410   my @ret;
411   my ($i, $x, $y);
412   @ret = ();
413   foreach $ii (@{$programs{$prog}}) {
414     $i = $objname{$ii};
415     if (substr($i, (length $i) - (length $suffix)) eq $suffix) {
416       push @ret, $i;
417     }
418   }
419   return join " ", @ret;
420 }
421
422 sub splitline {
423   my ($line, $width, $splitchar) = @_;
424   my $result = "";
425   my $len;
426   $len = (defined $width ? $width : 76);
427   $splitchar = (defined $splitchar ? $splitchar : '\\');
428   while (length $line > $len) {
429     $line =~ /^(.{0,$len})\s(.*)$/ or $line =~ /^(.{$len,}?\s(.*)$/;
430     $result .= $1;
431     $result .= " ${splitchar}\n\t\t" if $2 ne '';
432     $line = $2;
433     $len = 60;
434   }
435   return $result . $line;
436 }
437
438 sub deps {
439   my ($otmpl, $rtmpl, $prefix, $dirsep, $depchar, $splitchar) = @_;
440   my ($i, $x, $y);
441   my @deps;
442   my @ret;
443   @ret = ();
444   $depchar ||= ':';
445   foreach $ii (sort keys %depends) {
446     $i = $objname{$ii};
447     next if $specialobj{$mftyp}->{$i};
448     if ($i =~ /^(.*)\.(res|rsrc)/) {
449       next if !defined $rtmpl;
450       $y = $1;
451       ($x = $rtmpl) =~ s/X/$y/;
452     } else {
453       ($x = $otmpl) =~ s/X/$i/;
454     }
455     @deps = @{$depends{$ii}};
456     # Skip things which are their own dependency.
457     next if grep { $_ eq $i } @deps;
458     @deps = map {
459       $_ = &findfile($_);
460       s/\//$dirsep/g;
461       $_ = $prefix . $_;
462     } @deps;
463     push @ret, {obj => $x, deps => [@deps], defs => $defs{$ii}};
464   }
465   return @ret;
466 }
467
468 sub prognames {
469   my ($types) = @_;
470   my ($n, $prog, $type);
471   my @ret;
472   @ret = ();
473   foreach $n (@prognames) {
474     ($prog, $type) = split ",", $n;
475     push @ret, $n if index(":$types:", ":$type:") >= 0;
476   }
477   return @ret;
478 }
479
480 sub progrealnames {
481   my ($types) = @_;
482   my ($n, $prog, $type);
483   my @ret;
484   @ret = ();
485   foreach $n (@prognames) {
486     ($prog, $type) = split ",", $n;
487     push @ret, $prog if index(":$types:", ":$type:") >= 0;
488   }
489   return @ret;
490 }
491
492 sub manpages {
493   my ($types,$suffix) = @_;
494
495   # assume that all UNIX programs have a man page
496   if($suffix eq "1" && $types =~ /:X:/) {
497     return map("$_.1", &progrealnames($types));
498   }
499   return ();
500 }
501
502 $orig_dir = cwd;
503
504 # Now we're ready to output the actual Makefiles.
505
506 if (defined $makefiles{'clangcl'}) {
507     $mftyp = 'clangcl';
508     $dirpfx = &dirpfx($makefiles{'clangcl'}, "/");
509
510     ##-- Makefile for cross-compiling using clang-cl, lld-link, and
511     ##   MinGW's windres for resource compilation.
512     #
513     # This makefile allows a complete Linux-based cross-compile, but
514     # using the real Visual Studio header files and libraries. In
515     # order to run it, you will need:
516     #
517     #  - MinGW windres on your PATH.
518     #     * On Ubuntu as of 16.04, you can apt-get install
519     #       binutils-mingw-w64-x86-64 and binutils-mingw-w64-i686
520     #       which will provide (respectively) 64- and 32-bit versions,
521     #       under the names to which RCCMD is defined below.
522     #  - clang-cl and lld-link on your PATH.
523     #     * I built these from the up-to-date LLVM project trunk git
524     #       repositories, as of 2017-02-05.
525     #  - case-mashed copies of the Visual Studio include directories.
526     #     * On a real VS installation, run vcvars32.bat and look at
527     #       the resulting value of %INCLUDE%. Take a full copy of each
528     #       of those directories, and inside the copy, for each
529     #       include file that has an uppercase letter in its name,
530     #       make a lowercased symlink to it. Additionally, one of the
531     #       directories will contain files called driverspecs.h and
532     #       specstrings.h, and those will need symlinks called
533     #       DriverSpecs.h and SpecStrings.h.
534     #     * Now, on Linux, define the environment variable INCLUDE to
535     #       be a list, separated by *semicolons* (in the Windows
536     #       style), of those directories, but before all of them you
537     #       must also include lib/clang/5.0.0/include from the clang
538     #       installation area (which contains in particular a
539     #       clang-compatible stdarg.h overriding the Visual Studio
540     #       one).
541     #  - similarly case-mashed copies of the library directories.
542     #     * Again, on a real VS installation, run vcvars32 or
543     #       vcvarsx86_amd64 (as appropriate), look at %LIB%, make a
544     #       copy of each directory, and provide symlinks within that
545     #       directory so that all the files can be opened as
546     #       lowercase.
547     #     * Then set LIB to be a semicolon-separated list of those
548     #       directories (but you'll need to change which set of
549     #       directories depending on whether you want to do a 32-bit
550     #       or 64-bit build).
551     #  - for a 64-bit build, set 'Platform=x64' in the environment as
552     #    well, or else on the make command line.
553     #     * This is a variable understood only by this makefile - none
554     #       of the tools we invoke will know it - but it's consistent
555     #       with the way the VS scripts like vcvarsx86_amd64.bat set
556     #       things up, and since the environment has to change
557     #       _anyway_ between 32- and 64-bit builds (different set of
558     #       paths in $LIB) it's reasonable to have the choice of
559     #       compilation target driven by another environment variable
560     #       set in parallel with that one.
561     #  - for older versions of the VS libraries you may also have to
562     #    set EXTRA_console and/or EXTRA_windows to the name of an
563     #    object file manually extracted from one of those libraries.
564     #     * This is because old VS seems to manage its startup code by
565     #       having libcmt.lib contain lots of *crt0.obj objects, one
566     #       for each possible user entry point (main, WinMain and the
567     #       wide-char versions of both), of which the linker arranges
568     #       to include the right one by special-case code. But lld
569     #       only seems to mimic half of that code - it does include
570     #       the right crt0 object, but it doesn't also deliberately
571     #       _avoid_ including the _wrong_ ones, and since all those
572     #       objects define a common set of global symbols for other
573     #       parts of the library to use, lld may well select an
574     #       arbitrary one of them the first time it sees a reference
575     #       to one of those global symbols, and then later also select
576     #       the _right_ one for the application's entry point, causing
577     #       a multiple-definitions crash.
578     #     * So the workaround is to explicitly include the right
579     #       *crt0.obj file on the linker command line before lld even
580     #       begins searching libraries. Hence, for a console
581     #       application, you might extract crt0.obj from the library
582     #       in question and set EXTRA_console=crt0.obj, and for a GUI
583     #       application, do the same with wincrt0.obj. Then this
584     #       makefile will include the right one of those objects
585     #       alongside the matching /subsystem linker option.
586
587     open OUT, ">$makefiles{'clangcl'}"; select OUT;
588     print
589     "# Makefile for cross-compiling $project_name using clang-cl, lld-link,\n".
590     "# and MinGW's windres, using GNU make on Linux.\n".
591     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
592     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
593     print $help;
594     print
595     "\n".
596     "CCCMD = clang-cl\n".
597     "ifeq (\$(Platform),x64)\n".
598     "CCTARGET = x86_64-pc-windows-msvc18.0.0\n".
599     "RCCMD = x86_64-w64-mingw32-windres\n".
600     "else\n".
601     "CCTARGET = i386-pc-windows-msvc18.0.0\n".
602     "RCCMD = i686-w64-mingw32-windres\n".
603     "endif\n".
604     "CC = \$(CCCMD) --target=\$(CCTARGET)\n".
605     &splitline("RC = \$(RCCMD) --preprocessor=\$(CCCMD) ".
606                "--preprocessor-arg=/TC --preprocessor-arg=/E")."\n".
607     "LD = lld-link\n".
608     "\n".
609     "# C compilation flags\n".
610     &splitline("CFLAGS = /nologo /W3 /O1 " .
611                (join " ", map {"-I$dirpfx$_"} @srcdirs) .
612                " /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401 ".
613                "/D_CRT_SECURE_NO_WARNINGS")."\n".
614     "LFLAGS = /incremental:no /dynamicbase /nxcompat\n".
615     &splitline("RCFLAGS = ".(join " ", map {"-I$dirpfx$_"} @srcdirs).
616                " -DWIN32 -D_WIN32 -DWINVER=0x0400 --define MINGW32_FIX=1")."\n".
617     "\n".
618     "\n";
619     print &splitline("all:" . join "", map { " \$(BUILDDIR)$_.exe" } &progrealnames("G:C"));
620     print "\n\n";
621     foreach $p (&prognames("G:C")) {
622         ($prog, $type) = split ",", $p;
623         $objstr = &objects($p, "\$(BUILDDIR)X.obj", "\$(BUILDDIR)X.res", undef);
624         print &splitline("\$(BUILDDIR)$prog.exe: " . $objstr), "\n";
625
626         $objstr = &objects($p, "\$(BUILDDIR)X.obj", "\$(BUILDDIR)X.res", "X.lib");
627         $subsys = ($type eq "G") ? "windows" : "console";
628         print &splitline("\t\$(LD) \$(LFLAGS) \$(XLFLAGS) ".
629                          "/out:\$(BUILDDIR)$prog.exe ".
630                          "/lldmap:\$(BUILDDIR)$prog.map ".
631                          "/subsystem:$subsys\$(SUBSYSVER) ".
632                          "\$(EXTRA_$subsys) $objstr")."\n\n";
633     }
634     foreach $d (&deps("\$(BUILDDIR)X.obj", "\$(BUILDDIR)X.res", $dirpfx, "/", "vc")) {
635         print &splitline(sprintf("%s: %s", $d->{obj},
636                                  join " ", @{$d->{deps}})), "\n";
637         if ($d->{obj} =~ /\.res$/) {
638             print "\t\$(RC) \$(RCFLAGS) ".$d->{deps}->[0]." -o ".$d->{obj}."\n\n";
639         } else {
640             $deflist = join "", map { " /D$_" } @{$d->{defs}};
641             print "\t\$(CC) /Fo\$(BUILDDIR)".$d->{obj}." \$(COMPAT) \$(CFLAGS) \$(XFLAGS)$deflist /c \$<\n\n";
642         }
643     }
644     print "\nclean:\n".
645         &splitline("\trm -f \$(BUILDDIR)*.obj \$(BUILDDIR)*.exe ".
646                    "\$(BUILDDIR)*.res \$(BUILDDIR)*.map ".
647                    "\$(BUILDDIR)*.exe.manifest")."\n";
648     select STDOUT; close OUT;
649 }
650
651 if (defined $makefiles{'cygwin'}) {
652     $mftyp = 'cygwin';
653     $dirpfx = &dirpfx($makefiles{'cygwin'}, "/");
654
655     ##-- CygWin makefile
656     open OUT, ">$makefiles{'cygwin'}"; select OUT;
657     print
658     "# Makefile for $project_name under cygwin.\n".
659     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
660     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
661     # gcc command line option is -D not /D
662     ($_ = $help) =~ s/=\/D/=-D/gs;
663     print $_;
664     print
665     "\n".
666     "# You can define this path to point at your tools if you need to\n".
667     "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
668     "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
669     "CC = \$(TOOLPATH)gcc\n".
670     "RC = \$(TOOLPATH)windres\n".
671     "# Uncomment the following two lines to compile under Winelib\n".
672     "# CC = winegcc\n".
673     "# RC = wrc\n".
674     "# You may also need to tell windres where to find include files:\n".
675     "# RCINC = --include-dir c:\\cygwin\\include\\\n".
676     "\n".
677     &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
678       " -D_NO_OLDNAMES -DNO_MULTIMON -DNO_HTMLHELP " .
679                (join " ", map {"-I$dirpfx$_"} @srcdirs)) .
680                "\n".
681     "LDFLAGS = -mno-cygwin -s\n".
682     &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
683       " --define WINVER=0x0400 --define MINGW32_FIX=1 " .
684         (join " ", map {"--include $dirpfx$_"} @srcdirs) )."\n".
685     "\n";
686     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
687     print "\n\n";
688     foreach $p (&prognames("G:C")) {
689       ($prog, $type) = split ",", $p;
690       $objstr = &objects($p, "X.o", "X.res.o", undef);
691       print &splitline($prog . ".exe: " . $objstr), "\n";
692       my $mw = $type eq "G" ? " -mwindows" : "";
693       $libstr = &objects($p, undef, undef, "-lX");
694       print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
695                        "-Wl,-Map,$prog.map " .
696                        $objstr . " $libstr", 69), "\n\n";
697     }
698     foreach $d (&deps("X.o", "X.res.o", $dirpfx, "/")) {
699       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
700         "\n";
701       if ($d->{obj} =~ /\.res\.o$/) {
702         print "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n";
703       } else {
704         $deflist = join "", map { " -D$_" } @{$d->{defs}};
705         print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS)" .
706             " \$(XFLAGS)$deflist -c \$< -o \$\@\n";
707       }
708     }
709     print "\n";
710     print $makefile_extra{'cygwin'} || "";
711     print "\nclean:\n".
712     "\trm -f *.o *.exe *.res.o *.map\n".
713     "\n";
714     select STDOUT; close OUT;
715
716 }
717
718 ##-- Borland makefile
719 if (defined $makefiles{'borland'}) {
720     $mftyp = 'borland';
721     $dirpfx = &dirpfx($makefiles{'borland'}, "\\");
722
723     %stdlibs = (  # Borland provides many Win32 API libraries intrinsically
724       "advapi32" => 1,
725       "comctl32" => 1,
726       "comdlg32" => 1,
727       "gdi32" => 1,
728       "imm32" => 1,
729       "shell32" => 1,
730       "user32" => 1,
731       "winmm" => 1,
732       "winspool" => 1,
733       "wsock32" => 1,
734     );
735     open OUT, ">$makefiles{'borland'}"; select OUT;
736     print
737     "# Makefile for $project_name under Borland C.\n".
738     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
739     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
740     # bcc32 command line option is -D not /D
741     ($_ = $help) =~ s/=\/D/=-D/gs;
742     print $_;
743     print
744     "\n".
745     "# If you rename this file to `Makefile', you should change this line,\n".
746     "# so that the .rsp files still depend on the correct makefile.\n".
747     "MAKEFILE = Makefile.bor\n".
748     "\n".
749     "# C compilation flags\n".
750     "CFLAGS = -D_WINDOWS -DWINVER=0x0401\n".
751     "\n".
752     "# Get include directory for resource compiler\n".
753     "!if !\$d(BCB)\n".
754     "BCB = \$(MAKEDIR)\\..\n".
755     "!endif\n".
756     "\n";
757     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
758     print "\n\n";
759     foreach $p (&prognames("G:C")) {
760       ($prog, $type) = split ",", $p;
761       $objstr =  &objects($p, "X.obj", "X.res", undef);
762       print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
763       my $ap = ($type eq "G") ? "-aa" : "-ap";
764       print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
765     }
766     foreach $p (&prognames("G:C")) {
767       ($prog, $type) = split ",", $p;
768       print $prog, ".rsp: \$(MAKEFILE)\n";
769       $objstr = &objects($p, "X.obj", undef, undef);
770       @objlist = split " ", $objstr;
771       @objlines = ("");
772       foreach $i (@objlist) {
773         if (length($objlines[$#objlines] . " $i") > 50) {
774           push @objlines, "";
775         }
776         $objlines[$#objlines] .= " $i";
777       }
778       $c0w = ($type eq "G") ? "c0w32" : "c0x32";
779       print "\techo $c0w + > $prog.rsp\n";
780       for ($i=0; $i<=$#objlines; $i++) {
781         $plus = ($i < $#objlines ? " +" : "");
782         print "\techo$objlines[$i]$plus >> $prog.rsp\n";
783       }
784       print "\techo $prog.exe >> $prog.rsp\n";
785       $objstr = &objects($p, "X.obj", "X.res", undef);
786       @libs = split " ", &objects($p, undef, undef, "X");
787       @libs = grep { !$stdlibs{$_} } @libs;
788       unshift @libs, "cw32", "import32";
789       $libstr = join ' ', @libs;
790       print "\techo nul,$libstr, >> $prog.rsp\n";
791       print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
792       print "\n";
793     }
794     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\")) {
795       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
796         "\n";
797       if ($d->{obj} =~ /\.res$/) {
798         print &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) " .
799                          "-i \$(BCB)\\include -r -DNO_WINRESRC_H -DWIN32".
800                          " -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n";
801       } else {
802         $deflist = join "", map { " -D$_" } @{$d->{defs}};
803         print &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT)" .
804                          " \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist ".
805                          (join " ", map {"-I$dirpfx$_"} @srcdirs) .
806                          " /o$d->{obj} /c ".$d->{deps}->[0],69)."\n";
807       }
808     }
809     print "\n";
810     print $makefile_extra{'borland'} || "";
811     print "\nclean:\n".
812     "\t-del *.obj\n".
813     "\t-del *.exe\n".
814     "\t-del *.res\n".
815     "\t-del *.pch\n".
816     "\t-del *.aps\n".
817     "\t-del *.il*\n".
818     "\t-del *.pdb\n".
819     "\t-del *.rsp\n".
820     "\t-del *.tds\n".
821     "\t-del *.\$\$\$\$\$\$\n";
822     select STDOUT; close OUT;
823 }
824
825 if (defined $makefiles{'vc'}) {
826     $mftyp = 'vc';
827     $dirpfx = &dirpfx($makefiles{'vc'}, "\\");
828
829     ##-- Visual C++ makefile
830     open OUT, ">$makefiles{'vc'}"; select OUT;
831     print
832       "# Makefile for $project_name under Visual C.\n".
833       "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
834       "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
835     print $help;
836     print
837       "\n".
838       "# If you rename this file to `Makefile', you should change this line,\n".
839       "# so that the .rsp files still depend on the correct makefile.\n".
840       "MAKEFILE = Makefile.vc\n".
841       "\n".
842       "# C compilation flags\n".
843       "CFLAGS = /nologo /W3 /O1 /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401 /I.\n".
844       "LFLAGS = /incremental:no /fixed\n".
845       "\n";
846     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
847     print "\n\n";
848     foreach $p (&prognames("G:C")) {
849         ($prog, $type) = split ",", $p;
850         $objstr = &objects($p, "X.obj", "X.res", undef);
851         print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
852         print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
853     }
854     foreach $p (&prognames("G:C")) {
855         ($prog, $type) = split ",", $p;
856         print $prog, ".rsp: \$(MAKEFILE)\n";
857         $objstr = &objects($p, "X.obj", "X.res", "X.lib");
858         @objlist = split " ", $objstr;
859         @objlines = ("");
860         foreach $i (@objlist) {
861             if (length($objlines[$#objlines] . " $i") > 50) {
862                 push @objlines, "";
863             }
864             $objlines[$#objlines] .= " $i";
865         }
866         $subsys = ($type eq "G") ? "windows" : "console";
867         print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
868         for ($i=0; $i<=$#objlines; $i++) {
869             print "\techo$objlines[$i] >> $prog.rsp\n";
870         }
871         print "\n";
872     }
873     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\")) {
874         print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
875           "\n";
876         if ($d->{obj} =~ /\.res$/) {
877             print "\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 ".
878               "-DWINVER=0x0400 -fo".$d->{obj}." ".$d->{deps}->[0]."\n";
879         } else {
880             $deflist = join "", map { " /D$_" } @{$d->{defs}};
881             print "\tcl \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist".
882               " /c ".$d->{deps}->[0]." /Fo$d->{obj}\n";
883         }
884     }
885     print "\n";
886     print $makefile_extra{'vc'} || "";
887     print "\nclean: tidy\n".
888       "\t-del *.exe\n\n".
889       "tidy:\n".
890       "\t-del *.obj\n".
891       "\t-del *.res\n".
892       "\t-del *.pch\n".
893       "\t-del *.aps\n".
894       "\t-del *.ilk\n".
895       "\t-del *.pdb\n".
896       "\t-del *.rsp\n".
897       "\t-del *.dsp\n".
898       "\t-del *.dsw\n".
899       "\t-del *.ncb\n".
900       "\t-del *.opt\n".
901       "\t-del *.plg\n".
902       "\t-del *.map\n".
903       "\t-del *.idb\n".
904       "\t-del debug.log\n";
905     select STDOUT; close OUT;
906 }
907
908 if (defined $makefiles{'wce'}) {
909     $mftyp = 'wce';
910     $dirpfx = &dirpfx($makefiles{'wce'}, "\\");
911
912     ##-- eMbedded Visual C PocketPC makefile
913     open OUT, ">$makefiles{'wce'}"; select OUT;
914     print
915       "# Makefile for $project_name on PocketPC using eMbedded Visual C.\n".
916       "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
917       "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
918     print $help;
919     print
920       "\n".
921       "# If you rename this file to `Makefile', you should change this line,\n".
922       "# so that the .rsp files still depend on the correct makefile.\n".
923       "MAKEFILE = Makefile.wce\n".
924       "\n".
925       "# This makefile expects the environment to have been set up by one\n".
926       "# of the PocketPC batch files wcearmv4.bat and wceemulator.bat. No\n".
927       "# other build targets are currently supported, because they would\n".
928       "# need a section in this if statement.\n".
929       "!if \"\$(TARGETCPU)\" == \"emulator\"\n".
930       "PLATFORM_DEFS=/D \"_i386_\" /D \"i_386_\" /D \"_X86_\" /D \"x86\"\n".
931       "CC=cl\n".
932       "BASELIBS=commctrl.lib coredll.lib corelibc.lib aygshell.lib\n".
933       "MACHINE=IX86\n".
934       "!else\n".
935       "PLATFORM_DEFS=/D \"ARM\" /D \"_ARM_\" /D \"ARMV4\"\n".
936       "CC=clarm\n".
937       "BASELIBS=commctrl.lib coredll.lib aygshell.lib\n".
938       "MACHINE=ARM\n".
939       "!endif\n".
940       "\n".
941       "# C compilation flags\n".
942       "CFLAGS = /nologo /W3 /O1 /MC /D _WIN32_WCE=420 /D \"WIN32_PLATFORM_PSPC=400\" /D UNDER_CE=420 \\\n".
943       "         \$(PLATFORM_DEFS) \\\n".
944       "         /D \"UNICODE\" /D \"_UNICODE\" /D \"NDEBUG\" /D \"NO_HTMLHELP\"\n".
945       "\n".
946       "LFLAGS = /nologo /incremental:no \\\n".
947       "         /base:0x00010000 /stack:0x10000,0x1000 /entry:WinMainCRTStartup \\\n".
948       "         /nodefaultlib:libc.lib /nodefaultlib:libcmt.lib /nodefaultlib:msvcrt.lib /nodefaultlib:OLDNAMES.lib \\\n".
949       "         /subsystem:windowsce,4.20 /align:4096 /MACHINE:\$(MACHINE)\n".
950       "\n".
951       "RCFL = /d UNDER_CE=420 /d _WIN32_WCE=420 /d \"WIN32_PLATFORM_PSPC=400\" \\\n".
952       "       \$(PLATFORM_DEFS) \\\n".
953       "       /d \"NDEBUG\" /d \"UNICODE\" /d \"_UNICODE\"\n".
954       "\n";
955     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G"));
956     print "\n\n";
957     foreach $p (&prognames("G")) {
958         ($prog, $type) = split ",", $p;
959         $objstr = &objects($p, "X.obj", "X.res", undef);
960         print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
961         print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
962     }
963     foreach $p (&prognames("G")) {
964         ($prog, $type) = split ",", $p;
965         print $prog, ".rsp: \$(MAKEFILE)\n";
966         $objstr = &objects($p, "X.obj", "X.res", undef);
967         @objlist = split " ", $objstr;
968         @objlines = ("");
969         foreach $i (@objlist) {
970             if (length($objlines[$#objlines] . " $i") > 50) {
971                 push @objlines, "";
972             }
973             $objlines[$#objlines] .= " $i";
974         }
975         print "\techo \$(BASELIBS) > $prog.rsp\n";
976         for ($i=0; $i<=$#objlines; $i++) {
977             print "\techo$objlines[$i] >> $prog.rsp\n";
978         }
979         print "\n";
980     }
981     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\")) {
982         print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
983           "\n";
984         if ($d->{obj} =~ /\.res$/) {
985             print "\trc \$(FWHACK) \$(RCFL) -r -fo".
986               $d->{obj}." ".$d->{deps}->[0]."\n";
987         } else {
988             $deflist = join "", map { " /D$_" } @{$d->{defs}};
989             print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist".
990               " /c ".$d->{deps}->[0]." /Fo$d->{obj}\n";
991         }
992     }
993     print "\n";
994     print $makefile_extra{'wce'} || "";
995     print "\nclean: tidy\n".
996       "\t-del *.exe\n\n".
997       "tidy:\n".
998       "\t-del *.obj\n".
999       "\t-del *.res\n".
1000       "\t-del *.pch\n".
1001       "\t-del *.aps\n".
1002       "\t-del *.ilk\n".
1003       "\t-del *.pdb\n".
1004       "\t-del *.rsp\n".
1005       "\t-del *.dsp\n".
1006       "\t-del *.dsw\n".
1007       "\t-del *.ncb\n".
1008       "\t-del *.opt\n".
1009       "\t-del *.plg\n".
1010       "\t-del *.map\n".
1011       "\t-del *.idb\n".
1012       "\t-del debug.log\n";
1013     select STDOUT; close OUT;
1014 }
1015
1016 if (defined $makefiles{'vcproj'}) {
1017     $mftyp = 'vcproj';
1018
1019     ##-- MSVC 6 Workspace and projects
1020     #
1021     # Note: All files created in this section are written in binary
1022     # mode, because although MSVC's command-line make can deal with
1023     # LF-only line endings, MSVC project files really _need_ to be
1024     # CRLF. Hence, in order for mkfiles.pl to generate usable project
1025     # files even when run from Unix, I make sure all files are binary
1026     # and explicitly write the CRLFs.
1027     #
1028     # Create directories if necessary
1029     mkdir $makefiles{'vcproj'}
1030         if(! -d $makefiles{'vcproj'});
1031     chdir $makefiles{'vcproj'};
1032     @deps = &deps("X.obj", "X.res", "", "\\");
1033     %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
1034     # Create the project files
1035     # Get names of all Windows projects (GUI and console)
1036     my @prognames = &prognames("G:C");
1037     foreach $progname (@prognames) {
1038         create_project(\%all_object_deps, $progname);
1039     }
1040     # Create the workspace file
1041     open OUT, ">$project_name.dsw"; binmode OUT; select OUT;
1042     print
1043     "Microsoft Developer Studio Workspace File, Format Version 6.00\r\n".
1044     "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n".
1045     "\r\n".
1046     "###############################################################################\r\n".
1047     "\r\n";
1048     # List projects
1049     foreach $progname (@prognames) {
1050       ($windows_project, $type) = split ",", $progname;
1051         print "Project: \"$windows_project\"=\".\\$windows_project\\$windows_project.dsp\" - Package Owner=<4>\r\n";
1052     }
1053     print
1054     "\r\n".
1055     "Package=<5>\r\n".
1056     "{{{\r\n".
1057     "}}}\r\n".
1058     "\r\n".
1059     "Package=<4>\r\n".
1060     "{{{\r\n".
1061     "}}}\r\n".
1062     "\r\n".
1063     "###############################################################################\r\n".
1064     "\r\n".
1065     "Global:\r\n".
1066     "\r\n".
1067     "Package=<5>\r\n".
1068     "{{{\r\n".
1069     "}}}\r\n".
1070     "\r\n".
1071     "Package=<3>\r\n".
1072     "{{{\r\n".
1073     "}}}\r\n".
1074     "\r\n".
1075     "###############################################################################\r\n".
1076     "\r\n";
1077     select STDOUT; close OUT;
1078     chdir $orig_dir;
1079
1080     sub create_project {
1081         my ($all_object_deps, $progname) = @_;
1082         # Construct program's dependency info
1083         %seen_objects = ();
1084         %lib_files = ();
1085         %source_files = ();
1086         %header_files = ();
1087         %resource_files = ();
1088         @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
1089         foreach $object_file (@object_files) {
1090             next if defined $seen_objects{$object_file};
1091             $seen_objects{$object_file} = 1;
1092             if($object_file =~ /\.lib$/io) {
1093                 $lib_files{$object_file} = 1;
1094                 next;
1095             }
1096             $object_deps = $all_object_deps{$object_file};
1097             foreach $object_dep (@$object_deps) {
1098                 if($object_dep =~ /\.c$/io) {
1099                     $source_files{$object_dep} = 1;
1100                     next;
1101                 }
1102                 if($object_dep =~ /\.h$/io) {
1103                     $header_files{$object_dep} = 1;
1104                     next;
1105                 }
1106                 if($object_dep =~ /\.(rc|ico)$/io) {
1107                     $resource_files{$object_dep} = 1;
1108                     next;
1109                 }
1110             }
1111         }
1112         $libs = join " ", sort keys %lib_files;
1113         @source_files = sort keys %source_files;
1114         @header_files = sort keys %header_files;
1115         @resources = sort keys %resource_files;
1116         ($windows_project, $type) = split ",", $progname;
1117         mkdir $windows_project
1118             if(! -d $windows_project);
1119         chdir $windows_project;
1120         $subsys = ($type eq "G") ? "windows" : "console";
1121         open OUT, ">$windows_project.dsp"; binmode OUT; select OUT;
1122         print
1123         "# Microsoft Developer Studio Project File - Name=\"$windows_project\" - Package Owner=<4>\r\n".
1124         "# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n".
1125         "# ** DO NOT EDIT **\r\n".
1126         "\r\n".
1127         "# TARGTYPE \"Win32 (x86) Application\" 0x0101\r\n".
1128         "\r\n".
1129         "CFG=$windows_project - Win32 Debug\r\n".
1130         "!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n".
1131         "!MESSAGE use the Export Makefile command and run\r\n".
1132         "!MESSAGE \r\n".
1133         "!MESSAGE NMAKE /f \"$windows_project.mak\".\r\n".
1134         "!MESSAGE \r\n".
1135         "!MESSAGE You can specify a configuration when running NMAKE\r\n".
1136         "!MESSAGE by defining the macro CFG on the command line. For example:\r\n".
1137         "!MESSAGE \r\n".
1138         "!MESSAGE NMAKE /f \"$windows_project.mak\" CFG=\"$windows_project - Win32 Debug\"\r\n".
1139         "!MESSAGE \r\n".
1140         "!MESSAGE Possible choices for configuration are:\r\n".
1141         "!MESSAGE \r\n".
1142         "!MESSAGE \"$windows_project - Win32 Release\" (based on \"Win32 (x86) Application\")\r\n".
1143         "!MESSAGE \"$windows_project - Win32 Debug\" (based on \"Win32 (x86) Application\")\r\n".
1144         "!MESSAGE \r\n".
1145         "\r\n".
1146         "# Begin Project\r\n".
1147         "# PROP AllowPerConfigDependencies 0\r\n".
1148         "# PROP Scc_ProjName \"\"\r\n".
1149         "# PROP Scc_LocalPath \"\"\r\n".
1150         "CPP=cl.exe\r\n".
1151         "MTL=midl.exe\r\n".
1152         "RSC=rc.exe\r\n".
1153         "\r\n".
1154         "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
1155         "\r\n".
1156         "# PROP BASE Use_MFC 0\r\n".
1157         "# PROP BASE Use_Debug_Libraries 0\r\n".
1158         "# PROP BASE Output_Dir \"Release\"\r\n".
1159         "# PROP BASE Intermediate_Dir \"Release\"\r\n".
1160         "# PROP BASE Target_Dir \"\"\r\n".
1161         "# PROP Use_MFC 0\r\n".
1162         "# PROP Use_Debug_Libraries 0\r\n".
1163         "# PROP Output_Dir \"Release\"\r\n".
1164         "# PROP Intermediate_Dir \"Release\"\r\n".
1165         "# PROP Ignore_Export_Lib 0\r\n".
1166         "# PROP Target_Dir \"\"\r\n".
1167         "# ADD BASE CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
1168         "# ADD CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
1169         "# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
1170         "# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
1171         "# ADD BASE RSC /l 0x809 /d \"NDEBUG\"\r\n".
1172         "# ADD RSC /l 0x809 /d \"NDEBUG\"\r\n".
1173         "BSC32=bscmake.exe\r\n".
1174         "# ADD BASE BSC32 /nologo\r\n".
1175         "# ADD BSC32 /nologo\r\n".
1176         "LINK32=link.exe\r\n".
1177         "# 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".
1178         "# ADD LINK32 $libs /nologo /subsystem:$subsys /machine:I386\r\n".
1179         "# SUBTRACT LINK32 /pdb:none\r\n".
1180         "\r\n".
1181         "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
1182         "\r\n".
1183         "# PROP BASE Use_MFC 0\r\n".
1184         "# PROP BASE Use_Debug_Libraries 1\r\n".
1185         "# PROP BASE Output_Dir \"Debug\"\r\n".
1186         "# PROP BASE Intermediate_Dir \"Debug\"\r\n".
1187         "# PROP BASE Target_Dir \"\"\r\n".
1188         "# PROP Use_MFC 0\r\n".
1189         "# PROP Use_Debug_Libraries 1\r\n".
1190         "# PROP Output_Dir \"Debug\"\r\n".
1191         "# PROP Intermediate_Dir \"Debug\"\r\n".
1192         "# PROP Ignore_Export_Lib 0\r\n".
1193         "# PROP Target_Dir \"\"\r\n".
1194         "# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
1195         "# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
1196         "# ADD BASE MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
1197         "# ADD MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
1198         "# ADD BASE RSC /l 0x809 /d \"_DEBUG\"\r\n".
1199         "# ADD RSC /l 0x809 /d \"_DEBUG\"\r\n".
1200         "BSC32=bscmake.exe\r\n".
1201         "# ADD BASE BSC32 /nologo\r\n".
1202         "# ADD BSC32 /nologo\r\n".
1203         "LINK32=link.exe\r\n".
1204         "# 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".
1205         "# ADD LINK32 $libs /nologo /subsystem:$subsys /debug /machine:I386 /pdbtype:sept\r\n".
1206         "# SUBTRACT LINK32 /pdb:none\r\n".
1207         "\r\n".
1208         "!ENDIF \r\n".
1209         "\r\n".
1210         "# Begin Target\r\n".
1211         "\r\n".
1212         "# Name \"$windows_project - Win32 Release\"\r\n".
1213         "# Name \"$windows_project - Win32 Debug\"\r\n".
1214         "# Begin Group \"Source Files\"\r\n".
1215         "\r\n".
1216         "# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\"\r\n";
1217         foreach $source_file (@source_files) {
1218             print
1219               "# Begin Source File\r\n".
1220               "\r\n".
1221               "SOURCE=..\\..\\$source_file\r\n";
1222             if($source_file =~ /ssh\.c/io) {
1223                 # Disable 'Edit and continue' as Visual Studio can't handle the macros
1224                 print
1225                   "\r\n".
1226                   "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
1227                   "\r\n".
1228                   "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
1229                   "\r\n".
1230                   "# ADD CPP /Zi\r\n".
1231                   "\r\n".
1232                   "!ENDIF \r\n".
1233                   "\r\n";
1234             }
1235             print "# End Source File\r\n";
1236         }
1237         print
1238         "# End Group\r\n".
1239         "# Begin Group \"Header Files\"\r\n".
1240         "\r\n".
1241         "# PROP Default_Filter \"h;hpp;hxx;hm;inl\"\r\n";
1242         foreach $header_file (@header_files) {
1243             print
1244               "# Begin Source File\r\n".
1245               "\r\n".
1246               "SOURCE=..\\..\\$header_file\r\n".
1247               "# End Source File\r\n";
1248         }
1249         print
1250         "# End Group\r\n".
1251         "# Begin Group \"Resource Files\"\r\n".
1252         "\r\n".
1253         "# PROP Default_Filter \"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\"\r\n";
1254         foreach $resource_file (@resources) {
1255             print
1256               "# Begin Source File\r\n".
1257               "\r\n".
1258               "SOURCE=..\\..\\$resource_file\r\n".
1259               "# End Source File\r\n";
1260         }
1261         print
1262         "# End Group\r\n".
1263         "# End Target\r\n".
1264         "# End Project\r\n";
1265         select STDOUT; close OUT;
1266         chdir "..";
1267     }
1268 }
1269
1270 if (defined $makefiles{'gtk'}) {
1271     $mftyp = 'gtk';
1272     $dirpfx = &dirpfx($makefiles{'gtk'}, "/");
1273
1274     ##-- X/GTK/Unix makefile
1275     open OUT, ">$makefiles{'gtk'}"; select OUT;
1276     print
1277     "# Makefile for $project_name under X/GTK and Unix.\n".
1278     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1279     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1280     # gcc command line option is -D not /D
1281     ($_ = $help) =~ s/=\/D/=-D/gs;
1282     print $_;
1283     print
1284     "\n".
1285     "# You can define this path to point at your tools if you need to\n".
1286     "# TOOLPATH = /opt/gcc/bin\n".
1287     "CC := \$(TOOLPATH)\$(CC)\n".
1288     "# You can manually set this to `gtk-config' or `pkg-config gtk+-1.2'\n".
1289     "# (depending on what works on your system) if you want to enforce\n".
1290     "# building with GTK 1.2, or you can set it to `pkg-config gtk+-2.0'\n".
1291     "# if you want to enforce 2.0. The default is to try 2.0 and fall back\n".
1292     "# to 1.2 if it isn't found.\n".
1293     "GTK_CONFIG = sh -c 'pkg-config gtk+-2.0 \$\$0 2>/dev/null || gtk-config \$\$0'\n".
1294     "\n".
1295     &splitline("CFLAGS := -O2 -Wall -Werror -ansi  -g " .
1296                (join " ", map {"-I$dirpfx$_"} @srcdirs) .
1297                " `\$(GTK_CONFIG) --cflags` \$(CFLAGS)")."\n".
1298     "XLIBS = `\$(GTK_CONFIG) --libs` -lm\n".
1299     "ULIBS = -lm#\n".
1300     "INSTALL=install\n",
1301     "INSTALL_PROGRAM=\$(INSTALL)\n",
1302     "INSTALL_DATA=\$(INSTALL)\n",
1303     "prefix=/usr/local\n",
1304     "exec_prefix=\$(prefix)\n",
1305     "bindir=\$(exec_prefix)/bin\n",
1306     "gamesdir=\$(exec_prefix)/games\n",
1307     "mandir=\$(prefix)/man\n",
1308     "man1dir=\$(mandir)/man1\n",
1309     "\n";
1310     print &splitline("all:" . join "", map { " \$(BINPREFIX)$_" }
1311                      &progrealnames("X:U"));
1312     print "\n\n";
1313     foreach $p (&prognames("X:U")) {
1314       ($prog, $type) = split ",", $p;
1315       $objstr = &objects($p, "X.o", undef, undef);
1316       print &splitline("\$(BINPREFIX)" . $prog . ": " . $objstr), "\n";
1317       $libstr = &objects($p, undef, undef, "-lX");
1318       print &splitline("\t\$(CC) -o \$@ $objstr $libstr \$(XLFLAGS) \$(${type}LIBS)", 69),
1319           "\n\n";
1320     }
1321     foreach $d (&deps("X.o", undef, $dirpfx, "/")) {
1322       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1323           "\n";
1324       $deflist = join "", map { " -D$_" } @{$d->{defs}};
1325       print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist" .
1326           " -c \$< -o \$\@\n";
1327     }
1328     print "\n";
1329     print $makefile_extra{'gtk'} || "";
1330     print "\nclean:\n".
1331     "\trm -f *.o". (join "", map { " \$(BINPREFIX)$_" } &progrealnames("X:U")) . "\n";
1332     select STDOUT; close OUT;
1333 }
1334
1335 if (defined $makefiles{'am'}) {
1336     $mftyp = 'am';
1337     die "Makefile.am in a subdirectory is not supported\n"
1338         if &dirpfx($makefiles{'am'}, "/") ne "";
1339
1340     ##-- Unix/autoconf Makefile.am
1341     open OUT, ">$makefiles{'am'}"; select OUT;
1342     print
1343     "# Makefile.am for $project_name under Unix with Autoconf/Automake.\n".
1344     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1345     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n\n";
1346
1347     print $makefile_extra{'am_begin'} || "";
1348
1349     # All programs go in noinstprogs by default. If you want them
1350     # installed anywhere else, you have to also add them to
1351     # bin_PROGRAMS using '!begin am'. (Automake doesn't seem to mind
1352     # having a program name in _both_ of bin_PROGRAMS and
1353     # noinst_PROGRAMS.)
1354     @noinstprogs = ();
1355     foreach $p (&prognames("X:U")) {
1356         ($prog, $type) = split ",", $p;
1357         push @noinstprogs, $prog;
1358     }
1359     print &splitline(join " ", "noinst_PROGRAMS", "=", @noinstprogs), "\n";
1360
1361     %objtosrc = ();
1362     %amspeciallibs = ();
1363     %amlibobjname = ();
1364     %allsources = ();
1365     foreach $d (&deps("X", undef, "", "/", "am")) {
1366         my $obj = $d->{obj};
1367         my $use_archive = 0;
1368         
1369         if (defined $d->{defs}) {
1370             # This file needs to go in an archive, so that we can
1371             # change the preprocess flags to include some -Ds
1372             $use_archive = 1;
1373             $archivecppflags{$obj} = [map { " -D$_" } @{$d->{defs}}];
1374         }
1375         if (defined $cflags{'am'} && $cflags{'am'}->{$obj}) {
1376             # This file needs to go in an archive, so that we can
1377             # change the compile flags as specified in Recipe
1378             $use_archive = 1;
1379             $archivecflags{$obj} = [$cflags{'am'}->{$obj}];
1380         }
1381         if ($use_archive) {
1382             $amspeciallibs{$obj} = "lib${obj}.a";
1383             $amlibobjname{$obj} = "lib${obj}_a-" .
1384                 basename($d->{deps}->[0], ".c", ".m") .
1385                 ".\$(OBJEXT)";
1386         }
1387         $objtosrc{$obj} = $d->{deps};
1388         map { $allsources{$_} = 1 } @{$d->{deps}};
1389     }
1390
1391     # 2014-02-22: as of automake-1.14 we begin to get complained at if
1392     # we don't use this option
1393     print "AUTOMAKE_OPTIONS = subdir-objects\n\n";
1394
1395     # Complete list of source and header files. Not used by the
1396     # auto-generated parts of this makefile, but Recipe might like to
1397     # have it available as a variable so that mandatory-rebuild things
1398     # (version.o) can conveniently be made to depend on it.
1399     print &splitline(join " ", "allsources", "=",
1400                      sort {$a cmp $b} keys %allsources), "\n\n";
1401
1402     @amcppflags = map {"-I\$(srcdir)/$_"} @srcdirs;
1403     print &splitline(join " ", "AM_CPPFLAGS", "=", @amcppflags, "\n");
1404
1405     @amcflags = ("\$(GTK_CFLAGS)", "\$(WARNINGOPTS)");
1406     print &splitline(join " ", "AM_CFLAGS", "=", @amcflags), "\n";
1407
1408     %amlibsused = ();
1409     foreach $p (&prognames("X:U")) {
1410         ($prog, $type) = split ",", $p;
1411         @progsources = ("${prog}_SOURCES", "=");
1412         %sourcefiles = ();
1413         @ldadd = ();
1414         $objstr = &objects($p, "X", undef, undef);
1415         foreach $obj (split / /,$objstr) {
1416             if ($amspeciallibs{$obj}) {
1417                 $amlibsused{$obj} = 1;
1418                 push @ldadd, $amlibobjname{$obj};
1419             } else {
1420                 map { $sourcefiles{$_} = 1 } @{$objtosrc{$obj}};
1421             }
1422         }
1423         push @progsources, sort { $a cmp $b } keys %sourcefiles;
1424         print &splitline(join " ", @progsources), "\n";
1425         if ($type eq "X") {
1426             push @ldadd, "\$(GTK_LIBS)";
1427         }
1428         push @ldadd, "-lm";
1429         print &splitline(join " ", "${prog}_LDADD", "=", @ldadd), "\n";
1430         print "\n";
1431     }
1432
1433     foreach $obj (sort { $a cmp $b } keys %amlibsused) {
1434         print &splitline(join " ", "lib${obj}_a_SOURCES", "=",
1435                          @{$objtosrc{$obj}}), "\n";
1436         print &splitline(join " ", "lib${obj}_a_CPPFLAGS", "=",
1437                          @amcflags, @{$archivecppflags{$obj}}), "\n"
1438                              if $archivecppflags{$obj};
1439         print &splitline(join " ", "lib${obj}_a_CFLAGS", "=",
1440                          @amcflags, @{$archivecflags{$obj}}), "\n"
1441                              if $archivecflags{$obj};
1442     }
1443     print &splitline(join " ", "noinst_LIBRARIES", "=",
1444                      sort { $a cmp $b }
1445                       map { $amspeciallibs{$_} }
1446                      keys %amlibsused),
1447                      "\n\n";
1448
1449     print $makefile_extra{'am'} || "";
1450     select STDOUT; close OUT;
1451 }
1452
1453 if (defined $makefiles{'mpw'}) {
1454     $mftyp = 'mpw';
1455     ##-- MPW Makefile
1456     open OUT, ">$makefiles{'mpw'}"; select OUT;
1457     print
1458     "# Makefile for $project_name under MPW.\n#\n".
1459     "# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1460     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1461     # MPW command line option is -d not /D
1462     ($_ = $help) =~ s/=\/D/=-d /gs;
1463     print $_;
1464     print "\n\n".
1465     "ROptions     = `Echo \"{VER}\" | StreamEdit -e \"1,\$ replace /=(\xc5)\xa81\xb0/ 'STR=\xb6\xb6\xb6\xb6\xb6\"' \xa81 '\xb6\xb6\xb6\xb6\xb6\"'\"`".
1466     "\n".
1467     "C_68K = {C}\n".
1468     "C_CFM68K = {C}\n".
1469     "C_PPC = {PPCC}\n".
1470     "C_Carbon = {PPCC}\n".
1471     "\n".
1472     "# -w 35 disables \"unused parameter\" warnings\n".
1473     "COptions     = -i : -i :: -i ::charset -w 35 -w err -proto strict -ansi on \xb6\n".
1474     "          -notOnce\n".
1475     "COptions_68K = {COptions} -model far -opt time\n".
1476     "# Enabling \"-opt space\" for CFM-68K gives me undefined references to\n".
1477     "# _\$LDIVT and _\$LMODT.\n".
1478     "COptions_CFM68K = {COptions} -model cfmSeg -opt time\n".
1479     "COptions_PPC = {COptions} -opt size -traceback\n".
1480     "COptions_Carbon = {COptions} -opt size -traceback -d TARGET_API_MAC_CARBON\n".
1481     "\n".
1482     "Link_68K = ILink\n".
1483     "Link_CFM68K = ILink\n".
1484     "Link_PPC = PPCLink\n".
1485     "Link_Carbon = PPCLink\n".
1486     "\n".
1487     "LinkOptions = -c 'pTTY'\n".
1488     "LinkOptions_68K = {LinkOptions} -br 68k -model far -compact\n".
1489     "LinkOptions_CFM68K = {LinkOptions} -br 020 -model cfmseg -compact\n".
1490     "LinkOptions_PPC = {LinkOptions}\n".
1491     "LinkOptions_Carbon = -m __appstart -w {LinkOptions}\n".
1492     "\n".
1493     "Libs_68K = \"{CLibraries}StdCLib.far.o\" \xb6\n".
1494     "           \"{Libraries}MacRuntime.o\" \xb6\n".
1495     "           \"{Libraries}MathLib.far.o\" \xb6\n".
1496     "           \"{Libraries}IntEnv.far.o\" \xb6\n".
1497     "           \"{Libraries}Interface.o\" \xb6\n".
1498     "           \"{Libraries}Navigation.far.o\" \xb6\n".
1499     "           \"{Libraries}OpenTransport.o\" \xb6\n".
1500     "           \"{Libraries}OpenTransportApp.o\" \xb6\n".
1501     "           \"{Libraries}OpenTptInet.o\" \xb6\n".
1502     "           \"{Libraries}UnicodeConverterLib.far.o\"\n".
1503     "\n".
1504     "Libs_CFM = \"{SharedLibraries}InterfaceLib\" \xb6\n".
1505     "           \"{SharedLibraries}StdCLib\" \xb6\n".
1506     "           \"{SharedLibraries}AppearanceLib\" \xb6\n".
1507     "                   -weaklib AppearanceLib \xb6\n".
1508     "           \"{SharedLibraries}NavigationLib\" \xb6\n".
1509     "                   -weaklib NavigationLib \xb6\n".
1510     "           \"{SharedLibraries}TextCommon\" \xb6\n".
1511     "                   -weaklib TextCommon \xb6\n".
1512     "           \"{SharedLibraries}UnicodeConverter\" \xb6\n".
1513     "                   -weaklib UnicodeConverter\n".
1514     "\n".
1515     "Libs_CFM68K =      {Libs_CFM} \xb6\n".
1516     "           \"{CFM68KLibraries}NuMacRuntime.o\"\n".
1517     "\n".
1518     "Libs_PPC = {Libs_CFM} \xb6\n".
1519     "           \"{SharedLibraries}ControlsLib\" \xb6\n".
1520     "                   -weaklib ControlsLib \xb6\n".
1521     "           \"{SharedLibraries}WindowsLib\" \xb6\n".
1522     "                   -weaklib WindowsLib \xb6\n".
1523     "           \"{SharedLibraries}OpenTransportLib\" \xb6\n".
1524     "                   -weaklib OTClientLib \xb6\n".
1525     "                   -weaklib OTClientUtilLib \xb6\n".
1526     "           \"{SharedLibraries}OpenTptInternetLib\" \xb6\n".
1527     "                   -weaklib OTInetClientLib \xb6\n".
1528     "           \"{PPCLibraries}StdCRuntime.o\" \xb6\n".
1529     "           \"{PPCLibraries}PPCCRuntime.o\" \xb6\n".
1530     "           \"{PPCLibraries}CarbonAccessors.o\" \xb6\n".
1531     "           \"{PPCLibraries}OpenTransportAppPPC.o\" \xb6\n".
1532     "           \"{PPCLibraries}OpenTptInetPPC.o\"\n".
1533     "\n".
1534     "Libs_Carbon =      \"{PPCLibraries}CarbonStdCLib.o\" \xb6\n".
1535     "           \"{PPCLibraries}StdCRuntime.o\" \xb6\n".
1536     "           \"{PPCLibraries}PPCCRuntime.o\" \xb6\n".
1537     "           \"{SharedLibraries}CarbonLib\" \xb6\n".
1538     "           \"{SharedLibraries}StdCLib\"\n".
1539     "\n";
1540     print &splitline("all \xc4 " . join(" ", &progrealnames("M")), undef, "\xb6");
1541     print "\n\n";
1542     foreach $p (&prognames("M")) {
1543       ($prog, $type) = split ",", $p;
1544
1545       print &splitline("$prog \xc4 $prog.68k $prog.ppc $prog.carbon",
1546                    undef, "\xb6"), "\n\n";
1547
1548       $rsrc = &objects($p, "", "X.rsrc", undef);
1549
1550       foreach $arch (qw(68K CFM68K PPC Carbon)) {
1551           $objstr = &objects($p, "X.\L$arch\E.o", "", undef);
1552           print &splitline("$prog.\L$arch\E \xc4 $objstr $rsrc", undef, "\xb6");
1553           print "\n";
1554           print &splitline("\tDuplicate -y $rsrc {Targ}", 69, "\xb6"), "\n";
1555           print &splitline("\t{Link_$arch} -o {Targ} -fragname $prog " .
1556                        "{LinkOptions_$arch} " .
1557                        $objstr . " {Libs_$arch}", 69, "\xb6"), "\n";
1558           print &splitline("\tSetFile -a BMi {Targ}", 69, "\xb6"), "\n\n";
1559       }
1560
1561     }
1562     foreach $d (&deps("", "X.rsrc", "::", ":")) {
1563       next unless $d->{obj};
1564       print &splitline(sprintf("%s \xc4 %s", $d->{obj}, join " ", @{$d->{deps}}),
1565                    undef, "\xb6"), "\n";
1566       print "\tRez ", $d->{deps}->[0], " -o {Targ} {ROptions}\n\n";
1567     }
1568     foreach $arch (qw(68K CFM68K)) {
1569         foreach $d (&deps("X.\L$arch\E.o", "", "::", ":")) {
1570          next unless $d->{obj};
1571         print &splitline(sprintf("%s \xc4 %s", $d->{obj},
1572                                  join " ", @{$d->{deps}}),
1573                          undef, "\xb6"), "\n";
1574          print "\t{C_$arch} ", $d->{deps}->[0],
1575                " -o {Targ} {COptions_$arch}\n\n";
1576          }
1577     }
1578     foreach $arch (qw(PPC Carbon)) {
1579         foreach $d (&deps("X.\L$arch\E.o", "", "::", ":")) {
1580          next unless $d->{obj};
1581         print &splitline(sprintf("%s \xc4 %s", $d->{obj},
1582                                  join " ", @{$d->{deps}}),
1583                          undef, "\xb6"), "\n";
1584          # The odd stuff here seems to stop afpd getting confused.
1585          print "\techo -n > {Targ}\n";
1586          print "\tsetfile -t XCOF {Targ}\n";
1587          print "\t{C_$arch} ", $d->{deps}->[0],
1588                " -o {Targ} {COptions_$arch}\n\n";
1589          }
1590     }
1591     select STDOUT; close OUT;
1592 }
1593
1594 if (defined $makefiles{'lcc'}) {
1595     $mftyp = 'lcc';
1596     $dirpfx = &dirpfx($makefiles{'lcc'}, "\\");
1597
1598     ##-- lcc makefile
1599     open OUT, ">$makefiles{'lcc'}"; select OUT;
1600     print
1601     "# Makefile for $project_name under lcc.\n".
1602     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1603     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1604     # lcc command line option is -D not /D
1605     ($_ = $help) =~ s/=\/D/=-D/gs;
1606     print $_;
1607     print
1608     "\n".
1609     "# If you rename this file to `Makefile', you should change this line,\n".
1610     "# so that the .rsp files still depend on the correct makefile.\n".
1611     "MAKEFILE = Makefile.lcc\n".
1612     "\n".
1613     "# C compilation flags\n".
1614     "CFLAGS = -D_WINDOWS " .
1615       (join " ", map {"-I$dirpfx$_"} @srcdirs) .
1616       "\n".
1617     "\n".
1618     "# Get include directory for resource compiler\n".
1619     "\n";
1620     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
1621     print "\n\n";
1622     foreach $p (&prognames("G:C")) {
1623       ($prog, $type) = split ",", $p;
1624       $objstr = &objects($p, "X.obj", "X.res", undef);
1625       print &splitline("$prog.exe: " . $objstr ), "\n";
1626       $subsystemtype = undef;
1627       if ($type eq "G") { $subsystemtype = "-subsystem  windows"; }
1628       my $libss = "shell32.lib wsock32.lib ws2_32.lib winspool.lib winmm.lib imm32.lib";
1629       print &splitline("\tlcclnk $subsystemtype -o $prog.exe $objstr $libss");
1630       print "\n\n";
1631     }
1632
1633     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\")) {
1634       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1635         "\n";
1636       if ($d->{obj} =~ /\.res$/) {
1637         print &splitline("\tlrc \$(FWHACK) \$(RCFL) -r \$*.rc",69)."\n";
1638       } else {
1639         $deflist = join "", map { " -D$_" } @{$d->{defs}};
1640         print &splitline("\tlcc -O -p6 \$(COMPAT) \$(FWHACK) \$(CFLAGS)".
1641                          " \$(XFLAGS)$deflist ".$d->{deps}->[0]." -o \$\@",69)."\n";
1642       }
1643     }
1644     print "\n";
1645     print $makefile_extra{'lcc'} || "";
1646     print "\nclean:\n".
1647     "\t-del *.obj\n".
1648     "\t-del *.exe\n".
1649     "\t-del *.res\n";
1650
1651     select STDOUT; close OUT;
1652 }
1653
1654 if (defined $makefiles{'nestedvm'}) {
1655     $mftyp = 'nestedvm';
1656     $dirpfx = &dirpfx($makefiles{'nestedvm'}, "/");
1657
1658     ##-- NestedVM makefile
1659     open OUT, ">$makefiles{'nestedvm'}"; select OUT;
1660     print
1661     "# Makefile for $project_name under NestedVM.\n".
1662     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1663     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1664     # gcc command line option is -D not /D
1665     ($_ = $help) =~ s/=\/D/=-D/gs;
1666     print $_;
1667     print
1668     "\n".
1669     "# This path points at the nestedvm root directory\n".
1670     "NESTEDVM = /opt/nestedvm\n".
1671     "# You can define this path to point at your tools if you need to\n".
1672     "TOOLPATH = \$(NESTEDVM)/upstream/install/bin\n".
1673     "CC = \$(TOOLPATH)/mips-unknown-elf-gcc\n".
1674     "\n".
1675     &splitline("CFLAGS = -O2 -Wall -Werror -DSLOW_SYSTEM -g " .
1676                (join " ", map {"-I$dirpfx$_"} @srcdirs))."\n".
1677     "\n";
1678     print &splitline("all:" . join "", map { " $_.jar" } &progrealnames("X"));
1679     print "\n\n";
1680     foreach $p (&prognames("X")) {
1681       ($prog, $type) = split ",", $p;
1682       $objstr = &objects($p, "X.o", undef, undef);
1683       $objstr =~ s/gtk\.o/nestedvm\.o/g;
1684       print &splitline($prog . ".mips: " . $objstr), "\n";
1685       $libstr = &objects($p, undef, undef, "-lX");
1686       print &splitline("\t\$(CC) \$(${type}LDFLAGS) -o \$@ " .
1687                        $objstr . " $libstr -lm", 69), "\n\n";
1688     }
1689     foreach $d (&deps("X.o", undef, $dirpfx, "/")) {
1690       $oobjs = $d->{obj};
1691       $ddeps= join " ", @{$d->{deps}};
1692       $oobjs =~ s/gtk/nestedvm/g;
1693       $ddeps =~ s/gtk/nestedvm/g;
1694       print &splitline(sprintf("%s: %s", $oobjs, $ddeps)),
1695           "\n";
1696       $deflist = join "", map { " -D$_" } @{$d->{defs}};
1697       print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist" .
1698           " -c \$< -o \$\@\n";
1699     }
1700     print "\n";
1701     print $makefile_extra{'nestedvm'} || "";
1702     print "\nclean:\n".
1703     "\trm -rf *.o *.mips *.class *.html *.jar org applet.manifest\n";
1704     select STDOUT; close OUT;
1705 }
1706
1707 if (defined $makefiles{'osx'}) {
1708     $mftyp = 'osx';
1709     $dirpfx = &dirpfx($makefiles{'osx'}, "/");
1710     @osxarchs = ('i386');
1711
1712     ##-- Mac OS X makefile
1713     open OUT, ">$makefiles{'osx'}"; select OUT;
1714     print
1715     "# Makefile for $project_name under Mac OS X.\n".
1716     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1717     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1718     # gcc command line option is -D not /D
1719     ($_ = $help) =~ s/=\/D/=-D/gs;
1720     print $_;
1721     print
1722     "CC = \$(TOOLPATH)gcc\n".
1723     "LIPO = \$(TOOLPATH)lipo\n".
1724     "\n".
1725     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1726                (join " ", map {"-I$dirpfx$_"} @srcdirs))."\n".
1727     "LDFLAGS = -framework Cocoa\n".
1728     &splitline("all:" . join "", map { " $_" } &progrealnames("MX:U")) .
1729     "\n";
1730     print $makefile_extra{'osx'} || "";
1731     print "\n".
1732     ".SUFFIXES: .o .c .m\n".
1733     "\n";
1734     print "\n\n";
1735     foreach $p (&prognames("MX")) {
1736       ($prog, $type) = split ",", $p;
1737       $icon = &special($p, ".icns");
1738       $infoplist = &special($p, "info.plist");
1739       print "${prog}.app:\n\tmkdir -p \$\@\n";
1740       print "${prog}.app/Contents: ${prog}.app\n\tmkdir -p \$\@\n";
1741       print "${prog}.app/Contents/MacOS: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1742       $targets = "${prog}.app/Contents/MacOS/$prog";
1743       if (defined $icon) {
1744         print "${prog}.app/Contents/Resources: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1745         print "${prog}.app/Contents/Resources/${prog}.icns: ${prog}.app/Contents/Resources $icon\n\tcp $icon \$\@\n";
1746         $targets .= " ${prog}.app/Contents/Resources/${prog}.icns";
1747       }
1748       if (defined $infoplist) {
1749         print "${prog}.app/Contents/Info.plist: ${prog}.app/Contents/Resources $infoplist\n\tcp $infoplist \$\@\n";
1750         $targets .= " ${prog}.app/Contents/Info.plist";
1751       }
1752       $targets .= " \$(${prog}_extra)";
1753       print &splitline("${prog}: $targets", 69) . "\n\n";
1754       $libstr = &objects($p, undef, undef, "-lX");
1755       $archbins = "";
1756       foreach $arch (@osxarchs) {
1757         $objstr = &objects($p, "X.${arch}.o", undef, undef);
1758         print &splitline("${prog}.${arch}.bin: " . $objstr), "\n";
1759         print &splitline("\t\$(CC) -arch ${arch} -mmacosx-version-min=10.4 \$(LDFLAGS) -o \$@ " .
1760                        $objstr . " $libstr", 69), "\n\n";
1761         $archbins .= " ${prog}.${arch}.bin";
1762       }
1763       print &splitline("${prog}.app/Contents/MacOS/$prog: ".
1764                        "${prog}.app/Contents/MacOS" . $archbins), "\n";
1765       print &splitline("\t\$(LIPO) -create $archbins -output \$@", 69), "\n\n";
1766     }
1767     foreach $p (&prognames("U")) {
1768       ($prog, $type) = split ",", $p;
1769       $libstr = &objects($p, undef, undef, "-lX");
1770       $archbins = "";
1771       foreach $arch (@osxarchs) {
1772         $objstr = &objects($p, "X.${arch}.o", undef, undef);
1773         print &splitline("${prog}.${arch}: " . $objstr), "\n";
1774         print &splitline("\t\$(CC) -arch ${arch} -mmacosx-version-min=10.4 \$(ULDFLAGS) -o \$@ " .
1775                        $objstr . " $libstr", 69), "\n\n";
1776         $archbins .= " ${prog}.${arch}";
1777       }
1778       print &splitline("${prog}:" . $archbins), "\n";
1779       print &splitline("\t\$(LIPO) -create $archbins -output \$@", 69), "\n\n";
1780     }
1781     foreach $arch (@osxarchs) {
1782       foreach $d (&deps("X.${arch}.o", undef, $dirpfx, "/")) {
1783         print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1784             "\n";
1785         $deflist = join "", map { " -D$_" } @{$d->{defs}};
1786         if ($d->{deps}->[0] =~ /\.m$/) {
1787           print "\t\$(CC) -arch $arch -mmacosx-version-min=10.4 -x objective-c \$(COMPAT) \$(FWHACK) \$(CFLAGS)".
1788               " \$(XFLAGS)$deflist -c \$< -o \$\@\n";
1789         } else {
1790           print "\t\$(CC) -arch $arch -mmacosx-version-min=10.4 \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist" .
1791               " -c \$< -o \$\@\n";
1792         }
1793       }
1794     }
1795     print "\nclean:\n".
1796     "\trm -f *.o *.dmg". (join "", map { my $a=$_; (" $a", map { " ${a}.$_" } @osxarchs) } &progrealnames("U")) . "\n".
1797     "\trm -rf *.app\n";
1798     select STDOUT; close OUT;
1799 }
1800
1801 if (defined $makefiles{'gnustep'}) {
1802     $mftyp = 'gnustep';
1803     $dirpfx = &dirpfx($makefiles{'gnustep'}, "/");
1804
1805     ##-- GNUstep makefile (use with 'gs_make -f Makefile.gnustep')
1806
1807     # This is a pretty evil way to do things. In an ideal world, I'd
1808     # use the approved GNUstep makefile mechanism which just defines a
1809     # variable or two saying what source files go into what binary and
1810     # then includes application.make. Unfortunately, that has the
1811     # automake-ish limitation that it doesn't let you choose different
1812     # command lines for each object, so I can't arrange for all those
1813     # files with -DTHIS and -DTHAT to Just Work.
1814     #
1815     # A simple if ugly fix would be to have mkfiles.pl construct a
1816     # directory full of stub C files of the form '#define thing',
1817     # '#include "real_source_file"', and then reference those in this
1818     # makefile. That would also make it easy to build a proper
1819     # automake makefile.
1820     open OUT, ">$makefiles{'gnustep'}"; select OUT;
1821     print
1822     "# Makefile for $project_name under GNUstep.\n".
1823     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1824     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1825     # gcc command line option is -D not /D
1826     ($_ = $help) =~ s/=\/D/=-D/gs;
1827     print $_;
1828     print
1829     "NEEDS_GUI=yes\n".
1830     "include \$(GNUSTEP_MAKEFILES)/common.make\n".
1831     "include \$(GNUSTEP_MAKEFILES)/rules.make\n".
1832     "include \$(GNUSTEP_MAKEFILES)/Instance/rules.make\n".
1833     "\n".
1834     &splitline("all::" . join "", map { " $_" } &progrealnames("MX:U")) .
1835     "\n";
1836     print $makefile_extra{'gnustep'} || "";
1837     print "\n".
1838     ".SUFFIXES: .o .c .m\n".
1839     "\n";
1840     print "\n\n";
1841     foreach $p (&prognames("MX")) {
1842       ($prog, $type) = split ",", $p;
1843       $icon = &special($p, ".icns");
1844       $infoplist = &special($p, "info.plist");
1845       print "${prog}.app:\n\tmkdir -p \$\@\n";
1846       $targets = "${prog}.app ${prog}.app/$prog";
1847       if (defined $icon) {
1848         print "${prog}.app/Resources: ${prog}.app\n\tmkdir -p \$\@\n";
1849         print "${prog}.app/Resources/${prog}.icns: ${prog}.app/Resources $icon\n\tcp $icon \$\@\n";
1850         $targets .= " ${prog}.app/Resources/${prog}.icns";
1851       }
1852       if (defined $infoplist) {
1853         print "${prog}.app/Info.plist: ${prog}.app $infoplist\n\tcp $infoplist \$\@\n";
1854         $targets .= " ${prog}.app/Info.plist";
1855       }
1856       $targets .= " \$(${prog}_extra)";
1857       print &splitline("${prog}: $targets", 69) . "\n\n";
1858       $libstr = &objects($p, undef, undef, "-lX");
1859       $objstr = &objects($p, "X.o", undef, undef);
1860       print &splitline("${prog}.app/$prog: " . $objstr), "\n";
1861       print &splitline("\t\$(CC) \$(ALL_LDFLAGS) -o \$@ " . $objstr . " \$(ALL_LIB_DIRS) $libstr \$(ALL_LIBS)", 69), "\n\n";
1862     }
1863     foreach $p (&prognames("U")) {
1864       ($prog, $type) = split ",", $p;
1865       $libstr = &objects($p, undef, undef, "-lX");
1866       $objstr = &objects($p, "X.o", undef, undef);
1867       print &splitline("${prog}: " . $objstr), "\n";
1868       print &splitline("\t\$(CC) \$(ULDFLAGS) -o \$@ " . $objstr . " $libstr", 69), "\n\n";
1869     }
1870     foreach $d (&deps("X.o", undef, $dirpfx, "/")) {
1871       print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1872       "\n";
1873       $deflist = join "", map { " -D$_" } @{$d->{defs}};
1874       if ($d->{deps}->[0] =~ /\.m$/) {
1875         print "\t\$(CC) -DGNUSTEP \$(ALL_OBJCFLAGS) \$(COMPAT) \$(FWHACK) \$(OBJCFLAGS)".
1876                 " \$(XFLAGS)$deflist -c \$< -o \$\@\n";
1877       } else {
1878         print "\t\$(CC) \$(ALL_CFLAGS) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS)$deflist" .
1879               " -c \$< -o \$\@\n";
1880       }
1881     }
1882     print "\nclean::\n".
1883     "\trm -f *.o ". (join " ", &progrealnames("U")) . "\n".
1884     "\trm -rf *.app\n";
1885     select STDOUT; close OUT;
1886 }
1887
1888 if (defined $makefiles{'emcc'}) {
1889     $mftyp = 'emcc';
1890     $dirpfx = &dirpfx($makefiles{'emcc'}, "/");
1891
1892     ##-- Makefile for building Javascript puzzles via Emscripten
1893
1894     open OUT, ">$makefiles{'emcc'}"; select OUT;
1895     print
1896     "# Makefile for $project_name using Emscripten. Requires GNU make.\n".
1897     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1898     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1899     # emcc command line option is -D not /D
1900     ($_ = $help) =~ s/=\/D/=-D/gs;
1901     print $_;
1902     print
1903     "\n".
1904     "# This can be set on the command line to point at the emcc command,\n".
1905     "# if it is not on your PATH.\n".
1906     "EMCC = emcc\n".
1907     "\n".
1908     &splitline("CFLAGS = -DSLOW_SYSTEM " .
1909                (join " ", map {"-I$dirpfx$_"} @srcdirs))."\n".
1910     "\n";
1911     $output_js_files = join "", map { " \$(OUTPREFIX)$_.js" } &progrealnames("X");
1912     print &splitline("all:" . $output_js_files);
1913     print "\n\n";
1914     foreach $p (&prognames("X")) {
1915       ($prog, $type) = split ",", $p;
1916       $objstr = &objects($p, "X.o", undef, undef);
1917       $objstr =~ s/gtk\.o/emcc\.o/g;
1918       print &splitline("\$(OUTPREFIX)" . $prog . ".js: " . $objstr . " emccpre.js emcclib.js emccx.json"), "\n";
1919       print "\t\$(EMCC) -o \$(OUTPREFIX)".$prog.".js ".
1920           "-O2 ".
1921           "-s ASM_JS=1 ".
1922           "--pre-js emccpre.js ".
1923           "--js-library emcclib.js ".
1924           "-s EXPORTED_FUNCTIONS=\"`sed 's://.*::' emccx.json | tr -d ' \\n'`\" " . $objstr . "\n\n";
1925     }
1926     foreach $d (&deps("X.o", undef, $dirpfx, "/")) {
1927       $oobjs = $d->{obj};
1928       $ddeps= join " ", @{$d->{deps}};
1929       $oobjs =~ s/gtk/emcc/g;
1930       $ddeps =~ s/gtk/emcc/g;
1931       print &splitline(sprintf("%s: %s", $oobjs, $ddeps)),
1932           "\n";
1933       $deflist = join "", map { " -D$_" } @{$d->{defs}};
1934       print "\t\$(EMCC) \$(CFLAGS) \$(XFLAGS)$deflist" .
1935           " -c \$< -o \$\@\n";
1936     }
1937     print "\n";
1938     print $makefile_extra{'emcc'} || "";
1939     print "\nclean:\n".
1940     "\trm -rf *.o $output_js_files\n";
1941     select STDOUT; close OUT;
1942 }
1943
1944 # All done, so do the Unix postprocessing if asked to.
1945
1946 if ($do_unix) {
1947     chdir $orig_dir;
1948     system "./mkauto.sh";
1949     die "mkfiles.pl: mkauto.sh returned $?\n" if $? > 0;
1950     system "./configure", @confargs;
1951     die "mkfiles.pl: configure returned $?\n" if $? > 0;
1952 }