]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mkfiles.pl
Switch to using automake for the Unix autoconfigured build.
[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 Cwd;
21
22 open IN, "Recipe" or do {
23     # We want to deal correctly with being run from one of the
24     # subdirs in the source tree. So if we can't find Recipe here,
25     # try one level up.
26     chdir "..";
27     open IN, "Recipe" or die "unable to open Recipe file\n";
28 };
29
30 # HACK: One of the source files in `charset' is auto-generated by
31 # sbcsgen.pl. We need to generate that _now_, before attempting
32 # dependency analysis.
33 eval 'chdir "charset"; require "sbcsgen.pl"; chdir ".."';
34
35 @srcdirs = ("./");
36
37 $divert = undef; # ref to scalar in which text is currently being put
38 $help = ""; # list of newline-free lines of help text
39 $project_name = "project"; # this is a good enough default
40 %makefiles = (); # maps makefile types to output makefile pathnames
41 %makefile_extra = (); # maps makefile types to extra Makefile text
42 %programs = (); # maps prog name + type letter to listref of objects/resources
43 %groups = (); # maps group name to listref of objects/resources
44
45 while (<IN>) {
46   chomp;
47   @_ = split;
48
49   # If we're gathering help text, keep doing so.
50   if (defined $divert) {
51       if ((defined $_[0]) && $_[0] eq "!end") {
52           $divert = undef;
53       } else {
54           ${$divert} .= "$_\n";
55       }
56       next;
57   }
58   # Skip comments and blank lines.
59   next if /^\s*#/ or scalar @_ == 0;
60
61   if ($_[0] eq "!begin" and $_[1] eq "help") { $divert = \$help; next; }
62   if ($_[0] eq "!end") { $divert = undef; next; }
63   if ($_[0] eq "!name") { $project_name = $_[1]; next; }
64   if ($_[0] eq "!srcdir") { push @srcdirs, $_[1]; next; }
65   if ($_[0] eq "!makefile" and &mfval($_[1])) { $makefiles{$_[1]}=$_[2]; next;}
66   if ($_[0] eq "!specialobj" and &mfval($_[1])) { $specialobj{$_[1]}->{$_[2]} = 1; next;}
67   if ($_[0] eq "!cflags" and &mfval($_[1])) {
68       ($rest = $_) =~ s/^\s*\S+\s+\S+\s+\S+\s*//; # find rest of input line
69       $rest = 1 if $rest eq "";
70       $cflags{$_[1]}->{$_[2]} = $rest;
71       next;
72   }
73   if ($_[0] eq "!forceobj") { $forceobj{$_[1]} = 1; next; }
74   if ($_[0] eq "!begin") {
75       if (&mfval($_[1])) {
76           $sect = $_[2] ? $_[2] : "end";
77           $divert = \($makefile_extra{$_[1]}->{$sect});
78       } else {
79           $dummy = '';
80           $divert = \$dummy;
81       }
82       next;
83   }
84   # If we're gathering help/verbatim text, keep doing so.
85   if (defined $divert) { ${$divert} .= "$_\n"; next; }
86   # Ignore blank lines.
87   next if scalar @_ == 0;
88
89   # Now we have an ordinary line. See if it's an = line, a : line
90   # or a + line.
91   @objs = @_;
92
93   if ($_[0] eq "+") {
94     $listref = $lastlistref;
95     $prog = undef;
96     die "$.: unexpected + line\n" if !defined $lastlistref;
97   } elsif ($_[1] eq "=") {
98     $groups{$_[0]} = [] if !defined $groups{$_[0]};
99     $listref = $groups{$_[0]};
100     $prog = undef;
101     shift @objs; # eat the group name
102   } elsif ($_[1] eq ":") {
103     $listref = [];
104     $prog = $_[0];
105     shift @objs; # eat the program name
106   } else {
107     die "$.: unrecognised line type\n";
108   }
109   shift @objs; # eat the +, the = or the :
110
111   while (scalar @objs > 0) {
112     $i = shift @objs;
113     if ($groups{$i}) {
114       foreach $j (@{$groups{$i}}) { unshift @objs, $j; }
115     } elsif (($i eq "[G]" or $i eq "[C]" or $i eq "[M]" or
116               $i eq "[X]" or $i eq "[U]" or $i eq "[MX]") and defined $prog) {
117       $type = substr($i,1,(length $i)-2);
118     } else {
119       push @$listref, $i;
120     }
121   }
122   if ($prog and $type) {
123     die "multiple program entries for $prog [$type]\n"
124         if defined $programs{$prog . "," . $type};
125     $programs{$prog . "," . $type} = $listref;
126   }
127   $lastlistref = $listref;
128 }
129
130 close IN;
131
132 # Now retrieve the complete list of objects and resource files, and
133 # construct dependency data for them. While we're here, expand the
134 # object list for each program, and complain if its type isn't set.
135 @prognames = sort keys %programs;
136 %depends = ();
137 @scanlist = ();
138 foreach $i (@prognames) {
139   ($prog, $type) = split ",", $i;
140   # Strip duplicate object names.
141   $prev = '';
142   @list = grep { $status = ($prev ne $_); $prev=$_; $status }
143           sort @{$programs{$i}};
144   $programs{$i} = [@list];
145   foreach $j (@list) {
146     # Dependencies for "x" start with "x.c" or "x.m" (depending on
147     # which one exists).
148     # Dependencies for "x.res" start with "x.rc".
149     # Dependencies for "x.rsrc" start with "x.r".
150     # Both types of file are pushed on the list of files to scan.
151     # Libraries (.lib) don't have dependencies at all.
152     if ($j =~ /^(.*)\.res$/) {
153       $file = "$1.rc";
154       $depends{$j} = [$file];
155       push @scanlist, $file;
156     } elsif ($j =~ /^(.*)\.rsrc$/) {
157       $file = "$1.r";
158       $depends{$j} = [$file];
159       push @scanlist, $file;
160     } elsif ($j !~ /\./) {
161       $file = "$j.c";
162       $file = "$j.m" unless &findfile($file);
163       $depends{$j} = [$file];
164       push @scanlist, $file;
165     }
166   }
167 }
168
169 # Scan each file on @scanlist and find further inclusions.
170 # Inclusions are given by lines of the form `#include "otherfile"'
171 # (system headers are automatically ignored by this because they'll
172 # be given in angle brackets). Files included by this method are
173 # added back on to @scanlist to be scanned in turn (if not already
174 # done).
175 #
176 # Resource scripts (.rc) can also include a file by means of:
177 #  - a line # ending `ICON "filename"';
178 #  - a line ending `RT_MANIFEST "filename"'.
179 # Files included by this method are not added to @scanlist because
180 # they can never include further files.
181 #
182 # In this pass we write out a hash %further which maps a source
183 # file name into a listref containing further source file names.
184
185 %further = ();
186 %allsourcefiles = (); # this is wanted by some makefiles
187 while (scalar @scanlist > 0) {
188   $file = shift @scanlist;
189   next if defined $further{$file}; # skip if we've already done it
190   $further{$file} = [];
191   $dirfile = &findfile($file);
192   $allsourcefiles{$dirfile} = 1;
193   open IN, "$dirfile" or die "unable to open source file $file\n";
194   while (<IN>) {
195     chomp;
196     /^\s*#include\s+\"([^\"]+)\"/ and do {
197       push @{$further{$file}}, $1;
198       push @scanlist, $1;
199       next;
200     };
201     /(RT_MANIFEST|ICON)\s+\"([^\"]+)\"\s*$/ and do {
202       push @{$further{$file}}, $2;
203       next;
204     }
205   }
206   close IN;
207 }
208
209 # Now we're ready to generate the final dependencies section. For
210 # each key in %depends, we must expand the dependencies list by
211 # iteratively adding entries from %further.
212 foreach $i (keys %depends) {
213   %dep = ();
214   @scanlist = @{$depends{$i}};
215   foreach $i (@scanlist) { $dep{$i} = 1; }
216   while (scalar @scanlist > 0) {
217     $file = shift @scanlist;
218     foreach $j (@{$further{$file}}) {
219       if (!$dep{$j}) {
220         $dep{$j} = 1;
221         push @{$depends{$i}}, $j;
222         push @scanlist, $j;
223       }
224     }
225   }
226 #  printf "%s: %s\n", $i, join ' ',@{$depends{$i}};
227 }
228
229 # Validation of input.
230
231 sub mfval($) {
232     my ($type) = @_;
233     # Returns true if the argument is a known makefile type. Otherwise,
234     # prints a warning and returns false;
235     if (grep { $type eq $_ }
236         ("vc","vcproj","cygwin","borland","lcc","devcppproj","gtk","unix",
237          "am","osx",)) {
238             return 1;
239         }
240     warn "$.:unknown makefile type '$type'\n";
241     return 0;
242 }
243
244 # Utility routines while writing out the Makefiles.
245
246 sub def {
247     my ($x) = shift @_;
248     return (defined $x) ? $x : "";
249 }
250
251 sub dirpfx {
252     my ($path) = shift @_;
253     my ($sep) = shift @_;
254     my $ret = "";
255     my $i;
256
257     while (($i = index $path, $sep) >= 0 ||
258            ($j = index $path, "/") >= 0) {
259         if ($i >= 0 and ($j < 0 or $i < $j)) {
260             $path = substr $path, ($i + length $sep);
261         } else {
262             $path = substr $path, ($j + 1);
263         }
264         $ret .= "..$sep";
265     }
266     return $ret;
267 }
268
269 sub findfile {
270   my ($name) = @_;
271   my $dir = '';
272   my $i;
273   my $outdir = undef;
274   unless (defined $findfilecache{$name}) {
275     $i = 0;
276     foreach $dir (@srcdirs) {
277       if (-f "$dir$name") {
278         $outdir = $dir;
279         $i++;
280         $outdir =~ s/^\.\///;
281       }
282     }
283     die "multiple instances of source file $name\n" if $i > 1;
284     $findfilecache{$name} = (defined $outdir ? $outdir . $name : undef);
285   }
286   return $findfilecache{$name};
287 }
288
289 sub objects {
290   my ($prog, $otmpl, $rtmpl, $ltmpl, $prefix, $dirsep) = @_;
291   my @ret;
292   my ($i, $x, $y);
293   ($otmpl, $rtmpl, $ltmpl) = map { defined $_ ? $_ : "" } ($otmpl, $rtmpl, $ltmpl);
294   @ret = ();
295   foreach $i (@{$programs{$prog}}) {
296     $x = "";
297     if ($i =~ /^(.*)\.(res|rsrc)/) {
298       $y = $1;
299       ($x = $rtmpl) =~ s/X/$y/;
300     } elsif ($i =~ /^(.*)\.lib/) {
301       $y = $1;
302       ($x = $ltmpl) =~ s/X/$y/;
303     } elsif ($i !~ /\./) {
304       ($x = $otmpl) =~ s/X/$i/;
305     }
306     push @ret, $x if $x ne "";
307   }
308   return join " ", @ret;
309 }
310
311 sub special {
312   my ($prog, $suffix) = @_;
313   my @ret;
314   my ($i, $x, $y);
315   ($otmpl, $rtmpl, $ltmpl) = map { defined $_ ? $_ : "" } ($otmpl, $rtmpl, $ltmpl);
316   @ret = ();
317   foreach $i (@{$programs{$prog}}) {
318     if (substr($i, (length $i) - (length $suffix)) eq $suffix) {
319       push @ret, $i;
320     }
321   }
322   return (scalar @ret) ? (join " ", @ret) : undef;
323 }
324
325 sub splitline {
326   my ($line, $width, $splitchar) = @_;
327   my $result = "";
328   my $len;
329   $len = (defined $width ? $width : 76);
330   $splitchar = (defined $splitchar ? $splitchar : '\\');
331   while (length $line > $len) {
332     $line =~ /^(.{0,$len})\s(.*)$/ or $line =~ /^(.{$len,}?\s(.*)$/;
333     $result .= $1;
334     $result .= " ${splitchar}\n\t\t" if $2 ne '';
335     $line = $2;
336     $len = 60;
337   }
338   return $result . $line;
339 }
340
341 sub deps {
342   my ($otmpl, $rtmpl, $prefix, $dirsep, $mftyp, $depchar, $splitchar) = @_;
343   my ($i, $x, $y);
344   my @deps;
345   my @ret;
346   @ret = ();
347   $depchar ||= ':';
348   foreach $i (sort keys %depends) {
349     next if $specialobj{$mftyp}->{$i};
350     if ($i =~ /^(.*)\.(res|rsrc)/) {
351       next if !defined $rtmpl;
352       $y = $1;
353       ($x = $rtmpl) =~ s/X/$y/;
354     } else {
355       ($x = $otmpl) =~ s/X/$i/;
356     }
357     @deps = @{$depends{$i}};
358     @deps = map {
359       $_ = &findfile($_);
360       s/\//$dirsep/g;
361       $_ = $prefix . $_;
362     } @deps;
363     push @ret, {obj => $x, obj_orig => $i, deps => [@deps]};
364   }
365   return @ret;
366 }
367
368 sub prognames {
369   my ($types) = @_;
370   my ($n, $prog, $type);
371   my @ret;
372   @ret = ();
373   foreach $n (@prognames) {
374     ($prog, $type) = split ",", $n;
375     push @ret, $n if index(":$types:", ":$type:") >= 0;
376   }
377   return @ret;
378 }
379
380 sub progrealnames {
381   my ($types) = @_;
382   my ($n, $prog, $type);
383   my @ret;
384   @ret = ();
385   foreach $n (@prognames) {
386     ($prog, $type) = split ",", $n;
387     push @ret, $prog if index(":$types:", ":$type:") >= 0;
388   }
389   return @ret;
390 }
391
392 sub manpages {
393   my ($types,$suffix) = @_;
394
395   # assume that all UNIX programs have a man page
396   if($suffix eq "1" && $types =~ /:X:/) {
397     return map("$_.1", &progrealnames($types));
398   }
399   return ();
400 }
401
402 # Now we're ready to output the actual Makefiles.
403
404 if (defined $makefiles{'cygwin'}) {
405     $dirpfx = &dirpfx($makefiles{'cygwin'}, "/");
406
407     ##-- CygWin makefile
408     open OUT, ">$makefiles{'cygwin'}"; select OUT;
409     print
410     "# Makefile for $project_name under cygwin.\n".
411     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
412     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
413     # gcc command line option is -D not /D
414     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
415     print $_;
416     print
417     "\n".
418     "# You can define this path to point at your tools if you need to\n".
419     "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
420     "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
421     "CC = \$(TOOLPATH)gcc\n".
422     "RC = \$(TOOLPATH)windres\n".
423     "# Uncomment the following two lines to compile under Winelib\n".
424     "# CC = winegcc\n".
425     "# RC = wrc\n".
426     "# You may also need to tell windres where to find include files:\n".
427     "# RCINC = --include-dir c:\\cygwin\\include\\\n".
428     "\n".
429     &splitline("CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT".
430       " -D_NO_OLDNAMES -DNO_MULTIMON -DNO_HTMLHELP " .
431                (join " ", map {"-I$dirpfx$_"} @srcdirs)) .
432                "\n".
433     "LDFLAGS = -mno-cygwin -s\n".
434     &splitline("RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1".
435       " --define WINVER=0x0400")."\n".
436     "\n".
437     $makefile_extra{'cygwin'}->{'vars'} .
438     "\n".
439     ".SUFFIXES:\n".
440     "\n";
441     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
442     print "\n\n";
443     foreach $p (&prognames("G:C")) {
444       ($prog, $type) = split ",", $p;
445       $objstr = &objects($p, "X.o", "X.res.o", undef);
446       print &splitline($prog . ".exe: " . $objstr), "\n";
447       my $mw = $type eq "G" ? " -mwindows" : "";
448       $libstr = &objects($p, undef, undef, "-lX");
449       print &splitline("\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " .
450                        "-Wl,-Map,$prog.map " .
451                        $objstr . " $libstr", 69), "\n\n";
452     }
453     foreach $d (&deps("X.o", "X.res.o", $dirpfx, "/", "cygwin")) {
454       if ($forceobj{$d->{obj_orig}}) {
455         printf ("%s: FORCE\n", $d->{obj});
456       } else {
457         print &splitline(sprintf("%s: %s", $d->{obj},
458                          join " ", @{$d->{deps}})), "\n";
459       }
460       if ($d->{obj} =~ /\.res\.o$/) {
461           print "\t\$(RC) \$(RCFL) \$(RCFLAGS) ".$d->{deps}->[0]." ".$d->{obj}."\n\n";
462       } else {
463           print "\t\$(CC) \$(COMPAT) \$(CFLAGS) \$(XFLAGS) -c ".$d->{deps}->[0]."\n\n";
464       }
465     }
466     print "\n";
467     print $makefile_extra{'cygwin'}->{'end'};
468     print "\nclean:\n".
469     "\trm -f *.o *.exe *.res.o *.map\n".
470     "\n".
471     "FORCE:\n";
472     select STDOUT; close OUT;
473
474 }
475
476 ##-- Borland makefile
477 if (defined $makefiles{'borland'}) {
478     $dirpfx = &dirpfx($makefiles{'borland'}, "\\");
479
480     %stdlibs = (  # Borland provides many Win32 API libraries intrinsically
481       "advapi32" => 1,
482       "comctl32" => 1,
483       "comdlg32" => 1,
484       "gdi32" => 1,
485       "imm32" => 1,
486       "shell32" => 1,
487       "user32" => 1,
488       "winmm" => 1,
489       "winspool" => 1,
490       "wsock32" => 1,
491     );
492     open OUT, ">$makefiles{'borland'}"; select OUT;
493     print
494     "# Makefile for $project_name under Borland C.\n".
495     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
496     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
497     # bcc32 command line option is -D not /D
498     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
499     print $_;
500     print
501     "\n".
502     "# If you rename this file to `Makefile', you should change this line,\n".
503     "# so that the .rsp files still depend on the correct makefile.\n".
504     "MAKEFILE = Makefile.bor\n".
505     "\n".
506     "# C compilation flags\n".
507     "CFLAGS = -D_WINDOWS -DWINVER=0x0500\n".
508     "# Resource compilation flags\n".
509     "RCFLAGS = -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0401\n".
510     "\n".
511     "# Get include directory for resource compiler\n".
512     "!if !\$d(BCB)\n".
513     "BCB = \$(MAKEDIR)\\..\n".
514     "!endif\n".
515     "\n".
516     $makefile_extra{'borland'}->{'vars'} .
517     "\n".
518     ".c.obj:\n".
519     &splitline("\tbcc32 -w-aus -w-ccc -w-par -w-pia \$(COMPAT)".
520                " \$(CFLAGS) \$(XFLAGS) ".
521                (join " ", map {"-I$dirpfx$_"} @srcdirs) .
522                " /c \$*.c",69)."\n".
523     ".rc.res:\n".
524     &splitline("\tbrcc32 \$(RCFL) -i \$(BCB)\\include -r".
525       " \$(RCFLAGS) \$*.rc",69)."\n".
526     "\n";
527     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
528     print "\n\n";
529     foreach $p (&prognames("G:C")) {
530       ($prog, $type) = split ",", $p;
531       $objstr =  &objects($p, "X.obj", "X.res", undef);
532       print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
533       my $ap = ($type eq "G") ? "-aa" : "-ap";
534       print "\tilink32 $ap -Gn -L\$(BCB)\\lib \@$prog.rsp\n\n";
535     }
536     foreach $p (&prognames("G:C")) {
537       ($prog, $type) = split ",", $p;
538       print $prog, ".rsp: \$(MAKEFILE)\n";
539       $objstr = &objects($p, "X.obj", undef, undef);
540       @objlist = split " ", $objstr;
541       @objlines = ("");
542       foreach $i (@objlist) {
543         if (length($objlines[$#objlines] . " $i") > 50) {
544           push @objlines, "";
545         }
546         $objlines[$#objlines] .= " $i";
547       }
548       $c0w = ($type eq "G") ? "c0w32" : "c0x32";
549       print "\techo $c0w + > $prog.rsp\n";
550       for ($i=0; $i<=$#objlines; $i++) {
551         $plus = ($i < $#objlines ? " +" : "");
552         print "\techo$objlines[$i]$plus >> $prog.rsp\n";
553       }
554       print "\techo $prog.exe >> $prog.rsp\n";
555       $objstr = &objects($p, "X.obj", "X.res", undef);
556       @libs = split " ", &objects($p, undef, undef, "X");
557       @libs = grep { !$stdlibs{$_} } @libs;
558       unshift @libs, "cw32", "import32";
559       $libstr = join ' ', @libs;
560       print "\techo nul,$libstr, >> $prog.rsp\n";
561       print "\techo " . &objects($p, undef, "X.res", undef) . " >> $prog.rsp\n";
562       print "\n";
563     }
564     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\", "borland")) {
565       if ($forceobj{$d->{obj_orig}}) {
566         printf("%s: FORCE\n", $d->{obj});
567       } else {
568         print &splitline(sprintf("%s: %s", $d->{obj},
569                                  join " ", @{$d->{deps}})), "\n";
570       }
571     }
572     print "\n";
573     print $makefile_extra{'borland'}->{'end'};
574     print "\nclean:\n".
575     "\t-del *.obj\n".
576     "\t-del *.exe\n".
577     "\t-del *.res\n".
578     "\t-del *.pch\n".
579     "\t-del *.aps\n".
580     "\t-del *.il*\n".
581     "\t-del *.pdb\n".
582     "\t-del *.rsp\n".
583     "\t-del *.tds\n".
584     "\t-del *.\$\$\$\$\$\$\n".
585     "\n".
586     "FORCE:\n".
587     "\t-rem dummy command\n";
588     select STDOUT; close OUT;
589 }
590
591 if (defined $makefiles{'vc'}) {
592     $dirpfx = &dirpfx($makefiles{'vc'}, "\\");
593
594     ##-- Visual C++ makefile
595     open OUT, ">$makefiles{'vc'}"; select OUT;
596     print
597       "# Makefile for $project_name under Visual C.\n".
598       "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
599       "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
600     print $help;
601     print
602       "\n".
603       "# If you rename this file to `Makefile', you should change this line,\n".
604       "# so that the .rsp files still depend on the correct makefile.\n".
605       "MAKEFILE = Makefile.vc\n".
606       "\n".
607       "# C compilation flags\n".
608       "CFLAGS = /nologo /W3 /O1 " .
609       (join " ", map {"-I$dirpfx$_"} @srcdirs) .
610       " /D_WINDOWS /D_WIN32_WINDOWS=0x500 /DWINVER=0x500\n".
611       "LFLAGS = /incremental:no /fixed\n".
612       "RCFLAGS = -DWIN32 -D_WIN32 -DWINVER=0x0400\n".
613       "\n".
614       $makefile_extra{'vc'}->{'vars'} .
615       "\n".
616       "\n";
617     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
618     print "\n\n";
619     foreach $p (&prognames("G:C")) {
620         ($prog, $type) = split ",", $p;
621         $objstr = &objects($p, "X.obj", "X.res", undef);
622         print &splitline("$prog.exe: " . $objstr . " $prog.rsp"), "\n";
623         print "\tlink \$(LFLAGS) \$(XLFLAGS) -out:$prog.exe -map:$prog.map \@$prog.rsp\n\n";
624     }
625     foreach $p (&prognames("G:C")) {
626         ($prog, $type) = split ",", $p;
627         print $prog, ".rsp: \$(MAKEFILE)\n";
628         $objstr = &objects($p, "X.obj", "X.res", "X.lib");
629         @objlist = split " ", $objstr;
630         @objlines = ("");
631         foreach $i (@objlist) {
632             if (length($objlines[$#objlines] . " $i") > 50) {
633                 push @objlines, "";
634             }
635             $objlines[$#objlines] .= " $i";
636         }
637         $subsys = ($type eq "G") ? "windows" : "console";
638         print "\techo /nologo /subsystem:$subsys > $prog.rsp\n";
639         for ($i=0; $i<=$#objlines; $i++) {
640             print "\techo$objlines[$i] >> $prog.rsp\n";
641         }
642         print "\n";
643     }
644     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\", "vc")) {
645         $extradeps = $forceobj{$d->{obj_orig}} ? ["*.c","*.h","*.rc"] : [];
646         print &splitline(sprintf("%s: %s", $d->{obj},
647                                  join " ", @$extradeps, @{$d->{deps}})), "\n";
648         if ($d->{obj} =~ /.obj$/) {
649             print "\tcl \$(COMPAT) \$(CFLAGS) \$(XFLAGS) /c ".$d->{deps}->[0],"\n\n";
650         } else {
651             print "\trc \$(RCFL) -r \$(RCFLAGS) ".$d->{deps}->[0],"\n\n";
652         }
653     }
654     print "\n";
655     print $makefile_extra{'vc'}->{'end'};
656     print "\nclean: tidy\n".
657       "\t-del *.exe\n\n".
658       "tidy:\n".
659       "\t-del *.obj\n".
660       "\t-del *.res\n".
661       "\t-del *.pch\n".
662       "\t-del *.aps\n".
663       "\t-del *.ilk\n".
664       "\t-del *.pdb\n".
665       "\t-del *.rsp\n".
666       "\t-del *.dsp\n".
667       "\t-del *.dsw\n".
668       "\t-del *.ncb\n".
669       "\t-del *.opt\n".
670       "\t-del *.plg\n".
671       "\t-del *.map\n".
672       "\t-del *.idb\n".
673       "\t-del debug.log\n";
674     select STDOUT; close OUT;
675 }
676
677 if (defined $makefiles{'vcproj'}) {
678     $dirpfx = &dirpfx($makefiles{'vcproj'}, "\\");
679
680     $orig_dir = cwd;
681
682     ##-- MSVC 6 Workspace and projects
683     #
684     # Note: All files created in this section are written in binary
685     # mode, because although MSVC's command-line make can deal with
686     # LF-only line endings, MSVC project files really _need_ to be
687     # CRLF. Hence, in order for mkfiles.pl to generate usable project
688     # files even when run from Unix, I make sure all files are binary
689     # and explicitly write the CRLFs.
690     #
691     # Create directories if necessary
692     mkdir $makefiles{'vcproj'}
693         if(! -d $makefiles{'vcproj'});
694     chdir $makefiles{'vcproj'};
695     @deps = &deps("X.obj", "X.res", $dirpfx, "\\", "vcproj");
696     %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
697     # Create the project files
698     # Get names of all Windows projects (GUI and console)
699     my @prognames = &prognames("G:C");
700     foreach $progname (@prognames) {
701       create_vc_project(\%all_object_deps, $progname);
702     }
703     # Create the workspace file
704     open OUT, ">$project_name.dsw"; binmode OUT; select OUT;
705     print
706     "Microsoft Developer Studio Workspace File, Format Version 6.00\r\n".
707     "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n".
708     "\r\n".
709     "###############################################################################\r\n".
710     "\r\n";
711     # List projects
712     foreach $progname (@prognames) {
713       ($windows_project, $type) = split ",", $progname;
714         print "Project: \"$windows_project\"=\".\\$windows_project\\$windows_project.dsp\" - Package Owner=<4>\r\n";
715     }
716     print
717     "\r\n".
718     "Package=<5>\r\n".
719     "{{{\r\n".
720     "}}}\r\n".
721     "\r\n".
722     "Package=<4>\r\n".
723     "{{{\r\n".
724     "}}}\r\n".
725     "\r\n".
726     "###############################################################################\r\n".
727     "\r\n".
728     "Global:\r\n".
729     "\r\n".
730     "Package=<5>\r\n".
731     "{{{\r\n".
732     "}}}\r\n".
733     "\r\n".
734     "Package=<3>\r\n".
735     "{{{\r\n".
736     "}}}\r\n".
737     "\r\n".
738     "###############################################################################\r\n".
739     "\r\n";
740     select STDOUT; close OUT;
741     chdir $orig_dir;
742
743     sub create_vc_project {
744         my ($all_object_deps, $progname) = @_;
745         # Construct program's dependency info
746         %seen_objects = ();
747         %lib_files = ();
748         %source_files = ();
749         %header_files = ();
750         %resource_files = ();
751         @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
752         foreach $object_file (@object_files) {
753             next if defined $seen_objects{$object_file};
754             $seen_objects{$object_file} = 1;
755             if($object_file =~ /\.lib$/io) {
756                 $lib_files{$object_file} = 1;
757                 next;
758             }
759             $object_deps = $all_object_deps{$object_file};
760             foreach $object_dep (@$object_deps) {
761                 if($object_dep =~ /\.c$/io) {
762                     $source_files{$object_dep} = 1;
763                     next;
764                 }
765                 if($object_dep =~ /\.h$/io) {
766                     $header_files{$object_dep} = 1;
767                     next;
768                 }
769                 if($object_dep =~ /\.(rc|ico)$/io) {
770                     $resource_files{$object_dep} = 1;
771                     next;
772                 }
773             }
774         }
775         $libs = join " ", sort keys %lib_files;
776         @source_files = sort keys %source_files;
777         @header_files = sort keys %header_files;
778         @resources = sort keys %resource_files;
779         ($windows_project, $type) = split ",", $progname;
780         mkdir $windows_project
781             if(! -d $windows_project);
782         chdir $windows_project;
783         $subsys = ($type eq "G") ? "windows" : "console";
784         open OUT, ">$windows_project.dsp"; binmode OUT; select OUT;
785         print
786         "# Microsoft Developer Studio Project File - Name=\"$windows_project\" - Package Owner=<4>\r\n".
787         "# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n".
788         "# ** DO NOT EDIT **\r\n".
789         "\r\n".
790         "# TARGTYPE \"Win32 (x86) Application\" 0x0101\r\n".
791         "\r\n".
792         "CFG=$windows_project - Win32 Debug\r\n".
793         "!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n".
794         "!MESSAGE use the Export Makefile command and run\r\n".
795         "!MESSAGE \r\n".
796         "!MESSAGE NMAKE /f \"$windows_project.mak\".\r\n".
797         "!MESSAGE \r\n".
798         "!MESSAGE You can specify a configuration when running NMAKE\r\n".
799         "!MESSAGE by defining the macro CFG on the command line. For example:\r\n".
800         "!MESSAGE \r\n".
801         "!MESSAGE NMAKE /f \"$windows_project.mak\" CFG=\"$windows_project - Win32 Debug\"\r\n".
802         "!MESSAGE \r\n".
803         "!MESSAGE Possible choices for configuration are:\r\n".
804         "!MESSAGE \r\n".
805         "!MESSAGE \"$windows_project - Win32 Release\" (based on \"Win32 (x86) Application\")\r\n".
806         "!MESSAGE \"$windows_project - Win32 Debug\" (based on \"Win32 (x86) Application\")\r\n".
807         "!MESSAGE \r\n".
808         "\r\n".
809         "# Begin Project\r\n".
810         "# PROP AllowPerConfigDependencies 0\r\n".
811         "# PROP Scc_ProjName \"\"\r\n".
812         "# PROP Scc_LocalPath \"\"\r\n".
813         "CPP=cl.exe\r\n".
814         "MTL=midl.exe\r\n".
815         "RSC=rc.exe\r\n".
816         "\r\n".
817         "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
818         "\r\n".
819         "# PROP BASE Use_MFC 0\r\n".
820         "# PROP BASE Use_Debug_Libraries 0\r\n".
821         "# PROP BASE Output_Dir \"Release\"\r\n".
822         "# PROP BASE Intermediate_Dir \"Release\"\r\n".
823         "# PROP BASE Target_Dir \"\"\r\n".
824         "# PROP Use_MFC 0\r\n".
825         "# PROP Use_Debug_Libraries 0\r\n".
826         "# PROP Output_Dir \"Release\"\r\n".
827         "# PROP Intermediate_Dir \"Release\"\r\n".
828         "# PROP Ignore_Export_Lib 0\r\n".
829         "# PROP Target_Dir \"\"\r\n".
830         "# ADD BASE CPP /nologo /W3 /GX /O2 ".
831           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
832           " /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
833         "# ADD CPP /nologo /W3 /GX /O2 ".
834           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
835           " /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /c\r\n".
836         "# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
837         "# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n".
838         "# ADD BASE RSC /l 0x809 /d \"NDEBUG\"\r\n".
839         "# ADD RSC /l 0x809 /d \"NDEBUG\"\r\n".
840         "BSC32=bscmake.exe\r\n".
841         "# ADD BASE BSC32 /nologo\r\n".
842         "# ADD BSC32 /nologo\r\n".
843         "LINK32=link.exe\r\n".
844         "# 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".
845         "# ADD LINK32 $libs /nologo /subsystem:$subsys /machine:I386\r\n".
846         "# SUBTRACT LINK32 /pdb:none\r\n".
847         "\r\n".
848         "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
849         "\r\n".
850         "# PROP BASE Use_MFC 0\r\n".
851         "# PROP BASE Use_Debug_Libraries 1\r\n".
852         "# PROP BASE Output_Dir \"Debug\"\r\n".
853         "# PROP BASE Intermediate_Dir \"Debug\"\r\n".
854         "# PROP BASE Target_Dir \"\"\r\n".
855         "# PROP Use_MFC 0\r\n".
856         "# PROP Use_Debug_Libraries 1\r\n".
857         "# PROP Output_Dir \"Debug\"\r\n".
858         "# PROP Intermediate_Dir \"Debug\"\r\n".
859         "# PROP Ignore_Export_Lib 0\r\n".
860         "# PROP Target_Dir \"\"\r\n".
861         "# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od ".
862           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
863           " /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
864         "# ADD CPP /nologo /W3 /Gm /GX /ZI /Od ".
865           (join " ", map {"/I \"..\\..\\$dirpfx$_\""} @srcdirs) .
866           " /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /D \"_MBCS\" /YX /FD /GZ /c\r\n".
867         "# ADD BASE MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
868         "# ADD MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n".
869         "# ADD BASE RSC /l 0x809 /d \"_DEBUG\"\r\n".
870         "# ADD RSC /l 0x809 /d \"_DEBUG\"\r\n".
871         "BSC32=bscmake.exe\r\n".
872         "# ADD BASE BSC32 /nologo\r\n".
873         "# ADD BSC32 /nologo\r\n".
874         "LINK32=link.exe\r\n".
875         "# 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".
876         "# ADD LINK32 $libs /nologo /subsystem:$subsys /debug /machine:I386 /pdbtype:sept\r\n".
877         "# SUBTRACT LINK32 /pdb:none\r\n".
878         "\r\n".
879         "!ENDIF \r\n".
880         "\r\n".
881         "# Begin Target\r\n".
882         "\r\n".
883         "# Name \"$windows_project - Win32 Release\"\r\n".
884         "# Name \"$windows_project - Win32 Debug\"\r\n".
885         "# Begin Group \"Source Files\"\r\n".
886         "\r\n".
887         "# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\"\r\n";
888         foreach $source_file (@source_files) {
889             print
890               "# Begin Source File\r\n".
891               "\r\n".
892               "SOURCE=..\\..\\$source_file\r\n";
893             if($source_file =~ /ssh\.c/io) {
894                 # Disable 'Edit and continue' as Visual Studio can't handle the macros
895                 print
896                   "\r\n".
897                   "!IF  \"\$(CFG)\" == \"$windows_project - Win32 Release\"\r\n".
898                   "\r\n".
899                   "!ELSEIF  \"\$(CFG)\" == \"$windows_project - Win32 Debug\"\r\n".
900                   "\r\n".
901                   "# ADD CPP /Zi\r\n".
902                   "\r\n".
903                   "!ENDIF \r\n".
904                   "\r\n";
905             }
906             print "# End Source File\r\n";
907         }
908         print
909         "# End Group\r\n".
910         "# Begin Group \"Header Files\"\r\n".
911         "\r\n".
912         "# PROP Default_Filter \"h;hpp;hxx;hm;inl\"\r\n";
913         foreach $header_file (@header_files) {
914             print
915               "# Begin Source File\r\n".
916               "\r\n".
917               "SOURCE=..\\..\\$header_file\r\n".
918               "# End Source File\r\n";
919         }
920         print
921         "# End Group\r\n".
922         "# Begin Group \"Resource Files\"\r\n".
923         "\r\n".
924         "# PROP Default_Filter \"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\"\r\n";
925         foreach $resource_file (@resources) {
926             print
927               "# Begin Source File\r\n".
928               "\r\n".
929               "SOURCE=..\\..\\$resource_file\r\n".
930               "# End Source File\r\n";
931         }
932         print
933         "# End Group\r\n".
934         "# End Target\r\n".
935         "# End Project\r\n";
936         select STDOUT; close OUT;
937         chdir "..";
938     }
939 }
940
941 if (defined $makefiles{'gtk'}) {
942     $dirpfx = &dirpfx($makefiles{'gtk'}, "/");
943
944     ##-- X/GTK/Unix makefile
945     open OUT, ">$makefiles{'gtk'}"; select OUT;
946     print
947     "# Makefile for $project_name under X/GTK and Unix.\n".
948     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
949     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
950     # gcc command line option is -D not /D
951     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
952     print $_;
953     print
954     "\n".
955     "# You can define this path to point at your tools if you need to\n".
956     "# TOOLPATH = /opt/gcc/bin\n".
957     "CC = \$(TOOLPATH)cc\n".
958     "# If necessary set the path to krb5-config here\n".
959     "KRB5CONFIG=krb5-config\n".
960     "# You can manually set this to `gtk-config' or `pkg-config gtk+-1.2'\n".
961     "# (depending on what works on your system) if you want to enforce\n".
962     "# building with GTK 1.2, or you can set it to `pkg-config gtk+-2.0 x11'\n".
963     "# if you want to enforce 2.0. The default is to try 2.0 and fall back\n".
964     "# to 1.2 if it isn't found.\n".
965     "GTK_CONFIG = sh -c 'pkg-config gtk+-2.0 x11 \$\$0 2>/dev/null || gtk-config \$\$0'\n".
966     "\n".
967     "-include Makefile.local\n".
968     "\n".
969     "unexport CFLAGS # work around a weird issue with krb5-config\n".
970     "\n".
971     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
972                (join " ", map {"-I$dirpfx$_"} @srcdirs) .
973                " \$(shell \$(GTK_CONFIG) --cflags)").
974                  " -D _FILE_OFFSET_BITS=64\n".
975     "XLDFLAGS = \$(LDFLAGS) \$(shell \$(GTK_CONFIG) --libs)\n".
976     "ULDFLAGS = \$(LDFLAGS)\n".
977     "ifeq (,\$(findstring NO_GSSAPI,\$(COMPAT)))\n".
978     "ifeq (,\$(findstring STATIC_GSSAPI,\$(COMPAT)))\n".
979     "XLDFLAGS+= -ldl\n".
980     "ULDFLAGS+= -ldl\n".
981     "else\n".
982     "CFLAGS+= -DNO_LIBDL \$(shell \$(KRB5CONFIG) --cflags gssapi)\n".
983     "XLDFLAGS+= \$(shell \$(KRB5CONFIG) --libs gssapi)\n".
984     "ULDFLAGS+= \$(shell \$(KRB5CONFIG) --libs gssapi)\n".
985     "endif\n".
986     "endif\n".
987     "INSTALL=install\n".
988     "INSTALL_PROGRAM=\$(INSTALL)\n".
989     "INSTALL_DATA=\$(INSTALL)\n".
990     "prefix=/usr/local\n".
991     "exec_prefix=\$(prefix)\n".
992     "bindir=\$(exec_prefix)/bin\n".
993     "mandir=\$(prefix)/man\n".
994     "man1dir=\$(mandir)/man1\n".
995     "\n".
996     &def($makefile_extra{'gtk'}->{'vars'}) .
997     "\n".
998     ".SUFFIXES:\n".
999     "\n".
1000     "\n";
1001     print &splitline("all:" . join "", map { " $_" } &progrealnames("X:U"));
1002     print "\n\n";
1003     foreach $p (&prognames("X:U")) {
1004       ($prog, $type) = split ",", $p;
1005       $objstr = &objects($p, "X.o", undef, undef);
1006       print &splitline($prog . ": " . $objstr), "\n";
1007       $libstr = &objects($p, undef, undef, "-lX");
1008       print &splitline("\t\$(CC) -o \$@ " .
1009                        $objstr . " \$(${type}LDFLAGS) $libstr", 69), "\n\n";
1010     }
1011     foreach $d (&deps("X.o", undef, $dirpfx, "/", "gtk")) {
1012       if ($forceobj{$d->{obj_orig}}) {
1013         printf("%s: FORCE\n", $d->{obj});
1014       } else {
1015         print &splitline(sprintf("%s: %s", $d->{obj},
1016                                  join " ", @{$d->{deps}})), "\n";
1017       }
1018       print &splitline("\t\$(CC) \$(COMPAT) \$(CFLAGS) \$(XFLAGS) -c $d->{deps}->[0]\n");
1019     }
1020     print "\n";
1021     print $makefile_extra{'gtk'}->{'end'};
1022     print "\nclean:\n".
1023     "\trm -f *.o". (join "", map { " $_" } &progrealnames("X:U")) . "\n";
1024     print "\nFORCE:\n";
1025     select STDOUT; close OUT;
1026 }
1027
1028 if (defined $makefiles{'unix'}) {
1029     $dirpfx = &dirpfx($makefiles{'unix'}, "/");
1030
1031     ##-- GTK-free pure-Unix makefile for non-GUI apps only
1032     open OUT, ">$makefiles{'unix'}"; select OUT;
1033     print
1034     "# Makefile for $project_name under Unix.\n".
1035     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1036     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1037     # gcc command line option is -D not /D
1038     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
1039     print $_;
1040     print
1041     "\n".
1042     "# You can define this path to point at your tools if you need to\n".
1043     "# TOOLPATH = /opt/gcc/bin\n".
1044     "CC = \$(TOOLPATH)cc\n".
1045     "\n".
1046     "-include Makefile.local\n".
1047     "\n".
1048     "unexport CFLAGS # work around a weird issue with krb5-config\n".
1049     "\n".
1050     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1051                (join " ", map {"-I$dirpfx$_"} @srcdirs)).
1052                  " -D _FILE_OFFSET_BITS=64\n".
1053     "ULDFLAGS = \$(LDFLAGS)\n".
1054     "INSTALL=install\n".
1055     "INSTALL_PROGRAM=\$(INSTALL)\n".
1056     "INSTALL_DATA=\$(INSTALL)\n".
1057     "prefix=/usr/local\n".
1058     "exec_prefix=\$(prefix)\n".
1059     "bindir=\$(exec_prefix)/bin\n".
1060     "mandir=\$(prefix)/man\n".
1061     "man1dir=\$(mandir)/man1\n".
1062     "\n".
1063     &def($makefile_extra{'unix'}->{'vars'}) .
1064     "\n".
1065     ".SUFFIXES:\n".
1066     "\n".
1067     "\n";
1068     print &splitline("all:" . join "", map { " $_" } &progrealnames("U"));
1069     print "\n\n";
1070     foreach $p (&prognames("U")) {
1071       ($prog, $type) = split ",", $p;
1072       $objstr = &objects($p, "X.o", undef, undef);
1073       print &splitline($prog . ": " . $objstr), "\n";
1074       $libstr = &objects($p, undef, undef, "-lX");
1075       print &splitline("\t\$(CC) -o \$@ " .
1076                        $objstr . " \$(${type}LDFLAGS) $libstr", 69), "\n\n";
1077     }
1078     foreach $d (&deps("X.o", undef, $dirpfx, "/", "unix")) {
1079       if ($forceobj{$d->{obj_orig}}) {
1080         printf("%s: FORCE\n", $d->{obj});
1081       } else {
1082         print &splitline(sprintf("%s: %s", $d->{obj},
1083                                  join " ", @{$d->{deps}})), "\n";
1084       }
1085       print &splitline("\t\$(CC) \$(COMPAT) \$(CFLAGS) \$(XFLAGS) -c $d->{deps}->[0]\n");
1086     }
1087     print "\n";
1088     print &def($makefile_extra{'unix'}->{'end'});
1089     print "\nclean:\n".
1090     "\trm -f *.o". (join "", map { " $_" } &progrealnames("U")) . "\n";
1091     print "\nFORCE:\n";
1092     select STDOUT; close OUT;
1093 }
1094
1095 if (defined $makefiles{'am'}) {
1096     $dirpfx = "\$(srcdir)/" . &dirpfx($makefiles{'am'}, "/");
1097
1098     ##-- Unix/autoconf Makefile.am
1099     open OUT, ">$makefiles{'am'}"; select OUT;
1100     print
1101     "# Makefile.am for $project_name under Unix with Autoconf/Automake.\n".
1102     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1103     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n\n";
1104
1105     # Complete list of source and header files. Not used by the
1106     # auto-generated parts of this makefile, but Recipe might like to
1107     # have it available as a variable so that mandatory-rebuild things
1108     # (version.o) can conveniently be made to depend on it.
1109     @sources = ("allsources", "=",
1110                 map {"${dirpfx}$_"} sort keys %allsourcefiles);
1111     print &splitline(join " ", @sources), "\n\n";
1112
1113     @cliprogs = ("bin_PROGRAMS", "=");
1114     foreach $p (&prognames("U")) {
1115       ($prog, $type) = split ",", $p;
1116       push @cliprogs, $prog;
1117     }
1118     @allprogs = @cliprogs;
1119     foreach $p (&prognames("X")) {
1120       ($prog, $type) = split ",", $p;
1121       push @allprogs, $prog;
1122     }
1123     print "if HAVE_GTK\n";
1124     print &splitline(join " ", @allprogs), "\n";
1125     print "else\n";
1126     print &splitline(join " ", @cliprogs), "\n";
1127     print "endif\n\n";
1128
1129     %objtosrc = ();
1130     foreach $d (&deps("X", undef, $dirpfx, "/", "am")) {
1131       $objtosrc{$d->{obj}} = $d->{deps}->[0];
1132     }
1133
1134     @amcflags = ("\$(COMPAT)", "\$(XFLAGS)", map {"-I$dirpfx$_"} @srcdirs);
1135     print "if HAVE_GTK\n";
1136     print &splitline(join " ", "AM_CFLAGS", "=",
1137                      "\$(GTK_CFLAGS)", @amcflags), "\n";
1138     print "else\n";
1139     print &splitline(join " ", "AM_CFLAGS", "=", @amcflags), "\n";
1140     print "endif\n\n";
1141
1142     %amspeciallibs = ();
1143     foreach $obj (sort { $a cmp $b } keys %{$cflags{'am'}}) {
1144       print "lib${obj}_a_SOURCES = ", $objtosrc{$obj}, "\n";
1145       print &splitline(join " ", "lib${obj}_a_CFLAGS", "=", @amcflags,
1146                        $cflags{'am'}->{$obj}), "\n";
1147       $amspeciallibs{$obj} = "lib${obj}.a";
1148     }
1149     print &splitline(join " ", "noinst_LIBRARIES", "=",
1150                      sort { $a cmp $b } values %amspeciallibs), "\n\n";
1151
1152     foreach $p (&prognames("X:U")) {
1153       ($prog, $type) = split ",", $p;
1154       print "if HAVE_GTK\n" if $type eq "X";
1155       @progsources = ("${prog}_SOURCES", "=");
1156       %sourcefiles = ();
1157       @ldadd = ();
1158       $objstr = &objects($p, "X", undef, undef);
1159       foreach $obj (split / /,$objstr) {
1160         if ($amspeciallibs{$obj}) {
1161           push @ldadd, $amspeciallibs{$obj};
1162         } else {
1163           $sourcefiles{$objtosrc{$obj}} = 1;
1164         }
1165       }
1166       push @progsources, sort { $a cmp $b } keys %sourcefiles;
1167       print &splitline(join " ", @progsources), "\n";
1168       if ($type eq "X") {
1169         push @ldadd, "\$(GTK_LIBS)";
1170       }
1171       if (@ldadd) {
1172         print &splitline(join " ", "${prog}_LDADD", "=", @ldadd), "\n";
1173       }
1174       print "endif\n" if $type eq "X";
1175       print "\n";
1176     }
1177     print $makefile_extra{'am'}->{'end'};
1178     select STDOUT; close OUT;
1179 }
1180
1181 if (defined $makefiles{'lcc'}) {
1182     $dirpfx = &dirpfx($makefiles{'lcc'}, "\\");
1183
1184     ##-- lcc makefile
1185     open OUT, ">$makefiles{'lcc'}"; select OUT;
1186     print
1187     "# Makefile for $project_name under lcc.\n".
1188     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1189     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1190     # lcc command line option is -D not /D
1191     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
1192     print $_;
1193     print
1194     "\n".
1195     "# If you rename this file to `Makefile', you should change this line,\n".
1196     "# so that the .rsp files still depend on the correct makefile.\n".
1197     "MAKEFILE = Makefile.lcc\n".
1198     "\n".
1199     "# C compilation flags\n".
1200     "CFLAGS = -D_WINDOWS " .
1201       (join " ", map {"-I$dirpfx$_"} @srcdirs) .
1202       "\n".
1203     "# Resource compilation flags\n".
1204     "RCFLAGS = \n".
1205     "\n".
1206     "# Get include directory for resource compiler\n".
1207     "\n".
1208     $makefile_extra{'lcc'}->{'vars'} .
1209     "\n";
1210     print &splitline("all:" . join "", map { " $_.exe" } &progrealnames("G:C"));
1211     print "\n\n";
1212     foreach $p (&prognames("G:C")) {
1213       ($prog, $type) = split ",", $p;
1214       $objstr = &objects($p, "X.obj", "X.res", undef);
1215       print &splitline("$prog.exe: " . $objstr ), "\n";
1216       $subsystemtype = '';
1217       if ($type eq "G") { $subsystemtype = "-subsystem  windows"; }
1218       my $libss = "shell32.lib wsock32.lib ws2_32.lib winspool.lib winmm.lib imm32.lib";
1219       print &splitline("\tlcclnk $subsystemtype -o $prog.exe $objstr $libss");
1220       print "\n\n";
1221     }
1222
1223     foreach $d (&deps("X.obj", "X.res", $dirpfx, "\\", "lcc")) {
1224       if ($forceobj{$d->{obj_orig}}) {
1225          printf("%s: FORCE\n", $d->{obj});
1226       } else {
1227          print &splitline(sprintf("%s: %s", $d->{obj},
1228                           join " ", @{$d->{deps}})), "\n";
1229       }
1230       if ($d->{obj} =~ /\.obj$/) {
1231           print &splitline("\tlcc -O -p6 \$(COMPAT)".
1232                            " \$(CFLAGS) \$(XFLAGS) ".$d->{deps}->[0],69)."\n";
1233       } else {
1234           print &splitline("\tlrc \$(RCFL) -r \$(RCFLAGS) ".
1235                            $d->{deps}->[0],69)."\n";
1236       }
1237     }
1238     print "\n";
1239     print $makefile_extra{'lcc'}->{'end'};
1240     print "\nclean:\n".
1241     "\t-del *.obj\n".
1242     "\t-del *.exe\n".
1243     "\t-del *.res\n".
1244     "\n".
1245     "FORCE:\n";
1246
1247     select STDOUT; close OUT;
1248 }
1249
1250 if (defined $makefiles{'osx'}) {
1251     $dirpfx = &dirpfx($makefiles{'osx'}, "/");
1252
1253     ##-- Mac OS X makefile
1254     open OUT, ">$makefiles{'osx'}"; select OUT;
1255     print
1256     "# Makefile for $project_name under Mac OS X.\n".
1257     "#\n# This file was created by `mkfiles.pl' from the `Recipe' file.\n".
1258     "# DO NOT EDIT THIS FILE DIRECTLY; edit Recipe or mkfiles.pl instead.\n";
1259     # gcc command line option is -D not /D
1260     ($_ = $help) =~ s/([=" ])\/D/$1-D/gs;
1261     print $_;
1262     print
1263     "CC = \$(TOOLPATH)gcc\n".
1264     "\n".
1265     &splitline("CFLAGS = -O2 -Wall -Werror -g " .
1266                (join " ", map {"-I$dirpfx$_"} @srcdirs))."\n".
1267     "MLDFLAGS = -framework Cocoa\n".
1268     "ULDFLAGS =\n".
1269     "\n" .
1270     $makefile_extra{'osx'}->{'vars'} .
1271     "\n" .
1272     &splitline("all:" . join "", map { " $_" } &progrealnames("MX:U")) .
1273     "\n";
1274     foreach $p (&prognames("MX")) {
1275       ($prog, $type) = split ",", $p;
1276       $objstr = &objects($p, "X.o", undef, undef);
1277       $icon = &special($p, ".icns");
1278       $infoplist = &special($p, "info.plist");
1279       print "${prog}.app:\n\tmkdir -p \$\@\n";
1280       print "${prog}.app/Contents: ${prog}.app\n\tmkdir -p \$\@\n";
1281       print "${prog}.app/Contents/MacOS: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1282       $targets = "${prog}.app/Contents/MacOS/$prog";
1283       if (defined $icon) {
1284         print "${prog}.app/Contents/Resources: ${prog}.app/Contents\n\tmkdir -p \$\@\n";
1285         print "${prog}.app/Contents/Resources/${prog}.icns: ${prog}.app/Contents/Resources $icon\n\tcp $icon \$\@\n";
1286         $targets .= " ${prog}.app/Contents/Resources/${prog}.icns";
1287       }
1288       if (defined $infoplist) {
1289         print "${prog}.app/Contents/Info.plist: ${prog}.app/Contents/Resources $infoplist\n\tcp $infoplist \$\@\n";
1290         $targets .= " ${prog}.app/Contents/Info.plist";
1291       }
1292       $targets .= " \$(${prog}_extra)";
1293       print &splitline("${prog}: $targets", 69) . "\n\n";
1294       print &splitline("${prog}.app/Contents/MacOS/$prog: ".
1295                        "${prog}.app/Contents/MacOS " . $objstr), "\n";
1296       $libstr = &objects($p, undef, undef, "-lX");
1297       print &splitline("\t\$(CC) \$(MLDFLAGS) -o \$@ " .
1298                        $objstr . " $libstr", 69), "\n\n";
1299     }
1300     foreach $p (&prognames("U")) {
1301       ($prog, $type) = split ",", $p;
1302       $objstr = &objects($p, "X.o", undef, undef);
1303       print &splitline($prog . ": " . $objstr), "\n";
1304       $libstr = &objects($p, undef, undef, "-lX");
1305       print &splitline("\t\$(CC) \$(ULDFLAGS) -o \$@ " .
1306                        $objstr . " $libstr", 69), "\n\n";
1307     }
1308     foreach $d (&deps("X.o", undef, $dirpfx, "/", "osx")) {
1309       if ($forceobj{$d->{obj_orig}}) {
1310          printf("%s: FORCE\n", $d->{obj});
1311       } else {
1312          print &splitline(sprintf("%s: %s", $d->{obj},
1313                                   join " ", @{$d->{deps}})), "\n";
1314       }
1315       $firstdep = $d->{deps}->[0];
1316       if ($firstdep =~ /\.c$/) {
1317           print "\t\$(CC) \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS) -c \$<\n";
1318       } elsif ($firstdep =~ /\.m$/) {
1319           print "\t\$(CC) -x objective-c \$(COMPAT) \$(FWHACK) \$(CFLAGS) \$(XFLAGS) -c \$<\n";
1320       }
1321     }
1322     print "\n".&def($makefile_extra{'osx'}->{'end'});
1323     print "\nclean:\n".
1324     "\trm -f *.o *.dmg". (join "", map { " $_" } &progrealnames("U")) . "\n".
1325     "\trm -rf *.app\n".
1326     "\n".
1327     "FORCE:\n";
1328     select STDOUT; close OUT;
1329 }
1330
1331 if (defined $makefiles{'devcppproj'}) {
1332     $dirpfx = &dirpfx($makefiles{'devcppproj'}, "\\");
1333     $orig_dir = cwd;
1334
1335     ##-- Dev-C++ 5 projects
1336     #
1337     # Note: All files created in this section are written in binary
1338     # mode to prevent any posibility of misinterpreted line endings.
1339     # I don't know if Dev-C++ is as touchy as MSVC with LF-only line
1340     # endings. But however, CRLF line endings are the common way on
1341     # Win32 machines where Dev-C++ is running.
1342     # Hence, in order for mkfiles.pl to generate CRLF project files
1343     # even when run from Unix, I make sure all files are binary and
1344     # explicitly write the CRLFs.
1345     #
1346     # Create directories if necessary
1347     mkdir $makefiles{'devcppproj'}
1348         if(! -d $makefiles{'devcppproj'});
1349     chdir $makefiles{'devcppproj'};
1350     @deps = &deps("X.obj", "X.res", $dirpfx, "\\", "devcppproj");
1351     %all_object_deps = map {$_->{obj} => $_->{deps}} @deps;
1352     # Make dir names FAT/NTFS compatible
1353     my @srcdirs = @srcdirs;
1354     for ($i=0; $i<@srcdirs; $i++) {
1355       $srcdirs[$i] =~ s/\//\\/g;
1356       $srcdirs[$i] =~ s/\\$//;
1357     }
1358     # Create the project files
1359     # Get names of all Windows projects (GUI and console)
1360     my @prognames = &prognames("G:C");
1361     foreach $progname (@prognames) {
1362       create_devcpp_project(\%all_object_deps, $progname);
1363     }
1364
1365     sub create_devcpp_project {
1366       my ($all_object_deps, $progname) = @_;
1367       # Construct program's dependency info (Taken from 'vcproj', seems to work right here, too.)
1368       %seen_objects = ();
1369       %lib_files = ();
1370       %source_files = ();
1371       %header_files = ();
1372       %resource_files = ();
1373       @object_files = split " ", &objects($progname, "X.obj", "X.res", "X.lib");
1374       foreach $object_file (@object_files) {
1375       next if defined $seen_objects{$object_file};
1376       $seen_objects{$object_file} = 1;
1377       if($object_file =~ /\.lib$/io) {
1378     $lib_files{$object_file} = 1;
1379     next;
1380       }
1381       $object_deps = $all_object_deps{$object_file};
1382       foreach $object_dep (@$object_deps) {
1383     if($object_dep =~ /\.c$/io) {
1384         $source_files{$object_dep} = 1;
1385         next;
1386     }
1387     if($object_dep =~ /\.h$/io) {
1388         $header_files{$object_dep} = 1;
1389         next;
1390     }
1391     if($object_dep =~ /\.(rc|ico)$/io) {
1392         $resource_files{$object_dep} = 1;
1393         next;
1394     }
1395       }
1396       }
1397       $libs = join " ", sort keys %lib_files;
1398       @source_files = sort keys %source_files;
1399       @header_files = sort keys %header_files;
1400       @resources = sort keys %resource_files;
1401   ($windows_project, $type) = split ",", $progname;
1402       mkdir $windows_project
1403       if(! -d $windows_project);
1404       chdir $windows_project;
1405
1406   $subsys = ($type eq "G") ? "0" : "1";  # 0 = Win32 GUI, 1 = Win32 Console
1407       open OUT, ">$windows_project.dev"; binmode OUT; select OUT;
1408       print
1409       "# DEV-C++ 5 Project File - $windows_project.dev\r\n".
1410       "# ** DO NOT EDIT **\r\n".
1411       "\r\n".
1412       # No difference between DEBUG and RELEASE here as in 'vcproj', because
1413       # Dev-C++ does not support mutiple compilation profiles in one single project.
1414       # (At least I can say this for Dev-C++ 5 Beta)
1415       "[Project]\r\n".
1416       "FileName=$windows_project.dev\r\n".
1417       "Name=$windows_project\r\n".
1418       "Ver=1\r\n".
1419       "IsCpp=1\r\n".
1420       "Type=$subsys\r\n".
1421       # Multimon is disabled here, as Dev-C++ (Version 5 Beta) does not have multimon.h
1422       "Compiler=-W -D__GNUWIN32__ -DWIN32 -DNDEBUG -D_WINDOWS -DNO_MULTIMON -D_MBCS_\@\@_\r\n".
1423       "CppCompiler=-W -D__GNUWIN32__ -DWIN32 -DNDEBUG -D_WINDOWS -DNO_MULTIMON -D_MBCS_\@\@_\r\n".
1424       "Includes=" . (join ";", map {"..\\..\\$dirpfx$_"} @srcdirs) . "\r\n".
1425       "Linker=-ladvapi32 -lcomctl32 -lcomdlg32 -lgdi32 -limm32 -lshell32 -luser32 -lwinmm -lwinspool_\@\@_\r\n".
1426       "Libs=\r\n".
1427       "UnitCount=" . (@source_files + @header_files + @resources) . "\r\n".
1428       "Folders=\"Header Files\",\"Resource Files\",\"Source Files\"\r\n".
1429       "ObjFiles=\r\n".
1430       "PrivateResource=${windows_project}_private.rc\r\n".
1431       "ResourceIncludes=..\\..\\..\\WINDOWS\r\n".
1432       "MakeIncludes=\r\n".
1433       "Icon=\r\n". # It's ok to leave this blank.
1434       "ExeOutput=\r\n".
1435       "ObjectOutput=\r\n".
1436       "OverrideOutput=0\r\n".
1437       "OverrideOutputName=$windows_project.exe\r\n".
1438       "HostApplication=\r\n".
1439       "CommandLine=\r\n".
1440       "UseCustomMakefile=0\r\n".
1441       "CustomMakefile=\r\n".
1442       "IncludeVersionInfo=0\r\n".
1443       "SupportXPThemes=0\r\n".
1444       "CompilerSet=0\r\n".
1445       "CompilerSettings=0000000000000000000000\r\n".
1446       "\r\n";
1447       $unit_count = 1;
1448       foreach $source_file (@source_files) {
1449       print
1450         "[Unit$unit_count]\r\n".
1451         "FileName=..\\..\\$source_file\r\n".
1452         "Folder=Source Files\r\n".
1453         "Compile=1\r\n".
1454         "CompileCpp=0\r\n".
1455         "Link=1\r\n".
1456         "Priority=1000\r\n".
1457         "OverrideBuildCmd=0\r\n".
1458         "BuildCmd=\r\n".
1459         "\r\n";
1460       $unit_count++;
1461   }
1462       foreach $header_file (@header_files) {
1463       print
1464         "[Unit$unit_count]\r\n".
1465         "FileName=..\\..\\$header_file\r\n".
1466         "Folder=Header Files\r\n".
1467         "Compile=1\r\n".
1468         "CompileCpp=1\r\n". # Dev-C++ want's to compile all header files with both compilers C and C++. It does not hurt.
1469         "Link=1\r\n".
1470         "Priority=1000\r\n".
1471         "OverrideBuildCmd=0\r\n".
1472         "BuildCmd=\r\n".
1473         "\r\n";
1474       $unit_count++;
1475   }
1476       foreach $resource_file (@resources) {
1477       if ($resource_file =~ /.*\.(ico|cur|bmp|dlg|rc2|rct|bin|rgs|gif|jpg|jpeg|jpe)/io) { # Default filter as in 'vcproj'
1478         $Compile = "0";    # Don't compile images and other binary resource files
1479         $CompileCpp = "0";
1480       } else {
1481         $Compile = "1";
1482         $CompileCpp = "1"; # Dev-C++ want's to compile all .rc files with both compilers C and C++. It does not hurt.
1483       }
1484       print
1485         "[Unit$unit_count]\r\n".
1486         "FileName=..\\..\\$resource_file\r\n".
1487         "Folder=Resource Files\r\n".
1488         "Compile=$Compile\r\n".
1489         "CompileCpp=$CompileCpp\r\n".
1490         "Link=0\r\n".
1491         "Priority=1000\r\n".
1492         "OverrideBuildCmd=0\r\n".
1493         "BuildCmd=\r\n".
1494         "\r\n";
1495       $unit_count++;
1496   }
1497       #Note: By default, [VersionInfo] is not used.
1498       print
1499       "[VersionInfo]\r\n".
1500       "Major=0\r\n".
1501       "Minor=0\r\n".
1502       "Release=1\r\n".
1503       "Build=1\r\n".
1504       "LanguageID=1033\r\n".
1505       "CharsetID=1252\r\n".
1506       "CompanyName=\r\n".
1507       "FileVersion=0.1\r\n".
1508       "FileDescription=\r\n".
1509       "InternalName=\r\n".
1510       "LegalCopyright=\r\n".
1511       "LegalTrademarks=\r\n".
1512       "OriginalFilename=$windows_project.exe\r\n".
1513       "ProductName=$windows_project\r\n".
1514       "ProductVersion=0.1\r\n".
1515       "AutoIncBuildNr=0\r\n";
1516       select STDOUT; close OUT;
1517       chdir "..";
1518     }
1519 }