Preparatory work before attempting a Unix port of plink: mkfiles.pl
[u/mdw/putty] / 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 die "unable to open Recipe file\n";
13
14 @incdirs = ("", "unix/");
15
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
19
20 while (<IN>) {
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
25 chomp;
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 ":") {
48 $listref = [];
49 $prog = $_[0];
50 shift @objs; # eat the program name
51 } else {
52 die "$.: unrecognised line type\n";
53 }
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; }
60 } elsif (($i eq "[G]" or $i eq "[C]" or $i eq "[X]") and defined $prog) {
61 $type = substr($i,1,1);
62 } else {
63 push @$listref, $i;
64 }
65 }
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 }
71 $lastlistref = $listref;
72 }
73
74 close IN;
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 = ();
82 foreach $i (@prognames) {
83 ($prog, $type) = split ",", $i;
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 = ();
123 while (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} = [];
128 $dirfile = &findfile($file);
129 open IN, "$dirfile" or die "unable to open source file $file\n";
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;
143 }
144
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.
148 foreach $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}};
163 }
164
165 # Utility routines while writing out the Makefiles.
166
167 sub 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
178 sub objects {
179 my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
180 my @ret;
181 my ($i, $x, $y);
182 @ret = ();
183 foreach $i (@{$programs{$prog}}) {
184 $x = "";
185 if ($i =~ /^(.*)\.res/) {
186 $y = $1;
187 ($x = $rtmpl) =~ s/X/$y/;
188 } elsif ($i =~ /^(.*)\.lib/) {
189 $y = $1;
190 ($x = $ltmpl) =~ s/X/$y/;
191 } else {
192 ($x = $otmpl) =~ s/X/$i/;
193 }
194 push @ret, $x if $x ne "";
195 }
196 return join " ", @ret;
197 }
198
199 sub 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
212 sub deps {
213 my ($otmpl, $rtmpl, $prefix, $dirsep) = @_;
214 my ($i, $x, $y);
215 my @deps;
216 foreach $i (sort keys %depends) {
217 if ($i =~ /^(.*)\.res/) {
218 next if !defined $rtmpl;
219 $y = $1;
220 ($x = $rtmpl) =~ s/X/$y/;
221 } else {
222 ($x = $otmpl) =~ s/X/$i/;
223 }
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";
231 }
232 }
233
234 sub prognames {
235 my ($types) = @_;
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
246 sub progrealnames {
247 my ($types) = @_;
248 my ($n, $prog, $type);
249 my @ret;
250 @ret = ();
251 foreach $n (@prognames) {
252 ($prog, $type) = split ",", $n;
253 push @ret, $prog if index($types, $type) >= 0;
254 }
255 return @ret;
256 }
257
258 # Now we're ready to output the actual Makefiles.
259
260 ##-- CygWin makefile
261 open OUT, ">Makefile.cyg"; select OUT;
262 print
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";
266 # gcc command line option is -D not /D
267 ($_ = $help) =~ s/=\/D/=-D/gs;
268 print $_;
269 print
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".
279 &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
280 " -DNO_SECURITY -D_NO_OLDNAMES -DNO_MULTIMON -I.")."\n".
281 "LDFLAGS = -mno-cygwin -s\n".
282 &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
283 " --define WINVER=0x0400 --define MINGW32_FIX=1")."\n".
284 "\n".
285 ".SUFFIXES:\n".
286 "\n".
287 "%.o: %.c\n".
288 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
289 "\n".
290 "%.res.o: %.rc\n".
291 "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
292 "\n";
293 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
294 print "\n\n";
295 foreach $p (&prognames("GC")) {
296 ($prog, $type) = split ",", $p;
297 $objstr = &objects($p, "X.o", "X.res.o", undef);
298 print &splitline($prog . ".exe: " . $objstr), "\n";
299 my $mw = $type eq "G" ? " -mwindows" : "";
300 $libstr = &objects($p, undef, undef, "-lX");
301 print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
302 $objstr . " $libstr", 69), "\n\n";
303 }
304 &deps("X.o", "X.res.o", "", "/");
305 print
306 "\n".
307 "version.o: FORCE;\n".
308 "# Hack to force version.o to be rebuilt always\n".
309 "FORCE:\n".
310 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
311 "clean:\n".
312 "\trm -f *.o *.exe *.res.o\n".
313 "\n";
314 select STDOUT; close OUT;
315
316 ##-- Borland makefile
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 );
329 open OUT, ">Makefile.bor"; select OUT;
330 print
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";
334 # bcc32 command line option is -D not /D
335 ($_ = $help) =~ s/=\/D/=-D/gs;
336 print $_;
337 print
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".
343 "# C compilation flags\n".
344 "CFLAGS = -DWINVER=0x0401\n".
345 "\n".
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".
352 &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
353 " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
354 ".rc.res:\n".
355 &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
356 " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
357 "\n";
358 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
359 print "\n\n";
360 foreach $p (&prognames("GC")) {
361 ($prog, $type) = split ",", $p;
362 $objstr = &objects($p, "X.obj", "X.res", undef);
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";
366 }
367 foreach $p (&prognames("GC")) {
368 ($prog, $type) = split ",", $p;
369 print $prog, ".rsp: \$(MAKEFILE)\n";
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 }
379 $c0w = ($type eq "G") ? "c0w32" : "c0x32";
380 print "\techo $c0w + > $prog.rsp\n";
381 for ($i=0; $i<=$#objlines; $i++) {
382 $plus = ($i < $#objlines ? " +" : "");
383 print "\techo$objlines[$i]$plus >> $prog.rsp\n";
384 }
385 print "\techo $prog.exe >> $prog.rsp\n";
386 $objstr = &objects($p, "X.obj", "X.res", undef);
387 @libs = split " ", &objects($p, undef, undef, "X");
388 @libs = grep { !$stdlibs{$_} } @libs;
389 unshift @libs, "cw32", "import32";
390 $libstr = join ' ', @libs;
391 print "\techo nul,$libstr, >> $prog.rsp\n";
392 print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
393 print "\n";
394 }
395 &deps("X.obj", "X.res", "", "\\");
396 print
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".
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";
413 select STDOUT; close OUT;
414
415 ##-- Visual C++ makefile
416 open OUT, ">Makefile.vc"; select OUT;
417 print
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";
421 print $help;
422 print
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";
437 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
438 print "\n\n";
439 foreach $p (&prognames("GC")) {
440 ($prog, $type) = split ",", $p;
441 $objstr = &objects($p, "X.obj", "X.res", undef);
442 print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
443 print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
444 }
445 foreach $p (&prognames("GC")) {
446 ($prog, $type) = split ",", $p;
447 print $prog, ".rsp: \$(MAKEFILE)\n";
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 }
457 $subsys = ($type eq "G") ? "windows" : "console";
458 print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
459 for ($i=0; $i<=$#objlines; $i++) {
460 print "\techo$objlines[$i] >> $prog.rsp\n";
461 }
462 print "\n";
463 }
464 &deps("X.obj", "X.res", "", "\\");
465 print
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";
488 select STDOUT; close OUT;
489
490 ##-- X/GTK/Unix makefile
491 open OUT, ">unix/Makefile.gtk"; select OUT;
492 print
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;
498 print $_;
499 print
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".
505 &splitline("CFLAGS = -Wall -g -I. -I.. `gtk-config --cflags`")."\n".
506 "LDFLAGS = `gtk-config --libs`\n".
507 "\n".
508 ".SUFFIXES:\n".
509 "\n".
510 "%.o:\n".
511 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
512 "\n";
513 print &splitline("all:" . join "", map { " $_" } &progrealnames("X"));
514 print "\n\n";
515 foreach $p (&prognames("X")) {
516 ($prog, $type) = split ",", $p;
517 $objstr = &objects($p, "X.o", undef, undef);
518 print &splitline($prog . ": " . $objstr), "\n";
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, "../", "/");
524 print
525 "\n".
526 "version.o: FORCE;\n".
527 "# Hack to force version.o to be rebuilt always\n".
528 "FORCE:\n".
529 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c ../version.c\n".
530 "clean:\n".
531 "\trm -f *.o". (join "", map { " $_" } &progrealnames("X")) . "\n".
532 "\n";
533 select STDOUT; close OUT;