From 2720f373369f2230567e192b6ce6b10d5e6ad304 Mon Sep 17 00:00:00 2001
From: Tsutomu Yamada <tsutomu@sraoss.co.jp>
Date: Fri, 28 Aug 2009 19:35:38 +0900
Subject: [PATCH 3/3] MSVC build scripts for Windows x64

* add platform x64
* support VC2008
---
 src/tools/msvc/Mkvcbuild.pm |   14 ++++++++
 src/tools/msvc/Project.pm   |   75 +++++++++++++++++++++++++++++++++---------
 src/tools/msvc/Solution.pm  |   20 +++++++----
 src/tools/msvc/clean.bat    |    5 +++
 src/tools/msvc/config.pl    |    5 +++
 src/tools/msvc/gendef.pl    |   29 +++++++++++-----
 6 files changed, 115 insertions(+), 33 deletions(-)

diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 9b5d149..1264524 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -387,6 +387,20 @@ sub mkvcbuild
     $pgregress->AddReference($libpgport);
 
     $solution->Save();
+
+    # If this is Studio 2008 and up, then the projects need up be upgraded first
+    if ($config->{msvc_version} ge 1500)
+    {
+	print "Converting projects to current Visual Studio version...\n";
+	foreach my $fld (keys %{$solution->{projects}})
+	{
+	    foreach my $proj (@{$solution->{projects}->{$fld}})
+	    {
+		print "\n\nUpgrading $proj->{name}\n\n";
+		system("vcbuild /upgrade $proj->{name}.vcproj");
+	    }
+	}
+    }
 }
 
 #####################
diff --git a/src/tools/msvc/Project.pm b/src/tools/msvc/Project.pm
index 4118011..415fdd1 100644
--- a/src/tools/msvc/Project.pm
+++ b/src/tools/msvc/Project.pm
@@ -338,6 +338,7 @@ sub DisableLinkerWarnings
 sub Save
 {
     my ($self) = @_;
+    my $platform = $self->{solution}->{options}->{platform};
 
     # If doing DLL and haven't specified a DEF file, do a full export of all symbols
     # in the project.
@@ -391,7 +392,7 @@ EOF
             $of =~ s{^src\\pl\\plpgsql\\src\\gram.c$}{src\\pl\\plpgsql\\src\\pl_gram.c};
             print F '>'
               . GenerateCustomTool('Running bison on ' . $f,
-                'cmd /V:ON /c src\tools\msvc\pgbison.bat ' . $f, $of)
+                'cmd /V:ON /c src\tools\msvc\pgbison.bat ' . $f, $of, $platform)
               . '</File>' . "\n";
         }
         elsif ($f =~ /\.l$/)
@@ -399,7 +400,7 @@ EOF
             my $of = $f;
             $of =~ s/\.l$/.c/;
             print F '>'
-              . GenerateCustomTool('Running flex on ' . $f, 'src\tools\msvc\pgflex.bat ' . $f,$of)
+              . GenerateCustomTool('Running flex on ' . $f, 'src\tools\msvc\pgflex.bat ' . $f,$of, $platform)
               . '</File>' . "\n";
         }
         elsif (defined($uniquefiles{$file}))
@@ -409,8 +410,8 @@ EOF
             my $obj = $dir;
             $obj =~ s/\\/_/g;
             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"
+"><FileConfiguration Name=\"Debug|$platform\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\debug\\$self->{name}\\$obj"
+              . "_$file.obj\" /></FileConfiguration><FileConfiguration Name=\"Release|$platform\"><Tool Name=\"VCCLCompilerTool\" ObjectFile=\".\\release\\$self->{name}\\$obj"
               . "_$file.obj\" /></FileConfiguration></File>\n";
         }
         else
@@ -430,14 +431,14 @@ EOF
 
 sub GenerateCustomTool
 {
-    my ($desc, $tool, $output, $cfg) = @_;
+    my ($desc, $tool, $output, $platform, $cfg) = @_;
     if (!defined($cfg))
     {
-        return GenerateCustomTool($desc, $tool, $output, 'Debug')
-          .GenerateCustomTool($desc, $tool, $output, 'Release');
+        return GenerateCustomTool($desc, $tool, $output, $platform, 'Debug')
+          .GenerateCustomTool($desc, $tool, $output, $platform, 'Release');
     }
     return
-"<FileConfiguration Name=\"$cfg|Win32\"><Tool Name=\"VCCustomBuildTool\" Description=\"$desc\" CommandLine=\"$tool\" AdditionalDependencies=\"\" Outputs=\"$output\" /></FileConfiguration>";
+"<FileConfiguration Name=\"$cfg|$platform\"><Tool Name=\"VCCustomBuildTool\" Description=\"$desc\" CommandLine=\"$tool\" AdditionalDependencies=\"\" Outputs=\"$output\" /></FileConfiguration>";
 }
 
 sub WriteReferences
@@ -455,11 +456,12 @@ sub WriteReferences
 sub WriteHeader
 {
     my ($self, $f) = @_;
+    my $platform = $self->{solution}->{options}->{platform};
 
     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>
+ <Platforms><Platform Name="$platform"/></Platforms>
  <Configurations>
 EOF
     $self->WriteConfiguration($f, 'Debug',
@@ -477,6 +479,47 @@ sub WriteConfiguration
     my $cfgtype = ($self->{type} eq "exe")?1:($self->{type} eq "dll"?2:4);
     my $libcfg = (uc $cfgname eq "RELEASE")?"MD":"MDd";
     my $libs = '';
+    my $platform = $self->{solution}->{options}->{platform};
+    my $local_compiler_opt = $self->{solution}->{options}->{compiler_opt};
+    my $local_linker_opt = $self->{solution}->{options}->{linker_opt};
+
+    # Enable to use of multiple CPUs/cores
+    $local_compiler_opt .= ' ' unless ($local_compiler_opt eq '');
+    $local_compiler_opt .= ' /MP';
+
+    # We should specify the target machine as an additional option, since Microsoft uses cryptic
+    # undocumented codes for different target platforms.
+    $local_linker_opt .= ' ' unless ($local_linker_opt eq ''); # add a space
+    if ($platform eq 'Win32')
+    {
+        $local_linker_opt .= '/Machine:X86';
+    }
+    elsif ($platform eq 'x64')
+    {
+        $local_linker_opt .= '/Machine:X64';
+    }
+    else
+    {
+        die 'That platform is not supported';
+    }
+
+    # if we're using x64, we need to disable LNK4197.  This link explains why:
+    # http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99193
+    if ($platform eq 'x64')
+    {
+        # from some reason (?) it is an error to specify this together with the other /ignore arguments...
+        # making it separate makes the error disappear
+        $local_linker_opt .= ' ' unless ($local_linker_opt eq ''); # add a space
+        $local_linker_opt .= '/ignore:4197';
+    }
+
+    # Fold the disable linker warnings stuff into the local_linker_opt variable
+    if ($self->{disablelinkerwarnings})
+    {
+        $local_linker_opt .= ' ' unless ($local_linker_opt eq ''); # add a space
+        $local_linker_opt .= "/ignore:$self->{disablelinkerwarnings}";
+    }
+
     foreach my $lib (@{$self->{libraries}})
     {
         my $xlib = $lib;
@@ -493,20 +536,21 @@ sub WriteConfiguration
     $libs =~ s/ $//;
     $libs =~ s/__CFGNAME__/$cfgname/g;
     print $f <<EOF;
-  <Configuration Name="$cfgname|Win32" OutputDirectory=".\\$cfgname\\$self->{name}" IntermediateDirectory=".\\$cfgname\\$self->{name}"
+  <Configuration Name="$cfgname|$platform" 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}"
+		AdditionalOptions="$local_compiler_opt"
 		AdditionalIncludeDirectories="$self->{prefixincludes}src/include;src/include/port/win32;src/include/port/win32_msvc;$self->{includes}"
 		PreprocessorDefinitions="WIN32;_WINDOWS;__WINDOWS__;__WIN32__;EXEC_BACKEND;WIN32_STACK_RLIMIT=4194304;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE$self->{defines}$p->{defs}"
 		StringPooling="$p->{strpool}"
 		RuntimeLibrary="$p->{runtime}" DisableSpecificWarnings="$self->{disablewarnings}"
-		AdditionalOptions="/MP"
 EOF
     print $f <<EOF;
 		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=".\\$cfgname\\$self->{name}\\$self->{name}.$self->{type}"
+		AdditionalOptions="$local_linker_opt"
 		AdditionalDependencies="$libs"
 		LinkIncremental="0" SuppressStartupBanner="TRUE" AdditionalLibraryDirectories="" IgnoreDefaultLibraryNames="libc"
 		StackReserveSize="4194304" DisableSpecificWarnings="$self->{disablewarnings}"
@@ -514,10 +558,6 @@ EOF
 		GenerateMapFile="FALSE" MapFileName=".\\$cfgname\\$self->{name}\\$self->{name}.map"
 		SubSystem="1" TargetMachine="1"
 EOF
-    if ($self->{disablelinkerwarnings})
-    {
-        print $f "\t\tAdditionalOptions=\"/ignore:$self->{disablelinkerwarnings}\"\n";
-    }
     if ($self->{implib})
     {
         my $l = $self->{implib};
@@ -538,8 +578,11 @@ EOF
       "\t<Tool Name=\"VCResourceCompilerTool\" AdditionalIncludeDirectories=\"src\\include\" />\n";
     if ($self->{builddef})
     {
+        # gendef needs to be passed the platform now, because the x64 COFF ABI is different
+        # than the 32 bit ABI.  The old one placed a _ in front of all the symbols we need, while
+        # the x64 version has no special prefix
         print $f
-"\t<Tool Name=\"VCPreLinkEventTool\" Description=\"Generate DEF file\" CommandLine=\"perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name}\" />\n";
+"\t<Tool Name=\"VCPreLinkEventTool\" Description=\"Generate DEF file\" CommandLine=\"perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name} $platform\" />\n";
     }
     print $f <<EOF;
   </Configuration>
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index d2621cb..80804ff 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -87,6 +87,10 @@ sub copyFile
 sub GenerateFiles
 {
     my $self = shift;
+    my $platform = $self->{options}->{platform};
+    my $bits = "unknown";
+    $bits = "32-bit" if ($platform eq 'Win32');
+    $bits = "64-bit" if ($platform eq 'x64');
 
     # Parse configure.in to get version numbers
     open(C,"configure.in") || confess("Could not open configure.in for reading\n");
@@ -122,8 +126,7 @@ sub GenerateFiles
         {
             s{PG_VERSION "[^"]+"}{PG_VERSION "$self->{strver}"};
             s{PG_VERSION_NUM \d+}{PG_VERSION_NUM $self->{numver}};
-            # XXX: When we support 64-bit, need to remove this hardcoding
-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) ", 32-bit"};
+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) ", $bits"};
             print O;
         }
 		print O "#define PG_MAJORVERSION \"$self->{majorver}\"\n";
@@ -385,6 +388,7 @@ sub AddProject
 sub Save
 {
     my ($self) = @_;
+    my $platform = $self->{options}->{platform};
     my %flduid;
 
     $self->GenerateFiles();
@@ -424,8 +428,8 @@ EOF
     print SLN <<EOF;
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
-		Debug|Win32 = Debug|Win32
-		Release|Win32 = Release|Win32
+		Debug|$platform = Debug|$platform
+		Release|$platform = Release|$platform
 	EndGlobalSection
 	GlobalSection(ProjectConfigurationPlatforms) = postSolution
 EOF
@@ -435,10 +439,10 @@ EOF
         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
+		$proj->{guid}.Debug|$platform.ActiveCfg = Debug|$platform
+		$proj->{guid}.Debug|$platform.Build.0  = Debug|$platform	
+		$proj->{guid}.Release|$platform.ActiveCfg = Release|$platform
+		$proj->{guid}.Release|$platform.Build.0 = Release|$platform
 EOF
         }
     }
diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat
index e165b33..4c417c6 100755
--- a/src/tools/msvc/clean.bat
+++ b/src/tools/msvc/clean.bat
@@ -9,8 +9,13 @@ if exist ..\msvc if exist ..\..\..\src cd ..\..\..
 
 if exist debug rd /s /q debug
 if exist release rd /s /q release
+if exist _UpgradeReport_Files rd /s /q _UpgradeReport_Files
+for %%f in (UpgradeLog*.xml) do del %%f
+for %%f in (*.old) do del %%f
+for %%f in (*.user) do del %%f
 for %%f in (*.vcproj) do del %%f
 if exist pgsql.sln del /q pgsql.sln
+if exist pgsql.ncb del /q pgsql.ncb
 del /s /q src\bin\win32ver.rc 2> NUL
 del /s /q src\interfaces\win32ver.rc 2> NUL
 if exist src\backend\win32ver.rc del /q src\backend\win32ver.rc
diff --git a/src/tools/msvc/config.pl b/src/tools/msvc/config.pl
index 1e37505..fe884bb 100644
--- a/src/tools/msvc/config.pl
+++ b/src/tools/msvc/config.pl
@@ -3,6 +3,11 @@ use strict;
 use warnings;
 
 our $config = {
+    msvc_version=>1400,	      # 1400 = VC 2005, 1500 = VC 2008
+    platform=>'Win32',        # 'Win32' for x86, 'x64' for x64
+    compiler_opt=>'',         # additional compiler command line arguments
+    linker_opt=>'',           # additional linker command line arguments
+    
     asserts=>0,			# --enable-cassert
     # integer_datetimes=>1,   # --enable-integer-datetimes - on is now default
     # float4byval=>1,         # --disable-float4-byval, on by default
diff --git a/src/tools/msvc/gendef.pl b/src/tools/msvc/gendef.pl
index d30417c..c942c5b 100644
--- a/src/tools/msvc/gendef.pl
+++ b/src/tools/msvc/gendef.pl
@@ -5,24 +5,36 @@ my @def;
 # $PostgreSQL$
 #
 
-die "Usage: gendef.pl <modulepath>\n" unless ($ARGV[0] =~ /\\([^\\]+$)/);
+die "Usage: gendef.pl <modulepath> <platform>\n" unless ($ARGV[0] =~ /\\([^\\]+$)/ && ($ARGV[1] == 'Win32' || $ARGV[1] == 'x64'));
 my $defname = uc $1;
 
-if (-f "$ARGV[0]/$defname.def")
+# First, change to the project directory.  Instead of producing symbol.out in the root directory
+# like before and then and renaming it to (configuration)/project/(object file).sym, we directly
+# output the symbol table as ((object file base name).sym.  The reason for doing this is that when
+# compiling in parallel, several attempts might be made to work on the same file (symbols.out)
+# but this way we can keep everything separate. 
+chdir($ARGV[0]);
+
+if (-f "$defname.def")
 {
     print "Not re-generating $defname.DEF, file already exists.\n";
     exit(0);
 }
 
+# We need access to the platform, because the symbol tables are different for Win32 and x64.
+# The x64 symbols don't have a leading _ character, unlike their 32-bit counterparts
+$platform = $ARGV[1];
+
 print "Generating $defname.DEF from directory $ARGV[0]\n";
 
-while (<$ARGV[0]/*.obj>)
+while (<*.obj>)
 {
     my $symfile = $_;
-    $symfile=~ s/\.obj$/.sym/i;
+    $symfile =~ s/\.obj$/.sym/i;
+
     print ".";
-    system("dumpbin /symbols /out:symbols.out $_ >NUL") && die "Could not call dumpbin";
-    open(F, "<symbols.out") || die "Could not open symbols.out for $_\n";
+    system("dumpbin /symbols /out:$symfile $_ >NUL") && die "Could not call dumpbin";
+    open(F, "<$symfile") || die "Could not open $symfile for $_\n";
     while (<F>)
     {
         s/\(\)//g;
@@ -43,11 +55,10 @@ while (<$ARGV[0]/*.obj>)
         push @def, $pieces[6];
     }
     close(F);
-    rename("symbols.out",$symfile);
 }
 print "\n";
 
-open(DEF,">$ARGV[0]/$defname.def") || die "Could not write to $defname\n";
+open(DEF,">$defname.def") || die "Could not write to $defname\n";
 print DEF "EXPORTS\n";
 my $i = 0;
 my $last = "";
@@ -55,7 +66,7 @@ foreach my $f (sort @def)
 {
     next if ($f eq $last);
     $last = $f;
-    $f =~ s/^_//;
+    $f =~ s/^_// if ($platform eq 'Win32');
     $i++;
 
     #   print DEF "  $f \@ $i\n";  # ordinaled exports?
-- 
1.6.4

