Richard's patch to fix `make clean' under Unix.
[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 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
20
21 while (<IN>) {
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
26 chomp;
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
53 } else {
54 die "$.: unrecognised line type\n";
55 }
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; }
62 } elsif (($i eq "[G]" or $i eq "[C]" or $i eq "[X]") and defined $prog) {
63 $types{$prog} = substr($i,1,1);
64 } else {
65 push @$listref, $i;
66 }
67 }
68 $lastlistref = $listref;
69 }
70
71 close IN;
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 = ();
79 foreach $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 = ();
120 while (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} = [];
125 $dirfile = &findfile($file);
126 open IN, "$dirfile" or die "unable to open source file $file\n";
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;
140 }
141
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.
145 foreach $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}};
160 }
161
162 # Utility routines while writing out the Makefiles.
163
164 sub 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
175 sub objects {
176 my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
177 my @ret;
178 my ($i, $x, $y);
179 @ret = ();
180 foreach $i (@{$programs{$prog}}) {
181 $x = "";
182 if ($i =~ /^(.*)\.res/) {
183 $y = $1;
184 ($x = $rtmpl) =~ s/X/$y/;
185 } elsif ($i =~ /^(.*)\.lib/) {
186 $y = $1;
187 ($x = $ltmpl) =~ s/X/$y/;
188 } else {
189 ($x = $otmpl) =~ s/X/$i/;
190 }
191 push @ret, $x if $x ne "";
192 }
193 return join " ", @ret;
194 }
195
196 sub 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
209 sub deps {
210 my ($otmpl, $rtmpl, $prefix, $dirsep) = @_;
211 my ($i, $x, $y);
212 my @deps;
213 foreach $i (sort keys %depends) {
214 if ($i =~ /^(.*)\.res/) {
215 next if !defined $rtmpl;
216 $y = $1;
217 ($x = $rtmpl) =~ s/X/$y/;
218 } else {
219 ($x = $otmpl) =~ s/X/$i/;
220 }
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";
228 }
229 }
230
231 sub 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
242 # Now we're ready to output the actual Makefiles.
243
244 ##-- CygWin makefile
245 open OUT, ">Makefile.cyg"; select OUT;
246 print
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";
250 # gcc command line option is -D not /D
251 ($_ = $help) =~ s/=\/D/=-D/gs;
252 print $_;
253 print
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".
263 &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
264 " -DNO_SECURITY -D_NO_OLDNAMES -DNO_MULTIMON -I.")."\n".
265 "LDFLAGS = -mno-cygwin -s\n".
266 &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
267 " --define WINVER=0x0400 --define MINGW32_FIX=1")."\n".
268 "\n".
269 ".SUFFIXES:\n".
270 "\n".
271 "%.o: %.c\n".
272 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
273 "\n".
274 "%.res.o: %.rc\n".
275 "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
276 "\n";
277 print &splitline("all:" . join "", map { " $_.exe" } &prognames("GC"));
278 print "\n\n";
279 foreach $p (&prognames("GC")) {
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";
286 }
287 &deps("X.o", "X.res.o", "", "/");
288 print
289 "\n".
290 "version.o: FORCE;\n".
291 "# Hack to force version.o to be rebuilt always\n".
292 "FORCE:\n".
293 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
294 "clean:\n".
295 "\trm -f *.o *.exe *.res.o\n".
296 "\n";
297 select STDOUT; close OUT;
298
299 ##-- Borland makefile
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 );
312 open OUT, ">Makefile.bor"; select OUT;
313 print
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";
317 # bcc32 command line option is -D not /D
318 ($_ = $help) =~ s/=\/D/=-D/gs;
319 print $_;
320 print
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".
326 "# C compilation flags\n".
327 "CFLAGS = -DWINVER=0x0401\n".
328 "\n".
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".
335 &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
336 " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
337 ".rc.res:\n".
338 &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
339 " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
340 "\n";
341 print &splitline("all:" . join "", map { " $_.exe" } &prognames("GC"));
342 print "\n\n";
343 foreach $p (&prognames("GC")) {
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";
348 }
349 foreach $p (&prognames("GC")) {
350 print $p, ".rsp: \$(MAKEFILE)\n";
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";
361 print "\techo $c0w + > $p.rsp\n";
362 for ($i=0; $i<=$#objlines; $i++) {
363 $plus = ($i < $#objlines ? " +" : "");
364 print "\techo$objlines[$i]$plus >> $p.rsp\n";
365 }
366 print "\techo $p.exe >> $p.rsp\n";
367 $objstr = &objects($p, "X.obj", "X.res", undef);
368 @libs = split " ", &objects($p, undef, undef, "X");
369 @libs = grep { !$stdlibs{$_} } @libs;
370 unshift @libs, "cw32", "import32";
371 $libstr = join ' ', @libs;
372 print "\techo nul,$libstr, >> $p.rsp\n";
373 print "\techo " . &objects($p, undef, "X.res", undef) . " >> $p.rsp\n";
374 print "\n";
375 }
376 &deps("X.obj", "X.res", "", "\\");
377 print
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".
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";
394 select STDOUT; close OUT;
395
396 ##-- Visual C++ makefile
397 open OUT, ">Makefile.vc"; select OUT;
398 print
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";
402 print $help;
403 print
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";
418 print &splitline("all:" . join "", map { " $_.exe" } &prognames("GC"));
419 print "\n\n";
420 foreach $p (&prognames("GC")) {
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 }
425 foreach $p (&prognames("GC")) {
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 }
443 &deps("X.obj", "X.res", "", "\\");
444 print
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";
467 select STDOUT; close OUT;
468
469 ##-- X/GTK/Unix makefile
470 open OUT, ">unix/Makefile.gtk"; select OUT;
471 print
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;
477 print $_;
478 print
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".
484 &splitline("CFLAGS = -Wall -g -I. -I.. `gtk-config --cflags`")."\n".
485 "LDFLAGS = `gtk-config --libs`\n".
486 "\n".
487 ".SUFFIXES:\n".
488 "\n".
489 "%.o:\n".
490 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
491 "\n";
492 print &splitline("all:" . join "", map { " $_" } &prognames("X"));
493 print "\n\n";
494 foreach $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, "../", "/");
502 print
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".
509 "\trm -f *.o". (join "", map { " $_" } &prognames("X")) . "\n".
510 "\n";
511 select STDOUT; close OUT;