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