]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mkfiles.pl
Fix an EOF-testing goof in winhandl.c.
[PuTTY.git] / mkfiles.pl
1 #!/usr/bin/env perl
2 #
3 # Cross-platform Makefile generator.
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 # PuTTY specifics which could still do with removing:
11 #  - Mac makefile is not portabilised at all. Include directories
12 #    are hardwired, and also the libraries are fixed. This is
13 #    mainly because I was too scared to go anywhere near it.
14 #  - sbcsgen.pl is still run at startup.
15 #
16 # FIXME: no attempt made to handle !forceobj in the project files.
17
18 use warnings;
19 use FileHandle;
20 use File::Basename;
21 use Cwd;
22 use Digest::SHA qw(sha512_hex);
23
24 if ($#ARGV >= 0 and ($ARGV[0] eq "-u" or $ARGV[0] eq "-U")) {
25     # Convenience for Unix users: -u means that after we finish what
26     # we're doing here, we also run mkauto.sh and then 'configure' in
27     # the Unix subdirectory. So it's a one-stop shop for regenerating
28     # the actual end-product Unix makefile.
29     #
30     # Arguments supplied after -u go to configure.
31     #
32     # -U is identical, but runs 'configure' at the _top_ level, for
33     # people who habitually do that.
34     $do_unix = ($ARGV[0] eq "-U" ? 2 : 1);
35     shift @ARGV;
36     @confargs = @ARGV;
37 }
38
39 open IN, "Recipe" or do {
40     # We want to deal correctly with being run from one of the
41     # subdirs in the source tree. So if we can't find Recipe here,
42     # try one level up.
43     chdir "..";
44     open IN, "Recipe" or die "unable to open Recipe file\n";
45 };
46
47 # HACK: One of the source files in `charset' is auto-generated by
48 # sbcsgen.pl, and licence.h is likewise generated by licence.pl. We
49 # need to generate those _now_, before attempting dependency analysis.
50 eval 'chdir "charset"; require "./sbcsgen.pl"; chdir ".."; select STDOUT;';
51 eval 'require "./licence.pl"; select STDOUT;';
52
53 @srcdirs = ("./");
54
55 $divert = undef; # ref to scalar in which text is currently being put
56 $help = ""; # list of newline-free lines of help text
57 $project_name = "project"; # this is a good enough default
58 %makefiles = (); # maps makefile types to output makefile pathnames
59 %makefile_extra = (); # maps makefile types to extra Makefile text
60 %programs = (); # maps prog name + type letter to listref of objects/resources
61 %groups = (); # maps group name to listref of objects/resources
62
63 while (<IN>) {
64   chomp;
65   @_ = split;
66
67   # If we're gathering help text, keep doing so.
68   if (defined $divert) {
69       if ((defined $_[0]) && $_[0] eq "!end") {
70           $divert = undef;
71       } else {
72           ${$divert} .= "$_\n";
73       }
74       next;
75   }
76   # Skip comments and blank lines.
77   next if /^\s*#/ or scalar @_ == 0;
78
79   if ($_[0] eq "!begin" and $_[1] eq "help") { $divert = \$help; next; }
80   if ($_[0] eq "!end") { $divert = undef; next; }
81   if ($_[0] eq "!name") { $project_name = $_[1]; next; }
82   if ($_[0] eq "!srcdir") { push @srcdirs, $_[1]; next; }
83   if ($_[0] eq "!makefile" and &mfval($_[1])) { $makefiles{$_[1]}=$_[2]; next;}
84   if ($_[0] eq "!specialobj" and &mfval($_[1])) { $specialobj{$_[1]}->{$_[2]} = 1; next;}
85   if ($_[0] eq "!cflags" and &mfval($_[1])) {
86       ($rest = $_) =~ s/^\s*\S+\s+\S+\s+\S+\s*//; # find rest of input line
87       if ($rest eq "") {
88           # Make sure this file doesn't get lumped together with any
89           # other file's cflags.
90           $rest = "F" . $_[2];
91       } else {
92           # Give this file a specific set of cflags, but permit it to
93           # go together with other files using the same set.
94           $rest = "C" . $rest;
95       }
96       $cflags{$_[1]}->{$_[2]} = $rest;
97       next;
98   }
99   if ($_[0] eq "!forceobj") { $forceobj{$_[1]} = 1; next; }
100   if ($_[0] eq "!begin") {
101       if ($_[1] =~ /^>(.*)/) {
102           $divert = \$auxfiles{$1};
103       } elsif (&mfval($_[1])) {
104           $sect = $_[2] ? $_[2] : "end";
105           $divert = \($makefile_extra{$_[1]}->{$sect});
106       } else {
107           $dummy = '';
108           $divert = \$dummy;
109       }
110       next;
111   }
112   # If we're gathering help/verbatim text, keep doing so.
113   if (defined $divert) { ${$divert} .= "$_\n"; next; }
114   # Ignore blank lines.
115   next if scalar @_ == 0;
116
117   # Now we have an ordinary line. See if it's an = line, a : line
118   # or a + line.
119   @objs = @_;
120
121   if ($_[0] eq "+") {
122     $listref = $lastlistref;
123     $prog = undef;
124     die "$.: unexpected + line\n" if !defined $lastlistref;
125   } elsif ($_[1] eq "=") {
126     $groups{$_[0]} = [] if !defined $groups{$_[0]};
127     $listref = $groups{$_[0]};
128     $prog = undef;
129     shift @objs; # eat the group name
130   } elsif ($_[1] eq ":") {
131     $listref = [];
132     $prog = $_[0];
133     shift @objs; # eat the program name
134   } else {
135     die "$.: unrecognised line type\n";
136   }
137   shift @objs; # eat the +, the = or the :
138
139   while (scalar @objs > 0) {
140     $i = shift @objs;
141     if ($groups{$i}) {
142       foreach $j (@{$groups{$i}}) { unshift @objs, $j; }
143     } elsif (($i =~ /^\[([A-Z]*)\]$/) and defined $prog) {
144       $type = substr($i,1,(length $i)-2);
145       die "unrecognised program type for $prog [$type]\n"
146           if ! grep { $type eq $_ } qw(G C X U MX XT UT);
147     } else {
148       push @$listref, $i;
149     }
150   }
151   if ($prog and $type) {
152     die "multiple program entries for $prog [$type]\n"
153         if defined $programs{$prog . "," . $type};
154     $programs{$prog . "," . $type} = $listref;
155   }
156   $lastlistref = $listref;
157 }
158
159 close IN;
160
161 foreach $aux (sort keys %auxfiles) {
162     open AUX, ">$aux";
163     print AUX $auxfiles{$aux};
164     close AUX;
165 }
166
167 # Now retrieve the complete list of objects and resource files, and
168 # construct dependency data for them. While we're here, expand the
169 # object list for each program, and complain if its type isn't set.
170 @prognames = sort keys %programs;
171 %depends = ();
172 @scanlist = ();
173 foreach $i (@prognames) {
174   ($prog, $type) = split ",", $i;
175   # Strip duplicate object names.
176   $prev = '';
177   @list = grep { $status = ($prev ne $_); $prev=$_; $status }
178           sort @{$programs{$i}};
179   $programs{$i} = [@list];
180   foreach $j (@list) {
181     # Dependencies for "x" start with "x.c" or "x.m" (depending on
182     # which one exists).
183     # Dependencies for "x.res" start with "x.rc".
184     # Dependencies for "x.rsrc" start with "x.r".
185     # Both types of file are pushed on the list of files to scan.
186     # Libraries (.lib) don't have dependencies at all.
187     if ($j =~ /^(.*)\.res$/) {
188       $file = "$1.rc";
189       $depends{$j} = [$file];
190       push @scanlist, $file;
191     } elsif ($j =~ /^(.*)\.rsrc$/) {
192       $file = "$1.r";
193       $depends{$j} = [$file];
194       push @scanlist, $file;
195     } elsif ($j !~ /\./) {
196       $file = "$j.c";
197       $file = "$j.m" unless &findfile($file);
198       $depends{$j} = [$file];
199       push @scanlist, $file;
200     }
201   }
202 }
203
204 # Scan each file on @scanlist and find further inclusions.
205 # Inclusions are given by lines of the form `#include "otherfile"'
206 # (system headers are automatically ignored by this because they'll
207 # be given in angle brackets). Files included by this method are
208 # added back on to @scanlist to be scanned in turn (if not already
209 # done).
210 #
211 # Resource scripts (.rc) can also include a file by means of:
212 #  - a line # ending `ICON "filename"';
213 #  - a line ending `RT_MANIFEST "filename"'.
214 # Files included by this method are not added to @scanlist because
215 # they can never include further files.
216 #
217 # In this pass we write out a hash %further which maps a source
218 # file name into a listref containing further source file names.
219
220 %further = ();
221 %allsourcefiles = (); # this is wanted by some makefiles
222 while (scalar @scanlist > 0) {
223   $file = shift @scanlist;
224   next if defined $further{$file}; # skip if we've already done it
225   $further{$file} = [];
226   $dirfile = &findfile($file);
227   $allsourcefiles{$dirfile} = 1;
228   open IN, "$dirfile" or die "unable to open source file $file\n";
229   while (<IN>) {
230     chomp;
231     /^\s*#include\s+\"([^\"]+)\"/ and do {
232       push @{$further{$file}}, $1;
233       push @scanlist, $1;
234       next;
235     };
236     /(RT_MANIFEST|ICON)\s+\"([^\"]+)\"\s*$/ and do {
237       push @{$further{$file}}, $2;
238       next;
239     }
240   }
241   close IN;
242 }
243
244 # Now we're ready to generate the final dependencies section. For
245 # each key in %depends, we must expand the dependencies list by
246 # iteratively adding entries from %further.
247 foreach $i (keys %depends) {
248   %dep = ();
249   @scanlist = @{$depends{$i}};
250   foreach $i (@scanlist) { $dep{$i} = 1; }
251   while (scalar @scanlist > 0) {
252     $file = shift @scanlist;
253     foreach $j (@{$further{$file}}) {
254       if (!$dep{$j}) {
255         $dep{$j} = 1;
256         push @{$depends{$i}}, $j;
257         push @scanlist, $j;
258       }
259     }
260   }
261 #  printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
262 }
263
264 # Validation of input.
265
266 sub mfval($) {
267     my ($type) = @_;
268     # Returns true if the argument is a known makefile type. Otherwise,
269     # prints a warning and returns false;
270     if (grep { $type eq $_ }
271         ("vc","vcproj","cygwin","borland","lcc","devcppproj","gtk","unix",
272          "am","osx","vstudio10","vstudio12")) {
273         return 1;
274     }
275     warn "$.:unknown makefile type '$type'\n";
276     return 0;
277 }
278
279 # Utility routines while writing out the Makefiles.
280
281 sub def {
282     my ($x) = shift @_;
283     return (defined $x) ? $x : "";
284 }
285
286 sub dirpfx {
287     my ($path) = shift @_;
288     my ($sep) = shift @_;
289     my $ret = "";
290     my $i;
291
292     while (($i = index $path, $sep) >= 0 ||
293            ($j = index $path, "/") >= 0) {
294         if ($i >= 0 and ($j < 0 or $i < $j)) {
295             $path = substr $path, ($i + length $sep);
296         } else {
297             $path = substr $path, ($j + 1);
298         }
299         $ret .= "..$sep";
300     }
301     return $ret;
302 }
303
304 sub findfile {
305   my ($name) = @_;
306   my $dir = '';
307   my $i;
308   my $outdir = undef;
309   unless (defined $findfilecache{$name}) {
310     $i = 0;
311     foreach $dir (@srcdirs) {
312       if (-f "$dir$name") {
313         $outdir = $dir;
314         $i++;
315         $outdir =~ s/^\.\///;
316       }
317     }
318     die "multiple instances of source file $name\n" if $i > 1;
319     $findfilecache{$name} = (defined $outdir ? $outdir . $name : undef);
320   }
321   return $findfilecache{$name};
322 }
323
324 sub objects {
325   my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
326   my @ret;
327   my ($i, $x, $y);
328   ($otmpl, $rtmpl, $ltmpl) = map { defined $_ ? $_ : "" } ($otmpl, $rtmpl, $ltmpl);
329   @ret = ();
330   foreach $i (@{$programs{$prog}}) {
331     $x = "";
332     if ($i =~ /^(.*)\.(res|rsrc)/) {
333       $y = $1;
334       ($x = $rtmpl) =~ s/X/$y/;
335     } elsif ($i =~ /^(.*)\.lib/) {
336       $y = $1;
337       ($x = $ltmpl) =~ s/X/$y/;
338     } elsif ($i !~ /\./) {
339       ($x = $otmpl) =~ s/X/$i/;
340     }
341     push @ret, $x if $x ne "";
342   }
343   return join " ", @ret;
344 }
345
346 sub special {
347   my ($prog, $suffix) = @_;
348   my @ret;
349   my ($i, $x, $y);
350   ($otmpl, $rtmpl, $ltmpl) = map { defined $_ ? $_ : "" } ($otmpl, $rtmpl, $ltmpl);
351   @ret = ();
352   foreach $i (@{$programs{$prog}}) {
353     if (substr($i, (length $i) - (length $suffix)) eq $suffix) {
354       push @ret, $i;
355     }
356   }
357   return (scalar @ret) ? (join " ", @ret) : undef;
358 }
359
360 sub splitline {
361   my ($line, $width, $splitchar) = @_;
362   my $result = "";
363   my $len;
364   $len = (defined $width ? $width : 76);
365   $splitchar = (defined $splitchar ? $splitchar : '\\');
366   while (length $line > $len) {
367     $line =~ /^(.{0,$len})\s(.*)$/ or $line =~ /^(.{$len,}?\s(.*)$/;
368     $result .= $1;
369     $result .= " ${splitchar}\n\t\t" if $2 ne '';
370     $line = $2;
371     $len = 60;
372   }
373   return $result . $line;
374 }
375
376 sub deps {
377   my ($otmpl, $rtmpl, $prefix, $dirsep, $mftyp, $depchar, $splitchar) = @_;
378   my ($i, $x, $y);
379   my @deps;
380   my @ret;
381   @ret = ();
382   $depchar ||= ':';
383   foreach $i (sort keys %depends) {
384     next if $specialobj{$mftyp}->{$i};
385     if ($i =~ /^(.*)\.(res|rsrc)/) {
386       next if !defined $rtmpl;
387       $y = $1;
388       ($x = $rtmpl) =~ s/X/$y/;
389     } else {
390       ($x = $otmpl) =~ s/X/$i/;
391     }
392     @deps = @{$depends{$i}};
393     @deps = map {
394       $_ = &findfile($_);
395       s/\//$dirsep/g;
396       $_ = $prefix . $_;
397     } @deps;
398     push @ret, {obj => $x, obj_orig => $i, deps => [@deps]};
399   }
400   return @ret;
401 }
402
403 sub prognames {
404   my ($types) = @_;
405   my ($n, $prog, $type);
406   my @ret;
407   @ret = ();
408   foreach $n (@prognames) {
409     ($prog, $type) = split ",", $n;
410     push @ret, $n if index(":$types:", ":$type:") >= 0;
411   }
412   return @ret;
413 }
414
415 sub progrealnames {
416   my ($types) = @_;
417   my ($n, $prog, $type);
418   my @ret;
419   @ret = ();
420   foreach $n (@prognames) {
421     ($prog, $type) = split ",", $n;
422     push @ret, $prog if index(":$types:", ":$type:") >= 0;
423   }
424   return @ret;
425 }
426
427 sub manpages {
428   my ($types,$suffix) = @_;
429
430   # assume that all UNIX programs have a man page
431   if($suffix eq "1" && $types =~ /:X:/) {
432     return map("$_.1", &progrealnames($types));
433   }
434   return ();
435 }
436
437 $orig_dir = cwd;
438
439 # Now we're ready to output the actual Makefiles.
440
441 if (defined $makefiles{'cygwin'}) {
442     $dirpfx = &dirpfx($makefiles{'cygwin'}, "/");
443
444     ##-- MinGW/CygWin makefile (called 'cygwin' for historical reasons)
445     open OUT, ">$makefiles{'cygwin'}"; select OUT;
446     print
447     "# Makefile for $project_name under MinGW, Cygwin, or Winelib.\n".
448     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
449     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
450     # gcc command line option is -D not /D
451     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
452     print $_;
453     print
454     "\n".
455     "# You can define this path to point at your tools if you need to\n".
456     "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
457     "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
458     "# TOOLPATH = i686-w64-mingw32-\n".
459     "CC = \$(TOOLPATH)gcc\n".
460     "RC = \$(TOOLPATH)windres\n".
461     "# Uncomment the following two lines to compile under Winelib\n".
462     "# CC = winegcc\n".
463     "# RC = wrc\n".
464     "# You may also need to tell windres where to find include files:\n".
465     "# RCINC = --include-dir c:\\cygwin\\include\\\n".
466     "\n".
467     &splitline("CFLAGS = -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
468       " -D_NO_OLDNAMES " .
469                (join " ", map {"-I$dirpfx$_"} @srcdirs)) .
470                "\n".
471     "LDFLAGS = -s\n".
472     &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1 ".
473       "--define WINVER=0x0400 ".(join " ", map {"-I$dirpfx$_"} @srcdirs))."\n".
474     "\n".
475     &def($makefile_extra{'cygwin'}->{'vars'}) .
476     "\n".
477     ".SUFFIXES:\n".
478     "\n";
479     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
480     print "\n\n";
481     foreach $p (&prognames("G:C")) {
482       ($prog, $type) = split ",", $p;
483       $objstr = &objects($p, "X.o", "X.res.o", undef);
484       print &splitline($prog . ".exe: " . $objstr), "\n";
485       my $mw = $type eq "G" ? " -mwindows" : "";
486       $libstr = &objects($p, undef, undef, "-lX");
487       print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
488                        "-Wl,-Map,$prog.map " .
489                        $objstr . " $libstr", 69), "\n\n";
490     }
491     foreach $d (&deps("X.o", "X.res.o", $dirpfx, "/", "cygwin")) {
492       if ($forceobj{$d->{obj_orig}}) {
493         printf ("%s: FORCE\n", $d->{obj});
494       } else {
495         print &splitline(sprintf("%s: %s", $d->{obj},
496                          join " ", @{$d->{deps}})), "\n";
497       }
498       if ($d->{obj} =~ /\.res\.o$/) {
499           print "\t\$(RC) \$(RCFL) \$(RCFLAGS) ".$d->{deps}->[0]." -o ".$d->{obj}."\n\n";
500       } else {
501           print "\t\$(CC) \$(COMPAT) \$(CFLAGS) \$(XFLAGS) -c ".$d->{deps}->[0]."\n\n";
502       }
503     }
504     print "\n";
505     print &def($makefile_extra{'cygwin'}->{'end'});
506     print "\nclean:\n".
507     "\trm -f *.o *.exe *.res.o *.so *.map\n".
508     "\n".
509     "FORCE:\n";
510     select STDOUT; close OUT;
511
512 }
513
514 ##-- Borland makefile
515 if (defined $makefiles{'borland'}) {
516     $dirpfx = &dirpfx($makefiles{'borland'}, "\\");
517
518     %stdlibs = (  # Borland provides many Win32 API libraries intrinsically
519       "advapi32" => 1,
520       "comctl32" => 1,
521       "comdlg32" => 1,
522       "gdi32" => 1,
523       "imm32" => 1,
524       "shell32" => 1,
525       "user32" => 1,
526       "winmm" => 1,
527       "winspool" => 1,
528       "wsock32" => 1,
529     );
530     open OUT, ">$makefiles{'borland'}"; select OUT;
531     print
532     "# Makefile for $project_name under Borland C.\n".
533     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
534     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
535     # bcc32 command line option is -D not /D
536     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
537     print $_;
538     print
539     "\n".
540     "# If you rename this file to `Makefile', you should change this line,\n".
541     "# so that the .rsp files still depend on the correct makefile.\n".
542     "MAKEFILE = Makefile.bor\n".
543     "\n".
544     "# C compilation flags\n".
545     "CFLAGS = -D_WINDOWS -DWINVER=0x0500\n".
546     "# Resource compilation flags\n".
547     "RCFLAGS = -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401\n".
548     "\n".
549     "# Get include directory for resource compiler\n".
550     "!if !\$d(BCB)\n".
551     "BCB = \$(MAKEDIR)\\..\n".
552     "!endif\n".
553     "\n".
554     &def($makefile_extra{'borland'}->{'vars'}) .
555     "\n".
556     ".c.obj:\n".
557     &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT)".
558                " \$(CFLAGS) \$(XFLAGS) ".
559                (join " ", map {"-I$dirpfx$_"} @srcdirs) .
560                " /c \$*.c",69)."\n".
561     ".rc.res:\n".
562     &splitline("\tbrcc32 \$(RCFL) -i \$(BCB)\\include -r".
563       " \$(RCFLAGS) \$*.rc",69)."\n".
564     "\n";
565     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
566     print "\n\n";
567     foreach $p (&prognames("G:C")) {
568       ($prog, $type) = split ",", $p;
569       $objstr =  &objects($p, "X.obj", "X.res", undef);
570       print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
571       my $ap = ($type eq "G") ? "-aa" : "-ap";
572       print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
573     }
574     foreach $p (&prognames("G:C")) {
575       ($prog, $type) = split ",", $p;
576       print $prog, ".rsp: \$(MAKEFILE)\n";
577       $objstr = &objects($p, "X.obj", undef, undef);
578       @objlist = split " ", $objstr;
579       @objlines = ("");
580       foreach $i (@objlist) {
581         if (length($objlines[$#objlines] . " $i") > 50) {
582           push @objlines, "";
583         }
584         $objlines[$#objlines] .= " $i";
585       }
586       $c0w = ($type eq "G") ? "c0w32" : "c0x32";
587       print "\techo $c0w + > $prog.rsp\n";
588       for ($i=0; $i<=$#objlines; $i++) {
589         $plus = ($i < $#objlines ? " +" : "");
590         print "\techo$objlines[$i]$plus >> $prog.rsp\n";
591       }
592       print "\techo $prog.exe >> $prog.rsp\n";
593       $objstr = &objects($p, "X.obj", "X.res", undef);
594       @libs = split " ", &objects($p, undef, undef, "X");
595       @libs = grep { !$stdlibs{$_} } @libs;
596       unshift @libs, "cw32", "import32";
597       $libstr = join ' ', @libs;
598       print "\techo nul,$libstr, >> $prog.rsp\n";
599       print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
600       print "\n";
601     }
602     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\", "borland")) {
603       if ($forceobj{$d->{obj_orig}}) {
604         printf("%s: FORCE\n", $d->{obj});
605       } else {
606         print &splitline(sprintf("%s: %s", $d->{obj},
607                                  join " ", @{$d->{deps}})), "\n";
608       }
609     }
610     print "\n";
611     print &def($makefile_extra{'borland'}->{'end'});
612     print "\nclean:\n".
613     "\t-del *.obj\n".
614     "\t-del *.exe\n".
615     "\t-del *.res\n".
616     "\t-del *.pch\n".
617     "\t-del *.aps\n".
618     "\t-del *.il*\n".
619     "\t-del *.pdb\n".
620     "\t-del *.rsp\n".
621     "\t-del *.tds\n".
622     "\t-del *.\$\$\$\$\$\$\n".
623     "\n".
624     "FORCE:\n".
625     "\t-rem dummy command\n";
626     select STDOUT; close OUT;
627 }
628
629 if (defined $makefiles{'vc'}) {
630     $dirpfx = &dirpfx($makefiles{'vc'}, "\\");
631
632     ##-- Visual C++ makefile
633     open OUT, ">$makefiles{'vc'}"; select OUT;
634     print
635       "# Makefile for $project_name under Visual C.\n".
636       "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
637       "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
638     print $help;
639     print
640       "\n".
641       "# If you rename this file to `Makefile', you should change this line,\n".
642       "# so that the .rsp files still depend on the correct makefile.\n".
643       "MAKEFILE = Makefile.vc\n".
644       "\n".
645       "# C compilation flags\n".
646       "CFLAGS = /nologo /W3 /O1 " .
647       (join " ", map {"-I$dirpfx$_"} @srcdirs) .
648       " /D_WINDOWS /D_WIN32_WINDOWS=0x500 /DWINVER=0x500 /D_CRT_SECURE_NO_WARNINGS\n".
649       "LFLAGS = /incremental:no /dynamicbase /nxcompat\n".
650       "RCFLAGS = ".(join " ", map {"-I$dirpfx$_"} @srcdirs).
651       " -DWIN32 -D_WIN32 -DWINVER=0x0400\n".
652       "\n".
653       &def($makefile_extra{'vc'}->{'vars'}) .
654       "\n".
655       "\n";
656     print &splitline("all:" . join "", map { " \$(BUILDDIR)$_.exe" } &progrealnames("G:C"));
657     print "\n\n";
658     foreach $p (&prognames("G:C")) {
659         ($prog, $type) = split ",", $p;
660         $objstr = &objects($p, "\$(BUILDDIR)X.obj", "\$(BUILDDIR)X.res", undef);
661         print &splitline("\$(BUILDDIR)$prog.exe: " . $objstr), "\n";
662
663         $objstr = &objects($p, "\$(BUILDDIR)X.obj", "\$(BUILDDIR)X.res", "X.lib");
664         $subsys = ($type eq "G") ? "windows" : "console";
665         $inlinefilename = "link_$prog";
666         print "\ttype <<$inlinefilename\n";
667         @objlist = split " ", $objstr;
668         @objlines = ("");
669         foreach $i (@objlist) {
670             if (length($objlines[$#objlines] . " $i") > 72) {
671                 push @objlines, "";
672             }
673             $objlines[$#objlines] .= " $i";
674         }
675         for ($i=0; $i<=$#objlines; $i++) {
676             print "$objlines[$i]\n";
677         }
678         print "<<\n";
679         print "\tlink \$(LFLAGS) \$(XLFLAGS) -out:\$(BUILDDIR)$prog.exe -map:\$(BUILDDIR)$prog.map -nologo -subsystem:$subsys\$(SUBSYSVER) \@$inlinefilename\n\n";
680     }
681     foreach $d (&deps("\$(BUILDDIR)X.obj", "\$(BUILDDIR)X.res", $dirpfx, "\\", "vc")) {
682         $extradeps = $forceobj{$d->{obj_orig}} ? ["*.c","*.h","*.rc"] : [];
683         print &splitline(sprintf("%s: %s", $d->{obj},
684                                  join " ", @$extradeps, @{$d->{deps}})), "\n";
685         if ($d->{obj} =~ /.res$/) {
686             print "\trc /Fo@{[$d->{obj}]} \$(RCFL) -r \$(RCFLAGS) ".$d->{deps}->[0],"\n\n";
687         }
688     }
689     print "\n";
690     foreach $real_srcdir ("", @srcdirs) {
691         $srcdir = $real_srcdir;
692         if ($srcdir ne "") {
693             $srcdir =~ s!/!\\!g;
694             $srcdir = $dirpfx . $srcdir;
695             $srcdir =~ s!\\\.\\!\\!;
696             $srcdir = "{$srcdir}";
697         }
698         # The double colon at the end of the line makes this a
699         # 'batch-mode inference rule', which means that nmake will
700         # aggregate multiple invocations of the rule and issue just
701         # one cl command with multiple source-file arguments. That
702         # noticeably speeds up builds, since starting up the cl
703         # process is a noticeable overhead and now has to be done far
704         # fewer times.
705         print "${srcdir}.c.obj::\n\tcl /Fo\$(BUILDDIR) \$(COMPAT) \$(CFLAGS) \$(XFLAGS) /c \$<\n\n";
706     }
707     print &def($makefile_extra{'vc'}->{'end'});
708     print "\nclean: tidy\n".
709       "\t-del \$(BUILDDIR)*.exe\n\n".
710       "tidy:\n".
711       "\t-del \$(BUILDDIR)*.obj\n".
712       "\t-del \$(BUILDDIR)*.res\n".
713       "\t-del \$(BUILDDIR)*.pch\n".
714       "\t-del \$(BUILDDIR)*.aps\n".
715       "\t-del \$(BUILDDIR)*.ilk\n".
716       "\t-del \$(BUILDDIR)*.pdb\n".
717       "\t-del \$(BUILDDIR)*.rsp\n".
718       "\t-del \$(BUILDDIR)*.dsp\n".
719       "\t-del \$(BUILDDIR)*.dsw\n".
720       "\t-del \$(BUILDDIR)*.ncb\n".
721       "\t-del \$(BUILDDIR)*.opt\n".
722       "\t-del \$(BUILDDIR)*.plg\n".
723       "\t-del \$(BUILDDIR)*.map\n".
724       "\t-del \$(BUILDDIR)*.idb\n".
725       "\t-del \$(BUILDDIR)debug.log\n";
726     select STDOUT; close OUT;
727 }
728
729 if (defined $makefiles{'vcproj'}) {
730     $dirpfx = &dirpfx($makefiles{'vcproj'}, "\\");
731
732     ##-- MSVC 6 Workspace and projects
733     #
734     # Note: All files created in this section are written in binary
735     # mode, because although MSVC's command-line make can deal with
736     # LF-only line endings, MSVC project files really _need_ to be
737     # CRLF. Hence, in order for mkfiles.pl to generate usable project
738     # files even when run from Unix, I make sure all files are binary
739     # and explicitly write the CRLFs.
740     #
741     # Create directories if necessary
742     mkdir $makefiles{'vcproj'}
743         if(! -d $makefiles{'vcproj'});
744     chdir $makefiles{'vcproj'};
745     @deps = &deps("X.obj", "X.res", $dirpfx, "\\", "vcproj");
746     %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
747     # Create the project files
748     # Get names of all Windows projects (GUI and console)
749     my @prognames = &prognames("G:C");
750     foreach $progname (@prognames) {
751       create_vc_project(\%all_object_deps, $progname);
752     }
753     # Create the workspace file
754     open OUT, ">$project_name.dsw"; binmode OUT; select OUT;
755     print
756     "Microsoft Developer Studio Workspace File, Format Version 6.00\r\n".
757     "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n".
758     "\r\n".
759     "###############################################################################\r\n".
760     "\r\n";
761     # List projects
762     foreach $progname (@prognames) {
763       ($windows_project, $type) = split ",", $progname;
764         print "Project: \"$windows_project\"=\".\\$windows_project\\$windows_project.dsp\" - Package Owner=<4>\r\n";
765     }
766     print
767     "\r\n".
768     "Package=<5>\r\n".
769     "{{{\r\n".
770     "}}}\r\n".
771     "\r\n".
772     "Package=<4>\r\n".
773     "{{{\r\n".
774     "}}}\r\n".
775     "\r\n".
776     "###############################################################################\r\n".
777     "\r\n".
778     "Global:\r\n".
779     "\r\n".
780     "Package=<5>\r\n".
781     "{{{\r\n".
782     "}}}\r\n".
783     "\r\n".
784     "Package=<3>\r\n".
785     "{{{\r\n".
786     "}}}\r\n".
787     "\r\n".
788     "###############################################################################\r\n".
789     "\r\n";
790     select STDOUT; close OUT;
791     chdir $orig_dir;
792
793     sub create_vc_project {
794         my ($all_object_deps, $progname) = @_;
795         # Construct program's dependency info
796         %seen_objects = ();
797         %lib_files = ();
798         %source_files = ();
799         %header_files = ();
800         %resource_files = ();
801         @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
802         foreach $object_file (@object_files) {
803             next if defined $seen_objects{$object_file};
804             $seen_objects{$object_file} = 1;
805             if($object_file =~ /\.lib$/io) {
806                 $lib_files{$object_file} = 1;
807                 next;
808             }
809             $object_deps = $all_object_deps{$object_file};
810             foreach $object_dep (@$object_deps) {
811                 if($object_dep =~ /\.c$/io) {
812                     $source_files{$object_dep} = 1;
813                     next;
814                 }
815                 if($object_dep =~ /\.h$/io) {
816                     $header_files{$object_dep} = 1;
817                     next;
818                 }
819                 if($object_dep =~ /\.(rc|ico)$/io) {
820                     $resource_files{$object_dep} = 1;
821                     next;
822                 }
823             }
824         }
825         $libs = join " ", sort keys %lib_files;
826         @source_files = sort keys %source_files;
827         @header_files = sort keys %header_files;
828         @resources = sort keys %resource_files;
829         ($windows_project, $type) = split ",", $progname;
830         mkdir $windows_project
831             if(! -d $windows_project);
832         chdir $windows_project;
833         $subsys = ($type eq "G") ? "windows" : "console";
834         open OUT, ">$windows_project.dsp"; binmode OUT; select OUT;
835         print
836         "# Microsoft Developer Studio Project File - Name=\"$windows_project\" - Package Owner=<4>\r\n".
837         "# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n".
838         "# ** DO NOT EDIT **\r\n".
839         "\r\n".
840         "# TARGTYPE \"Win32 (x86) Application\" 0x0101\r\n".
841         "\r\n".
842         "CFG=$windows_project - Win32 Debug\r\n".
843         "!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n".
844         "!MESSAGE use the Export Makefile command and run\r\n".
845         "!MESSAGE \r\n".
846         "!MESSAGE NMAKE /f \"$windows_project.mak\".\r\n".
847         "!MESSAGE \r\n".
848         "!MESSAGE You can specify a configuration when running NMAKE\r\n".
849         "!MESSAGE by defining the macro CFG on the command line. For example:\r\n".
850         "!MESSAGE \r\n".
851         "!MESSAGE NMAKE /f \"$windows_project.mak\" CFG=\"$windows_project - Win32 Debug\"\r\n".
852         "!MESSAGE \r\n".
853         "!MESSAGE Possible choices for configuration are:\r\n".
854         "!MESSAGE \r\n".
855         "!MESSAGE \"$windows_project - Win32 Release\" (based on \"Win32 (x86) Application\")\r\n".
856         "!MESSAGE \"$windows_project - Win32 Debug\" (based on \"Win32 (x86) Application\")\r\n".
857         "!MESSAGE \r\n".
858         "\r\n".
859         "# Begin Project\r\n".
860         "# PROP AllowPerConfigDependencies 0\r\n".
861         "# PROP Scc_ProjName \"\"\r\n".
862         "# PROP Scc_LocalPath \"\"\r\n".
863         "CPP=cl.exe\r\n".
864         "MTL=midl.exe\r\n".
865         "RSC=rc.exe\r\n".
866         "\r\n".
867         "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
868         "\r\n".
869         "# PROP BASE Use_MFC 0\r\n".
870         "# PROP BASE Use_Debug_Libraries 0\r\n".
871         "# PROP BASE Output_Dir \"Release\"\r\n".
872         "# PROP BASE Intermediate_Dir \"Release\"\r\n".
873         "# PROP BASE Target_Dir \"\"\r\n".
874         "# PROP Use_MFC 0\r\n".
875         "# PROP Use_Debug_Libraries 0\r\n".
876         "# PROP Output_Dir \"Release\"\r\n".
877         "# PROP Intermediate_Dir \"Release\"\r\n".
878         "# PROP Ignore_Export_Lib 0\r\n".
879         "# PROP Target_Dir \"\"\r\n".
880         "# ADD BASE CPP /nologo /W3 /GX /O2 ".
881           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
882           " /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
883         "# ADD CPP /nologo /W3 /GX /O2 ".
884           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
885           " /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
886         "# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
887         "# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
888         "# ADD BASE RSC /l 0x809 /d \"NDEBUG\"\r\n".
889         "# ADD RSC /l 0x809 /d \"NDEBUG\"\r\n".
890         "BSC32=bscmake.exe\r\n".
891         "# ADD BASE BSC32 /nologo\r\n".
892         "# ADD BSC32 /nologo\r\n".
893         "LINK32=link.exe\r\n".
894         "# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:$subsys /machine:I386\r\n".
895         "# ADD LINK32 $libs /nologo /subsystem:$subsys /machine:I386\r\n".
896         "# SUBTRACT LINK32 /pdb:none\r\n".
897         "\r\n".
898         "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
899         "\r\n".
900         "# PROP BASE Use_MFC 0\r\n".
901         "# PROP BASE Use_Debug_Libraries 1\r\n".
902         "# PROP BASE Output_Dir \"Debug\"\r\n".
903         "# PROP BASE Intermediate_Dir \"Debug\"\r\n".
904         "# PROP BASE Target_Dir \"\"\r\n".
905         "# PROP Use_MFC 0\r\n".
906         "# PROP Use_Debug_Libraries 1\r\n".
907         "# PROP Output_Dir \"Debug\"\r\n".
908         "# PROP Intermediate_Dir \"Debug\"\r\n".
909         "# PROP Ignore_Export_Lib 0\r\n".
910         "# PROP Target_Dir \"\"\r\n".
911         "# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od ".
912           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
913           " /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
914         "# ADD CPP /nologo /W3 /Gm /GX /ZI /Od ".
915           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
916           " /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
917         "# ADD BASE MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
918         "# ADD MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
919         "# ADD BASE RSC /l 0x809 /d \"_DEBUG\"\r\n".
920         "# ADD RSC /l 0x809 /d \"_DEBUG\"\r\n".
921         "BSC32=bscmake.exe\r\n".
922         "# ADD BASE BSC32 /nologo\r\n".
923         "# ADD BSC32 /nologo\r\n".
924         "LINK32=link.exe\r\n".
925         "# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:$subsys /debug /machine:I386 /pdbtype:sept\r\n".
926         "# ADD LINK32 $libs /nologo /subsystem:$subsys /debug /machine:I386 /pdbtype:sept\r\n".
927         "# SUBTRACT LINK32 /pdb:none\r\n".
928         "\r\n".
929         "!ENDIF \r\n".
930         "\r\n".
931         "# Begin Target\r\n".
932         "\r\n".
933         "# Name \"$windows_project - Win32 Release\"\r\n".
934         "# Name \"$windows_project - Win32 Debug\"\r\n".
935         "# Begin Group \"Source Files\"\r\n".
936         "\r\n".
937         "# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\"\r\n";
938         foreach $source_file (@source_files) {
939             print
940               "# Begin Source File\r\n".
941               "\r\n".
942               "SOURCE=..\\..\\$source_file\r\n";
943             if($source_file =~ /ssh\.c/io) {
944                 # Disable 'Edit and continue' as Visual Studio can't handle the macros
945                 print
946                   "\r\n".
947                   "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
948                   "\r\n".
949                   "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
950                   "\r\n".
951                   "# ADD CPP /Zi\r\n".
952                   "\r\n".
953                   "!ENDIF \r\n".
954                   "\r\n";
955             }
956             print "# End Source File\r\n";
957         }
958         print
959         "# End Group\r\n".
960         "# Begin Group \"Header Files\"\r\n".
961         "\r\n".
962         "# PROP Default_Filter \"h;hpp;hxx;hm;inl\"\r\n";
963         foreach $header_file (@header_files) {
964             print
965               "# Begin Source File\r\n".
966               "\r\n".
967               "SOURCE=..\\..\\$header_file\r\n".
968               "# End Source File\r\n";
969         }
970         print
971         "# End Group\r\n".
972         "# Begin Group \"Resource Files\"\r\n".
973         "\r\n".
974         "# PROP Default_Filter \"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\"\r\n";
975         foreach $resource_file (@resources) {
976             print
977               "# Begin Source File\r\n".
978               "\r\n".
979               "SOURCE=..\\..\\$resource_file\r\n".
980               "# End Source File\r\n";
981         }
982         print
983         "# End Group\r\n".
984         "# End Target\r\n".
985         "# End Project\r\n";
986         select STDOUT; close OUT;
987         chdir "..";
988     }
989 }
990
991 if (defined $makefiles{'vstudio10'} || defined $makefiles{'vstudio12'}) {
992
993     ##-- Visual Studio 2010+ Solution and Projects
994
995     if (defined $makefiles{'vstudio10'}) {
996         create_vs_solution('vstudio10', "2010", "11.00", "v100");
997     }
998
999     if (defined $makefiles{'vstudio12'}) {
1000         create_vs_solution('vstudio12', "2012", "12.00", "v110");
1001     }
1002
1003     sub create_vs_solution {
1004         my ($makefilename, $name, $version, $toolsver) = @_;
1005
1006         $dirpfx = &dirpfx($makefiles{$makefilename}, "\\");
1007
1008         @deps = &deps("X.obj", "X.res", $dirpfx, "\\", $makefilename);
1009         %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
1010
1011         my @prognames = &prognames("G:C");
1012
1013         # Create the solution file.
1014         mkdir $makefiles{$makefilename}
1015            if(! -f $makefiles{$makefilename});
1016         chdir $makefiles{$makefilename};
1017
1018         open OUT, ">$project_name.sln"; select OUT;
1019
1020         print
1021             "Microsoft Visual Studio Solution File, Format Version $version\n" .
1022             "# Visual Studio $name\n";
1023
1024         my %projguids = ();
1025         foreach $progname (@prognames) {
1026             ($windows_project, $type) = split ",", $progname;
1027
1028             $projguids{$windows_project} = $guid =
1029                 &invent_guid("project:$progname");
1030         
1031             print
1032                 "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"$windows_project\", \"$windows_project\\$windows_project.vcxproj\", \"{$guid}\"\n" .
1033                 "EndProject\n";
1034         }
1035
1036         print
1037             "Global\n" .
1038             "    GlobalSection(SolutionConfigurationPlatforms) = preSolution\n" .
1039             "        Debug|Win32 = Debug|Win32\n" .
1040             "        Release|Win32 = Release|Win32\n" .
1041             "    EndGlobalSection\n" .
1042             "    GlobalSection(ProjectConfigurationPlatforms) = postSolution\n" ;
1043
1044         foreach my $projguid (values %projguids) {
1045             print
1046                 "        {$projguid}.Debug|Win32.ActiveCfg = Debug|Win32\n" .
1047                 "        {$projguid}.Debug|Win32.Build.0 = Debug|Win32\n" .
1048                 "        {$projguid}.Release|Win32.ActiveCfg = Release|Win32\n" .
1049                 "        {$projguid}.Release|Win32.Build.0 = Release|Win32\n";
1050         }
1051
1052         print
1053             "    EndGlobalSection\n" .
1054             "    GlobalSection(SolutionProperties) = preSolution\n" .
1055             "        HideSolutionNode = FALSE\n" .
1056             "    EndGlobalSection\n" .
1057             "EndGlobal\n";
1058
1059         select STDOUT; close OUT;
1060
1061         foreach $progname (@prognames) {
1062             ($windows_project, $type) = split ",", $progname;
1063             create_vs_project(\%all_object_deps, $windows_project, $type, $projguids{$windows_project}, $toolsver);
1064         }
1065     
1066         chdir $orig_dir;
1067     }
1068
1069     sub create_vs_project {
1070         my ($all_object_deps, $windows_project, $type, $projguid, $toolsver) = @_;
1071
1072         # Break down the project's dependency information into the appropriate
1073         # groups.
1074         %seen_objects = ();
1075         %lib_files = ();
1076         %source_files = ();
1077         %header_files = ();
1078         %resource_files = ();
1079         %icon_files = ();
1080
1081         @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
1082         foreach $object_file (@object_files) {
1083             next if defined $seen_objects{$object_file};
1084             $seen_objects{$object_file} = 1;
1085
1086             if($object_file =~ /\.lib$/io) {
1087                 $lib_files{$object_file} = 1;
1088                 next;
1089             }
1090
1091             $object_deps = $all_object_deps{$object_file};
1092             foreach $object_dep (@$object_deps) {
1093                 if($object_dep eq $object_deps->[0]) {
1094                     if($object_dep =~ /\.c$/io) {
1095                         $source_files{$object_dep} = 1;
1096                     } elsif($object_dep =~ /\.rc$/io) {
1097                         $resource_files{$object_dep} = 1;
1098                     }
1099                 } elsif ($object_dep =~ /\.[ch]$/io) {
1100                     $header_files{$object_dep} = 1;
1101                 } elsif ($object_dep =~ /\.ico$/io) {
1102                     $icon_files{$object_dep} = 1;
1103                 }
1104             }
1105         }
1106
1107         $libs = join ";", sort keys %lib_files;
1108         @source_files = sort keys %source_files;
1109         @header_files = sort keys %header_files;
1110         @resources = sort keys %resource_files;
1111         @icons = sort keys %icon_files;
1112         $subsystem = ($type eq "G") ? "Windows" : "Console";
1113
1114         mkdir $windows_project
1115             if(! -d $windows_project);
1116         chdir $windows_project;
1117         open OUT, ">$windows_project.vcxproj"; select OUT;
1118         open FILTERS, ">$windows_project.vcxproj.filters";
1119
1120         # The bulk of the project file is just boilerplate stuff, so we
1121         # can mostly just dump it out here. Note, buried in the ClCompile
1122         # item definition, that we use a debug information format of
1123         # ProgramDatabase, which disables the edit-and-continue support
1124         # that breaks most of the project builds.
1125         print
1126             "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" .
1127             "<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n" .
1128             "  <ItemGroup Label=\"ProjectConfigurations\">\n" .
1129             "    <ProjectConfiguration Include=\"Debug|Win32\">\n" .
1130             "      <Configuration>Debug</Configuration>\n" .
1131             "      <Platform>Win32</Platform>\n" .
1132             "    </ProjectConfiguration>\n" .
1133             "    <ProjectConfiguration Include=\"Release|Win32\">\n" .
1134             "      <Configuration>Release</Configuration>\n" .
1135             "      <Platform>Win32</Platform>\n" .
1136             "    </ProjectConfiguration>\n" .
1137             "  </ItemGroup>\n" .
1138             "  <PropertyGroup Label=\"Globals\">\n" .
1139             "    <SccProjectName />\n" .
1140             "    <SccLocalPath />\n" .
1141             "    <ProjectGuid>{$projguid}</ProjectGuid>\n" .
1142             "  </PropertyGroup>\n" .
1143             "  <Import Project=\"\$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n" .
1144             "  <PropertyGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n" .
1145             "    <ConfigurationType>Application</ConfigurationType>\n" .
1146             "    <UseOfMfc>false</UseOfMfc>\n" .
1147             "    <CharacterSet>MultiByte</CharacterSet>\n" .
1148             "    <PlatformToolset>$toolsver</PlatformToolset>\n" .
1149             "  </PropertyGroup>\n" .
1150             "  <PropertyGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n" .
1151             "    <ConfigurationType>Application</ConfigurationType>\n" .
1152             "    <UseOfMfc>false</UseOfMfc>\n" .
1153             "    <CharacterSet>MultiByte</CharacterSet>\n" .
1154             "    <PlatformToolset>$toolsver</PlatformToolset>\n" .
1155             "  </PropertyGroup>\n" .
1156             "  <Import Project=\"\$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n" .
1157             "  <ImportGroup Label=\"ExtensionTargets\">\n" .
1158             "  </ImportGroup>\n" .
1159             "  <ImportGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Debug|Win32'\" Label=\"PropertySheets\">\n" .
1160             "    <Import Project=\"\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props\" Condition=\"exists('\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n" .
1161             "  </ImportGroup>\n" .
1162             "  <ImportGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Release|Win32'\" Label=\"PropertySheets\">\n" .
1163             "    <Import Project=\"\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props\" Condition=\"exists('\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n" .
1164             "  </ImportGroup>\n" .
1165             "  <PropertyGroup Label=\"UserMacros\" />\n" .
1166             "  <PropertyGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Release|Win32'\">\n" .
1167             "    <OutDir>.\\Release\\</OutDir>\n" .
1168             "    <IntDir>.\\Release\\</IntDir>\n" .
1169             "    <LinkIncremental>false</LinkIncremental>\n" .
1170             "  </PropertyGroup>\n" .
1171             "  <PropertyGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Debug|Win32'\">\n" .
1172             "    <OutDir>.\\Debug\\</OutDir>\n" .
1173             "    <IntDir>.\\Debug\\</IntDir>\n" .
1174             "    <LinkIncremental>true</LinkIncremental>\n" .
1175             "  </PropertyGroup>\n" .
1176             "  <ItemDefinitionGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Release|Win32'\">\n" .
1177             "    <ClCompile>\n" .
1178             "      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n" .
1179             "      <InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>\n" .
1180             "      <StringPooling>true</StringPooling>\n" .
1181             "      <FunctionLevelLinking>true</FunctionLevelLinking>\n" .
1182             "      <Optimization>MaxSpeed</Optimization>\n" .
1183             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1184             "      <WarningLevel>Level3</WarningLevel>\n" .
1185             "      <AdditionalIncludeDirectories>" . (join ";", map {"..\\..\\$dirpfx$_"} @srcdirs) . ";%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n" .
1186             "      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;POSIX;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n" .
1187             "      <AssemblerListingLocation>.\\Release\\</AssemblerListingLocation>\n" .
1188             "      <PrecompiledHeaderOutputFile>.\\Release\\$windows_project.pch</PrecompiledHeaderOutputFile>\n" .
1189             "      <ObjectFileName>.\\Release\\</ObjectFileName>\n" .
1190             "      <ProgramDataBaseFileName>.\\Release\\</ProgramDataBaseFileName>\n" .
1191             "    </ClCompile>\n" .
1192             "    <Midl>\n" .
1193             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1194             "      <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n" .
1195             "      <TypeLibraryName>.\\Release\\$windows_project.tlb</TypeLibraryName>\n" .
1196             "      <MkTypLibCompatible>true</MkTypLibCompatible>\n" .
1197             "      <TargetEnvironment>Win32</TargetEnvironment>\n" .
1198             "    </Midl>\n" .
1199             "    <ResourceCompile>\n" .
1200             "      <Culture>0x0809</Culture>\n" .
1201             "      <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n" .
1202             "    </ResourceCompile>\n" .
1203             "    <Bscmake>\n" .
1204             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1205             "      <OutputFile>.\\Release\\$windows_project.bsc</OutputFile>\n" .
1206             "    </Bscmake>\n" .
1207             "    <Link>\n" .
1208             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1209             "      <SubSystem>$subsystem</SubSystem>\n" .
1210             "      <OutputFile>.\\Release\\$windows_project.exe</OutputFile>\n" .
1211             "      <AdditionalDependencies>$libs;%(AdditionalDependencies)</AdditionalDependencies>\n" .
1212             "    </Link>\n" .
1213             "  </ItemDefinitionGroup>\n" .
1214             "  <ItemDefinitionGroup Condition=\"'\$(Configuration)|\$(Platform)'=='Debug|Win32'\">\n" .
1215             "    <ClCompile>\n" .
1216             "      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n" .
1217             "      <InlineFunctionExpansion>Default</InlineFunctionExpansion>\n" .
1218             "      <FunctionLevelLinking>false</FunctionLevelLinking>\n" .
1219             "      <Optimization>Disabled</Optimization>\n" .
1220             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1221             "      <WarningLevel>Level3</WarningLevel>\n" .
1222             "      <MinimalRebuild>true</MinimalRebuild>\n" .
1223             "      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>\n" .
1224             "      <AdditionalIncludeDirectories>" . (join ";", map {"..\\..\\$dirpfx$_"} @srcdirs) . ";%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n" .
1225             "      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;POSIX;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n" .
1226             "      <AssemblerListingLocation>.\\Debug\\</AssemblerListingLocation>\n" .
1227             "      <PrecompiledHeaderOutputFile>.\\Debug\\$windows_project.pch</PrecompiledHeaderOutputFile>\n" .
1228             "      <ObjectFileName>.\\Debug\\</ObjectFileName>\n" .
1229             "      <ProgramDataBaseFileName>.\\Debug\\</ProgramDataBaseFileName>\n" .
1230             "      <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>\n" .
1231             "    </ClCompile>\n" .
1232             "    <Midl>\n" .
1233             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1234             "      <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n" .
1235             "      <TypeLibraryName>.\\Debug\\$windows_project.tlb</TypeLibraryName>\n" .
1236             "      <MkTypLibCompatible>true</MkTypLibCompatible>\n" .
1237             "      <TargetEnvironment>Win32</TargetEnvironment>\n" .
1238             "    </Midl>\n" .
1239             "    <ResourceCompile>\n" .
1240             "      <Culture>0x0809</Culture>\n" .
1241             "      <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n" .
1242             "    </ResourceCompile>\n" .
1243             "    <Bscmake>\n" .
1244             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1245             "      <OutputFile>.\\Debug\\$windows_project.bsc</OutputFile>\n" .
1246             "    </Bscmake>\n" .
1247             "    <Link>\n" .
1248             "      <SuppressStartupBanner>true</SuppressStartupBanner>\n" .
1249             "      <GenerateDebugInformation>true</GenerateDebugInformation>\n" .
1250             "      <SubSystem>$subsystem</SubSystem>\n" .
1251             "      <OutputFile>\$(TargetPath)</OutputFile>\n" .
1252             "      <AdditionalDependencies>$libs;%(AdditionalDependencies)</AdditionalDependencies>\n" .
1253             "    </Link>\n" .
1254             "  </ItemDefinitionGroup>\n";
1255
1256         # The VC++ projects don't have physical structure to them, instead
1257         # the files are organized by logical "filters" that are stored in
1258         # a separate file, so different users can organize things differently.
1259         # The filters file contains a copy of the ItemGroup elements from
1260         # the main project file that list the included items, but tack
1261         # on a filter name where needed.
1262         print FILTERS
1263             "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" .
1264             "<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n";
1265
1266         print "  <ItemGroup>\n";
1267         print FILTERS "  <ItemGroup>\n";
1268         foreach $icon_file (@icons) {
1269             $icon_file =~ s/..\\windows\\//;
1270             print "    <CustomBuild Include=\"..\\..\\$icon_file\" />\n";
1271             print FILTERS
1272                 "    <CustomBuild Include=\"..\\..\\$icon_file\">\n" .
1273                 "      <Filter>Resource Files</Filter>\n" .
1274                 "    </CustomBuild>\n";
1275         }
1276         print FILTERS "  </ItemGroup>\n";
1277         print "  </ItemGroup>\n";
1278
1279         print "  <ItemGroup>\n";
1280         print FILTERS "  <ItemGroup>\n";
1281         foreach $resource_file (@resources) {
1282             $resource_file =~ s/..\\windows\\//;
1283             print
1284                 "    <ResourceCompile Include=\"..\\..\\$resource_file\">\n" .
1285                 "      <AdditionalIncludeDirectories Condition=\"'\$(Configuration)|\$(Platform)'=='Release|Win32'\">..\\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n" .
1286                 "      <AdditionalIncludeDirectories Condition=\"'\$(Configuration)|\$(Platform)'=='Debug|Win32'\">..\\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n" .
1287                 "    </ResourceCompile>\n";
1288             print FILTERS
1289                 "    <ResourceCompile Include=\"..\\..\\$resource_file\">\n" .
1290                 "      <Filter>Resource Files</Filter>\n" .
1291                 "    </ResourceCompile>\n";
1292         }
1293         print FILTERS "  </ItemGroup>\n";
1294         print "  </ItemGroup>\n";
1295
1296         print "  <ItemGroup>\n";
1297         print FILTERS "  <ItemGroup>\n";
1298         foreach $source_file (@source_files) {
1299             $source_file =~ s/..\\windows\\//;
1300             print "    <ClCompile Include=\"..\\..\\$source_file\" />\n";
1301             print FILTERS
1302                 "    <ClCompile Include=\"..\\..\\$source_file\">\n" .
1303                 "      <Filter>Source Files</Filter>\n" .
1304                 "    </ClCompile>";
1305         }
1306         print FILTERS "  </ItemGroup>\n";
1307         print "  </ItemGroup>\n";
1308
1309         print "  <ItemGroup>\n";
1310         print FILTERS "  <ItemGroup>\n";
1311         foreach $header_file (@header_files) {
1312             $header_file  =~ s/..\\windows\\//;
1313             print "    <ClInclude Include=\"..\\..\\$header_file\" />\n";
1314             print FILTERS
1315                 "    <ClInclude Include=\"..\\..\\$header_file\">\n" .
1316                 "      <Filter>Header Files</Filter>\n" .
1317                 "    </ClInclude>";
1318         }
1319         print FILTERS "  </ItemGroup>\n";
1320         print "  </ItemGroup>\n";
1321
1322         print
1323             "  <Import Project=\"\$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n" .
1324             "</Project>";
1325
1326         print FILTERS
1327             "  <ItemGroup>\n" .
1328             "    <Filter Include=\"Source Files\">\n" .
1329             "      <UniqueIdentifier>{" . &invent_guid("sources:$windows_project") . "}</UniqueIdentifier>\n" .
1330             "    </Filter>\n" .
1331             "    <Filter Include=\"Header Files\">\n" .
1332             "      <UniqueIdentifier>{" . &invent_guid("headers:$windows_project") . "}</UniqueIdentifier>\n" .
1333             "    </Filter>\n" .
1334             "    <Filter Include=\"Resource Files\">\n" .
1335             "      <UniqueIdentifier>{" . &invent_guid("resources:$windows_project") . "}</UniqueIdentifier>\n" .
1336             "    </Filter>\n" .
1337             "  </ItemGroup>\n" .
1338             "</Project>";
1339
1340         select STDOUT; close OUT; close FILTERS;
1341         chdir "..";
1342     }
1343 }
1344
1345 if (defined $makefiles{'gtk'}) {
1346     $dirpfx = &dirpfx($makefiles{'gtk'}, "/");
1347
1348     ##-- X/GTK/Unix makefile
1349     open OUT, ">$makefiles{'gtk'}"; select OUT;
1350     print
1351     "# Makefile for $project_name under X/GTK and Unix.\n".
1352     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1353     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1354     # gcc command line option is -D not /D
1355     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
1356     print $_;
1357     print
1358     "\n".
1359     "# You can define this path to point at your tools if you need to\n".
1360     "# TOOLPATH = /opt/gcc/bin\n".
1361     "CC = \$(TOOLPATH)cc\n".
1362     "# If necessary set the path to krb5-config here\n".
1363     "KRB5CONFIG=krb5-config\n".
1364     "# You can manually set this to `gtk-config' or `pkg-config gtk+-1.2'\n".
1365     "# (depending on what works on your system) if you want to enforce\n".
1366     "# building with GTK 1.2, or you can set it to `pkg-config gtk+-2.0 x11'\n".
1367     "# if you want to enforce 2.0. The default is to try 2.0 and fall back\n".
1368     "# to 1.2 if it isn't found.\n".
1369     "GTK_CONFIG = sh -c 'pkg-config gtk+-3.0 x11 \$\$0 2>/dev/null || pkg-config gtk+-2.0 x11 \$\$0 2>/dev/null || gtk-config \$\$0'\n".
1370     "\n".
1371     "-include Makefile.local\n".
1372     "\n".
1373     "unexport CFLAGS # work around a weird issue with krb5-config\n".
1374     "\n".
1375     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1376                (join " ", map {"-I$dirpfx$_"} @srcdirs) .
1377                " \$(shell \$(GTK_CONFIG) --cflags)").
1378                  " -D _FILE_OFFSET_BITS=64\n".
1379     "XLDFLAGS = \$(LDFLAGS) \$(shell \$(GTK_CONFIG) --libs)\n".
1380     "ULDFLAGS = \$(LDFLAGS)\n".
1381     "ifeq (,\$(findstring NO_GSSAPI,\$(COMPAT)))\n".
1382     "ifeq (,\$(findstring STATIC_GSSAPI,\$(COMPAT)))\n".
1383     "XLDFLAGS+= -ldl\n".
1384     "ULDFLAGS+= -ldl\n".
1385     "else\n".
1386     "CFLAGS+= -DNO_LIBDL \$(shell \$(KRB5CONFIG) --cflags gssapi)\n".
1387     "XLDFLAGS+= \$(shell \$(KRB5CONFIG) --libs gssapi)\n".
1388     "ULDFLAGS+= \$(shell \$(KRB5CONFIG) --libs gssapi)\n".
1389     "endif\n".
1390     "endif\n".
1391     "INSTALL=install\n".
1392     "INSTALL_PROGRAM=\$(INSTALL)\n".
1393     "INSTALL_DATA=\$(INSTALL)\n".
1394     "prefix=/usr/local\n".
1395     "exec_prefix=\$(prefix)\n".
1396     "bindir=\$(exec_prefix)/bin\n".
1397     "mandir=\$(prefix)/man\n".
1398     "man1dir=\$(mandir)/man1\n".
1399     "\n".
1400     &def($makefile_extra{'gtk'}->{'vars'}) .
1401     "\n".
1402     ".SUFFIXES:\n".
1403     "\n".
1404     "\n";
1405     print &splitline("all:" . join "", map { " $_" }
1406                      &progrealnames("X:XT:U:UT"));
1407     print "\n\n";
1408     foreach $p (&prognames("X:XT:U:UT")) {
1409       ($prog, $type) = split ",", $p;
1410       ($ldflags = $type) =~ s/T$//;
1411       $objstr = &objects($p, "X.o", undef, undef);
1412       print &splitline($prog . ": " . $objstr), "\n";
1413       $libstr = &objects($p, undef, undef, "-lX");
1414       print &splitline("\t\$(CC) -o \$@ " .
1415                        $objstr . " \$(${ldflags}LDFLAGS) $libstr", 69), "\n\n";
1416     }
1417     foreach $d (&deps("X.o", undef, $dirpfx, "/", "gtk")) {
1418       if ($forceobj{$d->{obj_orig}}) {
1419         printf("%s: FORCE\n", $d->{obj});
1420       } else {
1421         print &splitline(sprintf("%s: %s", $d->{obj},
1422                                  join " ", @{$d->{deps}})), "\n";
1423       }
1424       print &splitline("\t\$(CC) \$(COMPAT) \$(CFLAGS) \$(XFLAGS) -c $d->{deps}->[0]\n");
1425     }
1426     print "\n";
1427     print &def($makefile_extra{'gtk'}->{'end'});
1428     print "\nclean:\n".
1429     "\trm -f *.o". (join "", map { " $_" } &progrealnames("X:XT:U:UT")) . "\n";
1430     print "\nFORCE:\n";
1431     select STDOUT; close OUT;
1432 }
1433
1434 if (defined $makefiles{'unix'}) {
1435     $dirpfx = &dirpfx($makefiles{'unix'}, "/");
1436
1437     ##-- GTK-free pure-Unix makefile for non-GUI apps only
1438     open OUT, ">$makefiles{'unix'}"; select OUT;
1439     print
1440     "# Makefile for $project_name under Unix.\n".
1441     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1442     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1443     # gcc command line option is -D not /D
1444     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
1445     print $_;
1446     print
1447     "\n".
1448     "# You can define this path to point at your tools if you need to\n".
1449     "# TOOLPATH = /opt/gcc/bin\n".
1450     "CC = \$(TOOLPATH)cc\n".
1451     "\n".
1452     "-include Makefile.local\n".
1453     "\n".
1454     "unexport CFLAGS # work around a weird issue with krb5-config\n".
1455     "\n".
1456     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1457                (join " ", map {"-I$dirpfx$_"} @srcdirs)).
1458                  " -D _FILE_OFFSET_BITS=64\n".
1459     "ULDFLAGS = \$(LDFLAGS)\n".
1460     "INSTALL=install\n".
1461     "INSTALL_PROGRAM=\$(INSTALL)\n".
1462     "INSTALL_DATA=\$(INSTALL)\n".
1463     "prefix=/usr/local\n".
1464     "exec_prefix=\$(prefix)\n".
1465     "bindir=\$(exec_prefix)/bin\n".
1466     "mandir=\$(prefix)/man\n".
1467     "man1dir=\$(mandir)/man1\n".
1468     "\n".
1469     &def($makefile_extra{'unix'}->{'vars'}) .
1470     "\n".
1471     ".SUFFIXES:\n".
1472     "\n".
1473     "\n";
1474     print &splitline("all:" . join "", map { " $_" } &progrealnames("U:UT"));
1475     print "\n\n";
1476     foreach $p (&prognames("U:UT")) {
1477       ($prog, $type) = split ",", $p;
1478       $objstr = &objects($p, "X.o", undef, undef);
1479       print &splitline($prog . ": " . $objstr), "\n";
1480       $libstr = &objects($p, undef, undef, "-lX");
1481       print &splitline("\t\$(CC) -o \$@ " .
1482                        $objstr . " \$(${type}LDFLAGS) $libstr", 69), "\n\n";
1483     }
1484     foreach $d (&deps("X.o", undef, $dirpfx, "/", "unix")) {
1485       if ($forceobj{$d->{obj_orig}}) {
1486         printf("%s: FORCE\n", $d->{obj});
1487       } else {
1488         print &splitline(sprintf("%s: %s", $d->{obj},
1489                                  join " ", @{$d->{deps}})), "\n";
1490       }
1491       print &splitline("\t\$(CC) \$(COMPAT) \$(CFLAGS) \$(XFLAGS) -c $d->{deps}->[0]\n");
1492     }
1493     print "\n";
1494     print &def($makefile_extra{'unix'}->{'end'});
1495     print "\nclean:\n".
1496     "\trm -f *.o". (join "", map { " $_" } &progrealnames("U:UT")) . "\n";
1497     print "\nFORCE:\n";
1498     select STDOUT; close OUT;
1499 }
1500
1501 if (defined $makefiles{'am'}) {
1502     die "Makefile.am in a subdirectory is not supported\n"
1503         if &dirpfx($makefiles{'am'}, "/") ne "";
1504
1505     ##-- Unix/autoconf Makefile.am
1506     open OUT, ">$makefiles{'am'}"; select OUT;
1507     print
1508     "# Makefile.am for $project_name under Unix with Autoconf/Automake.\n".
1509     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1510     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n\n";
1511
1512     # 2014-02-22: as of automake-1.14 we begin to get complained at if
1513     # we don't use this option
1514     print "AUTOMAKE_OPTIONS = subdir-objects\n\n";
1515
1516     # Complete list of source and header files. Not used by the
1517     # auto-generated parts of this makefile, but Recipe might like to
1518     # have it available as a variable so that mandatory-rebuild things
1519     # (version.o) can conveniently be made to depend on it.
1520     @sources = ("allsources", "=",
1521                 sort grep {$_ ne "empty.h"} keys %allsourcefiles);
1522     print &splitline(join " ", @sources), "\n\n";
1523
1524     @cliprogs = ("bin_PROGRAMS", "=");
1525     foreach $p (&prognames("U")) {
1526       ($prog, $type) = split ",", $p;
1527       push @cliprogs, $prog;
1528     }
1529     @allprogs = @cliprogs;
1530     foreach $p (&prognames("X")) {
1531       ($prog, $type) = split ",", $p;
1532       push @allprogs, $prog;
1533     }
1534     print "if HAVE_GTK\n";
1535     print &splitline(join " ", @allprogs), "\n";
1536     print "else\n";
1537     print &splitline(join " ", @cliprogs), "\n";
1538     print "endif\n\n";
1539
1540     @noinstcliprogs = ("noinst_PROGRAMS", "=");
1541     foreach $p (&prognames("XT:UT")) {
1542       ($prog, $type) = split ",", $p;
1543       push @noinstcliprogs, $prog;
1544     }
1545     print &splitline(join " ", @noinstcliprogs), "\n";
1546
1547     %objtosrc = ();
1548     foreach $d (&deps("X", undef, "", "/", "am")) {
1549       $objtosrc{$d->{obj}} = $d->{deps}->[0];
1550     }
1551
1552     print &splitline(join " ", "AM_CPPFLAGS", "=",
1553                      map {"-I\$(srcdir)/$_"} @srcdirs), "\n";
1554
1555     @amcflags = ("\$(COMPAT)", "\$(XFLAGS)", "\$(WARNINGOPTS)");
1556     print "if HAVE_GTK\n";
1557     print &splitline(join " ", "AM_CFLAGS", "=",
1558                      "\$(GTK_CFLAGS)", @amcflags), "\n";
1559     print "else\n";
1560     print &splitline(join " ", "AM_CFLAGS", "=", @amcflags), "\n";
1561     print "endif\n\n";
1562
1563     %amspeciallibs = ();
1564     foreach $obj (sort { $a cmp $b } keys %{$cflags{'am'}}) {
1565       my $flags = $cflags{'am'}->{$obj};
1566       $flags = "" if $flags !~ s/^C//;
1567       print "lib${obj}_a_SOURCES = ", $objtosrc{$obj}, "\n";
1568       print &splitline(join " ", "lib${obj}_a_CFLAGS", "=", @amcflags,
1569                        $flags), "\n";
1570       $amspeciallibs{$obj} = "lib${obj}.a";
1571     }
1572     print &splitline(join " ", "noinst_LIBRARIES", "=",
1573                      sort { $a cmp $b } values %amspeciallibs), "\n\n";
1574
1575     foreach $p (&prognames("X:XT:U:UT")) {
1576       ($prog, $type) = split ",", $p;
1577       print "if HAVE_GTK\n" if $type eq "X" || $type eq "XT";
1578       @progsources = ("${prog}_SOURCES", "=");
1579       %sourcefiles = ();
1580       @ldadd = ();
1581       $objstr = &objects($p, "X", undef, undef);
1582       foreach $obj (split / /,$objstr) {
1583         if ($amspeciallibs{$obj}) {
1584           push @ldadd, $amspeciallibs{$obj};
1585         } else {
1586           $sourcefiles{$objtosrc{$obj}} = 1;
1587         }
1588       }
1589       push @progsources, sort { $a cmp $b } keys %sourcefiles;
1590       print &splitline(join " ", @progsources), "\n";
1591       if ($type eq "X" || $type eq "XT") {
1592         push @ldadd, "\$(GTK_LIBS)";
1593       }
1594       if (@ldadd) {
1595         print &splitline(join " ", "${prog}_LDADD", "=", @ldadd), "\n";
1596       }
1597       print "endif\n" if $type eq "X" || $type eq "XT";
1598       print "\n";
1599     }
1600     print &def($makefile_extra{'am'}->{'end'});
1601     select STDOUT; close OUT;
1602 }
1603
1604 if (defined $makefiles{'lcc'}) {
1605     $dirpfx = &dirpfx($makefiles{'lcc'}, "\\");
1606
1607     ##-- lcc makefile
1608     open OUT, ">$makefiles{'lcc'}"; select OUT;
1609     print
1610     "# Makefile for $project_name under lcc.\n".
1611     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1612     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1613     # lcc command line option is -D not /D
1614     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
1615     print $_;
1616     print
1617     "\n".
1618     "# If you rename this file to `Makefile', you should change this line,\n".
1619     "# so that the .rsp files still depend on the correct makefile.\n".
1620     "MAKEFILE = Makefile.lcc\n".
1621     "\n".
1622     "# C compilation flags\n".
1623     "CFLAGS = -D_WINDOWS " .
1624       (join " ", map {"-I$dirpfx$_"} @srcdirs) .
1625       "\n".
1626     "# Resource compilation flags\n".
1627     "RCFLAGS = ".(join " ", map {"-I$dirpfx$_"} @srcdirs)."\n".
1628     "\n".
1629     "# Get include directory for resource compiler\n".
1630     "\n".
1631     &def($makefile_extra{'lcc'}->{'vars'}) .
1632     "\n";
1633     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
1634     print "\n\n";
1635     foreach $p (&prognames("G:C")) {
1636       ($prog, $type) = split ",", $p;
1637       $objstr = &objects($p, "X.obj", "X.res", undef);
1638       print &splitline("$prog.exe: " . $objstr ), "\n";
1639       $subsystemtype = '';
1640       if ($type eq "G") { $subsystemtype = "-subsystem  windows"; }
1641       my $libss = "shell32.lib wsock32.lib ws2_32.lib winspool.lib winmm.lib imm32.lib";
1642       print &splitline("\tlcclnk $subsystemtype -o $prog.exe $objstr $libss");
1643       print "\n\n";
1644     }
1645
1646     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\", "lcc")) {
1647       if ($forceobj{$d->{obj_orig}}) {
1648          printf("%s: FORCE\n", $d->{obj});
1649       } else {
1650          print &splitline(sprintf("%s: %s", $d->{obj},
1651                           join " ", @{$d->{deps}})), "\n";
1652       }
1653       if ($d->{obj} =~ /\.obj$/) {
1654           print &splitline("\tlcc -O -p6 \$(COMPAT)".
1655                            " \$(CFLAGS) \$(XFLAGS) ".$d->{deps}->[0],69)."\n";
1656       } else {
1657           print &splitline("\tlrc \$(RCFL) -r \$(RCFLAGS) ".
1658                            $d->{deps}->[0],69)."\n";
1659       }
1660     }
1661     print "\n";
1662     print &def($makefile_extra{'lcc'}->{'end'});
1663     print "\nclean:\n".
1664     "\t-del *.obj\n".
1665     "\t-del *.exe\n".
1666     "\t-del *.res\n".
1667     "\n".
1668     "FORCE:\n";
1669
1670     select STDOUT; close OUT;
1671 }
1672
1673 if (defined $makefiles{'osx'}) {
1674     $dirpfx = &dirpfx($makefiles{'osx'}, "/");
1675
1676     ##-- Mac OS X makefile
1677     open OUT, ">$makefiles{'osx'}"; select OUT;
1678     print
1679     "# Makefile for $project_name under Mac OS X.\n".
1680     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1681     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1682     # gcc command line option is -D not /D
1683     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
1684     print $_;
1685     print
1686     "CC = \$(TOOLPATH)gcc\n".
1687     "\n".
1688     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1689                (join " ", map {"-I$dirpfx$_"} @srcdirs))."\n".
1690     "MLDFLAGS = -framework Cocoa\n".
1691     "ULDFLAGS =\n".
1692     "\n" .
1693     &def($makefile_extra{'osx'}->{'vars'}) .
1694     "\n" .
1695     &splitline("all:" . join "", map { " $_" } &progrealnames("MX:U:UT")) .
1696     "\n";
1697     foreach $p (&prognames("MX")) {
1698       ($prog, $type) = split ",", $p;
1699       $objstr = &objects($p, "X.o", undef, undef);
1700       $icon = &special($p, ".icns");
1701       $infoplist = &special($p, "info.plist");
1702       print "${prog}.app:\n\tmkdir -p \$\@\n";
1703       print "${prog}.app/Contents: ${prog}.app\n\tmkdir -p \$\@\n";
1704       print "${prog}.app/Contents/MacOS: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1705       $targets = "${prog}.app/Contents/MacOS/$prog";
1706       if (defined $icon) {
1707         print "${prog}.app/Contents/Resources: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1708         print "${prog}.app/Contents/Resources/${prog}.icns: ${prog}.app/Contents/Resources $icon\n\tcp $icon \$\@\n";
1709         $targets .= " ${prog}.app/Contents/Resources/${prog}.icns";
1710       }
1711       if (defined $infoplist) {
1712         print "${prog}.app/Contents/Info.plist: ${prog}.app/Contents/Resources $infoplist\n\tcp $infoplist \$\@\n";
1713         $targets .= " ${prog}.app/Contents/Info.plist";
1714       }
1715       $targets .= " \$(${prog}_extra)";
1716       print &splitline("${prog}: $targets", 69) . "\n\n";
1717       print &splitline("${prog}.app/Contents/MacOS/$prog: ".
1718                        "${prog}.app/Contents/MacOS " . $objstr), "\n";
1719       $libstr = &objects($p, undef, undef, "-lX");
1720       print &splitline("\t\$(CC) \$(MLDFLAGS) -o \$@ " .
1721                        $objstr . " $libstr", 69), "\n\n";
1722     }
1723     foreach $p (&prognames("U:UT")) {
1724       ($prog, $type) = split ",", $p;
1725       $objstr = &objects($p, "X.o", undef, undef);
1726       print &splitline($prog . ": " . $objstr), "\n";
1727       $libstr = &objects($p, undef, undef, "-lX");
1728       print &splitline("\t\$(CC) \$(ULDFLAGS) -o \$@ " .
1729                        $objstr . " $libstr", 69), "\n\n";
1730     }
1731     foreach $d (&deps("X.o", undef, $dirpfx, "/", "osx")) {
1732       if ($forceobj{$d->{obj_orig}}) {
1733          printf("%s: FORCE\n", $d->{obj});
1734       } else {
1735          print &splitline(sprintf("%s: %s", $d->{obj},
1736                                   join " ", @{$d->{deps}})), "\n";
1737       }
1738       $firstdep = $d->{deps}->[0];
1739       if ($firstdep =~ /\.c$/) {
1740           print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS) -c \$<\n";
1741       } elsif ($firstdep =~ /\.m$/) {
1742           print "\t\$(CC) -x objective-c \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS) -c \$<\n";
1743       }
1744     }
1745     print "\n".&def($makefile_extra{'osx'}->{'end'});
1746     print "\nclean:\n".
1747     "\trm -f *.o *.dmg". (join "", map { " $_" } &progrealnames("U:UT")) . "\n".
1748     "\trm -rf *.app\n".
1749     "\n".
1750     "FORCE:\n";
1751     select STDOUT; close OUT;
1752 }
1753
1754 if (defined $makefiles{'devcppproj'}) {
1755     $dirpfx = &dirpfx($makefiles{'devcppproj'}, "\\");
1756     $orig_dir = cwd;
1757
1758     ##-- Dev-C++ 5 projects
1759     #
1760     # Note: All files created in this section are written in binary
1761     # mode to prevent any posibility of misinterpreted line endings.
1762     # I don't know if Dev-C++ is as touchy as MSVC with LF-only line
1763     # endings. But however, CRLF line endings are the common way on
1764     # Win32 machines where Dev-C++ is running.
1765     # Hence, in order for mkfiles.pl to generate CRLF project files
1766     # even when run from Unix, I make sure all files are binary and
1767     # explicitly write the CRLFs.
1768     #
1769     # Create directories if necessary
1770     mkdir $makefiles{'devcppproj'}
1771         if(! -d $makefiles{'devcppproj'});
1772     chdir $makefiles{'devcppproj'};
1773     @deps = &deps("X.obj", "X.res", $dirpfx, "\\", "devcppproj");
1774     %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
1775     # Make dir names FAT/NTFS compatible
1776     my @srcdirs = @srcdirs;
1777     for ($i=0; $i<@srcdirs; $i++) {
1778       $srcdirs[$i] =~ s/\//\\/g;
1779       $srcdirs[$i] =~ s/\\$//;
1780     }
1781     # Create the project files
1782     # Get names of all Windows projects (GUI and console)
1783     my @prognames = &prognames("G:C");
1784     foreach $progname (@prognames) {
1785       create_devcpp_project(\%all_object_deps, $progname);
1786     }
1787
1788     chdir $orig_dir;
1789
1790     sub create_devcpp_project {
1791       my ($all_object_deps, $progname) = @_;
1792       # Construct program's dependency info (Taken from 'vcproj', seems to work right here, too.)
1793       %seen_objects = ();
1794       %lib_files = ();
1795       %source_files = ();
1796       %header_files = ();
1797       %resource_files = ();
1798       @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
1799       foreach $object_file (@object_files) {
1800       next if defined $seen_objects{$object_file};
1801       $seen_objects{$object_file} = 1;
1802       if($object_file =~ /\.lib$/io) {
1803     $lib_files{$object_file} = 1;
1804     next;
1805       }
1806       $object_deps = $all_object_deps{$object_file};
1807       foreach $object_dep (@$object_deps) {
1808     if($object_dep =~ /\.c$/io) {
1809         $source_files{$object_dep} = 1;
1810         next;
1811     }
1812     if($object_dep =~ /\.h$/io) {
1813         $header_files{$object_dep} = 1;
1814         next;
1815     }
1816     if($object_dep =~ /\.(rc|ico)$/io) {
1817         $resource_files{$object_dep} = 1;
1818         next;
1819     }
1820       }
1821       }
1822       $libs = join " ", sort keys %lib_files;
1823       @source_files = sort keys %source_files;
1824       @header_files = sort keys %header_files;
1825       @resources = sort keys %resource_files;
1826   ($windows_project, $type) = split ",", $progname;
1827       mkdir $windows_project
1828       if(! -d $windows_project);
1829       chdir $windows_project;
1830
1831   $subsys = ($type eq "G") ? "0" : "1";  # 0 = Win32 GUI, 1 = Win32 Console
1832       open OUT, ">$windows_project.dev"; binmode OUT; select OUT;
1833       print
1834       "# DEV-C++ 5 Project File - $windows_project.dev\r\n".
1835       "# ** DO NOT EDIT **\r\n".
1836       "\r\n".
1837       # No difference between DEBUG and RELEASE here as in 'vcproj', because
1838       # Dev-C++ does not support mutiple compilation profiles in one single project.
1839       # (At least I can say this for Dev-C++ 5 Beta)
1840       "[Project]\r\n".
1841       "FileName=$windows_project.dev\r\n".
1842       "Name=$windows_project\r\n".
1843       "Ver=1\r\n".
1844       "IsCpp=1\r\n".
1845       "Type=$subsys\r\n".
1846       # Multimon is disabled here, as Dev-C++ (Version 5 Beta) does not have multimon.h
1847       "Compiler=-W -D__GNUWIN32__ -DWIN32 -DNDEBUG -D_WINDOWS -DNO_MULTIMON -D_MBCS_\@\@_\r\n".
1848       "CppCompiler=-W -D__GNUWIN32__ -DWIN32 -DNDEBUG -D_WINDOWS -DNO_MULTIMON -D_MBCS_\@\@_\r\n".
1849       "Includes=" . (join ";", map {"..\\..\\$dirpfx$_"} @srcdirs) . "\r\n".
1850       "Linker=-ladvapi32 -lcomctl32 -lcomdlg32 -lgdi32 -limm32 -lshell32 -luser32 -lwinmm -lwinspool_\@\@_\r\n".
1851       "Libs=\r\n".
1852       "UnitCount=" . (@source_files + @header_files + @resources) . "\r\n".
1853       "Folders=\"Header Files\",\"Resource Files\",\"Source Files\"\r\n".
1854       "ObjFiles=\r\n".
1855       "PrivateResource=${windows_project}_private.rc\r\n".
1856       "ResourceIncludes=..\\..\\..\\WINDOWS\r\n".
1857       "MakeIncludes=\r\n".
1858       "Icon=\r\n". # It's ok to leave this blank.
1859       "ExeOutput=\r\n".
1860       "ObjectOutput=\r\n".
1861       "OverrideOutput=0\r\n".
1862       "OverrideOutputName=$windows_project.exe\r\n".
1863       "HostApplication=\r\n".
1864       "CommandLine=\r\n".
1865       "UseCustomMakefile=0\r\n".
1866       "CustomMakefile=\r\n".
1867       "IncludeVersionInfo=0\r\n".
1868       "SupportXPThemes=0\r\n".
1869       "CompilerSet=0\r\n".
1870       "CompilerSettings=0000000000000000000000\r\n".
1871       "\r\n";
1872       $unit_count = 1;
1873       foreach $source_file (@source_files) {
1874       print
1875         "[Unit$unit_count]\r\n".
1876         "FileName=..\\..\\$source_file\r\n".
1877         "Folder=Source Files\r\n".
1878         "Compile=1\r\n".
1879         "CompileCpp=0\r\n".
1880         "Link=1\r\n".
1881         "Priority=1000\r\n".
1882         "OverrideBuildCmd=0\r\n".
1883         "BuildCmd=\r\n".
1884         "\r\n";
1885       $unit_count++;
1886   }
1887       foreach $header_file (@header_files) {
1888       print
1889         "[Unit$unit_count]\r\n".
1890         "FileName=..\\..\\$header_file\r\n".
1891         "Folder=Header Files\r\n".
1892         "Compile=1\r\n".
1893         "CompileCpp=1\r\n". # Dev-C++ want's to compile all header files with both compilers C and C++. It does not hurt.
1894         "Link=1\r\n".
1895         "Priority=1000\r\n".
1896         "OverrideBuildCmd=0\r\n".
1897         "BuildCmd=\r\n".
1898         "\r\n";
1899       $unit_count++;
1900   }
1901       foreach $resource_file (@resources) {
1902       if ($resource_file =~ /.*\.(ico|cur|bmp|dlg|rc2|rct|bin|rgs|gif|jpg|jpeg|jpe)/io) { # Default filter as in 'vcproj'
1903         $Compile = "0";    # Don't compile images and other binary resource files
1904         $CompileCpp = "0";
1905       } else {
1906         $Compile = "1";
1907         $CompileCpp = "1"; # Dev-C++ want's to compile all .rc files with both compilers C and C++. It does not hurt.
1908       }
1909       print
1910         "[Unit$unit_count]\r\n".
1911         "FileName=..\\..\\$resource_file\r\n".
1912         "Folder=Resource Files\r\n".
1913         "Compile=$Compile\r\n".
1914         "CompileCpp=$CompileCpp\r\n".
1915         "Link=0\r\n".
1916         "Priority=1000\r\n".
1917         "OverrideBuildCmd=0\r\n".
1918         "BuildCmd=\r\n".
1919         "\r\n";
1920       $unit_count++;
1921   }
1922       #Note: By default, [VersionInfo] is not used.
1923       print
1924       "[VersionInfo]\r\n".
1925       "Major=0\r\n".
1926       "Minor=0\r\n".
1927       "Release=1\r\n".
1928       "Build=1\r\n".
1929       "LanguageID=1033\r\n".
1930       "CharsetID=1252\r\n".
1931       "CompanyName=\r\n".
1932       "FileVersion=0.1\r\n".
1933       "FileDescription=\r\n".
1934       "InternalName=\r\n".
1935       "LegalCopyright=\r\n".
1936       "LegalTrademarks=\r\n".
1937       "OriginalFilename=$windows_project.exe\r\n".
1938       "ProductName=$windows_project\r\n".
1939       "ProductVersion=0.1\r\n".
1940       "AutoIncBuildNr=0\r\n";
1941       select STDOUT; close OUT;
1942       chdir "..";
1943     }
1944 }
1945
1946 # All done, so do the Unix postprocessing if asked to.
1947
1948 if ($do_unix) {
1949     chdir $orig_dir;
1950     system "./mkauto.sh";
1951     die "mkfiles.pl: mkauto.sh returned $?\n" if $? > 0;
1952     if ($do_unix == 1) {
1953         chdir ($targetdir = "unix")
1954             or die "$targetdir: chdir: $!\n";
1955     }
1956     system "./configure", @confargs;
1957     die "mkfiles.pl: configure returned $?\n" if $? > 0;
1958 }
1959
1960 sub invent_guid($) {
1961     my ($name) = @_;
1962
1963     # Invent a GUID for use in Visual Studio project files. We need
1964     # a few of these for every executable file we build.
1965     #
1966     # In order to avoid having to use the non-core Perl module
1967     # Data::GUID, and also arrange for GUIDs to be stable, we generate
1968     # our GUIDs by hashing a pile of fixed (but originally randomly
1969     # generated) data with the filename for which we need an id.
1970     #
1971     # Hashing _just_ the filenames would clearly be cheating (it's
1972     # quite conceivable that someone might hash the same string for
1973     # another reason and so generate a colliding GUID), but hashing a
1974     # whole SHA-512 data block of random gibberish as well should make
1975     # these GUIDs pseudo-random enough to not collide with anyone
1976     # else's.
1977
1978     my $randdata = pack "N*",
1979     0xD4AB035F,0x76998BA0,0x2DCCB0BD,0x6D3FA320,0x53638051,0xFE312F35,
1980     0xDE1CECC0,0x784DF852,0x6C9F4589,0x54B7AC23,0x14E7A1C4,0xF9BF04DF,
1981     0x19C08B6D,0x3FB69EF1,0xB2DA9043,0xDB5362F3,0x25718DB6,0x733560DA,
1982     0xFEF871B0,0xFECF7A0C,0x67D19C95,0xB492E911,0xF5D562A3,0xFCE1D478,
1983     0x02C50434,0xF7326B7E,0x93D39872,0xCF0D0269,0x9EF24C0F,0x827689AD,
1984     0x88BD20BC,0x74EA6AFE,0x29223682,0xB9AB9287,0x7EA7CE4F,0xCF81B379,
1985     0x9AE4A954,0x81C7AD97,0x2FF2F031,0xC51DA3C2,0xD311CCE7,0x0A31EB8B,
1986     0x1AB04242,0xAF53B714,0xFC574D40,0x8CB4ED01,0x29FEB16F,0x4904D7ED,
1987     0xF5C5F5E1,0xF138A4C2,0xA9D881CE,0xCEA65187,0x4421BA97,0x0EE8428E,
1988     0x9556E384,0x6D0484C9,0x561BD84B,0xD9516A40,0x6B4FD33F,0xDDFFE4C8,
1989     0x3D5DF8A5,0xFE6B7D99,0x3443371B,0xF4E30A3E,0xE62B9FDA,0x6BAA75DB,
1990     0x9EF3C2C7,0x6815CA42,0xE6536076,0xF851E6E2,0x39D16E69,0xBCDF3BB6,
1991     0x50EFFA41,0x378CDF2A,0xB5EC0D0C,0x1E94C433,0xE818241A,0x2689EB1F,
1992     0xB649CEF9,0xD7344D46,0x59C1BB13,0x27511FDF,0x7DAD1768,0xB355E29E,
1993     0xDFAE550C,0x2433005B,0x09DE10B0,0xAA00BA6B,0xC144ED2D,0x8513D007,
1994     0xB0315232,0x7A10DAB6,0x1D97654E,0xF048214D,0xE3059E75,0x83C225D1,
1995     0xFC7AB177,0x83F2B553,0x79F7A0AF,0x1C94582C,0xF5E4AF4B,0xFB39C865,
1996     0x58ABEB27,0xAAB28058,0x52C15A89,0x0EBE9741,0x343F4D26,0xF941202A,
1997     0xA32FD32F,0xDCC055B8,0x64281BF3,0x468BD7BA,0x0CEE09D3,0xBB5FD2B6,
1998     0xA528D412,0xA6A6967E,0xEAAF5DAE,0xDE7B2FAE,0xCA36887B,0x0DE196EB,
1999     0x74B95EF0,0x9EB8B7C2,0x020BFC83,0x1445086F,0xBF4B61B2,0x89AFACEC,
2000     0x80A5CD69,0xC790F744,0x435A6998,0x8DE7AC48,0x32F31BC9,0x8F760D3D,
2001     0xF02A74CB,0xD7B47E20,0x9EC91035,0x70FDE74D,0x9B531362,0x9D81739A,
2002     0x59ADC2EB,0x511555B5,0xCA84B8D5,0x3EC325FF,0x2E442A4C,0x82AF30D9,
2003     0xBFD3EC87,0x90C59E07,0x1C6DC991,0x2D16B822,0x7EA44EB5,0x3A655A39,
2004     0xAB640886,0x09311821,0x777801D9,0x489DBE61,0xA1FFEC65,0x978B49B1,
2005     0x7DB700CD,0x263CF3D6,0xF977E89F,0xBA0B3D01,0x6C6CED19,0x1BE6F23A,
2006     0x19E0ED98,0x8E71A499,0x70BA3271,0x3FB7EE98,0xABA46848,0x2B797959,
2007     0x72C6DE59,0xE08B795C,0x02936C39,0x02185CCB,0xD6F3CE18,0xD0157A40,
2008     0x833DEC3F,0x319B00C4,0x97B59513,0x900B81FD,0x9A022379,0x16E44E1A,
2009     0x0C4CC540,0xCA98E7F9,0xF9431A26,0x290BCFAC,0x406B82C0,0xBC1C4585,
2010     0x55C54528,0x811EBB77,0xD4EDD4F3,0xA70DC02E,0x8AD5C0D1,0x28D64EF4,
2011     0xBEFF5C69,0x99852C4A,0xB4BBFF7B,0x069230AC,0xA3E141FA,0x4E99FB0E,
2012     0xBC154DAA,0x323C7F15,0x86E0247E,0x2EEA3054,0xC9CA1D32,0x8964A006,
2013     0xC93978AC,0xF9B2C159,0x03F2079E,0xB051D284,0x4A7EA9A9,0xF001DA1F,
2014     0xD47A0DAA,0xCF7B6B73,0xF18293B2,0x84303E34,0xF8BC76C4,0xAFBEE24F,
2015     0xB589CA80,0x77B5BF86,0x21B9FD5B,0x1A5071DF,0xA3863110,0x0E50CA61,
2016     0x939151A5,0xD2A59021,0x83A9CDCE,0xCEC69767,0xC906BB16,0x3EE1FF4D,
2017     0x1321EAE4,0x0BF940D6,0x52471E61,0x8A087056,0x66E54293,0xF84AAB9B,
2018     0x08835EF1,0x8F12B77A,0xD86935A5,0x200281D7,0xCD3C37C9,0x30ABEC05,
2019     0x7067E8A0,0x608C4838,0xC9F51CDE,0xA6D318DE,0x41C05B2A,0x694CCE0E,
2020     0xC7842451,0xA3194393,0xFBDC2C84,0xA6D2B577,0xC91E7924,0x01EDA708,
2021     0x22FBB61E,0x662F9B7B,0xDE3150C3,0x2397058C;
2022     my $digest = sha512_hex($name . "\0" . $randdata);
2023     return sprintf("%s-%s-%04x-%04x-%s",
2024                    substr($digest,0,8),
2025                    substr($digest,8,4),
2026                    0x4000 | (0xFFF & hex(substr($digest,12,4))),
2027                    0x8000 | (0x3FFF & hex(substr($digest,16,4))),
2028                    substr($digest,20,12));
2029 }