From 83078e991235b6fc651dce2c6f5eb864f3c5848a Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 15 Sep 2006 21:42:02 +0000
Subject: [PATCH] Several fixes for MSVC build scripts, from Magnus.

---
 src/tools/msvc/Project.pm   | 74 +++++++++++++++++++++++++------------
 src/tools/msvc/Solution.pm  | 16 +++++++-
 src/tools/msvc/build.bat    | 30 +++++++++------
 src/tools/msvc/mkvcbuild.pl | 13 +++----
 4 files changed, 87 insertions(+), 46 deletions(-)

diff --git a/src/tools/msvc/Project.pm b/src/tools/msvc/Project.pm
index 239a4737dfd..e90b98d4b3e 100644
--- a/src/tools/msvc/Project.pm
+++ b/src/tools/msvc/Project.pm
@@ -81,7 +81,7 @@ sub AddReference {
 
 	while (my $ref = shift) {
 		push @{$self->{references}},$ref;
-		$self->AddLibrary("debug\\" . $ref->{name} . "\\" . $ref->{name} . ".lib") if ($ref->{type} eq "exe");
+		$self->AddLibrary("__CFGNAME__\\" . $ref->{name} . "\\" . $ref->{name} . ".lib");
 	}
 }
 
@@ -113,8 +113,8 @@ sub FullExportDLL {
 	my ($self, $libname) = @_;
 
 	$self->{builddef} = 1;
-	$self->{def} = ".\\debug\\$self->{name}\\$self->{name}.def";
-	$self->{implib} = "debug\\$self->{name}\\$libname";
+	$self->{def} = ".\\__CFGNAME__\\$self->{name}\\$self->{name}.def";
+	$self->{implib} = "__CFGNAME__\\$self->{name}\\$libname";
 }
 
 sub UseDef {
@@ -286,19 +286,19 @@ EOF
 			my $of = $f;
 			$of =~ s/\.y$/.c/;
 			$of =~ s{^src\\pl\\plpgsql\\src\\gram.c$}{src\\pl\\plpgsql\\src\\pl_gram.c};
-			print F '><FileConfiguration Name="Debug|Win32"><Tool Name="VCCustomBuildTool" Description="Running bison on ' . $f . '" CommandLine="vcbuild\pgbison.bat ' . $f . '" AdditionalDependencies="" Outputs="' . $of . '" /></FileConfiguration></File>' . "\n";
+			print F '>' . GenerateCustomTool('Running bison on ' . $f, 'src\tools\msvc\pgbison.bat ' . $f, $of) . '</File>' . "\n";
 		}
 		elsif ($f =~ /\.l$/) {
 			my $of = $f;
 			$of =~ s/\.l$/.c/;
 			$of =~ s{^src\\pl\\plpgsql\\src\\scan.c$}{src\\pl\\plpgsql\\src\\pl_scan.c};
-			print F "><FileConfiguration Name=\"Debug|Win32\"><Tool Name=\"VCCustomBuildTool\" Description=\"Running flex on $f\" CommandLine=\"vcbuild\\pgflex.bat $f\" AdditionalDependencies=\"\" Outputs=\"$of\" /></FileConfiguration></File>\n";
+			print F '>' . GenerateCustomTool('Running flex on ' . $f, 'src\tools\msvc\pgflex.bat ' . $f,$of) . '</File>' . "\n";
 		}
 		elsif (defined($uniquefiles{$file})) {
 # File already exists, so fake a new name
 			my $obj = $dir;
 			$obj =~ s/\\/_/g;
-			print F "><FileConfiguration Name=\"Debug|Win32\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\debug\\$self->{name}\\$obj" . "_$file.obj\" /></FileConfiguration></File>\n";
+			print F "><FileConfiguration Name=\"Debug|Win32\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\debug\\$self->{name}\\$obj" . "_$file.obj\" /></FileConfiguration><FileConfiguration Name=\"Release|Win32\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\release\\$self->{name}\\$obj" . "_$file.obj\" /></FileConfiguration></File>\n";
 		}
 		else {
 			$uniquefiles{$file} = 1;
@@ -313,6 +313,15 @@ EOF
 	close(F);
 }
 
+sub GenerateCustomTool {
+	my ($desc, $tool, $output, $cfg) = @_;
+	if (!defined($cfg)) {
+		return GenerateCustomTool($desc, $tool, $output, 'Debug') . 
+			   GenerateCustomTool($desc, $tool, $output, 'Release');
+	}
+	return "<FileConfiguration Name=\"$cfg|Win32\"><Tool Name=\"VCCustomBuildTool\" Description=\"$desc\" CommandLine=\"$tool\" AdditionalDependencies=\"\" Outputs=\"$output\" /></FileConfiguration>";
+}
+
 sub WriteReferences {
 	my ($self, $f) = @_;
 	print $f " <References>\n";
@@ -325,48 +334,65 @@ sub WriteReferences {
 sub WriteHeader {
 	my ($self, $f) = @_;
 
-	my $cfgtype = ($self->{type} eq "exe")?1:($self->{type} eq "dll"?2:4);
-
 	print $f <<EOF;
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject ProjectType="Visual C++" Version="8.00" Name="$self->{name}" ProjectGUID="$self->{guid}">
  <Platforms><Platform Name="Win32"/></Platforms>
  <Configurations>
-  <Configuration Name="Debug|Win32" OutputDirectory=".\\Debug\\$self->{name}" IntermediateDirectory=".\\Debug\\$self->{name}"
-	ConfigurationType="$cfgtype" UseOfMFC="0" ATLMinimizesCRunTimeLibraryUsage="FALSE" CharacterSet="2">
-	<Tool Name="VCCLCompilerTool" Optimization="0"
+EOF
+	$self->WriteConfiguration($f, 'Debug', { defs=>'_DEBUG;DEBUG=1;', wholeopt=>0 , opt=>0, strpool=>'false', runtime=>3 });
+	$self->WriteConfiguration($f, 'Release', { defs=>'', wholeopt=>0, opt=>3, strpool=>'true', runtime=>2 });
+print $f <<EOF;
+ </Configurations>
+EOF
+}
+
+sub WriteConfiguration
+{
+	my ($self, $f, $cfgname, $p) = @_;
+	my $cfgtype = ($self->{type} eq "exe")?1:($self->{type} eq "dll"?2:4);
+	my $libs = $self->{libraries};
+	$libs =~ s/__CFGNAME__/$cfgname/g;
+	print $f <<EOF;
+  <Configuration Name="$cfgname|Win32" OutputDirectory=".\\$cfgname\\$self->{name}" IntermediateDirectory=".\\$cfgname\\$self->{name}"
+	ConfigurationType="$cfgtype" UseOfMFC="0" ATLMinimizesCRunTimeLibraryUsage="FALSE" CharacterSet="2" WholeProgramOptimization="$p->{wholeopt}">
+	<Tool Name="VCCLCompilerTool" Optimization="$p->{opt}"
 		AdditionalIncludeDirectories="src/include;src/include/port/win32;src/include/port/win32_msvc;$self->{solution}->{options}->{pthread};$self->{includes}"
-		PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;__WINDOWS__;DEBUG=1;__WIN32__;EXEC_BACKEND;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE$self->{defines}"
-		RuntimeLibrary="3" DisableSpecificWarnings="$self->{disablewarnings}"
+		PreprocessorDefinitions="WIN32;_WINDOWS;__WINDOWS__;__WIN32__;EXEC_BACKEND;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE$self->{defines}$p->{defs}"
+		StringPooling="$p->{strpool}"
+		RuntimeLibrary="$p->{runtime}" DisableSpecificWarnings="$self->{disablewarnings}"
 EOF
 	print $f <<EOF;
-		AssemblerOutput="0" AssemblerListingLocation=".\\debug\\$self->{name}\\" ObjectFile=".\\debug\\$self->{name}\\"
-		ProgramDataBaseFileName=".\\debug\\$self->{name}\\" BrowseInformation="0"
+		AssemblerOutput="0" AssemblerListingLocation=".\\$cfgname\\$self->{name}\\" ObjectFile=".\\$cfgname\\$self->{name}\\"
+		ProgramDataBaseFileName=".\\$cfgname\\$self->{name}\\" BrowseInformation="0"
 		WarningLevel="3" SuppressStartupBanner="TRUE" DebugInformationFormat="3" CompileAs="0"/>
-	<Tool Name="VCLinkerTool" OutputFile=".\\debug\\$self->{name}\\$self->{name}.$self->{type}"
-		AdditionalDependencies="$self->{libraries}"
+	<Tool Name="VCLinkerTool" OutputFile=".\\$cfgname\\$self->{name}\\$self->{name}.$self->{type}"
+		AdditionalDependencies="$libs"
 		LinkIncremental="0" SuppressStartupBanner="TRUE" AdditionalLibraryDirectories="" IgnoreDefaultLibraryNames="libc"
 		StackReserveSize="4194304" DisableSpecificWarnings="$self->{disablewarnings}"
-		GenerateDebugInformation="TRUE" ProgramDatabaseFile=".\\debug\\$self->{name}\\$self->{name}.pdb"
-		GenerateMapFile="FALSE" MapFileName=".\\debug\\$self->{name}\\$self->{name}.map"
+		GenerateDebugInformation="TRUE" ProgramDatabaseFile=".\\$cfgname\\$self->{name}\\$self->{name}.pdb"
+		GenerateMapFile="FALSE" MapFileName=".\\$cfgname\\$self->{name}\\$self->{name}.map"
 		SubSystem="1" TargetMachine="1"
 EOF
 	if ($self->{implib}) {
-		print $f "\t\tImportLibrary=\"$self->{implib}\"\n";
+		my $l = $self->{implib};
+		$l =~ s/__CFGNAME__/$cfgname/g;
+		print $f "\t\tImportLibrary=\"$l\"\n";
 	}
 	if ($self->{def}) {
-		print $f "\t\tModuleDefinitionFile=\"$self->{def}\"\n";
+		my $d = $self->{def};
+		$d =~ s/__CFGNAME__/$cfgname/g;
+		print $f "\t\tModuleDefinitionFile=\"$d\"\n";
 	}
 
 	print $f "\t/>\n";
-	print $f "\t<Tool Name=\"VCLibrarianTool\" OutputFile=\".\\Debug\\$self->{name}\\$self->{name}.lib\" IgnoreDefaultLibraryNames=\"libc\" />\n";
+	print $f "\t<Tool Name=\"VCLibrarianTool\" OutputFile=\".\\$cfgname\\$self->{name}\\$self->{name}.lib\" IgnoreDefaultLibraryNames=\"libc\" />\n";
 	print $f "\t<Tool Name=\"VCResourceCompilerTool\" AdditionalIncludeDirectories=\"src\\include\" />\n";
 	if ($self->{builddef}) {
-		print $f "\t<Tool Name=\"VCPreLinkEventTool\" Description=\"Generate DEF file\" CommandLine=\"perl vcbuild\\gendef.pl debug\\$self->{name}\" />\n";
+		print $f "\t<Tool Name=\"VCPreLinkEventTool\" Description=\"Generate DEF file\" CommandLine=\"perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name}\" />\n";
 	}
 	print $f <<EOF;
   </Configuration>
- </Configurations>
 EOF
 }
 
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index 2b1357b09b0..13730075018 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -21,8 +21,8 @@ sub new {
 # Special case - if config.pl has changed, always return 1
 sub IsNewer {
 	my ($newfile, $oldfile) = @_;
-	if ($oldfile ne 'vcbuild\config.pl') {
-		return 1 if IsNewer($newfile, 'vcbuild\config.pl');
+	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);
@@ -177,6 +177,17 @@ sub GenerateFiles {
 		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";
@@ -271,6 +282,7 @@ EOF
 		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
 		}
diff --git a/src/tools/msvc/build.bat b/src/tools/msvc/build.bat
index 024819f98fd..b2b89f02004 100755
--- a/src/tools/msvc/build.bat
+++ b/src/tools/msvc/build.bat
@@ -1,12 +1,18 @@
-@echo off
-SET STARTDIR=%CD%
-
-perl mkvcbuild.pl
-if errorlevel 1 goto :eof
-
-if exist ..\vcbuild if exist ..\src cd ..
-
-if "%1" == "" msbuild pgsql.sln
-if not "%1" == "" vcbuild %1.vcproj
-
-cd %STARTDIR%
+@echo off
+SET STARTDIR=%CD%
+
+perl mkvcbuild.pl
+if errorlevel 1 goto :eof
+
+if exist ..\msvc if exist ..\..\..\src cd ..\..\..
+SET CONFIG=
+if "%1" == "" set CONFIG=Debug
+if "%CONFIG%" == "" if "%1" == "DEBUG" set CONFIG=Debug
+if "%CONFIG%" == "" if "%1" == "RELEASE" set CONFIG=Release
+if not "%CONFIG%" == "" shift
+if "%CONFIG%" == "" set CONFIG=Debug
+
+if "%1" == "" msbuild pgsql.sln /p:Configuration=%CONFIG%
+if not "%1" == "" vcbuild %1.vcproj %CONFIG%
+
+cd %STARTDIR%
diff --git a/src/tools/msvc/mkvcbuild.pl b/src/tools/msvc/mkvcbuild.pl
index 3eac344df0b..4020aa0d4c0 100644
--- a/src/tools/msvc/mkvcbuild.pl
+++ b/src/tools/msvc/mkvcbuild.pl
@@ -5,12 +5,12 @@ use warnings;
 use Project;
 use Solution;
 
-chdir('..') if (-d '..\vcbuild' && -d '..\src');
-die 'Must run from root directory or vcbuild directory' unless (-d 'vcbuild' && -d 'src');
-die 'Could not find config.pl' unless (-f 'vcbuild/config.pl');
+chdir('..\..\..') if (-d '..\msvc' && -d '..\..\..\src');
+die 'Must run from root or msvc directory' unless (-d 'src\tools\msvc' && -d 'src');
+die 'Could not find config.pl' unless (-f 'src/tools/msvc/config.pl');
 
 our $config;
-require 'vcbuild/config.pl';
+require 'src/tools/msvc/config.pl';
 
 my $solution = new Solution($config);
 
@@ -79,6 +79,7 @@ my $libpq = $solution->AddProject('libpq','dll','interfaces','src\interfaces\lib
 $libpq->AddDefine('FRONTEND');
 $libpq->AddIncludeDir('src\port');
 $libpq->AddLibrary('wsock32.lib');
+$libpq->AddLibrary('wldap32.lib') if ($solution->{options}->{ldap});
 $libpq->UseDef('src\interfaces\libpq\libpqdll.def');
 $libpq->ReplaceFile('src\interfaces\libpq\libpqrc.c','src\interfaces\libpq\libpq.rc');
 
@@ -188,8 +189,6 @@ foreach my $prg (split /\s+/,$1) {
 	$proj->AddIncludeDir('src\bin\psql');
 	$proj->AddReference($libpq,$libpgport);
 	$proj->AddResourceFile('src\bin\scripts','PostgreSQL Utility');
-	$proj->AddLibrary('debug\libpgport\libpgport.lib');
-	$proj->AddLibrary('debug\libpq\libpq.lib');
 }
 $/ = $t;
 
@@ -224,11 +223,9 @@ sub AddSimpleFrontend {
 	$p->AddDir('src\bin\\' . $n);
 	$p->AddDefine('FRONTEND');
 	$p->AddReference($libpgport);
-	$p->AddLibrary('debug\libpgport\libpgport.lib');
 	if ($uselibpq) {
 		$p->AddIncludeDir('src\interfaces\libpq');
 		$p->AddReference($libpq);
-		$p->AddLibrary('debug\libpq\libpq.lib');
 	}
 	return $p;
 }
-- 
GitLab