Mostly Mathias Leinmueller's PuTTYgen documentation, modified slightly
[sgt/putty] / mkfiles.pl
1 #!/usr/bin/env perl
2 #
3 # Makefile generator.
4 # Produces the other PuTTY makefiles from the master one.
5
6 open IN,"Makefile";
7 @current = ();
8 while (<IN>) {
9 chomp;
10 if (/^##--/) {
11 @words = split /\s+/,$_;
12 shift @words; # remove ##--
13 $i = shift @words; # first word
14 if (!defined $i) { # no words
15 @current = ();
16 } elsif (!defined $words[0]) { # only one word
17 @current = ($i);
18 } else { # at least two words
19 @current = map { $i . "." . $_ } @words;
20 foreach $i (@words) { $projects{$i} = $i; }
21 push @current, "objdefs";
22 }
23 } else {
24 foreach $i (@current) { $store{$i} .= $_ . "\n"; }
25 }
26 }
27 close IN;
28 @projects = keys %projects;
29
30 foreach $i (split '\n',$store{'gui-apps'}) {
31 $i =~ s/^# //;
32 $speciallibs = [split ' ', $i];
33 $i = shift @$speciallibs; # take off project name
34 $gui{$i} = 1;
35 $libs{$i} = $speciallibs;
36 }
37
38 foreach $i (split '\n',$store{'console-apps'}) {
39 $i =~ s/^# //;
40 $speciallibs = [split ' ', $i];
41 $i = shift @$speciallibs; # take off project name
42 $gui{$i} = 0;
43 $libs{$i} = $speciallibs;
44 }
45
46 sub project {
47 my ($p) = @_;
48 my ($q) = $store{"$p"} . $store{"objects.$p"} . $store{"resources.$p"};
49 $q =~ s/(\S+)\s[^\n]*\n/\$($1) /gs;
50 $q =~ s/ $//;
51 $q;
52 }
53
54 sub projlist {
55 my ($p) = @_;
56 my ($q) = $store{"$p"} . $store{"objects.$p"} . $store{"resources.$p"};
57 $q =~ s/(\S+)\s[^\n]*\n/$1 /gs;
58 my (@q) = split ' ',$q;
59 @q;
60 }
61
62 ##-- CygWin makefile
63 open OUT, ">Makefile.cyg"; select OUT;
64 print
65 "# Makefile for PuTTY under cygwin.\n";
66 # gcc command line option is -D not /D
67 ($_ = $store{"help"}) =~ s/=\/D/=-D/gs;
68 print $_;
69 print
70 "\n".
71 "# You can define this path to point at your tools if you need to\n".
72 "# TOOLPATH = c:\\cygwin\\bin\\ # or similar, if you're running Windows\n".
73 "# TOOLPATH = /pkg/mingw32msvc/i386-mingw32msvc/bin/\n".
74 "CC = \$(TOOLPATH)gcc\n".
75 "RC = \$(TOOLPATH)windres\n".
76 "# You may also need to tell windres where to find include files:\n".
77 "# RCINC = --include-dir c:\\cygwin\\include\\\n".
78 "\n".
79 "CFLAGS = -mno-cygwin -Wall -O2 -D_WINDOWS -DDEBUG -DWIN32S_COMPAT -D_NO_OLDNAMES -I.\n".
80 "LDFLAGS = -mno-cygwin -s\n".
81 "RCFLAGS = \$(RCINC) --define WIN32=1 --define _WIN32=1 --define WINVER=0x0400 --define MINGW32_FIX=1\n".
82 "LIBS = -ladvapi32 -luser32 -lgdi32 -lwsock32 -lcomctl32 -lcomdlg32\n".
83 "OBJ=o\n".
84 "RES=res.o\n".
85 "\n";
86 print $store{"objdefs"};
87 print
88 "\n".
89 ".SUFFIXES:\n".
90 "\n".
91 "%.o: %.c\n".
92 "\t\$(CC) \$(FWHACK) \$(CFLAGS) -c \$<\n".
93 "\n".
94 "%.res.o: %.rc\n".
95 "\t\$(RC) \$(FWHACK) \$(RCFL) \$(RCFLAGS) \$< \$\@\n".
96 "\n";
97 foreach $p (@projects) {
98 print $p, ".exe: ", &project($p), "\n";
99 my $mw = $gui{$p} ? " -mwindows" : "";
100 $libstr = "";
101 foreach $lib (@{$libs{$p}}) { $libstr .= " -l$lib"; }
102 print "\t\$(CC)" . $mw . " \$(LDFLAGS) -o \$@ " . &project($p), " \$(LIBS)$libstr\n\n";
103 }
104 print $store{"dependencies"};
105 print
106 "\n".
107 "version.o: FORCE;\n".
108 "# Hack to force version.o to be rebuilt always\n".
109 "FORCE:\n".
110 "\t\$(CC) \$(FWHACK) \$(CFLAGS) \$(VER) -c version.c\n\n".
111 "clean:\n".
112 "\trm -f *.o *.exe *.res\n".
113 "\n";
114 select STDOUT; close OUT;
115
116 ##-- Borland makefile
117 open OUT, ">Makefile.bor"; select OUT;
118 print
119 "# Makefile for PuTTY under Borland C++.\n";
120 # bcc32 command line option is -D not /D
121 ($_ = $store{"help"}) =~ s/=\/D/=-D/gs;
122 print $_;
123 print
124 "\n".
125 "# If you rename this file to `Makefile', you should change this line,\n".
126 "# so that the .rsp files still depend on the correct makefile.\n".
127 "MAKEFILE = Makefile.bor\n".
128 "\n".
129 "# Get include directory for resource compiler\n".
130 "!if !\$d(BCB)\n".
131 "BCB = \$(MAKEDIR)\\..\n".
132 "!endif\n".
133 "\n".
134 ".c.obj:\n".
135 "\tbcc32 \$(COMPAT) \$(FWHACK) \$(CFLAGS) /c \$*.c\n".
136 ".rc.res:\n".
137 "\tbrcc32 \$(FWHACK) \$(RCFL) -i \$(BCB)\\include \\\n".
138 "\t\t-r -DNO_WINRESRC_H -DWIN32 -D_WIN32 -DWINVER=0x0400 \$*.rc\n".
139 "\n".
140 "OBJ=obj\n".
141 "RES=res\n".
142 "\n";
143 print $store{"objdefs"};
144 print "\n";
145 print "all:";
146 print map { " $_.exe" } @projects;
147 print "\n\n";
148 foreach $p (@projects) {
149 print $p, ".exe: ", &project($p), " $p.rsp\n";
150 $ap = $gui{$p} ? " -aa" : " -ap";
151 print "\tilink32$ap -Gn -L\$(BCB)\\lib \@$p.rsp\n\n";
152 }
153 foreach $p (@projects) {
154 print $p, ".rsp: \$(MAKEFILE)\n";
155 $c0w = $gui{$p} ? "c0w32" : "c0x32";
156 print "\techo $c0w + > $p.rsp\n";
157 @objlines = &projlist("objects.$p");
158 for ($i=0; $i<=$#objlines; $i++) {
159 $plus = ($i < $#objlines ? " +" : "");
160 print "\techo \$($objlines[$i])$plus >> $p.rsp\n";
161 }
162 print "\techo $p.exe >> $p.rsp\n";
163 @libs = @{$libs{$p}};
164 unshift @libs, "cw32", "import32";
165 $libstr = join ' ', @libs;
166 print "\techo nul,$libstr, >> $p.rsp\n";
167 print "\techo " . (join " ", &project("resources.$p")) . " >> $p.rsp\n";
168 print "\n";
169 }
170 print $store{"dependencies"};
171 print
172 "\n".
173 "version.o: FORCE\n".
174 "# Hack to force version.o to be rebuilt always\n".
175 "FORCE:\n".
176 "\tbcc32 \$(FWHACK) \$(VER) \$(CFLAGS) /c version.c\n\n".
177 "clean:\n".
178 "\tdel *.obj\n".
179 "\tdel *.exe\n".
180 "\tdel *.res\n".
181 "\tdel *.pch\n".
182 "\tdel *.aps\n".
183 "\tdel *.il*\n".
184 "\tdel *.pdb\n".
185 "\tdel *.rsp\n".
186 "\tdel *.tds\n".
187 "\tdel *.\$\$\$\$\$\$\n";
188 select STDOUT; close OUT;