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

2021-07-14 Thread Andrew Dunstan


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



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





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

2021-07-13 Thread Craig Ringer
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  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  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).
From ad9625026bcb7768c636fdf3a37a3403db195ae2 Mon Sep 17 00:00:00 2001
From: Craig Ringer 
Date: Tue, 13 Jul 2021 14:18:44 +1000
Subject: [PATCH v1 1/2] Teach pgflex.pl and pgbision.pl to read buildenv.pl
 for tool names

Some distributions of flex and bison on Windows use alternate executable
names such as win_flex.exe and win_bison.exe. Teach our pgflex.pl and
pgbison.pl wrappers how to handle them by reading the executables to use
from the new $flex and $bison variables in src/tools/msvc/buildenv.pl .

These may be bare names of commands on the PATH or they may be a fully
qualified path to the target executable.

While we're at it, notice when the test execution of flex or bision to
check the version fails and complain with a more informative error.
---
 src/tools/msvc/build.pl|  4 
 src/tools/msvc/buildenv_default.pl | 14 ++
 src/tools/msvc/pgbison.pl  | 27 +++
 src/tools/msvc/pgflex.pl   | 25 +
 4 files changed, 62 insertions(+), 8 deletions(-)
 create mode 100644 src/tools/msvc/buildenv_default.pl

diff --git a/src/tools/msvc/build.pl b/src/tools/msvc/build.pl
index de50554e7e..dedd515307 100644
--- a/src/tools/msvc/build.pl
+++ b/src/tools/msvc/build.pl
@@ -28,6 +28,10 @@ elsif (-e "./buildenv.pl")
 {
 	do "./buildenv.pl";
 }
+elsif (-e "src/tools/msvc/buildenv_default.pl")
+{
+	do "src/tools/msvc/buildenv_default.pl";
+}
 
 # set up the project
 our $config;
diff --git a/src/tools/msvc/buildenv_default.pl b/src/tools/msvc/buildenv_default.pl
new file mode 100644
index 00..b3868f2145
--- /dev/null
+++ b/src/tools/msvc/buildenv_default.pl
@@ -0,0 +1,14 @@
+# Copy this file to src\test\msvc\buildenv.pl and modify it as required
+# for your build toolchain, path, etc.
+#
+# Note that there's no way to run vcvarsall.bat from here to set up the
+# visual studio environment. You still have to do that before starting
+# the build.
+#
+# Examples:
+