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.
10 open IN, "Recipe" or die "unable to open Recipe file\n";
12 $help = ""; # list of newline-free lines of help text
13 %programs = (); # maps program name to listref of objects/resources
14 %types = (); # maps program name to "G" or "C"
15 %groups = (); # maps group name to listref of objects/resources
18 # Skip comments (unless the comments belong, for example because
19 # they're part of the help text).
20 next if /^\s*#/ and !$in_help;
24 if ($_[0] eq "!begin" and $_[1] eq "help") { $in_help = 1; next; }
25 if ($_[0] eq "!end" and $in_help) { $in_help = 0; next; }
26 # If we're gathering help text, keep doing so.
27 if ($in_help) { $help .= "$_\n"; next; }
29 next if scalar @_ == 0;
31 # Now we have an ordinary line. See if it's an = line, a : line
36 $listref = $lastlistref;
38 die "$.: unexpected + line\n" if !defined $lastlistref;
39 } elsif ($_[1] eq "=") {
40 $groups{$_[0]} = [] if !defined $groups{$_[0]};
41 $listref = $groups{$_[0]};
43 shift @objs; # eat the group name
44 } elsif ($_[1] eq ":") {
45 $programs{$_[0]} = [] if !defined $programs{$_[0]};
46 $listref = $programs{$_[0]};
48 shift @objs; # eat the program name
50 die "$.: unrecognised line type\n";
52 shift @objs; # eat the +, the = or the :
54 while (scalar @objs > 0) {
57 foreach $j (@{$groups{$i}}) { unshift @objs, $j; }
58 } elsif (($i eq "[G]" or $i eq "[C]") and defined $prog) {
59 $types{$prog} = substr($i,1,1);
64 $lastlistref = $listref;
69 # Now retrieve the complete list of objects and resource files, and
70 # construct dependency data for them. While we're here, expand the
71 # object list for each program, and complain if its type isn't set.
72 @prognames = sort keys %programs;
75 foreach $i (@prognames) {
76 if (!defined $types{$i}) { die "type not set for program $i\n"; }
77 # Strip duplicate object names.
79 @list = grep { $status = ($prev ne $_); $prev=$_; $status }
80 sort @{$programs{$i}};
81 $programs{$i} = [@list];
83 # Dependencies for "x" start with "x.c".
84 # Dependencies for "x.res" start with "x.rc".
85 # Both types of file are pushed on the list of files to scan.
86 # Libraries (.lib) don't have dependencies at all.
87 if ($j =~ /^(.*)\.res$/) {
89 $depends{$j} = [$file];
90 push @scanlist, $file;
91 } elsif ($j =~ /\.lib$/) {
92 # libraries don't have dependencies
95 $depends{$j} = [$file];
96 push @scanlist, $file;
101 # Scan each file on @scanlist and find further inclusions.
102 # Inclusions are given by lines of the form `#include "otherfile"'
103 # (system headers are automatically ignored by this because they'll
104 # be given in angle brackets). Files included by this method are
105 # added back on to @scanlist to be scanned in turn (if not already
108 # Resource scripts (.rc) can also include a file by means of a line
109 # ending `ICON "filename"'. Files included by this method are not
110 # added to @scanlist because they can never include further files.
112 # In this pass we write out a hash %further which maps a source
113 # file name into a listref containing further source file names.
116 while (scalar @scanlist > 0) {
117 $file = shift @scanlist;
118 next if defined $further{$file}; # skip if we've already done it
119 $resource = ($file =~ /\.rc$/ ? 1 : 0);
120 $further{$file} = [];
121 open IN, $file or die "unable to open source file $file\n";
124 /^\s*#include\s+\"([^\"]+)\"/ and do {
125 push @{$further{$file}}, $1;
129 /ICON\s+\"([^\"]+)\"\s*$/ and do {
130 push @{$further{$file}}, $1;
137 # Now we're ready to generate the final dependencies section. For
138 # each key in %depends, we must expand the dependencies list by
139 # iteratively adding entries from %further.
140 foreach $i (keys %depends) {
142 @scanlist = @{$depends{$i}};
143 foreach $i (@scanlist) { $dep{$i} = 1; }
144 while (scalar @scanlist > 0) {
145 $file = shift @scanlist;
146 foreach $j (@{$further{$file}}) {
149 push @{$depends{$i}}, $j;
154 # printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
157 # Utility routines while writing out the Makefiles.
160 my ($prog, $otmpl, $rtmpl, $ltmpl) = @_;
164 foreach $i (@{$programs{$prog}}) {
165 if ($i =~ /^(.*)\.res/) {
167 ($x = $rtmpl) =~ s/X/$y/;
168 push @ret, $x if $x ne "";
169 } elsif ($i =~ /^(.*)\.lib/) {
171 ($x = $ltmpl) =~ s/X/$y/;
172 push @ret, $x if $x ne "";
174 ($x = $otmpl) =~ s/X/$i/;
175 push @ret, $x if $x ne "";
178 return join " ", @ret;
182 my ($line, $width) = @_;
184 $len = (defined $width ? $width : 76);
185 while (length $line > $len) {
186 $line =~ /^(.{0,$len})\s(.*)$/ or $line =~ /^(.{$len,}?\s(.*)$/;
187 $result .= $1 . " \\\n\t\t";
191 return $result . $line;
195 my ($otmpl, $rtmpl) = @_;
197 foreach $i (sort keys %depends) {
198 if ($i =~ /^(.*)\.res/) {
200 ($x = $rtmpl) =~ s/X/$y/;
202 ($x = $otmpl) =~ s/X/$i/;
204 print &splitline(sprintf "%s: %s", $x, join " ", @{$depends{$i}}), "\n";
208 # Now we're ready to output the actual Makefiles.
211 open OUT, ">Makefile.cyg"; select OUT;
213 "# Makefile for PuTTY under cygwin.\n".
214 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
215 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
216 # gcc command line option is -D not /D
217 ($_ = $help) =~ s/=\/D/=-D/gs;
221 "# You can define this path to point at your tools if you need to\n".
222 "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
223 "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
224 "CC = \$(TOOLPATH)gcc\n".
225 "RC = \$(TOOLPATH)windres\n".
226 "# You may also need to tell windres where to find include files:\n".
227 "# RCINC = --include-dir c:\\cygwin\\include\\\n".
229 &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
230 " -DNO_SECURITY -D_NO_OLDNAMES -DNO_MULTIMON -I.")."\n".
231 "LDFLAGS = -mno-cygwin -s\n".
232 &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
233 " --define WINVER=0x0400 --define MINGW32_FIX=1")."\n".
238 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
241 "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
243 print &splitline("all:" . join "", map { " $_.exe" } @prognames);
245 foreach $p (@prognames) {
246 $objstr = &objects($p, "X.o", "X.res.o", undef);
247 print &splitline($p . ".exe: " . $objstr), "\n";
248 my $mw = $types{$p} eq "G" ? " -mwindows" : "";
249 $libstr = &objects($p, undef, undef, "-lX");
250 print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
251 $objstr . " $libstr", 69), "\n\n";
253 &deps("X.o", "X.res.o");
256 "version.o: FORCE;\n".
257 "# Hack to force version.o to be rebuilt always\n".
259 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
261 "\trm -f *.o *.exe *.res.o\n".
263 select STDOUT; close OUT;
265 ##-- Borland makefile
266 %stdlibs = ( # Borland provides many Win32 API libraries intrinsically
278 open OUT, ">Makefile.bor"; select OUT;
280 "# Makefile for PuTTY under Borland C.\n".
281 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
282 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
283 # bcc32 command line option is -D not /D
284 ($_ = $help) =~ s/=\/D/=-D/gs;
288 "# If you rename this file to `Makefile', you should change this line,\n".
289 "# so that the .rsp files still depend on the correct makefile.\n".
290 "MAKEFILE = Makefile.bor\n".
292 "# C compilation flags\n".
293 "CFLAGS = -DWINVER=0x0401\n".
295 "# Get include directory for resource compiler\n".
297 "BCB = \$(MAKEDIR)\\..\n".
301 &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
302 " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
304 &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
305 " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
307 print &splitline("all:" . join "", map { " $_.exe" } @prognames);
309 foreach $p (@prognames) {
310 $objstr = &objects($p, "X.obj", "X.res", undef);
311 print &splitline("$p.exe: " . $objstr . " $p.rsp"), "\n";
312 my $ap = ($types{$p} eq "G") ? "-aa" : "-ap";
313 print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$p.rsp\n\n";
315 foreach $p (@prognames) {
316 print $p, ".rsp: \$(MAKEFILE)\n";
317 $objstr = &objects($p, "X.obj", undef, undef);
318 @objlist = split " ", $objstr;
320 foreach $i (@objlist) {
321 if (length($objlines[$#objlines] . " $i") > 50) {
324 $objlines[$#objlines] .= " $i";
326 $c0w = ($types{$p} eq "G") ? "c0w32" : "c0x32";
327 print "\techo $c0w + > $p.rsp\n";
328 for ($i=0; $i<=$#objlines; $i++) {
329 $plus = ($i < $#objlines ? " +" : "");
330 print "\techo$objlines[$i]$plus >> $p.rsp\n";
332 print "\techo $p.exe >> $p.rsp\n";
333 $objstr = &objects($p, "X.obj", "X.res", undef);
334 @libs = split " ", &objects($p, undef, undef, "X");
335 @libs = grep { !$stdlibs{$_} } @libs;
336 unshift @libs, "cw32", "import32";
337 $libstr = join ' ', @libs;
338 print "\techo nul,$libstr, >> $p.rsp\n";
339 print "\techo " . &objects($p, undef, "X.res", undef) . " >> $p.rsp\n";
342 &deps("X.obj", "X.res");
345 "version.o: FORCE\n".
346 "# Hack to force version.o to be rebuilt always\n".
348 "\tbcc32 \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
359 "\t-del *.\$\$\$\$\$\$\n";
360 select STDOUT; close OUT;
362 ##-- Visual C++ makefile
363 open OUT, ">Makefile.vc"; select OUT;
365 "# Makefile for PuTTY under Visual C.\n".
366 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
367 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
371 "# If you rename this file to `Makefile', you should change this line,\n".
372 "# so that the .rsp files still depend on the correct makefile.\n".
373 "MAKEFILE = Makefile.vc\n".
375 "# C compilation flags\n".
376 "CFLAGS = /nologo /W3 /O1 /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401\n".
377 "LFLAGS = /incremental:no /fixed\n".
380 "\tcl \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) /c \$*.c\n".
382 "\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 -DWINVER=0x0400 \$*.rc\n".
384 print &splitline("all:" . join "", map { " $_.exe" } @prognames);
386 foreach $p (@prognames) {
387 $objstr = &objects($p, "X.obj", "X.res", undef);
388 print &splitline("$p.exe: " . $objstr . " $p.rsp"), "\n";
389 print "\tlink \$(LFLAGS) -out:$p.exe -map:$p.map \@$p.rsp\n\n";
391 foreach $p (@prognames) {
392 print $p, ".rsp: \$(MAKEFILE)\n";
393 $objstr = &objects($p, "X.obj", "X.res", "X.lib");
394 @objlist = split " ", $objstr;
396 foreach $i (@objlist) {
397 if (length($objlines[$#objlines] . " $i") > 50) {
400 $objlines[$#objlines] .= " $i";
402 $subsys = ($types{$p} eq "G") ? "windows" : "console";
403 print "\techo /nologo /subsystem:$subsys > $p.rsp\n";
404 for ($i=0; $i<=$#objlines; $i++) {
405 print "\techo$objlines[$i] >> $p.rsp\n";
409 &deps("X.obj", "X.res");
412 "# Hack to force version.o to be rebuilt always\n".
413 "version.obj: *.c *.h *.rc\n".
414 "\tcl \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
432 "\t-del debug.log\n";
433 select STDOUT; close OUT;