]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mkfiles.pl
RJK's `make install' patch for the Unix makefile.
[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 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 sub manpages {
259   my ($types,$suffix) = @_;
260
261   # assume that all UNIX programs have a man page
262   if($suffix eq "1" && $types =~ /X/) {
263     return map("$_.1", &progrealnames($types));
264   }
265   return ();
266 }
267
268 # Now we're ready to output the actual Makefiles.
269
270 ##-- CygWin makefile
271 open OUT, ">Makefile.cyg"; select OUT;
272 print
273 "# Makefile for PuTTY under cygwin.\n".
274 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
275 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
276 # gcc command line option is -D not /D
277 ($_ = $help) =~ s/=\/D/=-D/gs;
278 print $_;
279 print
280 "\n".
281 "# You can define this path to point at your tools if you need to\n".
282 "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
283 "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
284 "CC = \$(TOOLPATH)gcc\n".
285 "RC = \$(TOOLPATH)windres\n".
286 "# You may also need to tell windres where to find include files:\n".
287 "# RCINC = --include-dir c:\\cygwin\\include\\\n".
288 "\n".
289 &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
290   " -DNO_SECURITY -D_NO_OLDNAMES -DNO_MULTIMON -I.")."\n".
291 "LDFLAGS = -mno-cygwin -s\n".
292 &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
293   " --define WINVER=0x0400 --define MINGW32_FIX=1")."\n".
294 "\n".
295 ".SUFFIXES:\n".
296 "\n".
297 "%.o: %.c\n".
298 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
299 "\n".
300 "%.res.o: %.rc\n".
301 "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
302 "\n";
303 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
304 print "\n\n";
305 foreach $p (&prognames("GC")) {
306   ($prog, $type) = split ",", $p;
307   $objstr = &objects($p, "X.o", "X.res.o", undef);
308   print &splitline($prog . ".exe: " . $objstr), "\n";
309   my $mw = $type eq "G" ? " -mwindows" : "";
310   $libstr = &objects($p, undef, undef, "-lX");
311   print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
312                    $objstr . " $libstr", 69), "\n\n";
313 }
314 &deps("X.o", "X.res.o", "", "/");
315 print
316 "\n".
317 "version.o: FORCE;\n".
318 "# Hack to force version.o to be rebuilt always\n".
319 "FORCE:\n".
320 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c version.c\n".
321 "clean:\n".
322 "\trm -f *.o *.exe *.res.o\n".
323 "\n";
324 select STDOUT; close OUT;
325
326 ##-- Borland makefile
327 %stdlibs = (  # Borland provides many Win32 API libraries intrinsically
328   "advapi32" => 1,
329   "comctl32" => 1,
330   "comdlg32" => 1,
331   "gdi32" => 1,
332   "imm32" => 1,
333   "shell32" => 1,
334   "user32" => 1,
335   "winmm" => 1,
336   "winspool" => 1,
337   "wsock32" => 1,
338 );          
339 open OUT, ">Makefile.bor"; select OUT;
340 print
341 "# Makefile for PuTTY under Borland C.\n".
342 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
343 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
344 # bcc32 command line option is -D not /D
345 ($_ = $help) =~ s/=\/D/=-D/gs;
346 print $_;
347 print
348 "\n".
349 "# If you rename this file to `Makefile', you should change this line,\n".
350 "# so that the .rsp files still depend on the correct makefile.\n".
351 "MAKEFILE = Makefile.bor\n".
352 "\n".
353 "# C compilation flags\n".
354 "CFLAGS = -DWINVER=0x0401\n".
355 "\n".
356 "# Get include directory for resource compiler\n".
357 "!if !\$d(BCB)\n".
358 "BCB = \$(MAKEDIR)\\..\n".
359 "!endif\n".
360 "\n".
361 ".c.obj:\n".
362 &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT) \$(FWHACK)".
363   " \$(XFLAGS) \$(CFLAGS) /c \$*.c",69)."\n".
364 ".rc.res:\n".
365 &splitline("\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include -r".
366   " -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401 \$*.rc",69)."\n".
367 "\n";
368 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
369 print "\n\n";
370 foreach $p (&prognames("GC")) {
371   ($prog, $type) = split ",", $p;
372   $objstr = &objects($p, "X.obj", "X.res", undef);
373   print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
374   my $ap = ($type eq "G") ? "-aa" : "-ap";
375   print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
376 }
377 foreach $p (&prognames("GC")) {
378   ($prog, $type) = split ",", $p;
379   print $prog, ".rsp: \$(MAKEFILE)\n";
380   $objstr = &objects($p, "X.obj", undef, undef);
381   @objlist = split " ", $objstr;
382   @objlines = ("");
383   foreach $i (@objlist) {
384     if (length($objlines[$#objlines] . " $i") > 50) {
385       push @objlines, "";
386     }
387     $objlines[$#objlines] .= " $i";
388   }
389   $c0w = ($type eq "G") ? "c0w32" : "c0x32";
390   print "\techo $c0w + > $prog.rsp\n";
391   for ($i=0; $i<=$#objlines; $i++) {
392     $plus = ($i < $#objlines ? " +" : "");
393     print "\techo$objlines[$i]$plus >> $prog.rsp\n";
394   }
395   print "\techo $prog.exe >> $prog.rsp\n";
396   $objstr = &objects($p, "X.obj", "X.res", undef);
397   @libs = split " ", &objects($p, undef, undef, "X");
398   @libs = grep { !$stdlibs{$_} } @libs;
399   unshift @libs, "cw32", "import32";
400   $libstr = join ' ', @libs;
401   print "\techo nul,$libstr, >> $prog.rsp\n";
402   print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
403   print "\n";
404 }
405 &deps("X.obj", "X.res", "", "\\");
406 print
407 "\n".
408 "version.o: FORCE\n".
409 "# Hack to force version.o to be rebuilt always\n".
410 "FORCE:\n".
411 "\tbcc32 \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
412 "clean:\n".
413 "\t-del *.obj\n".
414 "\t-del *.exe\n".
415 "\t-del *.res\n".
416 "\t-del *.pch\n".
417 "\t-del *.aps\n".
418 "\t-del *.il*\n".
419 "\t-del *.pdb\n".
420 "\t-del *.rsp\n".
421 "\t-del *.tds\n".
422 "\t-del *.\$\$\$\$\$\$\n";
423 select STDOUT; close OUT;
424
425 ##-- Visual C++ makefile
426 open OUT, ">Makefile.vc"; select OUT;
427 print
428 "# Makefile for PuTTY under Visual C.\n".
429 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
430 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
431 print $help;
432 print
433 "\n".
434 "# If you rename this file to `Makefile', you should change this line,\n".
435 "# so that the .rsp files still depend on the correct makefile.\n".
436 "MAKEFILE = Makefile.vc\n".
437 "\n".
438 "# C compilation flags\n".
439 "CFLAGS = /nologo /W3 /O1 /D_WINDOWS /D_WIN32_WINDOWS=0x401 /DWINVER=0x401\n".
440 "LFLAGS = /incremental:no /fixed\n".
441 "\n".
442 ".c.obj:\n".
443 "\tcl \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) /c \$*.c\n".
444 ".rc.res:\n".
445 "\trc \$(FWHACK) \$(RCFL) -r -DWIN32 -D_WIN32 -DWINVER=0x0400 \$*.rc\n".
446 "\n";
447 print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("GC"));
448 print "\n\n";
449 foreach $p (&prognames("GC")) {
450   ($prog, $type) = split ",", $p;
451   $objstr = &objects($p, "X.obj", "X.res", undef);
452   print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
453   print "\tlink \$(LFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
454 }
455 foreach $p (&prognames("GC")) {
456   ($prog, $type) = split ",", $p;
457   print $prog, ".rsp: \$(MAKEFILE)\n";
458   $objstr = &objects($p, "X.obj", "X.res", "X.lib");
459   @objlist = split " ", $objstr;
460   @objlines = ("");
461   foreach $i (@objlist) {
462     if (length($objlines[$#objlines] . " $i") > 50) {
463       push @objlines, "";
464     }
465     $objlines[$#objlines] .= " $i";
466   }
467   $subsys = ($type eq "G") ? "windows" : "console";
468   print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
469   for ($i=0; $i<=$#objlines; $i++) {
470     print "\techo$objlines[$i] >> $prog.rsp\n";
471   }
472   print "\n";
473 }
474 &deps("X.obj", "X.res", "", "\\");
475 print
476 "\n".
477 "# Hack to force version.o to be rebuilt always\n".
478 "version.obj: *.c *.h *.rc\n".
479 "\tcl \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
480 "clean: tidy\n".
481 "\t-del *.exe\n\n".
482 "tidy:\n".
483 "\t-del *.obj\n".
484 "\t-del *.res\n".
485 "\t-del *.pch\n".
486 "\t-del *.aps\n".
487 "\t-del *.ilk\n".
488 "\t-del *.pdb\n".
489 "\t-del *.rsp\n".
490 "\t-del *.dsp\n".
491 "\t-del *.dsw\n".
492 "\t-del *.ncb\n".
493 "\t-del *.opt\n".
494 "\t-del *.plg\n".
495 "\t-del *.map\n".
496 "\t-del *.idb\n".
497 "\t-del debug.log\n";
498 select STDOUT; close OUT;
499
500 ##-- X/GTK/Unix makefile
501 open OUT, ">unix/Makefile.gtk"; select OUT;
502 print
503 "# Makefile for PuTTY under X/GTK and Unix.\n".
504 "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
505 "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
506 # gcc command line option is -D not /D
507 ($_ = $help) =~ s/=\/D/=-D/gs;
508 print $_;
509 print
510 "\n".
511 "# You can define this path to point at your tools if you need to\n".
512 "# TOOLPATH = /opt/gcc/bin\n".
513 "CC = \$(TOOLPATH)cc\n".
514 "\n".
515 &splitline("CFLAGS = -Wall -g -I. -I.. `gtk-config --cflags`")."\n".
516 "LDFLAGS = `gtk-config --libs`\n".
517 "INSTALL=install\n",
518 "INSTALL_PROGRAM=\$(INSTALL)\n",
519 "INSTALL_DATA=\$(INSTALL)\n",
520 "prefix=/usr/local\n",
521 "exec_prefix=\$(prefix)\n",
522 "bindir=\$(exec_prefix)/bin\n",
523 "mandir=\$(prefix)/man\n",
524 "man1dir=\$(mandir)/man1\n",
525 "\n".
526 ".SUFFIXES:\n".
527 "\n".
528 "%.o:\n".
529 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) -c \$<\n".
530 "\n";
531 print &splitline("all:" . join "", map { " $_" } &progrealnames("X"));
532 print "\n\n";
533 foreach $p (&prognames("X")) {
534   ($prog, $type) = split ",", $p;
535   $objstr = &objects($p, "X.o", undef, undef);
536   print &splitline($prog . ": " . $objstr), "\n";
537   $libstr = &objects($p, undef, undef, "-lX");
538   print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
539                    $objstr . " $libstr", 69), "\n\n";
540 }
541 &deps("X.o", undef, "../", "/");
542 print
543 "\n".
544 "version.o: FORCE;\n".
545 "# Hack to force version.o to be rebuilt always\n".
546 "FORCE:\n".
547 "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(XFLAGS) \$(CFLAGS) \$(VER) -c ../version.c\n".
548 "clean:\n".
549 "\trm -f *.o". (join "", map { " $_" } &progrealnames("X")) . "\n".
550 "\n",
551 "install:\n",
552 map("\t\$(INSTALL_PROGRAM) -m 755 $_ \$(bindir)/$_\n", &progrealnames("X")),
553 map("\t\$(INSTALL_DATA) -m 644 $_ \$(man1dir)/$_\n", &manpages("X", "1")),
554 "\n",
555 "install-strip:\n",
556 "\t\$(MAKE) install INSTALL_PROGRAM=\"\$(INSTALL_PROGRAM) -s\"\n",
557 "\n";
558 select STDOUT; close OUT;