From 211f1ea9f4c433e540a1189c33939d3e681b6d4d Mon Sep 17 00:00:00 2001 From: Johannes Schmidt Date: Mon, 15 Jun 2026 12:14:56 +0200 Subject: [PATCH 1/3] Fix Visual Studio path detection in Windows build-scripts This makes use of VS's `vcwhere` command to detect the VS installation. Since the previously hardcoded VS version was also used to select the CMake generator, this has also been switched to use the version properties from `vcwhere`. --- tools/win32/configure-dev.ps1 | 6 +++-- tools/win32/configure.ps1 | 4 ++- tools/win32/find-vs.ps1 | 50 +++++++++++++++++++++++++++++++++++ tools/win32/load-vsenv.ps1 | 28 ++++---------------- 4 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 tools/win32/find-vs.ps1 diff --git a/tools/win32/configure-dev.ps1 b/tools/win32/configure-dev.ps1 index ff18fe157a4..c9ed8eeff2a 100644 --- a/tools/win32/configure-dev.ps1 +++ b/tools/win32/configure-dev.ps1 @@ -1,6 +1,8 @@ Set-PsDebug -Trace 1 -# Specify default targets for VS 2022 for developers. +. "$PSScriptRoot\find-vs.ps1" + +# Specify default targets for developers. if (-not (Test-Path env:ICINGA2_BUILDPATH)) { $env:ICINGA2_BUILDPATH = '.\debug' @@ -22,7 +24,7 @@ if (-not ($env:PATH -contains $env:CMAKE_PATH)) { $env:PATH = $env:CMAKE_PATH + ';' + $env:PATH } if (-not (Test-Path env:CMAKE_GENERATOR)) { - $env:CMAKE_GENERATOR = 'Visual Studio 17 2022' + $env:CMAKE_GENERATOR = Get-VSCMakeGenerator } if (-not (Test-Path env:CMAKE_GENERATOR_PLATFORM)) { $env:CMAKE_GENERATOR_PLATFORM = 'x64' diff --git a/tools/win32/configure.ps1 b/tools/win32/configure.ps1 index 7dba5ead6a6..59d78c38187 100644 --- a/tools/win32/configure.ps1 +++ b/tools/win32/configure.ps1 @@ -1,5 +1,7 @@ Set-PsDebug -Trace 1 +. "$PSScriptRoot\find-vs.ps1" + if (-not (Test-Path env:ICINGA2_BUILDPATH)) { $env:ICINGA2_BUILDPATH = '.\build' } @@ -17,7 +19,7 @@ if (-not ($env:PATH -contains $env:CMAKE_PATH)) { $env:PATH = $env:CMAKE_PATH + ';' + $env:PATH } if (-not (Test-Path env:CMAKE_GENERATOR)) { - $env:CMAKE_GENERATOR = 'Visual Studio 17 2022' + $env:CMAKE_GENERATOR = Get-VSCMakeGenerator } if (-not (Test-Path env:BITS)) { $env:BITS = 64 diff --git a/tools/win32/find-vs.ps1 b/tools/win32/find-vs.ps1 new file mode 100644 index 00000000000..0d7b16b2aa2 --- /dev/null +++ b/tools/win32/find-vs.ps1 @@ -0,0 +1,50 @@ +# Query a single `vswhere` property for the selected VS install. The auto-detection can be +# overriden by setting the VS_INSTALL_PATH environment variable. +# By default, the latest install that has the C++ (x86/x64) toolset will be selected. +# If `vswhere` itself cannot be found or if it did not successfully return the requested +# property an error will be thrown. +function Get-VSProperty($property) { + $vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" + if (-not (Test-Path $vswhere)) { + throw "Could not find vswhere.exe at: ${vswhere}" + } + + if (Test-Path env:VS_INSTALL_PATH) { + $selector = @('-path', $env:VS_INSTALL_PATH) + } else { + $selector = @('-latest', '-products', '*', + '-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64') + } + + $value = & $vswhere @selector -property $property + if ($LastExitCode -ne 0 -or [string]::IsNullOrEmpty($value)) { + throw "vswhere could not determine '${property}' for the selected Visual Studio installation" + } + + return $value +} + +# Root directory of the VS installation for locating the vcvars*.bat file. +function Get-VSInstallPath { + return Get-VSProperty 'installationPath' +} + +# CMake can auto-detect the latest installed VS version, but that doesn't have to align with the +# version we have selected above, for example when using the VS_INSTALL_PATH override. +# Therefore it is generated from the same `vswhere` result that is used to detect the VS path. +# An error is thrown if the VS version we have selected is not supported by CMake. +function Get-VSCMakeGenerator { + $major = (Get-VSProperty 'installationVersion').Split('.')[0] + + # Get the supported VS versions from CMake and check match it against the one we have. + $generator = & cmake.exe --help | + Select-String -Pattern "(Visual Studio $major\b[^=]*)=" | + ForEach-Object { $_.Matches.Groups[1].Value.Trim() } | + Select-Object -First 1 + + if (-not $generator) { + throw "Installed CMake has no 'Visual Studio $major' generator (CMake too old for this VS?); set CMAKE_GENERATOR to override" + } + + return $generator +} diff --git a/tools/win32/load-vsenv.ps1 b/tools/win32/load-vsenv.ps1 index a876c614869..320a3cb4f07 100644 --- a/tools/win32/load-vsenv.ps1 +++ b/tools/win32/load-vsenv.ps1 @@ -3,6 +3,8 @@ Set-PsDebug -Trace 1 +. "$PSScriptRoot\find-vs.ps1" + $SOURCE = Get-Location if (Test-Path env:ICINGA2_BUILDPATH) { @@ -15,35 +17,15 @@ if (-not (Test-Path $BUILD)) { mkdir $BUILD | Out-Null } -if (Test-Path env:VS_INSTALL_PATH) { - $VSBASE = $env:VS_INSTALL_PATH -} else { - $VSBASE = "C:\Program Files\Microsoft Visual Studio\2022" -} - if (Test-Path env:BITS) { $bits = $env:BITS } else { $bits = 64 } -# Execute vcvars in cmd and store env -$vcvars_locations = @( - "${VSBASE}\BuildTools\VC\Auxiliary\Build\vcvars${bits}.bat" - "${VSBASE}\Community\VC\Auxiliary\Build\vcvars${bits}.bat" - "${VSBASE}\Enterprise\VC\Auxiliary\Build\vcvars${bits}.bat" -) - -$vcvars = $null -foreach ($file in $vcvars_locations) { - if (Test-Path $file) { - $vcvars = $file - break - } -} - -if ($vcvars -eq $null) { - throw "Could not get Build environment script at locations: ${vcvars_locations}" +$vcvars = "$(Get-VSInstallPath)\VC\Auxiliary\Build\vcvars${bits}.bat" +if (-not (Test-Path $vcvars)) { + throw "Could not get Build environment script at location: ${vcvars}" } cmd.exe /c "call `"${vcvars}`" && set > `"${BUILD}\vcvars.txt`"" From 1edd9bcea5bb0093ab46a5f521f6a2e3c890c1ec Mon Sep 17 00:00:00 2001 From: Johannes Schmidt Date: Mon, 15 Jun 2026 14:44:27 +0200 Subject: [PATCH 2/3] Pin Visual Studio toolset to v143 --- tools/win32/configure-dev.ps1 | 6 +++++- tools/win32/configure.ps1 | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/win32/configure-dev.ps1 b/tools/win32/configure-dev.ps1 index c9ed8eeff2a..3f4a955bdb2 100644 --- a/tools/win32/configure-dev.ps1 +++ b/tools/win32/configure-dev.ps1 @@ -26,6 +26,9 @@ if (-not ($env:PATH -contains $env:CMAKE_PATH)) { if (-not (Test-Path env:CMAKE_GENERATOR)) { $env:CMAKE_GENERATOR = Get-VSCMakeGenerator } +if (-not (Test-Path env:CMAKE_GENERATOR_TOOLSET)) { + $env:CMAKE_GENERATOR_TOOLSET = 'v143' +} if (-not (Test-Path env:CMAKE_GENERATOR_PLATFORM)) { $env:CMAKE_GENERATOR_PLATFORM = 'x64' } @@ -59,7 +62,8 @@ if (Test-Path CMakeCache.txt) { & cmake.exe "$sourcePath" ` -DCMAKE_BUILD_TYPE="$env:CMAKE_BUILD_TYPE" ` - -G "$env:CMAKE_GENERATOR" -A "$env:CMAKE_GENERATOR_PLATFORM" -DCPACK_GENERATOR=WIX ` + -G "$env:CMAKE_GENERATOR" -A "$env:CMAKE_GENERATOR_PLATFORM" ` + -T "$env:CMAKE_GENERATOR_TOOLSET" -DCPACK_GENERATOR=WIX ` -DCMAKE_INSTALL_PREFIX="$env:ICINGA2_INSTALLPATH" ` -DOPENSSL_ROOT_DIR="$env:OPENSSL_ROOT_DIR" ` -DBOOST_LIBRARYDIR="$env:BOOST_LIBRARYDIR" ` diff --git a/tools/win32/configure.ps1 b/tools/win32/configure.ps1 index 59d78c38187..1a02982cb02 100644 --- a/tools/win32/configure.ps1 +++ b/tools/win32/configure.ps1 @@ -21,6 +21,9 @@ if (-not ($env:PATH -contains $env:CMAKE_PATH)) { if (-not (Test-Path env:CMAKE_GENERATOR)) { $env:CMAKE_GENERATOR = Get-VSCMakeGenerator } +if (-not (Test-Path env:CMAKE_GENERATOR_TOOLSET)) { + $env:CMAKE_GENERATOR_TOOLSET = 'v143' +} if (-not (Test-Path env:BITS)) { $env:BITS = 64 } @@ -63,7 +66,8 @@ if (Test-Path CMakeCache.txt) { & cmake.exe "$sourcePath" ` -DCMAKE_BUILD_TYPE="$env:CMAKE_BUILD_TYPE" ` - -G "$env:CMAKE_GENERATOR" -A "$env:CMAKE_GENERATOR_PLATFORM" -DCPACK_GENERATOR=WIX ` + -G "$env:CMAKE_GENERATOR" -A "$env:CMAKE_GENERATOR_PLATFORM" ` + -T "$env:CMAKE_GENERATOR_TOOLSET" -DCPACK_GENERATOR=WIX ` -DOPENSSL_ROOT_DIR="$env:OPENSSL_ROOT_DIR" ` -DBOOST_LIBRARYDIR="$env:BOOST_LIBRARYDIR" ` -DBOOST_INCLUDEDIR="$env:BOOST_ROOT" ` From 7d6bf77a2c82a907c05b02b77b4e75cf1a53f0c8 Mon Sep 17 00:00:00 2001 From: Johannes Schmidt Date: Mon, 15 Jun 2026 15:11:25 +0200 Subject: [PATCH 3/3] Increase .NET version in project file We don't use C# or .NET, but Visual Studio seems to crap itself if this non-optional setting is set to the wrong number. --- agent/windows-setup-agent/Icinga2SetupAgent.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent/windows-setup-agent/Icinga2SetupAgent.csproj b/agent/windows-setup-agent/Icinga2SetupAgent.csproj index 17fe54ff5b1..9578d48494c 100644 --- a/agent/windows-setup-agent/Icinga2SetupAgent.csproj +++ b/agent/windows-setup-agent/Icinga2SetupAgent.csproj @@ -9,7 +9,7 @@ Properties Icinga Icinga2SetupAgent - v4.6 + v4.8 512 publish\