]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mkfiles.pl
Move MODULE files out of individual project directories into a
[PuTTY.git] / 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 open IN, "Recipe" or die "unable to open Recipe file\n";
11
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
16
17 while (<IN>) {
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;
21
22   chomp;
23   split;
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; }
28   # Ignore blank lines.
29   next if scalar @_ == 0;
30
31   # Now we have an ordinary line. See if it's an = line, a : line
32   # or a + line.
33   @objs = @_;
34
35   if ($_[0] eq "+") {
36     $listref = $lastlistref;
37     $prog = undef;
38     die "$.: unexpected + line\n" if !defined $lastlistref;
39   } elsif ($_[1] eq "=") {
40     $groups{$_[0]} = [] if !defined $groups{$_[0]};
41     $listref = $groups{$_[0]};
42     $prog = undef;
43     shift @objs; # eat the group name
44   } elsif ($_[1] eq ":") {
45     $programs{$_[0]} = [] if !defined $programs{$_[0]};
46     $listref = $programs{$_[0]};
47     $prog = $_[0];
48     shift @objs; # eat the program name
49   } else {
50     die "$.: unrecognised line type\n";
51   }
52   shift @objs; # eat the +, the = or the :
53
54   while (scalar @objs > 0) {
55     $i = shift @objs;
56     if ($groups{$i}) {
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);
60     } else {
61       push @$listref, $i;
62     }
63   }
64   $lastlistref = $listref;
65 }
66
67 close IN;
68
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;
73 %depends = ();
74 @scanlist = ();
75 foreach $i (@prognames) {
76   if (!defined $types{$i}) { die "type not set for program $i\n"; }
77   # Strip duplicate object names.
78   $prev = undef;
79   @list = grep { $status = ($prev ne $_); $prev=$_; $status }
80           sort @{$programs{$i}};
81   $programs{$i} = [@list];
82   foreach $j (@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$/) {
88       $file = "$1.rc";
89       $depends{$j} = [$file];
90       push @scanlist, $file;
91     } elsif ($j =~ /\.lib$/) {
92       # libraries don't have dependencies
93     } else {
94       $file = "$j.c";
95       $depends{$j} = [$file];
96       push @scanlist, $file;
97     }
98   }
99 }
100
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
106 # done).
107 #
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.
111 #
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.
114
115 %further = ();
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";
122   while (<IN>) {
123     chomp;
124     /^\s*#include\s+\"([^\"]+)\"/ and do {
125       push @{$further{$file}}, $1;
126       push @scanlist, $1;
127       next;
128     };
129     /ICON\s+\"([^\"]+)\"\s*$/ and do {
130       push @{$further{$file}}, $1;
131       next;
132     }
133   }
134   close IN;
135 }
136
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) {
141   %dep = ();
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}}) {
147       if ($dep{$j} != 1) {
148         $dep{$j} = 1;
149         push @{$depends{$i}}, $j;
150         push @scanlist, $j;
151       }
152     }
153   }
154 #  printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
155 }
156
157 # Utility routines while writing out the Makefiles.
158
159 sub objects {
160   my ($prog, $otmpl, $rtmpl, $ltmpl) = @_;
161   my @ret;
162   my ($i, $x, $y);
163   @ret = ();
164   foreach $i (@{$programs{$prog}}) {
165     if ($i =~ /^(.*)\.res/) {
166       $y = $1;
167       ($x = $rtmpl) =~ s/X/$y/;
168       push @ret, $x if $x ne "";
169     } elsif ($i =~ /^(.*)\.lib/) {
170       $y = $1;
171       ($x = $ltmpl) =~ s/X/$y/;
172       push @ret, $x if $x ne "";
173     } else {
174       ($x = $otmpl) =~ s/X/$i/;
175       push @ret, $x if $x ne "";
176     }
177   }
178   return join " ", @ret;
179 }
180
181 sub splitline {
182   my ($line, $width) = @_;
183   my ($result, $len);
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";
188     $line = $2;
189     $len = 60;
190   }
191   return $result . $line;
192 }
193
194 sub deps {
195   my ($otmpl, $rtmpl) = @_;
196   my ($i, $x, $y);
197   foreach $i (sort keys %depends) {
198     if ($i =~ /^(.*)\.res/) {
199       $y = $1;
200       ($x = $rtmpl) =~ s/X/$y/;
201     } else {
202       ($x = $otmpl) =~ s/X/$i/;
203     }
204     print &splitline(sprintf "%s: %s", $x, join " ", @{$depends{$i}}), "\n";
205   }
206 }
207
208 # Now we're ready to output the actual Makefiles.
209
210 ##-- CygWin makefile
211 open OUT, ">Makefile.cyg"; select OUT;
212 print
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;
218 print $_;
219 print
220 "\n".
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".
228 "\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".
234 "\n".
235 ".SUFFIXES:\n".
236 "\n".
237 "%.o: %.c\n".
238 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
239 "\n".
240 "%.res.o: %.rc\n".
241 "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
242 "\n";
243 print &splitline("all:" . join "", map { " $_.exe" } @prognames);
244 print "\n\n";
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";
252 }
253 &deps("X.o", "X.res.o");
254 print
255 "\n".
256 "version.o: FORCE;\n".
257 "# Hack to force version.o to be rebuilt always\n".
258 "FORCE:\n".
259 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
260 "clean:\n".
261 "\trm -f *.o *.exe *.res.o\n".
262 "\n";
263 select STDOUT; close OUT;
264
265 ##-- Borland makefile
266 %stdlibs = (  # Borland provides many Win32 API libraries intrinsically
267   "advapi32" => 1,
268   "comctl32" => 1,
269   "comdlg32" => 1,
270   "gdi32" => 1,
271   "imm32" => 1,
272   "shell32" => 1,
273   "user32" => 1,
274   "winmm" => 1,
275   "winspool" => 1,
276   "wsock32" => 1,
277 );          
278 open OUT, ">Makefile.bor"; select OUT;
279 print
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;
285 print $_;
286 print
287 "\n".
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".
291 "\n".
292 "# C compilation flags\n".
293 "CFLAGS = -DWINVER=0x0401\n".
294 "\n".
295 "# Get include directory for resource compiler\n".
296 "!if !\$d(BCB)\n".
297 "BCB = \$(MAKEDIR)\\..\n".
298 "!endif\n".
299 "\n".
300 ".c.obj:\n".
301 &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
302   " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
303 ".rc.res:\n".
304 &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
305   " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
306 "\n";
307 print &splitline("all:" . join "", map { " $_.exe" } @prognames);
308 print "\n\n";
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";
314 }
315 foreach $p (@prognames) {
316   print $p, ".rsp: \$(MAKEFILE)\n";
317   $objstr = &objects($p, "X.obj", undef, undef);
318   @objlist = split " ", $objstr;
319   @objlines = ("");
320   foreach $i (@objlist) {
321     if (length($objlines[$#objlines] . " $i") > 50) {
322       push @objlines, "";
323     }
324     $objlines[$#objlines] .= " $i";
325   }
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";
331   }
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";
340   print "\n";
341 }
342 &deps("X.obj", "X.res");
343 print
344 "\n".
345 "version.o: FORCE\n".
346 "# Hack to force version.o to be rebuilt always\n".
347 "FORCE:\n".
348 "\tbcc32 \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
349 "clean:\n".
350 "\t-del *.obj\n".
351 "\t-del *.exe\n".
352 "\t-del *.res\n".
353 "\t-del *.pch\n".
354 "\t-del *.aps\n".
355 "\t-del *.il*\n".
356 "\t-del *.pdb\n".
357 "\t-del *.rsp\n".
358 "\t-del *.tds\n".
359 "\t-del *.\$\$\$\$\$\$\n";
360 select STDOUT; close OUT;
361
362 ##-- Visual C++ makefile
363 open OUT, ">Makefile.vc"; select OUT;
364 print
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";
368 print $help;
369 print
370 "\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".
374 "\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".
378 "\n".
379 ".c.obj:\n".
380 "\tcl \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) /c \$*.c\n".
381 ".rc.res:\n".
382 "\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 -DWINVER=0x0400 \$*.rc\n".
383 "\n";
384 print &splitline("all:" . join "", map { " $_.exe" } @prognames);
385 print "\n\n";
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";
390 }
391 foreach $p (@prognames) {
392   print $p, ".rsp: \$(MAKEFILE)\n";
393   $objstr = &objects($p, "X.obj", "X.res", "X.lib");
394   @objlist = split " ", $objstr;
395   @objlines = ("");
396   foreach $i (@objlist) {
397     if (length($objlines[$#objlines] . " $i") > 50) {
398       push @objlines, "";
399     }
400     $objlines[$#objlines] .= " $i";
401   }
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";
406   }
407   print "\n";
408 }
409 &deps("X.obj", "X.res");
410 print
411 "\n".
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".
415 "clean: tidy\n".
416 "\t-del *.exe\n\n".
417 "tidy:\n".
418 "\t-del *.obj\n".
419 "\t-del *.res\n".
420 "\t-del *.pch\n".
421 "\t-del *.aps\n".
422 "\t-del *.ilk\n".
423 "\t-del *.pdb\n".
424 "\t-del *.rsp\n".
425 "\t-del *.dsp\n".
426 "\t-del *.dsw\n".
427 "\t-del *.ncb\n".
428 "\t-del *.opt\n".
429 "\t-del *.plg\n".
430 "\t-del *.map\n".
431 "\t-del *.idb\n".
432 "\t-del debug.log\n";
433 select STDOUT; close OUT;