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