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\
diff --git a/tools/win32/configure-dev.ps1 b/tools/win32/configure-dev.ps1
index ff18fe157a4..3f4a955bdb2 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,10 @@ 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_TOOLSET)) {
+ $env:CMAKE_GENERATOR_TOOLSET = 'v143'
}
if (-not (Test-Path env:CMAKE_GENERATOR_PLATFORM)) {
$env:CMAKE_GENERATOR_PLATFORM = 'x64'
@@ -57,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 7dba5ead6a6..1a02982cb02 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,10 @@ 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_TOOLSET)) {
+ $env:CMAKE_GENERATOR_TOOLSET = 'v143'
}
if (-not (Test-Path env:BITS)) {
$env:BITS = 64
@@ -61,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" `
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`""