Quick tip on building pg with Visual Studio Build Tools 2019 + small patches

Started by Craig Ringeralmost 5 years ago2 messageshackers
Jump to latest
#1Craig Ringer
craig@2ndquadrant.com

Hi all

If you're trying to build postgres with Visual Studio Build Tools 16 2019
using the optional v140 toolset that installs the Visual Studio 14 2019
C/C++ toolchain to get binaries that're fully compatible with the EDB
postgres builds, you may run into some confusing issues.

Use this incantation in cmd.exe (not a powershell.exe or pwsh.exe session)
to select the VS 16 msbuild with vs 14 compiler:

"%PROGRAMFILES(x86)%\Microsoft Visual
Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" amd64
-vcvars_ver=14.0

all on one line, then run src\tools\msvc\build.bat as normal.

If you instead attempt to use the vcvarsall.bat from the v140 toolchain
that VS Build Tools 2019 installed for you, it'll appear to work, but
compilation will then fail by spamming:

some.vcxproj(17,3): error MSB4019: The imported project
"C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in
the <Import> declaration is correct, and that the file exists on disk.

This is because the v140 toolset does not include the v140 msbuild. You're
expected to use the v160 msbuild and configure it to use the v140 toolset
instead.

Similar issues occur when you try to use the CMake generator "Visual Studio
14 2015" with a VS Build Tools 2019-installed version of the 140 toolchain;
you have to instead use -G "Visual Studio 16 2019" -T "v140" to select the
VS 16 msbuild and tell it to use the v140 toolchain. Crazy stuff.

If you instead just run:

"%PROGRAMFILES(x86)%\Microsoft Visual
Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" amd64

Then compilation will run fine, but the resulting binary will use the
version 16 MSVC compilers, runtime library and redist, etc.

Note that all these builds will target the default Windows 10 SDK. That
should be fine; we're very conservative in postgres about new Windows
features and functions, and we do dynamic lookups for a few symbols when
we're not sure if they'll be available. But you can force it to compile for
Windows 7 and higher with by editing Mk.pm and adding the definitions

WINVER=0x0601
_WIN32_WINNT=0x0601

to your project. I didn't find a way to add custom preprocessor definitions
in config.pl so for testing purposes I hacked it into MSBuildProject.pm in
the <PreprocessorDefinitions> clause as

;WINVER=0x0601;_WIN32_WINNT=0x0601

I've attached a patch that teaches config.pl about a new 'definitions'
option to make this more graceful.

See
https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt?view=msvc-160

If you don't have the toolchain installed, you can install Chocolatey
(there's a one-liner on their website) then:

choco install -y visualstudio2019buildtools

choco install -y visualstudio2019-vc++ --packageparameters "--add
Microsoft.VisualStudio.Component.VC.140"

You may also want

choco install -y winflexbison

(I've attached a patch that teaches pgflex.pl and pgbision.pl to use
win_flex.exe and win_bison.exe if they're found, and to accept full paths
for these tools in buildenv.pl).

Attachments:

v1-0001-Teach-pgflex.pl-and-pgbision.pl-to-read-buildenv..patchtext/x-patch; charset=US-ASCII; name=v1-0001-Teach-pgflex.pl-and-pgbision.pl-to-read-buildenv..patchDownload+62-9
v1-0002-Support-extra-preprocessor-definitions-in-config..patchtext/x-patch; charset=US-ASCII; name=v1-0002-Support-extra-preprocessor-definitions-in-config..patchDownload+17-2
#2Andrew Dunstan
andrew@dunslane.net
In reply to: Craig Ringer (#1)
Re: Quick tip on building pg with Visual Studio Build Tools 2019 + small patches

On 7/13/21 2:10 AM, Craig Ringer wrote:

If you don't have the toolchain installed, you can install Chocolatey
(there's a one-liner on their website) then:

    choco install -y visualstudio2019buildtools

    choco install -y visualstudio2019-vc++ --packageparameters "--add
Microsoft.VisualStudio.Component.VC.140"

The first of these is probably redundant, and the second might install
more than required. Here's my recipe for what I use in testing patches
with MSVC:

choco install -y --no-progress --limit-output
visualstudio2019-workload-vctools --install-args="--add
Microsoft.VisualStudio.Component.VC.CLI.Support"

That gives you the normal command line compilers. After that these
packages are installed:

vcredist140 14.29.30037
visualstudio-installer 2.0.1
visualstudio2019-workload-vctools 1.0.1
visualstudio2019buildtools 16.10.1.0

You may also want

    choco install -y winflexbison

(I've attached a patch that teaches pgflex.pl <http://pgflex.pl&gt; and
pgbision.pl <http://pgbision.pl&gt; to use win_flex.exe and win_bison.exe
if they're found, and to accept full paths for these tools in
buildenv.pl <http://buildenv.pl&gt;).

A simpler alternative is just to rename the chocolatey shims. Here's a
ps1 fragment I use:

    $cbin = "c:\ProgramData\chocolatey\bin"
    Rename-Item -Path $cbin\win_bison.exe -NewName bison.exe
    Rename-Item -Path $cbin\win_flex.exe -NewName flex.exe

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com