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