]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mkfiles.pl
Oh yes, and the Unix Makefile needs to know that as well.
[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                    "-Wl,-Map,$prog.map " .
340                    $objstr . " $libstr", 69), "\n\n";
341 }
342 foreach $d (&deps("X.o", "X.res.o", "", "/")) {
343   print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
344     "\n";
345 }
346 print
347 "\n".
348 "version.o: FORCE;\n".
349 "# Hack to force version.o to be rebuilt always\n".
350 "FORCE:\n".
351 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
352 "clean:\n".
353 "\trm -f *.o *.exe *.res.o *.map\n".
354 "\n";
355 select STDOUT; close OUT;
356
357 ##-- Borland makefile
358 %stdlibs = (  # Borland provides many Win32 API libraries intrinsically
359   "advapi32" => 1,
360   "comctl32" => 1,
361   "comdlg32" => 1,
362   "gdi32" => 1,
363   "imm32" => 1,
364   "shell32" => 1,
365   "user32" => 1,
366   "winmm" => 1,
367   "winspool" => 1,
368   "wsock32" => 1,
369 );          
370 open OUT, ">Makefile.bor"; select OUT;
371 print
372 "# Makefile for PuTTY under Borland C.\n".
373 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
374 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
375 # bcc32 command line option is -D not /D
376 ($_ = $help) =~ s/=\/D/=-D/gs;
377 print $_;
378 print
379 "\n".
380 "# If you rename this file to `Makefile', you should change this line,\n".
381 "# so that the .rsp files still depend on the correct makefile.\n".
382 "MAKEFILE = Makefile.bor\n".
383 "\n".
384 "# C compilation flags\n".
385 "CFLAGS = -D_WINDOWS -DWINVER=0x0401\n".
386 "\n".
387 "# Get include directory for resource compiler\n".
388 "!if !\$d(BCB)\n".
389 "BCB = \$(MAKEDIR)\\..\n".
390 "!endif\n".
391 "\n".
392 ".c.obj:\n".
393 &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
394   " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
395 ".rc.res:\n".
396 &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
397   " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
398 "\n";
399 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
400 print "\n\n";
401 foreach $p (&prognames("GC")) {
402   ($prog, $type) = split ",", $p;
403   $objstr = &objects($p, "X.obj", "X.res", undef);
404   print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
405   my $ap = ($type eq "G") ? "-aa" : "-ap";
406   print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
407 }
408 foreach $p (&prognames("GC")) {
409   ($prog, $type) = split ",", $p;
410   print $prog, ".rsp: \$(MAKEFILE)\n";
411   $objstr = &objects($p, "X.obj", undef, undef);
412   @objlist = split " ", $objstr;
413   @objlines = ("");
414   foreach $i (@objlist) {
415     if (length($objlines[$#objlines] . " $i") > 50) {
416       push @objlines, "";
417     }
418     $objlines[$#objlines] .= " $i";
419   }
420   $c0w = ($type eq "G") ? "c0w32" : "c0x32";
421   print "\techo $c0w + > $prog.rsp\n";
422   for ($i=0; $i<=$#objlines; $i++) {
423     $plus = ($i < $#objlines ? " +" : "");
424     print "\techo$objlines[$i]$plus >> $prog.rsp\n";
425   }
426   print "\techo $prog.exe >> $prog.rsp\n";
427   $objstr = &objects($p, "X.obj", "X.res", undef);
428   @libs = split " ", &objects($p, undef, undef, "X");
429   @libs = grep { !$stdlibs{$_} } @libs;
430   unshift @libs, "cw32", "import32";
431   $libstr = join ' ', @libs;
432   print "\techo nul,$libstr, >> $prog.rsp\n";
433   print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
434   print "\n";
435 }
436 foreach $d (&deps("X.obj", "X.res", "", "\\")) {
437   print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
438     "\n";
439 }
440 print
441 "\n".
442 "version.o: FORCE\n".
443 "# Hack to force version.o to be rebuilt always\n".
444 "FORCE:\n".
445 "\tbcc32 \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
446 "clean:\n".
447 "\t-del *.obj\n".
448 "\t-del *.exe\n".
449 "\t-del *.res\n".
450 "\t-del *.pch\n".
451 "\t-del *.aps\n".
452 "\t-del *.il*\n".
453 "\t-del *.pdb\n".
454 "\t-del *.rsp\n".
455 "\t-del *.tds\n".
456 "\t-del *.\$\$\$\$\$\$\n";
457 select STDOUT; close OUT;
458
459 ##-- Visual C++ makefile
460 open OUT, ">Makefile.vc"; select OUT;
461 print
462 "# Makefile for PuTTY under Visual C.\n".
463 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
464 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
465 print $help;
466 print
467 "\n".
468 "# If you rename this file to `Makefile', you should change this line,\n".
469 "# so that the .rsp files still depend on the correct makefile.\n".
470 "MAKEFILE = Makefile.vc\n".
471 "\n".
472 "# C compilation flags\n".
473 "CFLAGS = /nologo /W3 /O1 /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401\n".
474 "LFLAGS = /incremental:no /fixed\n".
475 "\n".
476 ".c.obj:\n".
477 "\tcl \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) /c \$*.c\n".
478 ".rc.res:\n".
479 "\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 -DWINVER=0x0400 \$*.rc\n".
480 "\n";
481 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
482 print "\n\n";
483 foreach $p (&prognames("GC")) {
484   ($prog, $type) = split ",", $p;
485   $objstr = &objects($p, "X.obj", "X.res", undef);
486   print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
487   print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
488 }
489 foreach $p (&prognames("GC")) {
490   ($prog, $type) = split ",", $p;
491   print $prog, ".rsp: \$(MAKEFILE)\n";
492   $objstr = &objects($p, "X.obj", "X.res", "X.lib");
493   @objlist = split " ", $objstr;
494   @objlines = ("");
495   foreach $i (@objlist) {
496     if (length($objlines[$#objlines] . " $i") > 50) {
497       push @objlines, "";
498     }
499     $objlines[$#objlines] .= " $i";
500   }
501   $subsys = ($type eq "G") ? "windows" : "console";
502   print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
503   for ($i=0; $i<=$#objlines; $i++) {
504     print "\techo$objlines[$i] >> $prog.rsp\n";
505   }
506   print "\n";
507 }
508 foreach $d (&deps("X.obj", "X.res", "", "\\")) {
509   print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
510       "\n";
511 }
512 print
513 "\n".
514 "# Hack to force version.o to be rebuilt always\n".
515 "version.obj: *.c *.h *.rc\n".
516 "\tcl \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
517 "clean: tidy\n".
518 "\t-del *.exe\n\n".
519 "tidy:\n".
520 "\t-del *.obj\n".
521 "\t-del *.res\n".
522 "\t-del *.pch\n".
523 "\t-del *.aps\n".
524 "\t-del *.ilk\n".
525 "\t-del *.pdb\n".
526 "\t-del *.rsp\n".
527 "\t-del *.dsp\n".
528 "\t-del *.dsw\n".
529 "\t-del *.ncb\n".
530 "\t-del *.opt\n".
531 "\t-del *.plg\n".
532 "\t-del *.map\n".
533 "\t-del *.idb\n".
534 "\t-del debug.log\n";
535 select STDOUT; close OUT;
536
537 ##-- MSVC 6 Workspace and projects
538 #
539 # Note: All files created in this section are written in binary
540 # mode, because although MSVC's command-line make can deal with
541 # LF-only line endings, MSVC project files really _need_ to be
542 # CRLF. Hence, in order for mkfiles.pl to generate usable project
543 # files even when run from Unix, I make sure all files are binary
544 # and explicitly write the CRLFs.
545 #
546 # Create directories if necessary
547 mkdir 'MSVC'
548         if(! -d 'MSVC');
549 chdir 'MSVC';
550 @deps = &deps("X.obj", "X.res", "", "\\");
551 %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
552 # Create the project files
553 # Get names of all Windows projects (GUI and console)
554 my @prognames = &prognames("GC");
555 foreach $progname (@prognames) {
556         create_project(\%all_object_deps, $progname);
557 }
558 # Create the workspace file
559 open OUT, ">putty.dsw"; binmode OUT; select OUT;
560 print
561 "Microsoft Developer Studio Workspace File, Format Version 6.00\r\n".
562 "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n".
563 "\r\n".
564 "###############################################################################\r\n".
565 "\r\n";
566 # List projects
567 foreach $progname (@prognames) {
568   ($windows_project, $type) = split ",", $progname;
569         print "Project: \"$windows_project\"=\".\\$windows_project\\$windows_project.dsp\" - Package Owner=<4>\r\n";
570 }
571 print
572 "\r\n".
573 "Package=<5>\r\n".
574 "{{{\r\n".
575 "}}}\r\n".
576 "\r\n".
577 "Package=<4>\r\n".
578 "{{{\r\n".
579 "}}}\r\n".
580 "\r\n".
581 "###############################################################################\r\n".
582 "\r\n".
583 "Global:\r\n".
584 "\r\n".
585 "Package=<5>\r\n".
586 "{{{\r\n".
587 "}}}\r\n".
588 "\r\n".
589 "Package=<3>\r\n".
590 "{{{\r\n".
591 "}}}\r\n".
592 "\r\n".
593 "###############################################################################\r\n".
594 "\r\n";
595 select STDOUT; close OUT;
596 chdir '..';
597
598 sub create_project {
599         my ($all_object_deps, $progname) = @_;
600         # Construct program's dependency info
601         %seen_objects = ();
602         %lib_files = ();
603         %source_files = ();
604         %header_files = ();
605         %resource_files = ();
606         @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
607         foreach $object_file (@object_files) {
608                 next if defined $seen_objects{$object_file};
609                 $seen_objects{$object_file} = 1;
610                 if($object_file =~ /\.lib$/io) {
611                         $lib_files{$object_file} = 1;
612                         next;
613                 }
614                 $object_deps = $all_object_deps{$object_file};
615                 foreach $object_dep (@$object_deps) {
616                         if($object_dep =~ /\.c$/io) {
617                                 $source_files{$object_dep} = 1;
618                                 next;
619                         }
620                         if($object_dep =~ /\.h$/io) {
621                                 $header_files{$object_dep} = 1;
622                                 next;
623                         }
624                         if($object_dep =~ /\.(rc|ico)$/io) {
625                                 $resource_files{$object_dep} = 1;
626                                 next;
627                         }
628                 }
629         }
630         $libs = join " ", sort keys %lib_files;
631         @source_files = sort keys %source_files;
632         @header_files = sort keys %header_files;
633         @resources = sort keys %resource_files;
634   ($windows_project, $type) = split ",", $progname;
635         mkdir $windows_project
636                 if(! -d $windows_project);
637         chdir $windows_project;
638   $subsys = ($type eq "G") ? "windows" : "console";
639         open OUT, ">$windows_project.dsp"; binmode OUT; select OUT;
640         print
641         "# Microsoft Developer Studio Project File - Name=\"$windows_project\" - Package Owner=<4>\r\n".
642         "# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n".
643         "# ** DO NOT EDIT **\r\n".
644         "\r\n".
645         "# TARGTYPE \"Win32 (x86) Application\" 0x0101\r\n".
646         "\r\n".
647         "CFG=$windows_project - Win32 Debug\r\n".
648         "!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n".
649         "!MESSAGE use the Export Makefile command and run\r\n".
650         "!MESSAGE \r\n".
651         "!MESSAGE NMAKE /f \"$windows_project.mak\".\r\n".
652         "!MESSAGE \r\n".
653         "!MESSAGE You can specify a configuration when running NMAKE\r\n".
654         "!MESSAGE by defining the macro CFG on the command line. For example:\r\n".
655         "!MESSAGE \r\n".
656         "!MESSAGE NMAKE /f \"$windows_project.mak\" CFG=\"$windows_project - Win32 Debug\"\r\n".
657         "!MESSAGE \r\n".
658         "!MESSAGE Possible choices for configuration are:\r\n".
659         "!MESSAGE \r\n".
660         "!MESSAGE \"$windows_project - Win32 Release\" (based on \"Win32 (x86) Application\")\r\n".
661         "!MESSAGE \"$windows_project - Win32 Debug\" (based on \"Win32 (x86) Application\")\r\n".
662         "!MESSAGE \r\n".
663         "\r\n".
664         "# Begin Project\r\n".
665         "# PROP AllowPerConfigDependencies 0\r\n".
666         "# PROP Scc_ProjName \"\"\r\n".
667         "# PROP Scc_LocalPath \"\"\r\n".
668         "CPP=cl.exe\r\n".
669         "MTL=midl.exe\r\n".
670         "RSC=rc.exe\r\n".
671         "\r\n".
672         "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
673         "\r\n".
674         "# PROP BASE Use_MFC 0\r\n".
675         "# PROP BASE Use_Debug_Libraries 0\r\n".
676         "# PROP BASE Output_Dir \"Release\"\r\n".
677         "# PROP BASE Intermediate_Dir \"Release\"\r\n".
678         "# PROP BASE Target_Dir \"\"\r\n".
679         "# PROP Use_MFC 0\r\n".
680         "# PROP Use_Debug_Libraries 0\r\n".
681         "# PROP Output_Dir \"Release\"\r\n".
682         "# PROP Intermediate_Dir \"Release\"\r\n".
683         "# PROP Ignore_Export_Lib 0\r\n".
684         "# PROP Target_Dir \"\"\r\n".
685         "# ADD BASE CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
686         "# ADD CPP /nologo /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
687         "# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
688         "# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
689         "# ADD BASE RSC /l 0x809 /d \"NDEBUG\"\r\n".
690         "# ADD RSC /l 0x809 /d \"NDEBUG\"\r\n".
691         "BSC32=bscmake.exe\r\n".
692         "# ADD BASE BSC32 /nologo\r\n".
693         "# ADD BSC32 /nologo\r\n".
694         "LINK32=link.exe\r\n".
695         "# 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".
696         "# ADD LINK32 $libs /nologo /subsystem:$subsys /machine:I386\r\n".
697         "# SUBTRACT LINK32 /pdb:none\r\n".
698         "\r\n".
699         "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
700         "\r\n".
701         "# PROP BASE Use_MFC 0\r\n".
702         "# PROP BASE Use_Debug_Libraries 1\r\n".
703         "# PROP BASE Output_Dir \"Debug\"\r\n".
704         "# PROP BASE Intermediate_Dir \"Debug\"\r\n".
705         "# PROP BASE Target_Dir \"\"\r\n".
706         "# PROP Use_MFC 0\r\n".
707         "# PROP Use_Debug_Libraries 1\r\n".
708         "# PROP Output_Dir \"Debug\"\r\n".
709         "# PROP Intermediate_Dir \"Debug\"\r\n".
710         "# PROP Ignore_Export_Lib 0\r\n".
711         "# PROP Target_Dir \"\"\r\n".
712         "# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
713         "# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
714         "# ADD BASE MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
715         "# ADD MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
716         "# ADD BASE RSC /l 0x809 /d \"_DEBUG\"\r\n".
717         "# ADD RSC /l 0x809 /d \"_DEBUG\"\r\n".
718         "BSC32=bscmake.exe\r\n".
719         "# ADD BASE BSC32 /nologo\r\n".
720         "# ADD BSC32 /nologo\r\n".
721         "LINK32=link.exe\r\n".
722         "# 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".
723         "# ADD LINK32 $libs /nologo /subsystem:$subsys /debug /machine:I386 /pdbtype:sept\r\n".
724         "# SUBTRACT LINK32 /pdb:none\r\n".
725         "\r\n".
726         "!ENDIF \r\n".
727         "\r\n".
728         "# Begin Target\r\n".
729         "\r\n".
730         "# Name \"$windows_project - Win32 Release\"\r\n".
731         "# Name \"$windows_project - Win32 Debug\"\r\n".
732         "# Begin Group \"Source Files\"\r\n".
733         "\r\n".
734         "# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\"\r\n";
735         foreach $source_file (@source_files) {
736                 print
737                 "# Begin Source File\r\n".
738                 "\r\n".
739                 "SOURCE=..\\..\\$source_file\r\n";
740                 if($source_file =~ /ssh\.c/io) {
741                         # Disable 'Edit and continue' as Visual Studio can't handle the macros
742                         print
743                         "\r\n".
744                         "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
745                         "\r\n".
746                         "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
747                         "\r\n".
748                         "# ADD CPP /Zi\r\n".
749                         "\r\n".
750                         "!ENDIF \r\n".
751                         "\r\n";
752                 }
753                 print "# End Source File\r\n";
754         }
755         print
756         "# End Group\r\n".
757         "# Begin Group \"Header Files\"\r\n".
758         "\r\n".
759         "# PROP Default_Filter \"h;hpp;hxx;hm;inl\"\r\n";
760         foreach $header_file (@header_files) {
761                 print
762                 "# Begin Source File\r\n".
763                 "\r\n".
764                 "SOURCE=..\\..\\$header_file\r\n".
765                 "# End Source File\r\n";
766                 }
767         print
768         "# End Group\r\n".
769         "# Begin Group \"Resource Files\"\r\n".
770         "\r\n".
771         "# PROP Default_Filter \"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\"\r\n";
772         foreach $resource_file (@resources) {
773                 print
774                 "# Begin Source File\r\n".
775                 "\r\n".
776                 "SOURCE=..\\..\\$resource_file\r\n".
777                 "# End Source File\r\n";
778                 }
779         print
780         "# End Group\r\n".
781         "# End Target\r\n".
782         "# End Project\r\n";
783         select STDOUT; close OUT;
784         chdir '..';
785         }
786
787 ##-- X/GTK/Unix makefile
788 open OUT, ">unix/Makefile.gtk"; select OUT;
789 print
790 "# Makefile for PuTTY under X/GTK and Unix.\n".
791 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
792 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
793 # gcc command line option is -D not /D
794 ($_ = $help) =~ s/=\/D/=-D/gs;
795 print $_;
796 print
797 "\n".
798 "# You can define this path to point at your tools if you need to\n".
799 "# TOOLPATH = /opt/gcc/bin\n".
800 "CC = \$(TOOLPATH)cc\n".
801 "\n".
802 &splitline("CFLAGS = -O2 -Wall -Werror -g -I. -I.. -I../charset `gtk-config --cflags`")."\n".
803 "XLDFLAGS = `gtk-config --libs`\n".
804 "ULDFLAGS =#\n".
805 "INSTALL=install\n",
806 "INSTALL_PROGRAM=\$(INSTALL)\n",
807 "INSTALL_DATA=\$(INSTALL)\n",
808 "prefix=/usr/local\n",
809 "exec_prefix=\$(prefix)\n",
810 "bindir=\$(exec_prefix)/bin\n",
811 "mandir=\$(prefix)/man\n",
812 "man1dir=\$(mandir)/man1\n",
813 "\n".
814 ".SUFFIXES:\n".
815 "\n".
816 "%.o:\n".
817 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
818 "\n";
819 print &splitline("all:" . join "", map { " $_" } &progrealnames("XU"));
820 print "\n\n";
821 foreach $p (&prognames("XU")) {
822   ($prog, $type) = split ",", $p;
823   $objstr = &objects($p, "X.o", undef, undef);
824   print &splitline($prog . ": " . $objstr), "\n";
825   $libstr = &objects($p, undef, undef, "-lX");
826   print &splitline("\t\$(CC)" . $mw . " \$(${type}LDFLAGS) -o \$@ " .
827                    $objstr . " $libstr", 69), "\n\n";
828 }
829 foreach $d (&deps("X.o", undef, "../", "/")) {
830   print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
831       "\n";
832 }
833 print
834 "\n".
835 "version.o: FORCE;\n".
836 "# Hack to force version.o to be rebuilt always\n".
837 "FORCE:\n".
838 "\tif test -z \"\$(VER)\" && (cd ..; md5sum -c manifest); then \\\n".
839 "\t\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) `cat ../version.def` -c ../version.c; \\\n".
840 "\telse \\\n".
841 "\t\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c ../version.c; \\\n".
842 "\tfi\n".
843 "clean:\n".
844 "\trm -f *.o". (join "", map { " $_" } &progrealnames("XU")) . "\n".
845 "\n",
846 "install:\n",
847 map("\t\$(INSTALL_PROGRAM) -m 755 $_ \$(DESTDIR)\$(bindir)/$_\n", &progrealnames("XU")),
848 map("\t\$(INSTALL_DATA) -m 644 ../doc/$_ \$(DESTDIR)\$(man1dir)/$_\n", &manpages("XU", "1")),
849 "\n",
850 "install-strip:\n",
851 "\t\$(MAKE) install INSTALL_PROGRAM=\"\$(INSTALL_PROGRAM) -s\"\n",
852 "\n";
853 select STDOUT; close OUT;
854
855 ##-- MPW Makefile
856 open OUT, ">mac/Makefile.mpw"; select OUT;
857 print <<END;
858 # Makefile for PuTTY under MPW.
859 #
860 # This file was created by `mkfiles.pl' from the `Recipe' file.
861 # DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.
862 END
863 # MPW command line option is -d not /D
864 ($_ = $help) =~ s/=\/D/=-d /gs;
865 print $_;
866 print <<END;
867
868 ROptions     = `Echo "{VER}" | StreamEdit -e "1,\$ replace /=(\xc5)\xa81\xb0/ 'STR=\xb6\xb6\xb6\xb6\xb6"' \xa81 '\xb6\xb6\xb6\xb6\xb6"'"`
869
870 C_68K = {C}
871 C_CFM68K = {C}
872 C_PPC = {PPCC}
873 C_Carbon = {PPCC}
874
875 # -w 35 disables "unused parameter" warnings
876 COptions     = -i : -i :: -i ::charset -w 35 -w err -proto strict -ansi on \xb6
877                -notOnce
878 COptions_68K = {COptions} -model far -opt time
879 # Enabling "-opt space" for CFM-68K gives me undefined references to
880 # _\$LDIVT and _\$LMODT.
881 COptions_CFM68K = {COptions} -model cfmSeg -opt time
882 COptions_PPC = {COptions} -opt size -traceback
883 COptions_Carbon = {COptions} -opt size -traceback -d TARGET_API_MAC_CARBON
884
885 Link_68K = ILink
886 Link_CFM68K = ILink
887 Link_PPC = PPCLink
888 Link_Carbon = PPCLink
889
890 LinkOptions = -c 'pTTY'
891 LinkOptions_68K = {LinkOptions} -br 68k -model far -compact
892 LinkOptions_CFM68K = {LinkOptions} -br 020 -model cfmseg -compact
893 LinkOptions_PPC = {LinkOptions}
894 LinkOptions_Carbon = -m __appstart -w {LinkOptions}
895
896 Libs_68K =      "{CLibraries}StdCLib.far.o" \xb6
897                 "{Libraries}MacRuntime.o" \xb6
898                 "{Libraries}MathLib.far.o" \xb6
899                 "{Libraries}IntEnv.far.o" \xb6
900                 "{Libraries}Interface.o" \xb6
901                 "{Libraries}Navigation.far.o" \xb6
902                 "{Libraries}OpenTransport.o" \xb6
903                 "{Libraries}OpenTransportApp.o" \xb6
904                 "{Libraries}OpenTptInet.o" \xb6
905                 "{Libraries}UnicodeConverterLib.far.o"
906
907 Libs_CFM =      "{SharedLibraries}InterfaceLib" \xb6
908                 "{SharedLibraries}StdCLib" \xb6
909                 "{SharedLibraries}AppearanceLib" \xb6
910                         -weaklib AppearanceLib \xb6
911                 "{SharedLibraries}NavigationLib" \xb6
912                         -weaklib NavigationLib \xb6
913                 "{SharedLibraries}TextCommon" \xb6
914                         -weaklib TextCommon \xb6
915                 "{SharedLibraries}UnicodeConverter" \xb6
916                         -weaklib UnicodeConverter
917
918 Libs_CFM68K =   {Libs_CFM} \xb6
919                 "{CFM68KLibraries}NuMacRuntime.o"
920
921 Libs_PPC =      {Libs_CFM} \xb6
922                 "{SharedLibraries}ControlsLib" \xb6
923                         -weaklib ControlsLib \xb6
924                 "{SharedLibraries}WindowsLib" \xb6
925                         -weaklib WindowsLib \xb6
926                 "{SharedLibraries}OpenTransportLib" \xb6
927                         -weaklib OTClientLib \xb6
928                         -weaklib OTClientUtilLib \xb6
929                 "{SharedLibraries}OpenTptInternetLib" \xb6
930                         -weaklib OTInetClientLib \xb6
931                 "{PPCLibraries}StdCRuntime.o" \xb6
932                 "{PPCLibraries}PPCCRuntime.o" \xb6
933                 "{PPCLibraries}CarbonAccessors.o" \xb6
934                 "{PPCLibraries}OpenTransportAppPPC.o" \xb6
935                 "{PPCLibraries}OpenTptInetPPC.o"
936
937 Libs_Carbon =   "{PPCLibraries}CarbonStdCLib.o" \xb6
938                 "{PPCLibraries}StdCRuntime.o" \xb6
939                 "{PPCLibraries}PPCCRuntime.o" \xb6
940                 "{SharedLibraries}CarbonLib" \xb6
941                 "{SharedLibraries}StdCLib"
942
943 END
944 print &splitline("all \xc4 " . join(" ", &progrealnames("M")), undef, "\xb6");
945 print "\n\n";
946 foreach $p (&prognames("M")) {
947   ($prog, $type) = split ",", $p;
948
949   print &splitline("$prog \xc4 $prog.68k $prog.ppc $prog.carbon",
950                    undef, "\xb6"), "\n\n";
951
952   $rsrc = &objects($p, "", "X.rsrc", undef);
953
954   foreach $arch (qw(68K CFM68K PPC Carbon)) {
955       $objstr = &objects($p, "X.\L$arch\E.o", "", undef);
956       print &splitline("$prog.\L$arch\E \xc4 $objstr $rsrc", undef, "\xb6");
957       print "\n";
958       print &splitline("\tDuplicate -y $rsrc {Targ}", 69, "\xb6"), "\n";
959       print &splitline("\t{Link_$arch} -o {Targ} -fragname $prog " .
960                        "{LinkOptions_$arch} " .
961                        $objstr . " {Libs_$arch}", 69, "\xb6"), "\n";
962       print &splitline("\tSetFile -a BMi {Targ}", 69, "\xb6"), "\n\n";
963   }
964
965 }
966 foreach $d (&deps("", "X.rsrc", "::", ":")) {
967   next unless $d->{obj};
968   print &splitline(sprintf("%s \xc4 %s", $d->{obj}, join " ", @{$d->{deps}}),
969                    undef, "\xb6"), "\n";
970   print "\tRez ", $d->{deps}->[0], " -o {Targ} {ROptions}\n\n";
971 }
972 foreach $arch (qw(68K CFM68K)) {
973     foreach $d (&deps("X.\L$arch\E.o", "", "::", ":")) {
974          next unless $d->{obj};
975         print &splitline(sprintf("%s \xc4 %s", $d->{obj},
976                                  join " ", @{$d->{deps}}),
977                          undef, "\xb6"), "\n";
978          print "\t{C_$arch} ", $d->{deps}->[0],
979                " -o {Targ} {COptions_$arch}\n\n";
980      }
981 }
982 foreach $arch (qw(PPC Carbon)) {
983     foreach $d (&deps("X.\L$arch\E.o", "", "::", ":")) {
984          next unless $d->{obj};
985         print &splitline(sprintf("%s \xc4 %s", $d->{obj},
986                                  join " ", @{$d->{deps}}),
987                          undef, "\xb6"), "\n";
988          # The odd stuff here seems to stop afpd getting confused.
989          print "\techo -n > {Targ}\n";
990          print "\tsetfile -t XCOF {Targ}\n";
991          print "\t{C_$arch} ", $d->{deps}->[0],
992                " -o {Targ} {COptions_$arch}\n\n";
993      }
994 }
995 select STDOUT; close OUT;
996
997 ##-- lcc makefile
998 open OUT, ">Makefile.lcc"; select OUT;
999 print
1000 "# Makefile for PuTTY under lcc.\n".
1001 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1002 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1003 # lcc command line option is -D not /D
1004 ($_ = $help) =~ s/=\/D/=-D/gs;
1005 print $_;
1006 print
1007 "\n".
1008 "# If you rename this file to `Makefile', you should change this line,\n".
1009 "# so that the .rsp files still depend on the correct makefile.\n".
1010 "MAKEFILE = Makefile.lcc\n".
1011 "\n".
1012 "# C compilation flags\n".
1013 "CFLAGS = -D_WINDOWS\n".
1014 "\n".
1015 "# Get include directory for resource compiler\n".
1016 "\n".
1017 ".c.obj:\n".
1018 &splitline("\tlcc -O -p6 \$(COMPAT) \$(FWHACK)".
1019   " \$(XFLAGS) \$(CFLAGS)  \$*.c",69)."\n".
1020 ".rc.res:\n".
1021 &splitline("\tlrc \$(FWHACK) \$(RCFL) -r \$*.rc",69)."\n".
1022 "\n";
1023 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
1024 print "\n\n";
1025 foreach $p (&prognames("GC")) {
1026   ($prog, $type) = split ",", $p;
1027   $objstr = &objects($p, "X.obj", "X.res", undef);
1028   print &splitline("$prog.exe: " . $objstr ), "\n";
1029   $subsystemtype = undef;
1030   if ($prog eq "pageant" || $prog eq "putty" ||$prog eq "puttygen" || $prog eq "puttytel") { 
1031         $subsystemtype = "-subsystem  windows"; }
1032   my $libss = "shell32.lib wsock32.lib ws2_32.lib winspool.lib winmm.lib imm32.lib";
1033   print &splitline("\tlcclnk $subsystemtype -o $prog.exe $objstr $libss");
1034   print "\n\n";
1035 }
1036
1037
1038 foreach $d (&deps("X.obj", "X.res", "", "\\")) {
1039   print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
1040     "\n";
1041 }
1042 print
1043 "\n".
1044 "version.o: FORCE\n".
1045 "# Hack to force version.o to be rebuilt always\n".
1046 "FORCE:\n".
1047 "\tlcc \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
1048 "clean:\n".
1049 "\t-del *.obj\n".
1050 "\t-del *.exe\n".
1051 "\t-del *.res\n";
1052
1053 select STDOUT; close OUT;
1054