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