Quick tip on building pg with Visual Studio Build Tools 2019 + small patches - Mailing list pgsql-hackers

From Craig Ringer
Subject Quick tip on building pg with Visual Studio Build Tools 2019 + small patches
Date
Msg-id CAGRY4nxJNqEjr1NtdB8=dcOwwsWTqQfykyvgp1i_udCtw--BkQ@mail.gmail.com
Whole thread Raw
Responses Re: Quick tip on building pg with Visual Studio Build Tools 2019 + small patches  (Andrew Dunstan <andrew@dunslane.net>)
List pgsql-hackers
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.



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).
Attachment

pgsql-hackers by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: printf %s with NULL pointer (was Re: BUG #17098: Assert failed on composing an error message when adding a type to an extension being dropped)
Next
From: Julien Rouhaud
Date:
Subject: Re: Feature improvement: can we add queryId for pg_catalog.pg_stat_activity view?