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