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