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