3 # Makefile generator for PuTTY.
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.
12 open IN, "Recipe" or die "unable to open Recipe file\n";
14 @incdirs = ("", "unix/", "mac/");
16 $help = ""; # list of newline-free lines of help text
17 %programs = (); # maps prog name + type letter to listref of objects/resources
18 %groups = (); # maps group name to listref of objects/resources
21 # Skip comments (unless the comments belong, for example because
22 # they're part of the help text).
23 next if /^\s*#/ and !$in_help;
27 if ($_[0] eq "!begin" and $_[1] eq "help") { $in_help = 1; next; }
28 if ($_[0] eq "!end" and $in_help) { $in_help = 0; next; }
29 # If we're gathering help text, keep doing so.
30 if ($in_help) { $help .= "$_\n"; next; }
32 next if scalar @_ == 0;
34 # Now we have an ordinary line. See if it's an = line, a : line
39 $listref = $lastlistref;
41 die "$.: unexpected + line\n" if !defined $lastlistref;
42 } elsif ($_[1] eq "=") {
43 $groups{$_[0]} = [] if !defined $groups{$_[0]};
44 $listref = $groups{$_[0]};
46 shift @objs; # eat the group name
47 } elsif ($_[1] eq ":") {
50 shift @objs; # eat the program name
52 die "$.: unrecognised line type\n";
54 shift @objs; # eat the +, the = or the :
56 while (scalar @objs > 0) {
59 foreach $j (@{$groups{$i}}) { unshift @objs, $j; }
60 } elsif (($i eq "[G]" or $i eq "[C]" or $i eq "[M]" or
61 $i eq "[X]" or $i eq "[U]") and defined $prog) {
62 $type = substr($i,1,1);
67 if ($prog and $type) {
68 die "multiple program entries for $prog [$type]\n"
69 if defined $programs{$prog . "," . $type};
70 $programs{$prog . "," . $type} = $listref;
72 $lastlistref = $listref;
77 # Now retrieve the complete list of objects and resource files, and
78 # construct dependency data for them. While we're here, expand the
79 # object list for each program, and complain if its type isn't set.
80 @prognames = sort keys %programs;
83 foreach $i (@prognames) {
84 ($prog, $type) = split ",", $i;
85 # Strip duplicate object names.
87 @list = grep { $status = ($prev ne $_); $prev=$_; $status }
88 sort @{$programs{$i}};
89 $programs{$i} = [@list];
91 # Dependencies for "x" start with "x.c".
92 # Dependencies for "x.res" start with "x.rc".
93 # Both types of file are pushed on the list of files to scan.
94 # Libraries (.lib) don't have dependencies at all.
95 if ($j =~ /^(.*)\.res$/) {
97 $depends{$j} = [$file];
98 push @scanlist, $file;
99 } elsif ($j =~ /\.lib$/) {
100 # libraries don't have dependencies
103 $depends{$j} = [$file];
104 push @scanlist, $file;
109 # Scan each file on @scanlist and find further inclusions.
110 # Inclusions are given by lines of the form `#include "otherfile"'
111 # (system headers are automatically ignored by this because they'll
112 # be given in angle brackets). Files included by this method are
113 # added back on to @scanlist to be scanned in turn (if not already
116 # Resource scripts (.rc) can also include a file by means of a line
117 # ending `ICON "filename"'. Files included by this method are not
118 # added to @scanlist because they can never include further files.
120 # In this pass we write out a hash %further which maps a source
121 # file name into a listref containing further source file names.
124 while (scalar @scanlist > 0) {
125 $file = shift @scanlist;
126 next if defined $further{$file}; # skip if we've already done it
127 $resource = ($file =~ /\.rc$/ ? 1 : 0);
128 $further{$file} = [];
129 $dirfile = &findfile($file);
130 open IN, "$dirfile" or die "unable to open source file $file\n";
133 /^\s*#include\s+\"([^\"]+)\"/ and do {
134 push @{$further{$file}}, $1;
138 /ICON\s+\"([^\"]+)\"\s*$/ and do {
139 push @{$further{$file}}, $1;
146 # Now we're ready to generate the final dependencies section. For
147 # each key in %depends, we must expand the dependencies list by
148 # iteratively adding entries from %further.
149 foreach $i (keys %depends) {
151 @scanlist = @{$depends{$i}};
152 foreach $i (@scanlist) { $dep{$i} = 1; }
153 while (scalar @scanlist > 0) {
154 $file = shift @scanlist;
155 foreach $j (@{$further{$file}}) {
158 push @{$depends{$i}}, $j;
163 # printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
166 # Utility routines while writing out the Makefiles.
170 my $dir, $i, $outdir = "";
172 foreach $dir (@incdirs) {
173 $outdir = $dir, $i++ if -f "$dir$name";
175 die "multiple instances of source file $name\n" if $i > 1;
176 return "$outdir$name";
180 my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
184 foreach $i (@{$programs{$prog}}) {
186 if ($i =~ /^(.*)\.res/) {
188 ($x = $rtmpl) =~ s/X/$y/;
189 } elsif ($i =~ /^(.*)\.lib/) {
191 ($x = $ltmpl) =~ s/X/$y/;
193 ($x = $otmpl) =~ s/X/$i/;
195 push @ret, $x if $x ne "";
197 return join " ", @ret;
201 my ($line, $width, $splitchar) = @_;
203 $len = (defined $width ? $width : 76);
204 $splitchar = (defined $splitchar ? $splitchar : '\\');
205 while (length $line > $len) {
206 $line =~ /^(.{0,$len})\s(.*)$/ or $line =~ /^(.{$len,}?\s(.*)$/;
207 $result .= $1 . " ${splitchar}\n\t\t";
211 return $result . $line;
215 my ($otmpl, $rtmpl, $prefix, $dirsep, $depchar, $splitchar) = @_;
220 foreach $i (sort keys %depends) {
221 if ($i =~ /^(.*)\.res/) {
222 next if !defined $rtmpl;
224 ($x = $rtmpl) =~ s/X/$y/;
226 ($x = $otmpl) =~ s/X/$i/;
228 @deps = @{$depends{$i}};
234 push @ret, {obj => $x, deps => [@deps]};
241 my ($n, $prog, $type);
244 foreach $n (@prognames) {
245 ($prog, $type) = split ",", $n;
246 push @ret, $n if index($types, $type) >= 0;
253 my ($n, $prog, $type);
256 foreach $n (@prognames) {
257 ($prog, $type) = split ",", $n;
258 push @ret, $prog if index($types, $type) >= 0;
264 my ($types,$suffix) = @_;
266 # assume that all UNIX programs have a man page
267 if($suffix eq "1" && $types =~ /X/) {
268 return map("$_.1", &progrealnames($types));
273 # Now we're ready to output the actual Makefiles.
276 open OUT, ">Makefile.cyg"; select OUT;
278 "# Makefile for PuTTY under cygwin.\n".
279 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
280 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
281 # gcc command line option is -D not /D
282 ($_ = $help) =~ s/=\/D/=-D/gs;
286 "# You can define this path to point at your tools if you need to\n".
287 "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
288 "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
289 "CC = \$(TOOLPATH)gcc\n".
290 "RC = \$(TOOLPATH)windres\n".
291 "# You may also need to tell windres where to find include files:\n".
292 "# RCINC = --include-dir c:\\cygwin\\include\\\n".
294 &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
295 " -DNO_SECURITY -D_NO_OLDNAMES -DNO_MULTIMON -I.")."\n".
296 "LDFLAGS = -mno-cygwin -s\n".
297 &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
298 " --define WINVER=0x0400 --define MINGW32_FIX=1")."\n".
303 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
306 "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
308 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
310 foreach $p (&prognames("GC")) {
311 ($prog, $type) = split ",", $p;
312 $objstr = &objects($p, "X.o", "X.res.o", undef);
313 print &splitline($prog . ".exe: " . $objstr), "\n";
314 my $mw = $type eq "G" ? " -mwindows" : "";
315 $libstr = &objects($p, undef, undef, "-lX");
316 print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
317 $objstr . " $libstr", 69), "\n\n";
319 foreach $d (&deps("X.o", "X.res.o", "", "/")) {
320 print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
325 "version.o: FORCE;\n".
326 "# Hack to force version.o to be rebuilt always\n".
328 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
330 "\trm -f *.o *.exe *.res.o\n".
332 select STDOUT; close OUT;
334 ##-- Borland makefile
335 %stdlibs = ( # Borland provides many Win32 API libraries intrinsically
347 open OUT, ">Makefile.bor"; select OUT;
349 "# Makefile for PuTTY under Borland C.\n".
350 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
351 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
352 # bcc32 command line option is -D not /D
353 ($_ = $help) =~ s/=\/D/=-D/gs;
357 "# If you rename this file to `Makefile', you should change this line,\n".
358 "# so that the .rsp files still depend on the correct makefile.\n".
359 "MAKEFILE = Makefile.bor\n".
361 "# C compilation flags\n".
362 "CFLAGS = -D_WINDOWS -DWINVER=0x0401\n".
364 "# Get include directory for resource compiler\n".
366 "BCB = \$(MAKEDIR)\\..\n".
370 &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
371 " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
373 &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
374 " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
376 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
378 foreach $p (&prognames("GC")) {
379 ($prog, $type) = split ",", $p;
380 $objstr = &objects($p, "X.obj", "X.res", undef);
381 print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
382 my $ap = ($type eq "G") ? "-aa" : "-ap";
383 print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
385 foreach $p (&prognames("GC")) {
386 ($prog, $type) = split ",", $p;
387 print $prog, ".rsp: \$(MAKEFILE)\n";
388 $objstr = &objects($p, "X.obj", undef, undef);
389 @objlist = split " ", $objstr;
391 foreach $i (@objlist) {
392 if (length($objlines[$#objlines] . " $i") > 50) {
395 $objlines[$#objlines] .= " $i";
397 $c0w = ($type eq "G") ? "c0w32" : "c0x32";
398 print "\techo $c0w + > $prog.rsp\n";
399 for ($i=0; $i<=$#objlines; $i++) {
400 $plus = ($i < $#objlines ? " +" : "");
401 print "\techo$objlines[$i]$plus >> $prog.rsp\n";
403 print "\techo $prog.exe >> $prog.rsp\n";
404 $objstr = &objects($p, "X.obj", "X.res", undef);
405 @libs = split " ", &objects($p, undef, undef, "X");
406 @libs = grep { !$stdlibs{$_} } @libs;
407 unshift @libs, "cw32", "import32";
408 $libstr = join ' ', @libs;
409 print "\techo nul,$libstr, >> $prog.rsp\n";
410 print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
413 foreach $d (&deps("X.obj", "X.res", "", "\\")) {
414 print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
419 "version.o: FORCE\n".
420 "# Hack to force version.o to be rebuilt always\n".
422 "\tbcc32 \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
433 "\t-del *.\$\$\$\$\$\$\n";
434 select STDOUT; close OUT;
436 ##-- Visual C++ makefile
437 open OUT, ">Makefile.vc"; select OUT;
439 "# Makefile for PuTTY under Visual C.\n".
440 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
441 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
445 "# If you rename this file to `Makefile', you should change this line,\n".
446 "# so that the .rsp files still depend on the correct makefile.\n".
447 "MAKEFILE = Makefile.vc\n".
449 "# C compilation flags\n".
450 "CFLAGS = /nologo /W3 /O1 /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401\n".
451 "LFLAGS = /incremental:no /fixed\n".
454 "\tcl \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) /c \$*.c\n".
456 "\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 -DWINVER=0x0400 \$*.rc\n".
458 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
460 foreach $p (&prognames("GC")) {
461 ($prog, $type) = split ",", $p;
462 $objstr = &objects($p, "X.obj", "X.res", undef);
463 print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
464 print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
466 foreach $p (&prognames("GC")) {
467 ($prog, $type) = split ",", $p;
468 print $prog, ".rsp: \$(MAKEFILE)\n";
469 $objstr = &objects($p, "X.obj", "X.res", "X.lib");
470 @objlist = split " ", $objstr;
472 foreach $i (@objlist) {
473 if (length($objlines[$#objlines] . " $i") > 50) {
476 $objlines[$#objlines] .= " $i";
478 $subsys = ($type eq "G") ? "windows" : "console";
479 print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
480 for ($i=0; $i<=$#objlines; $i++) {
481 print "\techo$objlines[$i] >> $prog.rsp\n";
485 foreach $d (&deps("X.obj", "X.res", "", "\\")) {
486 print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
491 "# Hack to force version.o to be rebuilt always\n".
492 "version.obj: *.c *.h *.rc\n".
493 "\tcl \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
511 "\t-del debug.log\n";
512 select STDOUT; close OUT;
514 ##-- X/GTK/Unix makefile
515 open OUT, ">unix/Makefile.gtk"; select OUT;
517 "# Makefile for PuTTY under X/GTK and Unix.\n".
518 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
519 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
520 # gcc command line option is -D not /D
521 ($_ = $help) =~ s/=\/D/=-D/gs;
525 "# You can define this path to point at your tools if you need to\n".
526 "# TOOLPATH = /opt/gcc/bin\n".
527 "CC = \$(TOOLPATH)cc\n".
529 &splitline("CFLAGS = -Wall -g -I. -I.. `gtk-config --cflags`")."\n".
530 "XLDFLAGS = `gtk-config --libs`\n".
533 "INSTALL_PROGRAM=\$(INSTALL)\n",
534 "INSTALL_DATA=\$(INSTALL)\n",
535 "prefix=/usr/local\n",
536 "exec_prefix=\$(prefix)\n",
537 "bindir=\$(exec_prefix)/bin\n",
538 "mandir=\$(prefix)/man\n",
539 "man1dir=\$(mandir)/man1\n",
544 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
546 print &splitline("all:" . join "", map { " $_" } &progrealnames("XU"));
548 foreach $p (&prognames("XU")) {
549 ($prog, $type) = split ",", $p;
550 $objstr = &objects($p, "X.o", undef, undef);
551 print &splitline($prog . ": " . $objstr), "\n";
552 $libstr = &objects($p, undef, undef, "-lX");
553 print &splitline("\t\$(CC)" . $mw . " \$(${type}LDFLAGS) -o \$@ " .
554 $objstr . " $libstr", 69), "\n\n";
556 foreach $d (&deps("X.o", undef, "../", "/")) {
557 print &splitline(sprintf("%s: %s", $d->{obj}, join " ", @{$d->{deps}})),
562 "version.o: FORCE;\n".
563 "# Hack to force version.o to be rebuilt always\n".
565 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c ../version.c\n".
567 "\trm -f *.o". (join "", map { " $_" } &progrealnames("XU")) . "\n".
570 map("\t\$(INSTALL_PROGRAM) -m 755 $_ \$(bindir)/$_\n", &progrealnames("XU")),
571 map("\t\$(INSTALL_DATA) -m 644 $_ \$(man1dir)/$_\n", &manpages("XU", "1")),
574 "\t\$(MAKE) install INSTALL_PROGRAM=\"\$(INSTALL_PROGRAM) -s\"\n",
576 select STDOUT; close OUT;
579 open OUT, ">mac/Makefile.mpw"; select OUT;
581 # Makefile for PuTTY under MPW.
583 # This file was created by `mkfiles.pl' from the `Recipe' file.
584 # DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.
586 # MPW command line option is -d not /D
587 ($_ = $help) =~ s/=\/D/=-d /gs;
591 # -w 35 disables "unused parameter" warnings
592 COptions = -i : -i :: -w 35 -w err -proto strict
593 COptions_68K = {COptions} -model far -opt space
594 # Enabling "-opt space" for CFM-68K gives me undefined references to
595 # _$LDIVT and _$LMODT.
596 COptions_CFM68K = {COptions} -model cfmSeg -opt time
597 COptions_PPC = {COptions} -opt size
599 LinkOptions = -c 'pTTY' -fragname PuTTY
600 LinkOptions_68K = {LinkOptions} -br 68k -model far -compact
601 LinkOptions_CFM68K = {LinkOptions} -br 020 -model cfmseg -compact
602 LinkOptions_PPC = {LinkOptions}
604 Libs_68K = "{CLibraries}StdCLib.far.o" \xb6
605 "{Libraries}MacRuntime.o" \xb6
606 "{Libraries}MathLib.far.o" \xb6
607 "{Libraries}IntEnv.far.o" \xb6
608 "{Libraries}Interface.o" \xb6
609 "{Libraries}UnicodeConverterLib.far.o"
611 Libs_CFM = "{SharedLibraries}InterfaceLib" \xb6
612 "{SharedLibraries}StdCLib" \xb6
613 "{SharedLibraries}AppearanceLib" \xb6
614 -weaklib AppearanceLib \xb6
615 "{SharedLibraries}TextCommon" \xb6
616 -weaklib TextCommon \xb6
617 "{SharedLibraries}UnicodeConverter" \xb6
618 -weaklib UnicodeConverter
620 Libs_CFM68K = {Libs_CFM} \xb6
621 "{CFM68KLibraries}NuMacRuntime.o"
623 Libs_PPC = {Libs_CFM} \xb6
624 "{PPCLibraries}StdCRuntime.o" \xb6
625 "{PPCLibraries}PPCCRuntime.o" \xb6
626 "{PPCLibraries}CarbonAccessors.o"
629 print &splitline("all \xc4 " . join(" ", &progrealnames("M")), undef, "\xb6");
631 foreach $p (&prognames("M")) {
632 ($prog, $type) = split ",", $p;
634 print &splitline("$prog \xc4 $prog.68k $prog.cfm68k $prog.ppc",
635 undef, "\xb6"), "\n\n";
637 $objstr = &objects($p, "X.68k.o", undef, undef);
638 print &splitline("$prog.68k \xc4 $objstr", undef, "\xb6"), "\n";
639 print &splitline("\tILink -o {Targ} {LinkOptions_68K} " .
640 $objstr . " {Libs_68K}", 69, "\xb6"), "\n";
641 print &splitline("\tSetFile -a BM {Targ}", 69, "\xb6"), "\n\n";
643 $objstr = &objects($p, "X.cfm68k.o", undef, undef);
644 print &splitline("$prog.cfm68k \xc4 $objstr", undef, "\xb6"), "\n";
645 print &splitline("\tILink -o {Targ} {LinkOptions_CFM68K} " .
646 $objstr . " {Libs_CFM68K}", 69, "\xb6"), "\n";
647 print &splitline("\tSetFile -a BM {Targ}", 69, "\xb6"), "\n\n";
649 $objstr = &objects($p, "X.ppc.o", undef, undef);
650 print &splitline("$prog.ppc \xc4 $objstr", undef, "\xb6"), "\n";
651 print &splitline("\tPPCLink -o {Targ} {LinkOptions_PPC} " .
652 $objstr . " {Libs_PPC}", 69, "\xb6"), "\n";
653 print &splitline("\tSetFile -a BM {Targ}", 69, "\xb6"), "\n\n";
655 foreach $d (&deps("X.68k.o", undef, "::", ":")) {
656 print &splitline(sprintf("%s \xc4 %s", $d->{obj}, join " ", @{$d->{deps}}),
657 undef, "\xb6"), "\n";
658 print "\t{C} ", $d->{deps}->[0], " -o {Targ} {COptions_68K}\n\n";
660 foreach $d (&deps("X.cfm68k.o", undef, "::", ":")) {
661 print &splitline(sprintf("%s \xc4 %s", $d->{obj}, join " ", @{$d->{deps}}),
662 undef, "\xb6"), "\n";
663 print "\t{C} ", $d->{deps}->[0], " -o {Targ} {COptions_CFM68K}\n\n";
665 foreach $d (&deps("X.ppc.o", undef, "::", ":")) {
666 print &splitline(sprintf("%s \xc4 %s", $d->{obj}, join " ", @{$d->{deps}}),
667 undef, "\xb6"), "\n";
668 # The odd stuff here seems to stop afpd getting confused.
669 print "\techo -n > {Targ}\n";
670 print "\tsetfile -t XCOF {Targ}\n";
671 print "\t{PPCC} ", $d->{deps}->[0], " -o {Targ} {COptions_PPC}\n\n";
673 select STDOUT; close OUT;