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