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