Further deglobalisation: settings.c now has a more sensible interface.
[u/mdw/putty] / mkfiles.pl
CommitLineData
3d541627 1#!/usr/bin/env perl
2#
7603011f 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
f7f27309 10use FileHandle;
11
7603011f 12open IN, "Recipe" or die "unable to open Recipe file\n";
13
f7f27309 14@incdirs = ("", "unix/");
15
7603011f 16$help = ""; # list of newline-free lines of help text
c282cde3 17%programs = (); # maps prog name + type letter to listref of objects/resources
7603011f 18%groups = (); # maps group name to listref of objects/resources
3d541627 19
3d541627 20while (<IN>) {
7603011f 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;
24
3d541627 25 chomp;
7603011f 26 split;
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; }
31 # Ignore blank lines.
32 next if scalar @_ == 0;
33
34 # Now we have an ordinary line. See if it's an = line, a : line
35 # or a + line.
36 @objs = @_;
37
38 if ($_[0] eq "+") {
39 $listref = $lastlistref;
40 $prog = undef;
41 die "$.: unexpected + line\n" if !defined $lastlistref;
42 } elsif ($_[1] eq "=") {
43 $groups{$_[0]} = [] if !defined $groups{$_[0]};
44 $listref = $groups{$_[0]};
45 $prog = undef;
46 shift @objs; # eat the group name
47 } elsif ($_[1] eq ":") {
c282cde3 48 $listref = [];
7603011f 49 $prog = $_[0];
50 shift @objs; # eat the program name
3d541627 51 } else {
7603011f 52 die "$.: unrecognised line type\n";
3d541627 53 }
7603011f 54 shift @objs; # eat the +, the = or the :
55
56 while (scalar @objs > 0) {
57 $i = shift @objs;
58 if ($groups{$i}) {
59 foreach $j (@{$groups{$i}}) { unshift @objs, $j; }
f7f27309 60 } elsif (($i eq "[G]" or $i eq "[C]" or $i eq "[X]") and defined $prog) {
c282cde3 61 $type = substr($i,1,1);
7603011f 62 } else {
63 push @$listref, $i;
64 }
65 }
c282cde3 66 if ($prog and $type) {
67 die "multiple program entries for $prog [$type]\n"
68 if defined $programs{$prog . "," . $type};
69 $programs{$prog . "," . $type} = $listref;
70 }
7603011f 71 $lastlistref = $listref;
3d541627 72}
7603011f 73
3d541627 74close IN;
7603011f 75
76# Now retrieve the complete list of objects and resource files, and
77# construct dependency data for them. While we're here, expand the
78# object list for each program, and complain if its type isn't set.
79@prognames = sort keys %programs;
80%depends = ();
81@scanlist = ();
82foreach $i (@prognames) {
c282cde3 83 ($prog, $type) = split ",", $i;
7603011f 84 # Strip duplicate object names.
85 $prev = undef;
86 @list = grep { $status = ($prev ne $_); $prev=$_; $status }
87 sort @{$programs{$i}};
88 $programs{$i} = [@list];
89 foreach $j (@list) {
90 # Dependencies for "x" start with "x.c".
91 # Dependencies for "x.res" start with "x.rc".
92 # Both types of file are pushed on the list of files to scan.
93 # Libraries (.lib) don't have dependencies at all.
94 if ($j =~ /^(.*)\.res$/) {
95 $file = "$1.rc";
96 $depends{$j} = [$file];
97 push @scanlist, $file;
98 } elsif ($j =~ /\.lib$/) {
99 # libraries don't have dependencies
100 } else {
101 $file = "$j.c";
102 $depends{$j} = [$file];
103 push @scanlist, $file;
104 }
105 }
106}
107
108# Scan each file on @scanlist and find further inclusions.
109# Inclusions are given by lines of the form `#include "otherfile"'
110# (system headers are automatically ignored by this because they'll
111# be given in angle brackets). Files included by this method are
112# added back on to @scanlist to be scanned in turn (if not already
113# done).
114#
115# Resource scripts (.rc) can also include a file by means of a line
116# ending `ICON "filename"'. Files included by this method are not
117# added to @scanlist because they can never include further files.
118#
119# In this pass we write out a hash %further which maps a source
120# file name into a listref containing further source file names.
121
122%further = ();
123while (scalar @scanlist > 0) {
124 $file = shift @scanlist;
125 next if defined $further{$file}; # skip if we've already done it
126 $resource = ($file =~ /\.rc$/ ? 1 : 0);
127 $further{$file} = [];
f7f27309 128 $dirfile = &findfile($file);
129 open IN, "$dirfile" or die "unable to open source file $file\n";
7603011f 130 while (<IN>) {
131 chomp;
132 /^\s*#include\s+\"([^\"]+)\"/ and do {
133 push @{$further{$file}}, $1;
134 push @scanlist, $1;
135 next;
136 };
137 /ICON\s+\"([^\"]+)\"\s*$/ and do {
138 push @{$further{$file}}, $1;
139 next;
140 }
141 }
142 close IN;
3d541627 143}
144
7603011f 145# Now we're ready to generate the final dependencies section. For
146# each key in %depends, we must expand the dependencies list by
147# iteratively adding entries from %further.
148foreach $i (keys %depends) {
149 %dep = ();
150 @scanlist = @{$depends{$i}};
151 foreach $i (@scanlist) { $dep{$i} = 1; }
152 while (scalar @scanlist > 0) {
153 $file = shift @scanlist;
154 foreach $j (@{$further{$file}}) {
155 if ($dep{$j} != 1) {
156 $dep{$j} = 1;
157 push @{$depends{$i}}, $j;
158 push @scanlist, $j;
159 }
160 }
161 }
162# printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
3d541627 163}
164
7603011f 165# Utility routines while writing out the Makefiles.
166
f7f27309 167sub findfile {
168 my ($name) = @_;
169 my $dir, $i, $outdir = "";
170 $i = 0;
171 foreach $dir (@incdirs) {
172 $outdir = $dir, $i++ if -f "$dir$name";
173 }
174 die "multiple instances of source file $name\n" if $i > 1;
175 return "$outdir$name";
176}
177
7603011f 178sub objects {
f7f27309 179 my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
7603011f 180 my @ret;
181 my ($i, $x, $y);
182 @ret = ();
183 foreach $i (@{$programs{$prog}}) {
f7f27309 184 $x = "";
7603011f 185 if ($i =~ /^(.*)\.res/) {
186 $y = $1;
187 ($x = $rtmpl) =~ s/X/$y/;
7603011f 188 } elsif ($i =~ /^(.*)\.lib/) {
189 $y = $1;
190 ($x = $ltmpl) =~ s/X/$y/;
7603011f 191 } else {
192 ($x = $otmpl) =~ s/X/$i/;
7603011f 193 }
f7f27309 194 push @ret, $x if $x ne "";
7603011f 195 }
196 return join " ", @ret;
3d541627 197}
198
7603011f 199sub splitline {
200 my ($line, $width) = @_;
201 my ($result, $len);
202 $len = (defined $width ? $width : 76);
203 while (length $line > $len) {
204 $line =~ /^(.{0,$len})\s(.*)$/ or $line =~ /^(.{$len,}?\s(.*)$/;
205 $result .= $1 . " \\\n\t\t";
206 $line = $2;
207 $len = 60;
208 }
209 return $result . $line;
210}
211
212sub deps {
f7f27309 213 my ($otmpl, $rtmpl, $prefix, $dirsep) = @_;
7603011f 214 my ($i, $x, $y);
f7f27309 215 my @deps;
7603011f 216 foreach $i (sort keys %depends) {
217 if ($i =~ /^(.*)\.res/) {
f7f27309 218 next if !defined $rtmpl;
7603011f 219 $y = $1;
220 ($x = $rtmpl) =~ s/X/$y/;
221 } else {
222 ($x = $otmpl) =~ s/X/$i/;
223 }
f7f27309 224 @deps = @{$depends{$i}};
225 @deps = map {
226 $_ = &findfile($_);
227 s/\//$dirsep/g;
228 $_ = $prefix . $_;
229 } @deps;
230 print &splitline(sprintf "%s: %s", $x, join " ", @deps), "\n";
7603011f 231 }
3d541627 232}
233
f7f27309 234sub prognames {
235 my ($types) = @_;
c282cde3 236 my ($n, $prog, $type);
237 my @ret;
238 @ret = ();
239 foreach $n (@prognames) {
240 ($prog, $type) = split ",", $n;
241 push @ret, $n if index($types, $type) >= 0;
242 }
243 return @ret;
244}
245
246sub progrealnames {
247 my ($types) = @_;
248 my ($n, $prog, $type);
f7f27309 249 my @ret;
250 @ret = ();
251 foreach $n (@prognames) {
c282cde3 252 ($prog, $type) = split ",", $n;
253 push @ret, $prog if index($types, $type) >= 0;
f7f27309 254 }
255 return @ret;
256}
257
7603011f 258# Now we're ready to output the actual Makefiles.
259
3d541627 260##-- CygWin makefile
261open OUT, ">Makefile.cyg"; select OUT;
262print
7603011f 263"# Makefile for PuTTY under cygwin.\n".
264"#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
265"# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
3d541627 266# gcc command line option is -D not /D
7603011f 267($_ = $help) =~ s/=\/D/=-D/gs;
3d541627 268print $_;
269print
270"\n".
271"# You can define this path to point at your tools if you need to\n".
272"# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
273"# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
274"CC = \$(TOOLPATH)gcc\n".
275"RC = \$(TOOLPATH)windres\n".
276"# You may also need to tell windres where to find include files:\n".
277"# RCINC = --include-dir c:\\cygwin\\include\\\n".
278"\n".
7603011f 279&splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
280 " -DNO_SECURITY -D_NO_OLDNAMES -DNO_MULTIMON -I.")."\n".
f730b86a 281"LDFLAGS = -mno-cygwin -s\n".
7603011f 282&splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
283 " --define WINVER=0x0400 --define MINGW32_FIX=1")."\n".
3d541627 284"\n".
285".SUFFIXES:\n".
286"\n".
201e9294 287"%.o: %.c\n".
cf03419b 288"\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
3d541627 289"\n".
f730b86a 290"%.res.o: %.rc\n".
6ed3635b 291"\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
3d541627 292"\n";
c282cde3 293print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
14232990 294print "\n\n";
f7f27309 295foreach $p (&prognames("GC")) {
c282cde3 296 ($prog, $type) = split ",", $p;
7603011f 297 $objstr = &objects($p, "X.o", "X.res.o", undef);
c282cde3 298 print &splitline($prog . ".exe: " . $objstr), "\n";
299 my $mw = $type eq "G" ? " -mwindows" : "";
7603011f 300 $libstr = &objects($p, undef, undef, "-lX");
301 print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
302 $objstr . " $libstr", 69), "\n\n";
3d541627 303}
8fd3bdc4 304&deps("X.o", "X.res.o", "", "/");
3d541627 305print
306"\n".
74091dff 307"version.o: FORCE;\n".
3d541627 308"# Hack to force version.o to be rebuilt always\n".
309"FORCE:\n".
7603011f 310"\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
3d541627 311"clean:\n".
7603011f 312"\trm -f *.o *.exe *.res.o\n".
3d541627 313"\n";
314select STDOUT; close OUT;
315
316##-- Borland makefile
7603011f 317%stdlibs = ( # Borland provides many Win32 API libraries intrinsically
318 "advapi32" => 1,
319 "comctl32" => 1,
320 "comdlg32" => 1,
321 "gdi32" => 1,
322 "imm32" => 1,
323 "shell32" => 1,
324 "user32" => 1,
325 "winmm" => 1,
326 "winspool" => 1,
327 "wsock32" => 1,
328);
3d541627 329open OUT, ">Makefile.bor"; select OUT;
330print
7603011f 331"# Makefile for PuTTY under Borland C.\n".
332"#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
333"# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
3d541627 334# bcc32 command line option is -D not /D
7603011f 335($_ = $help) =~ s/=\/D/=-D/gs;
3d541627 336print $_;
337print
338"\n".
339"# If you rename this file to `Makefile', you should change this line,\n".
340"# so that the .rsp files still depend on the correct makefile.\n".
341"MAKEFILE = Makefile.bor\n".
342"\n".
a401e5f3 343"# C compilation flags\n".
344"CFLAGS = -DWINVER=0x0401\n".
345"\n".
3d541627 346"# Get include directory for resource compiler\n".
347"!if !\$d(BCB)\n".
348"BCB = \$(MAKEDIR)\\..\n".
349"!endif\n".
350"\n".
351".c.obj:\n".
8aa5bfd9 352&splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
7603011f 353 " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
3d541627 354".rc.res:\n".
7603011f 355&splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
356 " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
3d541627 357"\n";
c282cde3 358print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
3d541627 359print "\n\n";
f7f27309 360foreach $p (&prognames("GC")) {
c282cde3 361 ($prog, $type) = split ",", $p;
7603011f 362 $objstr = &objects($p, "X.obj", "X.res", undef);
c282cde3 363 print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
364 my $ap = ($type eq "G") ? "-aa" : "-ap";
365 print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
3d541627 366}
f7f27309 367foreach $p (&prognames("GC")) {
c282cde3 368 ($prog, $type) = split ",", $p;
369 print $prog, ".rsp: \$(MAKEFILE)\n";
7603011f 370 $objstr = &objects($p, "X.obj", undef, undef);
371 @objlist = split " ", $objstr;
372 @objlines = ("");
373 foreach $i (@objlist) {
374 if (length($objlines[$#objlines] . " $i") > 50) {
375 push @objlines, "";
376 }
377 $objlines[$#objlines] .= " $i";
378 }
c282cde3 379 $c0w = ($type eq "G") ? "c0w32" : "c0x32";
380 print "\techo $c0w + > $prog.rsp\n";
a7088bde 381 for ($i=0; $i<=$#objlines; $i++) {
382 $plus = ($i < $#objlines ? " +" : "");
c282cde3 383 print "\techo$objlines[$i]$plus >> $prog.rsp\n";
3d541627 384 }
c282cde3 385 print "\techo $prog.exe >> $prog.rsp\n";
7603011f 386 $objstr = &objects($p, "X.obj", "X.res", undef);
387 @libs = split " ", &objects($p, undef, undef, "X");
388 @libs = grep { !$stdlibs{$_} } @libs;
6d2d5e8d 389 unshift @libs, "cw32", "import32";
390 $libstr = join ' ', @libs;
c282cde3 391 print "\techo nul,$libstr, >> $prog.rsp\n";
392 print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
3d541627 393 print "\n";
394}
f7f27309 395&deps("X.obj", "X.res", "", "\\");
3d541627 396print
397"\n".
398"version.o: FORCE\n".
399"# Hack to force version.o to be rebuilt always\n".
400"FORCE:\n".
401"\tbcc32 \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
402"clean:\n".
2e792012 403"\t-del *.obj\n".
404"\t-del *.exe\n".
405"\t-del *.res\n".
406"\t-del *.pch\n".
407"\t-del *.aps\n".
408"\t-del *.il*\n".
409"\t-del *.pdb\n".
410"\t-del *.rsp\n".
411"\t-del *.tds\n".
412"\t-del *.\$\$\$\$\$\$\n";
3d541627 413select STDOUT; close OUT;
7603011f 414
415##-- Visual C++ makefile
416open OUT, ">Makefile.vc"; select OUT;
417print
418"# Makefile for PuTTY under Visual C.\n".
419"#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
420"# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
421print $help;
422print
423"\n".
424"# If you rename this file to `Makefile', you should change this line,\n".
425"# so that the .rsp files still depend on the correct makefile.\n".
426"MAKEFILE = Makefile.vc\n".
427"\n".
428"# C compilation flags\n".
429"CFLAGS = /nologo /W3 /O1 /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401\n".
430"LFLAGS = /incremental:no /fixed\n".
431"\n".
432".c.obj:\n".
433"\tcl \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) /c \$*.c\n".
434".rc.res:\n".
435"\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 -DWINVER=0x0400 \$*.rc\n".
436"\n";
c282cde3 437print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
7603011f 438print "\n\n";
f7f27309 439foreach $p (&prognames("GC")) {
c282cde3 440 ($prog, $type) = split ",", $p;
7603011f 441 $objstr = &objects($p, "X.obj", "X.res", undef);
c282cde3 442 print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
443 print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
7603011f 444}
f7f27309 445foreach $p (&prognames("GC")) {
c282cde3 446 ($prog, $type) = split ",", $p;
447 print $prog, ".rsp: \$(MAKEFILE)\n";
7603011f 448 $objstr = &objects($p, "X.obj", "X.res", "X.lib");
449 @objlist = split " ", $objstr;
450 @objlines = ("");
451 foreach $i (@objlist) {
452 if (length($objlines[$#objlines] . " $i") > 50) {
453 push @objlines, "";
454 }
455 $objlines[$#objlines] .= " $i";
456 }
c282cde3 457 $subsys = ($type eq "G") ? "windows" : "console";
458 print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
7603011f 459 for ($i=0; $i<=$#objlines; $i++) {
c282cde3 460 print "\techo$objlines[$i] >> $prog.rsp\n";
7603011f 461 }
462 print "\n";
463}
f7f27309 464&deps("X.obj", "X.res", "", "\\");
7603011f 465print
466"\n".
467"# Hack to force version.o to be rebuilt always\n".
468"version.obj: *.c *.h *.rc\n".
469"\tcl \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
470"clean: tidy\n".
471"\t-del *.exe\n\n".
472"tidy:\n".
473"\t-del *.obj\n".
474"\t-del *.res\n".
475"\t-del *.pch\n".
476"\t-del *.aps\n".
477"\t-del *.ilk\n".
478"\t-del *.pdb\n".
479"\t-del *.rsp\n".
480"\t-del *.dsp\n".
481"\t-del *.dsw\n".
482"\t-del *.ncb\n".
483"\t-del *.opt\n".
484"\t-del *.plg\n".
485"\t-del *.map\n".
486"\t-del *.idb\n".
487"\t-del debug.log\n";
488select STDOUT; close OUT;
f7f27309 489
490##-- X/GTK/Unix makefile
491open OUT, ">unix/Makefile.gtk"; select OUT;
492print
493"# Makefile for PuTTY under X/GTK and Unix.\n".
494"#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
495"# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
496# gcc command line option is -D not /D
497($_ = $help) =~ s/=\/D/=-D/gs;
498print $_;
499print
500"\n".
501"# You can define this path to point at your tools if you need to\n".
502"# TOOLPATH = /opt/gcc/bin\n".
503"CC = \$(TOOLPATH)cc\n".
504"\n".
1709795f 505&splitline("CFLAGS = -Wall -g -I. -I.. `gtk-config --cflags`")."\n".
506"LDFLAGS = `gtk-config --libs`\n".
f7f27309 507"\n".
508".SUFFIXES:\n".
509"\n".
1709795f 510"%.o:\n".
f7f27309 511"\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
512"\n";
c282cde3 513print &splitline("all:" . join "", map { " $_" } &progrealnames("X"));
f7f27309 514print "\n\n";
515foreach $p (&prognames("X")) {
c282cde3 516 ($prog, $type) = split ",", $p;
f7f27309 517 $objstr = &objects($p, "X.o", undef, undef);
c282cde3 518 print &splitline($prog . ": " . $objstr), "\n";
f7f27309 519 $libstr = &objects($p, undef, undef, "-lX");
520 print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
521 $objstr . " $libstr", 69), "\n\n";
522}
523&deps("X.o", undef, "../", "/");
524print
525"\n".
526"version.o: FORCE;\n".
527"# Hack to force version.o to be rebuilt always\n".
528"FORCE:\n".
c282cde3 529"\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c ../version.c\n".
f7f27309 530"clean:\n".
c282cde3 531"\trm -f *.o". (join "", map { " $_" } &progrealnames("X")) . "\n".
f7f27309 532"\n";
533select STDOUT; close OUT;