aboutsummaryrefslogtreecommitdiff
path: root/src/tools/msvc/Solution.pm
blob: 79a3d82790b7afb5ece9d4b7c6e1b2aa5a5d8aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package Solution;
use Carp;
use strict;
use warnings;

sub new {
	my $junk = shift;
	my $options = shift;
	my $self = {
        projects => {},
        options  => $options,
        numver   => '',
        strver   => '',
    };
	bless $self;
	if ($options->{xml}) {
      if (!($options->{xslt} && $options->{iconv})) {
         die "XML requires both XSLT and ICONV\n";
      }   
   }
	return $self;
}

# Return 1 if $oldfile is newer than $newfile, or if $newfile doesn't exist.
# Special case - if config.pl has changed, always return 1
sub IsNewer {
	my ($newfile, $oldfile) = @_;
	if ($oldfile ne 'src\tools\msvc\config.pl') {
		return 1 if IsNewer($newfile, 'src\tools\msvc\config.pl');
	}
	return 1 if (!(-e $newfile));
	my @nstat = stat($newfile);
	my @ostat = stat($oldfile);
	return 1 if ($nstat[9] < $ostat[9]);
	return 0;
}

# Copy a file, *not* preserving date. Only works for text files.
sub copyFile {
	my ($src, $dest) = @_;
	open(I,$src) || croak "Could not open $src";
	open(O,">$dest") || croak "Could not open $dest";
	while (<I>) {
		print O;
	}
	close(I);
	close(O);
}

sub GenerateFiles {
	my $self = shift;

# Parse configure.in to get version numbers
	open(C,"configure.in") || confess("Could not open configure.in for reading\n");
	while (<C>) {
		if (/^AC_INIT\(\[PostgreSQL\], \[([^\]]+)\]/) {
			$self->{strver} = $1;
			if ($self->{strver} !~ /^(\d+)\.(\d+)(?:\.(\d+))?/) {
				confess "Bad format of version: $self->{strver}\n"
			}
			$self->{numver} = sprintf("%d%02d%02d", $1, $2, $3?$3:0);
			$self->{majorver} = sprintf("%d.%d", $1, $2);
		}
	}
	close(C);
	confess "Unable to parse configure.in for all variables!"
		if ($self->{strver} eq '' || $self->{numver} eq '');

	if (IsNewer("src\\include\\pg_config_os.h","src\\include\\port\\win32.h")) {
		print "Copying pg_config_os.h...\n";
		copyFile("src\\include\\port\\win32.h","src\\include\\pg_config_os.h");
	}
	
	if (IsNewer("src\\include\\pg_config.h","src\\include\\pg_config.h.win32")) {
		print "Generating pg_config.h...\n";
		open(I,"src\\include\\pg_config.h.win32") || confess "Could not open pg_config.h.win32\n";
		open(O,">src\\include\\pg_config.h") || confess "Could not write to pg_config.h\n";
		while (<I>) {
			s{PG_VERSION "[^"]+"}{PG_VERSION "$self->{strver}"};
			s{PG_VERSION_NUM \d+}{PG_VERSION_NUM $self->{numver}};
			s{PG_VERSION_STR "[^"]+"}{__STRINGIFY(x) #x\n#define __STRINGIFY2(z) __STRINGIFY(z)\n#define PG_VERSION_STR "PostgreSQL $self->{strver}, compiled by Visual C++ build " __STRINGIFY2(_MSC_VER)};
			print O;
		}
		print O "/* defines added by config steps */\n";
		print O "#define USE_ASSERT_CHECKING 1\n" if ($self->{options}->{asserts});
		print O "#define USE_LDAP 1\n" if ($self->{options}->{ldap});
		print O "#define HAVE_LIBZ 1\n" if ($self->{options}->{zlib});
		print O "#define USE_SSL 1\n" if ($self->{options}->{openssl});
		print O "#define ENABLE_NLS 1\n" if ($self->{options}->{nls});
		print O "#define LOCALEDIR \"/share/locale\"\n" if ($self->{options}->{nls});
		if ($self->{options}->{xml}) {
         print O "#define HAVE_LIBXML2\n";
         print O "#define USE_LIBXML\n";
      }
		if ($self->{options}->{krb5}) {
			print O "#define KRB5 1\n";
			print O "#define HAVE_KRB5_ERROR_TEXT_DATA 1\n";
			print O "#define HAVE_KRB5_TICKET_ENC_PART2 1\n";
			print O "#define PG_KRB_SRVNAM \"postgres\"\n";
		}
		close(O);
		close(I);
	}

	if (IsNewer("src\\interfaces\\libpq\\libpqdll.def","src\\interfaces\\libpq\\exports.txt")) {
		print "Generating libpqdll.def...\n";
		open(I,"src\\interfaces\\libpq\\exports.txt") || confess("Could not open exports.txt\n");
		open(O,">src\\interfaces\\libpq\\libpqdll.def") || confess("Could not open libpqdll.def\n");
		print O "LIBRARY LIBPQ\nEXPORTS\n";
		while (<I>) {
			next if (/^#/);
			my ($f, $o) = split;
			print O " $f @ $o\n";
		}
		close(O);
		close(I);
	}

	if (IsNewer("src\\backend\\utils\\fmgrtab.c","src\\include\\catalog\\pg_proc.h")) {
		print "Generating fmgrtab.c and fmgroids.h...\n";
		open(I,"src\\include\\catalog\\pg_proc.h") || confess "Could not open pg_proc.h";
		my @fmgr = ();
		my %seenit;
		while (<I>) {
			next unless (/^DATA/);
			s/^.*OID[^=]*=[^0-9]*//;
			s/\(//g;
			s/[ \t]*\).*$//;
			my @p = split;
			next if ($p[4] ne "12");
			push @fmgr,{
                oid     => $p[0],
                proname => $p[1],
                prosrc  => $p[$#p-2],
                nargs   => $p[12],
                strict  => $p[9],
                retset  => $p[10],
            };
		}
		close(I);

		open(H,'>', 'src\include\utils\fmgroids.h') ||
            confess "Could not open fmgroids.h";
		print H "/* fmgroids.h generated for Visual C++ */\n#ifndef FMGROIDS_H\n#define FMGROIDS_H\n\n";
		open(T,">src\\backend\\utils\\fmgrtab.c") || confess "Could not open fmgrtab.c";
		print T "/* fmgrtab.c generated for Visual C++ */\n#include \"postgres.h\"\n#include \"utils/fmgrtab.h\"\n\n";
		foreach my $s (sort {$a->{oid} <=> $b->{oid}} @fmgr) {
			next if $seenit{$s->{prosrc}};
			$seenit{$s->{prosrc}} = 1;
			print H "#define F_" . uc $s->{prosrc} . " $s->{oid}\n"; 
			print T "extern Datum $s->{prosrc} (PG_FUNCTION_ARGS);\n";
		}
		print H "\n#endif\n /* FMGROIDS_H */\n";
		close(H);
		print T "const FmgrBuiltin fmgr_builtins[] = {\n";
		my %bmap;
	    $bmap{'t'} = 'true';
	    $bmap{'f'} = 'false';
		foreach my $s (sort {$a->{oid} <=> $b->{oid}} @fmgr) {
			print T "  { $s->{oid}, \"$s->{prosrc}\", $s->{nargs}, $bmap{$s->{strict}}, $bmap{$s->{retset}}, $s->{prosrc} },\n";
		}

		
		print T " { 0, NULL, 0, false, false, NULL }\n};\n\nconst int fmgr_nbuiltins = (sizeof(fmgr_builtins) / sizeof(FmgrBuiltin)) - 1;\n";
		close(T);
	}

	if (IsNewer('src\interfaces\libpq\libpq.rc','src\interfaces\libpq\libpq.rc.in')) {
		print "Generating libpq.rc...\n";
		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
		my $d = ($year - 100) . "$yday";
		open(I,'<', 'src\interfaces\libpq\libpq.rc.in') || confess "Could not open libpq.rc.in";
		open(O,'>', 'src\interfaces\libpq\libpq.rc') || confess "Could not open libpq.rc";
		while (<I>) {
			s/(VERSION.*),0/$1,$d/;
			print O;
		}
		close(I);
		close(O);
	}

	if (IsNewer('src\bin\psql\sql_help.h','src\bin\psql\create_help.pl')) {
		print "Generating sql_help.h...\n";
		chdir('src\bin\psql');
		system("perl create_help.pl ../../../doc/src/sgml/ref sql_help.h");
		chdir('..\..\..');
	}

	if (IsNewer('src\interfaces\ecpg\include\ecpg_config.h', 'src\interfaces\ecpg\include\ecpg_config.h.in')) {
		print "Generating ecpg_config.h...\n";
		open(O,'>','src\interfaces\ecpg\include\ecpg_config.h') || confess "Could not open ecpg_config.h";
		print O <<EOF;
#if (_MSC_VER > 1200)
#define HAVE_LONG_LONG_INT_64
#endif
EOF
		close(O);
	}

	unless (-f "src\\port\\pg_config_paths.h") {
		print "Generating pg_config_paths.h...\n";
		open(O,'>', 'src\port\pg_config_paths.h') || confess "Could not open pg_config_paths.h";
		print O  <<EOF;
#define PGBINDIR "/bin"
#define PGSHAREDIR "/share"
#define SYSCONFDIR "/etc"
#define INCLUDEDIR "/include"
#define PKGINCLUDEDIR "/include"
#define INCLUDEDIRSERVER "/include/server"
#define LIBDIR "/lib"
#define PKGLIBDIR "/lib"
#define LOCALEDIR "/share/locale"
#define DOCDIR "/doc"
#define MANDIR "/man"
EOF
		close(O);
	}

	my $mf = Project::read_file('src\backend\catalog\Makefile');
	$mf =~ s{\\s*[\r\n]+}{}mg;
	$mf =~ /^POSTGRES_BKI_SRCS\s*:?=[^,]+,(.*)\)$/gm || croak "Could not find POSTGRES_BKI_SRCS in Makefile\n";
	my @allbki = split /\s+/, $1;
    foreach my $bki (@allbki) {
		next if $bki eq "";
		if (IsNewer('src/backend/catalog/postgres.bki', "src/include/catalog/$bki")) {
 		   print "Generating postgres.bki...\n";
 		   system("perl src/tools/msvc/genbki.pl $self->{majorver} src/backend/catalog/postgres " . join(' src/include/catalog/',@allbki));
 		   last;
        }
  	}
}

sub AddProject {
	my ($self, $name, $type, $folder, $initialdir) = @_;

	my $proj = new Project($name, $type, $self);
	push @{$self->{projects}->{$folder}}, $proj;
	$proj->AddDir($initialdir) if ($initialdir);
	if ($self->{options}->{zlib}) {
		$proj->AddIncludeDir($self->{options}->{zlib} . '\include');
		$proj->AddLibrary($self->{options}->{zlib} . '\lib\zdll.lib');
	}
	if ($self->{options}->{openssl}) {
		$proj->AddIncludeDir($self->{options}->{openssl} . '\include');
		$proj->AddLibrary($self->{options}->{openssl} . '\lib\VC\ssleay32.lib', 1);
		$proj->AddLibrary($self->{options}->{openssl} . '\lib\VC\libeay32.lib', 1);
	}
	if ($self->{options}->{nls}) {
		$proj->AddIncludeDir($self->{options}->{nls} . '\include');
		$proj->AddLibrary($self->{options}->{nls} . '\lib\intl.lib');
	}
	if ($self->{options}->{krb5}) {
		$proj->AddIncludeDir($self->{options}->{krb5} . '\inc\krb5');
		$proj->AddLibrary($self->{options}->{krb5} . '\lib\i386\krb5_32.lib');
		$proj->AddLibrary($self->{options}->{krb5} . '\lib\i386\comerr32.lib');
	}
	if ($self->{options}->{xml}) {
      $proj->AddIncludeDir($self->{options}->{xml} . '\include');
      $proj->AddIncludeDir($self->{options}->{iconv} . '\include');
      $proj->AddLibrary($self->{options}->{xml} . '\lib\libxml2.lib');
   }
	return $proj;
}

sub Save {
	my ($self) = @_;
	my %flduid;

	$self->GenerateFiles();
	foreach my $fld (keys %{$self->{projects}}) {
		foreach my $proj (@{$self->{projects}->{$fld}}) {
			$proj->Save();
		}
	}

	open(SLN,">pgsql.sln") || croak "Could not write to pgsql.sln\n";
	print SLN <<EOF;
Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
EOF

	foreach my $fld (keys %{$self->{projects}}) {
		foreach my $proj (@{$self->{projects}->{$fld}}) {
			print SLN <<EOF;
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "$proj->{name}", "$proj->{name}.vcproj", "$proj->{guid}"
EndProject
EOF
		}
		if ($fld ne "") {
			$flduid{$fld} = Win32::GuidGen();
			print SLN <<EOF;
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "$fld", "$fld", "$flduid{$fld}"
EndProject
EOF
		}
	}

	print SLN <<EOF;
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Win32 = Debug|Win32
		Release|Win32 = Release|Win32
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
EOF

	foreach my $fld (keys %{$self->{projects}}) {
		foreach my $proj (@{$self->{projects}->{$fld}}) {
		print SLN <<EOF;
		$proj->{guid}.Debug|Win32.ActiveCfg = Debug|Win32
		$proj->{guid}.Debug|Win32.Build.0  = Debug|Win32	
		$proj->{guid}.Release|Win32.ActiveCfg = Release|Win32
		$proj->{guid}.Release|Win32.Build.0 = Release|Win32
EOF
		}
	}

	print SLN <<EOF;
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(NestedProjects) = preSolution
EOF

	foreach my $fld (keys %{$self->{projects}}) {
		next if ($fld eq "");
		foreach my $proj (@{$self->{projects}->{$fld}}) {
			print SLN "\t\t$proj->{guid} = $flduid{$fld}\n";
		}
	}

	print SLN <<EOF;
	EndGlobalSection
EndGlobal
EOF
	close(SLN);
}

1;