From 63876d3bac5a7471a7987da25a93c13a2534a644 Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Tue, 3 Jan 2012 08:44:26 -0500 Subject: [PATCH] Support for building with MS Visual Studio 2010. Brar Piening, reviewed by Craig Ringer. --- doc/src/sgml/install-windows.sgml | 54 ++--- src/include/port/win32.h | 22 ++ src/tools/msvc/Install.pm | 67 ++++-- src/tools/msvc/MSBuildProject.pm | 388 ++++++++++++++++++++++++++++++ src/tools/msvc/Mkvcbuild.pm | 19 +- src/tools/msvc/Project.pm | 232 ++---------------- src/tools/msvc/README | 84 ++++++- src/tools/msvc/Solution.pm | 102 ++++++-- src/tools/msvc/VCBuildProject.pm | 267 ++++++++++++++++++++ src/tools/msvc/VSObjectFactory.pm | 122 ++++++++++ src/tools/msvc/build.pl | 8 +- src/tools/msvc/builddoc.pl | 4 +- src/tools/msvc/clean.bat | 4 + 13 files changed, 1079 insertions(+), 294 deletions(-) create mode 100644 src/tools/msvc/MSBuildProject.pm create mode 100644 src/tools/msvc/VCBuildProject.pm create mode 100644 src/tools/msvc/VSObjectFactory.pm diff --git a/doc/src/sgml/install-windows.sgml b/doc/src/sgml/install-windows.sgml index f96b174fcc0..b6ea0abc40b 100644 --- a/doc/src/sgml/install-windows.sgml +++ b/doc/src/sgml/install-windows.sgml @@ -20,10 +20,10 @@ There are several different ways of building PostgreSQL on Windows. The simplest way to build with Microsoft tools is to install a supported version of the - Microsoft Platform SDK and use the included + Microsoft Windows SDK and use the included compiler. It is also possible to build with the full - Microsoft Visual C++ 2005 or 2008. In some cases - that requires the installation of the Platform SDK + Microsoft Visual C++ 2005, 2008 or 2010. In some cases + that requires the installation of the Windows SDK in addition to the compiler. @@ -69,32 +69,26 @@ Building with <productname>Visual C++</productname> or the - <productname>Platform SDK</productname> + Microsoft Windows SDK PostgreSQL can be built using the Visual C++ compiler suite from Microsoft. These compilers can be either from Visual Studio, Visual Studio Express or some versions of the - Platform SDK. If you do not already have a + Microsoft Windows SDK. If you do not already have a Visual Studio environment set up, the easiest - way us to use the compilers in the Platform SDK, + way is to use the compilers in the Windows SDK, which is a free download from Microsoft. - PostgreSQL supports the compilers from - Visual Studio 2005 and - Visual Studio 2008. When using the Platform SDK - only, or when building for 64-bit Windows, only - Visual Studio 2008 is supported. - Visual Studio 2010 is not yet supported. - - - - When building using the Platform SDK, versions - 6.0 to 7.0 of the SDK are supported. Older or newer versions will not work. - In particular, versions from 7.0a and later will not work, since - they include compilers from Visual Studio 2010. + PostgreSQL is known to support compilation using the compilers shipped with + Visual Studio 2005 to + Visual Studio 2010 (including Express editions), + as well as standalone Windows SDK releases 6.0 to 7.1. + 64-bit PostgreSQL builds are only supported with + Microsoft Windows SDK version 6.0a and above or + Visual Studio 2008 and above. @@ -104,11 +98,13 @@ Cygwin present in your system PATH. Also, make sure you have all the required Visual C++ tools available in the PATH. In Visual Studio, start the - Visual Studio Command Prompt. In the - Platform SDK, start the - CMD shell listed under the SDK on the Start Menu. + Visual Studio Command Prompt. If you wish to build a 64-bit version, you must use the 64-bit version of the command, and vice versa. + In the Microsoft Windows SDK, start the + CMD shell listed under the SDK on the Start Menu. + In recent SDK versions you can change the targeted CPU architecture by using + the setenv command. All commands should be run from the src\tools\msvc directory. @@ -148,17 +144,17 @@ $ENV{PATH}=$ENV{PATH} . ';c:\some\where\bison\bin'; - Microsoft Platform SDK + Microsoft Windows SDK It is recommended that you upgrade to the latest supported version - of the Microsoft Platform SDK (currently - version 7.0), available for download from + of the Microsoft Windows SDK (currently + version 7.1), available for download from . You must always include the Windows Headers and Libraries part of the SDK. - If you install the Platform SDK + If you install the Windows SDK including the Visual C++ Compilers, you don't need Visual Studio to build. @@ -202,6 +198,10 @@ $ENV{PATH}=$ENV{PATH} . ';c:\some\where\bison\bin'; Bison can be downloaded from . Flex can be downloaded from . + If you are using msysGit for accessing the + PostgreSQL Git repository you probably already + have recent versions of bison and flex in your Git + binary directory. @@ -479,7 +479,7 @@ $ENV{DOCROOT}='c:\docbook'; static library to link into an application. For normal use the MinGW or Visual Studio or - Platform SDK method is recommended. + Windows SDK method is recommended. diff --git a/src/include/port/win32.h b/src/include/port/win32.h index afc96283ebc..e2dd23b284a 100644 --- a/src/include/port/win32.h +++ b/src/include/port/win32.h @@ -103,7 +103,9 @@ #define IPC_STAT 4096 #define EACCESS 2048 +#ifndef EIDRM #define EIDRM 4096 +#endif #define SETALL 8192 #define GETNCNT 16384 @@ -299,6 +301,26 @@ typedef int pid_t; #define EOPNOTSUPP WSAEOPNOTSUPP #endif +/* + * For Microsoft Visual Studio 2010 and above we intentionally redefine + * the regular Berkeley error constants and set them to the WSA constants. + * Note that this will break if those constants are used for anything else + * than Windows Sockets errors. + */ +#if _MSC_VER >= 1600 +#pragma warning(disable:4005) +#define EMSGSIZE WSAEMSGSIZE +#define EAFNOSUPPORT WSAEAFNOSUPPORT +#define EWOULDBLOCK WSAEWOULDBLOCK +#define EPROTONOSUPPORT WSAEPROTONOSUPPORT +#define ECONNRESET WSAECONNRESET +#define EINPROGRESS WSAEINPROGRESS +#define ENOBUFS WSAENOBUFS +#define ECONNREFUSED WSAECONNREFUSED +#define EOPNOTSUPP WSAEOPNOTSUPP +#pragma warning(default:4005) +#endif + /* * Extended locale functions with gratuitous underscore prefixes. * (These APIs are nevertheless fully documented by Microsoft.) diff --git a/src/tools/msvc/Install.pm b/src/tools/msvc/Install.pm index a7720a060f8..6176743d8bd 100644 --- a/src/tools/msvc/Install.pm +++ b/src/tools/msvc/Install.pm @@ -56,11 +56,8 @@ sub Install my $majorver = DetermineMajorVersion(); print "Installing version $majorver for $conf in $target\n"; - EnsureDirectories( - $target, 'bin', 'lib', 'share', - 'share/timezonesets','share/extension', 'share/contrib','doc', - 'doc/extension', 'doc/contrib','symbols', 'share/tsearch_data' - ); + EnsureDirectories($target, 'bin', 'lib', 'share','share/timezonesets','share/extension', + 'share/contrib','doc','doc/extension', 'doc/contrib','symbols', 'share/tsearch_data'); CopySolutionOutput($conf, $target); lcopy($target . '/lib/libpq.dll', $target . '/bin/libpq.dll'); @@ -186,6 +183,13 @@ sub CopySolutionOutput my $rem = qr{Project\("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"\) = "([^"]+)"}; my $sln = read_file("pgsql.sln") || croak "Could not open pgsql.sln\n"; + + my $vcproj = 'vcproj'; + if ($sln =~ /Microsoft Visual Studio Solution File, Format Version (\d+)\.\d+/ && $1 >= 11) + { + $vcproj = 'vcxproj'; + } + print "Copying build output files..."; while ($sln =~ $rem) { @@ -195,26 +199,48 @@ sub CopySolutionOutput $sln =~ s/$rem//; - my $proj = read_file("$pf.vcproj") || croak "Could not open $pf.vcproj\n"; - if ($proj !~ qr{ConfigurationType="([^"]+)"}) + my $proj = read_file("$pf.$vcproj") || croak "Could not open $pf.$vcproj\n"; + if ($vcproj eq 'vcproj' && $proj =~ qr{ConfigurationType="([^"]+)"}) { - croak "Could not parse $pf.vcproj\n"; + if ($1 == 1) + { + $dir = "bin"; + $ext = "exe"; + } + elsif ($1 == 2) + { + $dir = "lib"; + $ext = "dll"; + } + else + { + + # Static lib, such as libpgport, only used internally during build, don't install + next; + } } - if ($1 == 1) + elsif ($vcproj eq 'vcxproj' && $proj =~ qr{(\w+)}) { - $dir = "bin"; - $ext = "exe"; - } - elsif ($1 == 2) - { - $dir = "lib"; - $ext = "dll"; + if ($1 eq 'Application') + { + $dir = "bin"; + $ext = "exe"; + } + elsif ($1 eq 'DynamicLibrary') + { + $dir = "lib"; + $ext = "dll"; + } + else # 'StaticLibrary' + { + + # Static lib, such as libpgport, only used internally during build, don't install + next; + } } else { - - # Static lib, such as libpgport, only used internally during build, don't install - next; + croak "Could not parse $pf.$vcproj\n"; } lcopy("$conf\\$pf\\$pf.$ext","$target\\$dir\\$pf.$ext") || croak "Could not copy $pf.$ext\n"; @@ -470,8 +496,7 @@ sub CopyIncludeFiles $target . '/include/server/', 'src/include/', 'pg_config.h', 'pg_config_os.h' ); - CopyFiles('Grammar header', $target . '/include/server/parser/','src/backend/parser/', - 'gram.h'); + CopyFiles('Grammar header', $target . '/include/server/parser/','src/backend/parser/','gram.h'); CopySetOfFiles('',[ glob("src\\include\\*.h") ],$target . '/include/server/'); my $D; opendir($D, 'src/include') || croak "Could not opendir on src/include!\n"; diff --git a/src/tools/msvc/MSBuildProject.pm b/src/tools/msvc/MSBuildProject.pm new file mode 100644 index 00000000000..fcce9ebb68d --- /dev/null +++ b/src/tools/msvc/MSBuildProject.pm @@ -0,0 +1,388 @@ +package MSBuildProject; + +# +# Package that encapsulates a MSBuild (Visual C++ 2010) project file +# +# src/tools/msvc/MSBuildProject.pm +# + +use Carp; +use strict; +use warnings; +use base qw(Project); + +sub _new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{filenameExtension} = '.vcxproj'; + + return $self; +} + +sub WriteHeader +{ + my ($self, $f) = @_; + + print $f < + + +EOF + $self->WriteConfigurationHeader($f, 'Debug'); + $self->WriteConfigurationHeader($f, 'Release'); + print $f < + + $self->{guid} + + +EOF + $self->WriteConfigurationPropertyGroup($f, 'Release',{ wholeopt=>'false' }); + $self->WriteConfigurationPropertyGroup($f, 'Debug',{ wholeopt=>'false' }); + print $f < + + +EOF + $self->WritePropertySheetsPropertyGroup($f, 'Release'); + $self->WritePropertySheetsPropertyGroup($f, 'Debug'); + print $f < + + <_ProjectFileVersion>10.0.30319.1 +EOF + $self->WriteAdditionalProperties($f, 'Debug'); + $self->WriteAdditionalProperties($f, 'Release'); + print $f < +EOF + $self->WriteItemDefinitionGroup( + $f, 'Debug', + { + defs=>'_DEBUG;DEBUG=1;', + opt=>'Disabled', + strpool=>'false', + runtime=>'MultiThreadedDebugDLL' + } + ); + $self->WriteItemDefinitionGroup($f, 'Release', + { defs=>'', opt=>'Full', strpool=>'true', runtime=>'MultiThreadedDLL' }); +} + +sub AddDefine +{ + my ($self, $def) = @_; + + $self->{defines} .= $def . ';'; +} + +sub WriteReferences +{ + my ($self, $f) = @_; + + my @references = @{$self->{references}}; + + if (scalar(@references)) + { + print $f < +EOF + foreach my $ref (@references) + { + print $f < + $ref->{guid} + +EOF + } + print $f < +EOF + } +} + +sub WriteFiles +{ + my ($self, $f) = @_; + print $f < +EOF + my @grammarFiles = (); + my @resourceFiles = (); + my %uniquefiles; + foreach my $fileNameWithPath (sort keys %{ $self->{files} }) + { + confess "Bad format filename '$fileNameWithPath'\n" + unless ($fileNameWithPath =~ /^(.*)\\([^\\]+)\.[r]?[cyl]$/); + my $dir = $1; + my $fileName = $2; + if ($fileNameWithPath =~ /\.y$/ or $fileNameWithPath =~ /\.l$/) + { + push @grammarFiles, $fileNameWithPath; + } + elsif ($fileNameWithPath =~ /\.rc$/) + { + push @resourceFiles, $fileNameWithPath; + } + elsif (defined($uniquefiles{$fileName})) + { + + # File already exists, so fake a new name + my $obj = $dir; + $obj =~ s/\\/_/g; + + print $f < + .\\debug\\$self->{name}\\${obj}_$fileName.obj + .\\release\\$self->{name}\\${obj}_$fileName.obj + +EOF + } + else + { + $uniquefiles{$fileName} = 1; + print $f < +EOF + } + + } + print $f < +EOF + if (scalar(@grammarFiles)) + { + print $f < +EOF + foreach my $grammarFile (@grammarFiles) + { + (my $outputFile = $grammarFile) =~ s/\.(y|l)$/.c/; + if ($grammarFile =~ /\.y$/) + { + $outputFile =~ s{^src\\pl\\plpgsql\\src\\gram.c$}{src\\pl\\plpgsql\\src\\pl_gram.c}; + print $f < + Running bison on $grammarFile + perl "src\\tools\\msvc\\pgbison.pl" "$grammarFile" + %(AdditionalInputs) + $outputFile;%(Outputs) + Running bison on $grammarFile + perl "src\\tools\\msvc\\pgbison.pl" "$grammarFile" + %(AdditionalInputs) + $outputFile;%(Outputs) + +EOF + } + else #if ($grammarFile =~ /\.l$/) + { + print $f < + Running flex on $grammarFile + perl "src\\tools\\msvc\\pgflex.pl" "$grammarFile" + %(AdditionalInputs) + $outputFile;%(Outputs) + Running flex on $grammarFile + perl "src\\tools\\msvc\\pgflex.pl" "$grammarFile" + %(AdditionalInputs) + $outputFile;%(Outputs) + +EOF + } + } + print $f < +EOF + } + if (scalar(@resourceFiles)) + { + print $f < +EOF + foreach my $rcFile (@resourceFiles) + { + print $f < +EOF + } + print $f < +EOF + } +} + +sub WriteConfigurationHeader +{ + my ($self, $f, $cfgname) = @_; + print $f < + $cfgname + $self->{platform} + +EOF +} + +sub WriteConfigurationPropertyGroup +{ + my ($self, $f, $cfgname, $p) = @_; + my $cfgtype = + ($self->{type} eq "exe") + ?'Application' + :($self->{type} eq "dll"?'DynamicLibrary':'StaticLibrary'); + + print $f < + $cfgtype + false + MultiByte + $p->{wholeopt} + +EOF +} + +sub WritePropertySheetsPropertyGroup +{ + my ($self, $f, $cfgname) = @_; + print $f < + + +EOF +} + +sub WriteAdditionalProperties +{ + my ($self, $f, $cfgname) = @_; + print $f <.\\$cfgname\\$self->{name}\\ + .\\$cfgname\\$self->{name}\\ + false +EOF +} + +sub WriteItemDefinitionGroup +{ + my ($self, $f, $cfgname, $p) = @_; + my $cfgtype = + ($self->{type} eq "exe") + ?'Application' + :($self->{type} eq "dll"?'DynamicLibrary':'StaticLibrary'); + my $libs = $self->GetAdditionalLinkerDependencies($cfgname, ';'); + + my $targetmachine = $self->{platform} eq 'Win32' ? 'MachineX86' : 'MachineX64'; + + my $includes = $self->{includes}; + unless ($includes eq '' or $includes =~ /;$/) + { + $includes .= ';'; + } + print $f < + + $p->{opt} + $self->{prefixincludes}src/include;src/include/port/win32;src/include/port/win32_msvc;$includes\%(AdditionalIncludeDirectories) + WIN32;_WINDOWS;__WINDOWS__;__WIN32__;EXEC_BACKEND;WIN32_STACK_RLIMIT=4194304;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE$self->{defines}$p->{defs}\%(PreprocessorDefinitions) + $p->{strpool} + $p->{runtime} + $self->{disablewarnings};\%(DisableSpecificWarnings) + /MP \%(AdditionalOptions) + + + .\\$cfgname\\$self->{name}\\ + .\\$cfgname\\$self->{name}\\ + .\\$cfgname\\$self->{name}\\ + false + Level3 + true + ProgramDatabase + Default + + + .\\$cfgname\\$self->{name}\\$self->{name}.$self->{type} + $libs;\%(AdditionalDependencies) + true + \%(AdditionalLibraryDirectories) + libc;\%(IgnoreSpecificDefaultLibraries) + 4194304 + true + .\\$cfgname\\$self->{name}\\$self->{name}.pdb + false + .\\$cfgname\\$self->{name}\\$self->{name}.map + Console + $targetmachine +EOF + if ($self->{disablelinkerwarnings}) + { + print $f +" /ignore:$self->{disablelinkerwarnings} \%(AdditionalOptions)\n"; + } + if ($self->{implib}) + { + my $l = $self->{implib}; + $l =~ s/__CFGNAME__/$cfgname/g; + print $f " $l\n"; + } + if ($self->{def}) + { + my $d = $self->{def}; + $d =~ s/__CFGNAME__/$cfgname/g; + print $f " $d\n"; + } + print $f < + + src\\include;\%(AdditionalIncludeDirectories) + +EOF + if ($self->{builddef}) + { + print $f < + Generate DEF file + perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name} $self->{platform} + +EOF + } + print $f < +EOF +} + +sub Footer +{ + my ($self, $f) = @_; + $self->WriteReferences($f); + + print $f < + + + +EOF +} + +package VC2010Project; + +# +# Package that encapsulates a Visual C++ 2010 project file +# + +use strict; +use warnings; +use base qw(MSBuildProject); + +sub new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{vcver} = '10.00'; + + return $self; +} + +1; diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index fb83224834b..7881c66cdf8 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -14,6 +14,7 @@ use Solution; use Cwd; use File::Copy; use Config; +use VSObjectFactory; use List::Util qw(first); use Exporter; @@ -47,7 +48,9 @@ sub mkvcbuild chdir('..\..\..') if (-d '..\msvc' && -d '..\..\..\src'); die 'Must run from root or msvc directory' unless (-d 'src\tools\msvc' && -d 'src'); - $solution = new Solution($config); + my $vsVersion = DetermineVisualStudioVersion(); + + $solution = CreateSolution($vsVersion, $config); our @pgportfiles = qw( chklocale.c crypt.c fseeko.c getrusage.c inet_aton.c random.c srandom.c @@ -344,12 +347,13 @@ sub mkvcbuild $pgdump->AddFile('src\backend\parser\kwlookup.c'); my $pgdumpall = AddSimpleFrontend('pg_dump', 1); - # pg_dumpall doesn't use the files in the Makefile's $(OBJS), unlike - # pg_dump and pg_restore. - # So remove their sources from the object, keeping the other setup that - # AddSimpleFrontend() has done. - my @nodumpall = grep { m/src\\bin\\pg_dump\\.*\.c$/ } - keys %{$pgdumpall->{files}}; + + # pg_dumpall doesn't use the files in the Makefile's $(OBJS), unlike + # pg_dump and pg_restore. + # So remove their sources from the object, keeping the other setup that + # AddSimpleFrontend() has done. + my @nodumpall = grep { m/src\\bin\\pg_dump\\.*\.c$/ } + keys %{$pgdumpall->{files}}; delete @{$pgdumpall->{files}}{@nodumpall}; $pgdumpall->{name} = 'pg_dumpall'; $pgdumpall->AddIncludeDir('src\backend'); @@ -508,6 +512,7 @@ sub mkvcbuild $pgregress->AddReference($libpgport); $solution->Save(); + return $solution->{vcver}; } ##################### diff --git a/src/tools/msvc/Project.pm b/src/tools/msvc/Project.pm index 66752f9033d..9db664a190e 100644 --- a/src/tools/msvc/Project.pm +++ b/src/tools/msvc/Project.pm @@ -10,9 +10,9 @@ use strict; use warnings; use File::Basename; -sub new +sub _new { - my ($junk, $name, $type, $solution) = @_; + my ($classname, $name, $type, $solution) = @_; my $good_types = { lib => 1, exe => 1, @@ -20,24 +20,23 @@ sub new }; confess("Bad project type: $type\n") unless exists $good_types->{$type}; my $self = { - name => $name, - type => $type, - guid => Win32::GuidGen(), - files => {}, - references => [], - libraries => [], - suffixlib => [], - includes => '', - prefixincludes => '', - defines => ';', - solution => $solution, - disablewarnings => '4018;4244;4273;4102;4090;4267', + name => $name, + type => $type, + guid => Win32::GuidGen(), + files => {}, + references => [], + libraries => [], + suffixlib => [], + includes => '', + prefixincludes => '', + defines => ';', + solution => $solution, + disablewarnings => '4018;4244;4273;4102;4090;4267', disablelinkerwarnings => '', - vcver => $solution->{vcver}, - platform => $solution->{platform}, + platform => $solution->{platform}, }; - bless $self; + bless($self, $classname); return $self; } @@ -355,135 +354,17 @@ sub Save $self->DisableLinkerWarnings('4197') if ($self->{platform} eq 'x64'); # Dump the project - open(F, ">$self->{name}.vcproj") || croak("Could not write to $self->{name}.vcproj\n"); + open(F, ">$self->{name}$self->{filenameExtension}") + || croak("Could not write to $self->{name}$self->{filenameExtension}\n"); $self->WriteHeader(*F); - $self->WriteReferences(*F); - print F < -EOF - my @dirstack = (); - my %uniquefiles; - foreach my $f (sort keys %{ $self->{files} }) - { - confess "Bad format filename '$f'\n" unless ($f =~ /^(.*)\\([^\\]+)\.[r]?[cyl]$/); - my $dir = $1; - my $file = $2; - - # Walk backwards down the directory stack and close any dirs we're done with - while ($#dirstack >= 0) - { - if (join('\\',@dirstack) eq substr($dir, 0, length(join('\\',@dirstack)))) - { - last if (length($dir) == length(join('\\',@dirstack))); - last if (substr($dir, length(join('\\',@dirstack)),1) eq '\\'); - } - print F ' ' x $#dirstack . " \n"; - pop @dirstack; - } - - # Now walk forwards and create whatever directories are needed - while (join('\\',@dirstack) ne $dir) - { - my $left = substr($dir, length(join('\\',@dirstack))); - $left =~ s/^\\//; - my @pieces = split /\\/, $left; - push @dirstack, $pieces[0]; - print F ' ' x $#dirstack . " \n"; - } - - print F ' ' x $#dirstack . " ' - . $self->GenerateCustomTool('Running bison on ' . $f, - 'cmd /V:ON /c src\tools\msvc\pgbison.bat ' . $f, $of) - . '' . "\n"; - } - elsif ($f =~ /\.l$/) - { - my $of = $f; - $of =~ s/\.l$/.c/; - print F '>' - . $self->GenerateCustomTool('Running flex on ' . $f, - 'src\tools\msvc\pgflex.bat ' . $f,$of) - . '' . "\n"; - } - elsif (defined($uniquefiles{$file})) - { - - # File already exists, so fake a new name - my $obj = $dir; - $obj =~ s/\\/_/g; - print F -">{platform}\">{name}\\$obj" - . "_$file.obj\" />{platform}\">{name}\\$obj" - . "_$file.obj\" />\n"; - } - else - { - $uniquefiles{$file} = 1; - print F " />\n"; - } - } - while ($#dirstack >= 0) - { - print F ' ' x $#dirstack . " \n"; - pop @dirstack; - } + $self->WriteFiles(*F); $self->Footer(*F); close(F); } -sub GenerateCustomTool +sub GetAdditionalLinkerDependencies { - my ($self, $desc, $tool, $output, $cfg) = @_; - if (!defined($cfg)) - { - return $self->GenerateCustomTool($desc, $tool, $output, 'Debug') - .$self->GenerateCustomTool($desc, $tool, $output, 'Release'); - } - return -"{platform}\">"; -} - -sub WriteReferences -{ - my ($self, $f) = @_; - print $f " \n"; - foreach my $ref (@{$self->{references}}) - { - print $f -" {guid}\" Name=\"$ref->{name}\" />\n"; - } - print $f " \n"; -} - -sub WriteHeader -{ - my ($self, $f) = @_; - - print $f < - - - -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 -} - -sub WriteConfiguration -{ - my ($self, $f, $cfgname, $p) = @_; - my $cfgtype = ($self->{type} eq "exe")?1:($self->{type} eq "dll"?2:4); + my ($self, $cfgname, $seperator) = @_; my $libcfg = (uc $cfgname eq "RELEASE")?"MD":"MDd"; my $libs = ''; foreach my $lib (@{$self->{libraries}}) @@ -497,76 +378,11 @@ sub WriteConfiguration last; } } - $libs .= $xlib . " "; + $libs .= $xlib . $seperator; } - $libs =~ s/ $//; + $libs =~ s/.$//; $libs =~ s/__CFGNAME__/$cfgname/g; - - my $targetmachine = $self->{platform} eq 'Win32' ? 1 : 17; - - print $f < - - {disablelinkerwarnings}) - { - print $f "\t\tAdditionalOptions=\"/ignore:$self->{disablelinkerwarnings}\"\n"; - } - if ($self->{implib}) - { - my $l = $self->{implib}; - $l =~ s/__CFGNAME__/$cfgname/g; - print $f "\t\tImportLibrary=\"$l\"\n"; - } - if ($self->{def}) - { - my $d = $self->{def}; - $d =~ s/__CFGNAME__/$cfgname/g; - print $f "\t\tModuleDefinitionFile=\"$d\"\n"; - } - - print $f "\t/>\n"; - print $f -"\t{name}\\$self->{name}.lib\" IgnoreDefaultLibraryNames=\"libc\" />\n"; - print $f - "\t\n"; - if ($self->{builddef}) - { - print $f -"\t{name} $self->{platform}\" />\n"; - } - print $f < -EOF -} - -sub Footer -{ - my ($self, $f) = @_; - - print $f < - - -EOF + return $libs; } # Utility function that loads a complete file diff --git a/src/tools/msvc/README b/src/tools/msvc/README index b8dd488b6f6..ad0eca3672b 100644 --- a/src/tools/msvc/README +++ b/src/tools/msvc/README @@ -4,19 +4,19 @@ MSVC build ========== This directory contains the tools required to build PostgreSQL using -Microsoft Visual Studio 2005. This builds the whole backend, not just +Microsoft Visual Studio 2005 - 2011. This builds the whole backend, not just the libpq frontend library. For more information, see the documentation -chapter "Installation on Windows". +chapter "Installation on Windows" and the description below. Notes about Visual Studio Express --------------------------------- -To build PostgreSQL using Visual Studio Express, the Platform SDK +To build PostgreSQL using Visual Studio Express, the Microsoft Windows SDK has to be installed. Since this is not included in the product originally, extra steps are needed to make it work. -First, download and install the latest Platform SDK from -www.microsoft.com. +First, download and install a supported version of the Microsoft Windows SDK +from www.microsoft.com (v6.0 or greater). Locate the files vcprojectengine.dll.express.config and vcprojectengine.dll.config in the vc\vcpackages directory of @@ -26,3 +26,77 @@ to add them to the beginning of the list. This should work for both GUI and commandline builds, but a restart may be necessary. + +If you are using a recent version of the Microsoft Windows SDK that includes +the compilers and build tools you probably don't even need Visual Studio +Express to build PostgreSQL. + + +Structure of the build tools +---------------------------- +The tools for building PostgreSQL using Microsoft Visual Studio currently +consist of the following files: + +- Configuration files - +config_default.pl default configuration arguments + +A typical build environment has two more files, buildenv.pl and config.pl +that contain the user's build environment settings and configuration +arguments. + + +- User tools - +build.pl tool to build the binaries +builddoc.pl tool to build the docs +clean.bat batch file for cleaning up generated files +install.pl tool to install the generated files +mkvcbuild.pl tool to generate the Visual Studio build files +vcregress.pl tool to run the regression tests + + +- Internal tools - +gendef.pl internal tool to generate .DEF files +pgbison.pl internal tool to process .y files using bison +pgflex.pl internal tool to process .l files using flex + +Many of those .pl files also have a corresponding .bat-wrapper that doesn't +contain any additional logic. + + +- Internal modules - +Install.pm module containing the install logic +Mkvcbuild.pm module containing the code to generate the Visual + Studio build (project/solution) files +MSBuildProject.pm module containing the code to generate MSBuild based + project files (Visual Studio 2010 or greater) +Project.pm module containing the common code to generate the + Visual Studio project files. Also provides the + common interface of all project file generators +Solution.pm module containing the code to generate the Visual + Studio solution files. +VCBuildProject.pm module containing the code to generate VCBuild based + project files (Visual Studio 2005/2008) +VSObjectFactory.pm factory module providing the code to create the + appropriate project/solution files for the current + environment + + +Description of the internals of the Visual Studio build process +--------------------------------------------------------------- +By typing 'build' the user starts the build.bat wrapper which simply passes +it's arguments to build.pl. +In build.pl the user's buildenv.pl is used to set up the build environment +(i. e. path to bison and flex). In addtion his config.pl file is merged into +config_default.pl to create the configuration arguments. +These configuration arguments are passed over to Mkvcbuild::mkvcbuild +(Mkvcbuild.pm) which creates the Visual Studio project and solution files. +It does this by using VSObjectFactory::CreateSolution to create an object +implementing the Solution interface (this could be either a VS2005Solution, +a VS2008Solution or a VS2010Solution, all in Solution.pm, depending on the +user's build environment) and adding objects implementing the corresponding +Project interface (VC2005Project or VC2008Project from VCBuildProject.pm or +VC2010Project from MSBuildProject.pm) to it. +When Solution::Save is called, the implementations of Solution and Project +save their content in the appropriate format. +The final step of starting the appropriate build program (msbuild or vcbuild) +is performed in build.pl again. diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm index 36fd4b71601..1725fbb5330 100644 --- a/src/tools/msvc/Solution.pm +++ b/src/tools/msvc/Solution.pm @@ -8,10 +8,11 @@ package Solution; use Carp; use strict; use warnings; +use VSObjectFactory; -sub new +sub _new { - my $junk = shift; + my $classname = shift; my $options = shift; my $self = { projects => {}, @@ -21,7 +22,7 @@ sub new vcver => undef, platform => undef, }; - bless $self; + bless($self, $classname); # integer_datetimes is now the default $options->{integer_datetimes} = 1 @@ -53,28 +54,15 @@ sub new die "Bad wal_segsize $options->{wal_segsize}" unless grep {$_ == $options->{wal_segsize}} (1,2,4,8,16,32,64); - $self->DetermineToolVersions(); + $self->DeterminePlatform(); return $self; } -sub DetermineToolVersions +sub DeterminePlatform { my $self = shift; - # Determine version of vcbuild command, to set proper verison of visual studio - open(P,"vcbuild /? |") || die "vcbuild command not found"; - my $line =

; - close(P); - if ($line !~ /^Microsoft\s*\(R\) Visual C\+\+ [^-]+ - \D+(\d+)\.00\.\d+/) - { - die "Unable to determine vcbuild version from first line of output!"; - } - if ($1 == 8) { $self->{vcver} = '8.00' } - elsif ($1 == 9) { $self->{vcver} = '9.00' } - else { die "Unsupported version of Visual Studio: $1" } - print "Detected Visual Studio version $self->{vcver}\n"; - # Determine if we are in 32 or 64-bit mode. Do this by seeing if CL has # 64-bit only parameters. $self->{platform} = 'Win32'; @@ -428,7 +416,7 @@ sub AddProject { my ($self, $name, $type, $folder, $initialdir) = @_; - my $proj = new Project($name, $type, $self); + my $proj = VSObjectFactory::CreateProject($self->{vcver}, $name, $type, $self); push @{$self->{projects}->{$folder}}, $proj; $proj->AddDir($initialdir) if ($initialdir); if ($self->{options}->{zlib}) @@ -488,8 +476,8 @@ sub Save open(SLN,">pgsql.sln") || croak "Could not write to pgsql.sln\n"; print SLN <{solutionFileVersion} +# $self->{visualStudioName} EOF foreach my $fld (keys %{$self->{projects}}) @@ -497,7 +485,7 @@ EOF foreach my $proj (@{$self->{projects}->{$fld}}) { print SLN <{name}.vcproj", "$proj->{guid}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "$proj->{name}", "$proj->{name}$proj->{filenameExtension}", "$proj->{guid}" EndProject EOF } @@ -579,4 +567,74 @@ sub GetFakeConfigure return $cfg; } +package VS2005Solution; + +# +# Package that encapsulates a Visual Studio 2005 solution file +# + +use strict; +use warnings; +use base qw(Solution); + +sub new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{solutionFileVersion} = '9.00'; + $self->{vcver} = '8.00'; + $self->{visualStudioName} = 'Visual Studio 2005'; + + return $self; +} + +package VS2008Solution; + +# +# Package that encapsulates a Visual Studio 2008 solution file +# + +use strict; +use warnings; +use base qw(Solution); + +sub new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{solutionFileVersion} = '10.00'; + $self->{vcver} = '9.00'; + $self->{visualStudioName} = 'Visual Studio 2008'; + + return $self; +} + +package VS2010Solution; + +# +# Package that encapsulates a Visual Studio 2010 solution file +# + +use Carp; +use strict; +use warnings; +use base qw(Solution); + +sub new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{solutionFileVersion} = '11.00'; + $self->{vcver} = '10.00'; + $self->{visualStudioName} = 'Visual Studio 2010'; + + return $self; +} + 1; diff --git a/src/tools/msvc/VCBuildProject.pm b/src/tools/msvc/VCBuildProject.pm new file mode 100644 index 00000000000..97439d9d5c4 --- /dev/null +++ b/src/tools/msvc/VCBuildProject.pm @@ -0,0 +1,267 @@ +package VCBuildProject; + +# +# Package that encapsulates a VCBuild (Visual C++ 2005/2008) project file +# +# src/tools/msvc/VCBuildProject.pm +# + +use Carp; +use strict; +use warnings; +use base qw(Project); + +sub _new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{filenameExtension} = '.vcproj'; + + return $self; +} + +sub WriteHeader +{ + my ($self, $f) = @_; + + print $f < + + + +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 + $self->WriteReferences($f); +} + +sub WriteFiles +{ + my ($self, $f) = @_; + print $f < +EOF + my @dirstack = (); + my %uniquefiles; + foreach my $fileNameWithPath (sort keys %{ $self->{files} }) + { + confess "Bad format filename '$fileNameWithPath'\n" + unless ($fileNameWithPath =~ /^(.*)\\([^\\]+)\.[r]?[cyl]$/); + my $dir = $1; + my $file = $2; + + # Walk backwards down the directory stack and close any dirs we're done with + while ($#dirstack >= 0) + { + if (join('\\',@dirstack) eq substr($dir, 0, length(join('\\',@dirstack)))) + { + last if (length($dir) == length(join('\\',@dirstack))); + last if (substr($dir, length(join('\\',@dirstack)),1) eq '\\'); + } + print $f ' ' x $#dirstack . " \n"; + pop @dirstack; + } + + # Now walk forwards and create whatever directories are needed + while (join('\\',@dirstack) ne $dir) + { + my $left = substr($dir, length(join('\\',@dirstack))); + $left =~ s/^\\//; + my @pieces = split /\\/, $left; + push @dirstack, $pieces[0]; + print $f ' ' x $#dirstack . " \n"; + } + + print $f ' ' x $#dirstack . " ' + . $self->GenerateCustomTool('Running bison on ' . $fileNameWithPath, + "perl src\\tools\\msvc\\pgbison.pl $fileNameWithPath", $of) + . '' . "\n"; + } + elsif ($fileNameWithPath =~ /\.l$/) + { + my $of = $fileNameWithPath; + $of =~ s/\.l$/.c/; + print $f '>' + . $self->GenerateCustomTool('Running flex on ' . $fileNameWithPath, + "perl src\\tools\\msvc\\pgflex.pl $fileNameWithPath", $of) + . '' . "\n"; + } + elsif (defined($uniquefiles{$file})) + { + + # File already exists, so fake a new name + my $obj = $dir; + $obj =~ s/\\/_/g; + print $f +">{platform}\">{name}\\$obj" + . "_$file.obj\" />{platform}\">{name}\\$obj" + . "_$file.obj\" />\n"; + } + else + { + $uniquefiles{$file} = 1; + print $f " />\n"; + } + } + while ($#dirstack >= 0) + { + print $f ' ' x $#dirstack . " \n"; + pop @dirstack; + } + print $f < +EOF +} + +sub Footer +{ + my ($self, $f) = @_; + + print $f < + +EOF +} + +sub WriteConfiguration +{ + my ($self, $f, $cfgname, $p) = @_; + my $cfgtype = ($self->{type} eq "exe")?1:($self->{type} eq "dll"?2:4); + my $libs = $self->GetAdditionalLinkerDependencies($cfgname, ' '); + + my $targetmachine = $self->{platform} eq 'Win32' ? 1 : 17; + + print $f < + + {disablelinkerwarnings}) + { + print $f "\t\tAdditionalOptions=\"/ignore:$self->{disablelinkerwarnings}\"\n"; + } + if ($self->{implib}) + { + my $l = $self->{implib}; + $l =~ s/__CFGNAME__/$cfgname/g; + print $f "\t\tImportLibrary=\"$l\"\n"; + } + if ($self->{def}) + { + my $d = $self->{def}; + $d =~ s/__CFGNAME__/$cfgname/g; + print $f "\t\tModuleDefinitionFile=\"$d\"\n"; + } + + print $f "\t/>\n"; + print $f +"\t{name}\\$self->{name}.lib\" IgnoreDefaultLibraryNames=\"libc\" />\n"; + print $f + "\t\n"; + if ($self->{builddef}) + { + print $f +"\t{name} $self->{platform}\" />\n"; + } + print $f < +EOF +} + +sub WriteReferences +{ + my ($self, $f) = @_; + print $f " \n"; + foreach my $ref (@{$self->{references}}) + { + print $f +" {guid}\" Name=\"$ref->{name}\" />\n"; + } + print $f " \n"; +} + +sub GenerateCustomTool +{ + my ($self, $desc, $tool, $output, $cfg) = @_; + if (!defined($cfg)) + { + return $self->GenerateCustomTool($desc, $tool, $output, 'Debug') + .$self->GenerateCustomTool($desc, $tool, $output, 'Release'); + } + return +"{platform}\">"; +} + +package VC2005Project; + +# +# Package that encapsulates a Visual C++ 2005 project file +# + +use strict; +use warnings; +use base qw(VCBuildProject); + +sub new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{vcver} = '8.00'; + + return $self; +} + +package VC2008Project; + +# +# Package that encapsulates a Visual C++ 2008 project file +# + +use strict; +use warnings; +use base qw(VCBuildProject); + +sub new +{ + my $classname = shift; + my $self = $classname->SUPER::_new(@_); + bless($self, $classname); + + $self->{vcver} = '9.00'; + + return $self; +} + +1; diff --git a/src/tools/msvc/VSObjectFactory.pm b/src/tools/msvc/VSObjectFactory.pm new file mode 100644 index 00000000000..44db1f6814c --- /dev/null +++ b/src/tools/msvc/VSObjectFactory.pm @@ -0,0 +1,122 @@ +package VSObjectFactory; + +# +# Package that creates Visual Studio wrapper objects for msvc build +# +# src/tools/msvc/VSObjectFactory.pm +# + +use Carp; +use strict; +use warnings; + +use Exporter; +use Project; +use Solution; +use VCBuildProject; +use MSBuildProject; + +our (@ISA, @EXPORT); +@ISA = qw(Exporter); +@EXPORT = qw(CreateSolution CreateProject DetermineVisualStudioVersion); + +sub CreateSolution +{ + my $visualStudioVersion = shift; + + if (!defined($visualStudioVersion)) + { + $visualStudioVersion = DetermineVisualStudioVersion(); + } + + if ($visualStudioVersion eq '8.00') + { + return new VS2005Solution(@_); + } + elsif ($visualStudioVersion eq '9.00') + { + return new VS2008Solution(@_); + } + elsif ($visualStudioVersion eq '10.00') + { + return new VS2010Solution(@_); + } + else + { + croak "The requested Visual Studio version is not supported."; + } +} + +sub CreateProject +{ + my $visualStudioVersion = shift; + + if (!defined($visualStudioVersion)) + { + $visualStudioVersion = DetermineVisualStudioVersion(); + } + + if ($visualStudioVersion eq '8.00') + { + return new VC2005Project(@_); + } + elsif ($visualStudioVersion eq '9.00') + { + return new VC2008Project(@_); + } + elsif ($visualStudioVersion eq '10.00') + { + return new VC2010Project(@_); + } + else + { + croak "The requested Visual Studio version is not supported."; + } +} + +sub DetermineVisualStudioVersion +{ + my $nmakeVersion = shift; + + if (!defined($nmakeVersion)) + { + + # Determine version of nmake command, to set proper verison of visual studio + # we use nmake as it has existed for a long time and still exists in visual studio 2010 + open(P,"nmake /? 2>&1 |") + || croak "Unable to determine Visual Studio version: The nmake command wasn't found."; + while(

) + { + chomp; + if (/(\d+)\.(\d+)\.\d+(\.\d+)?$/) + { + return _GetVisualStudioVersion($1, $2); + } + } + close(P); + } + elsif($nmakeVersion =~ /(\d+)\.(\d+)\.\d+(\.\d+)?$/) + { + return _GetVisualStudioVersion($1, $2); + } + croak "Unable to determine Visual Studio version: The nmake version could not be determined."; +} + +sub _GetVisualStudioVersion +{ + my($major, $minor) = @_; + if ($major > 10) + { + carp +"The determined version of Visual Studio is newer than the latest supported version. Returning the latest supported version instead."; + return '10.00'; + } + elsif ($major < 6) + { + croak +"Unable to determine Visual Studio version: Visual Studio versions before 6.0 aren't supported."; + } + return "$major.$minor"; +} + +1; diff --git a/src/tools/msvc/build.pl b/src/tools/msvc/build.pl index 66b5c4c59ba..151849bba51 100644 --- a/src/tools/msvc/build.pl +++ b/src/tools/msvc/build.pl @@ -33,7 +33,7 @@ our $config; require "config_default.pl"; require "config.pl" if (-f "src/tools/msvc/config.pl"); -Mkvcbuild::mkvcbuild($config); +my $vcver = Mkvcbuild::mkvcbuild($config); # check what sort of build we are doing @@ -50,7 +50,11 @@ elsif ($ARGV[0] ne "RELEASE") # ... and do it -if ($buildwhat) +if ($buildwhat and $vcver eq '10.00') +{ + system("msbuild $buildwhat.vcxproj /verbosity:detailed /p:Configuration=$bconf"); +} +elsif ($buildwhat) { system("vcbuild $buildwhat.vcproj $bconf"); } diff --git a/src/tools/msvc/builddoc.pl b/src/tools/msvc/builddoc.pl index 6531e98f1a6..8d8d8a35ccf 100644 --- a/src/tools/msvc/builddoc.pl +++ b/src/tools/msvc/builddoc.pl @@ -69,8 +69,8 @@ $cmd = ."| findstr /V \"DTDDECL catalog entries are not supported\" "; system($cmd); # die "openjade" if $?; print "Running collateindex...\n"; -$cmd ="perl \"$docroot/$dsssl/bin/collateindex.pl\" -f -g -i bookindex " - ."-o bookindex.sgml HTML.index"; +$cmd = + "perl \"$docroot/$dsssl/bin/collateindex.pl\" -f -g -i bookindex "."-o bookindex.sgml HTML.index"; system($cmd); die "collateindex" if $?; mkdir "html"; diff --git a/src/tools/msvc/clean.bat b/src/tools/msvc/clean.bat index a59bbe55da9..ac310382e32 100755 --- a/src/tools/msvc/clean.bat +++ b/src/tools/msvc/clean.bat @@ -10,8 +10,12 @@ if exist ..\msvc if exist ..\..\..\src cd ..\..\.. if exist debug rd /s /q debug if exist release rd /s /q release for %%f in (*.vcproj) do del %%f +for %%f in (*.vcxproj) do del %%f +for %%f in (*.vcxproj.user) do del %%f if exist pgsql.sln del /q pgsql.sln if exist pgsql.sln.cache del /q pgsql.sln.cache +if exist pgsql.sdf del /q pgsql.sdf +if exist pgsql.suo del /q /a:H pgsql.suo 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