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