From c484af60fe44ad3cbf14ca7c4250dab7f5199d26 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Sun, 30 Jan 2022 16:10:30 +0800 Subject: [PATCH] Find the correct MSVC toolset when building from command line with a different VS version (cherry picked from commit 5de95228de4aaf3a3d92a116d7170865a2037e6b) --- CMakeModules/FindMSVC.cmake | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/CMakeModules/FindMSVC.cmake b/CMakeModules/FindMSVC.cmake index 2a08a08bb..3e777a1f2 100644 --- a/CMakeModules/FindMSVC.cmake +++ b/CMakeModules/FindMSVC.cmake @@ -45,24 +45,35 @@ ELSE() IF(MSVC_VERSION LESS 1920) SET(_NAME "2017") - ELSE() + ELSEIF(MSVC_VERSION LESS 1930) SET(_NAME "2019") + ELSE() + SET(_NAME "2022") ENDIF() STRING(REGEX REPLACE "/MSVC/.+" "/MSVC" VC_DIR ${_COMPILER}) - FILE(GLOB MSVC_TOOLCHAIN_VERSIONS RELATIVE ${VC_DIR} "${VC_DIR}/*") + IF(DEFINED ENV{VCToolsVersion}) + + SET(MSVC_TOOLCHAIN_VERSION $ENV{VCToolsVersion}) + + ELSE() - IF(MSVC_TOOLCHAIN_VERSIONS) - LIST(SORT MSVC_TOOLCHAIN_VERSIONS) - LIST(REVERSE MSVC_TOOLCHAIN_VERSIONS) - ENDIF() + FILE(GLOB MSVC_TOOLCHAIN_VERSIONS RELATIVE ${VC_DIR} "${VC_DIR}/*") - IF(MSVC_TOOLCHAIN_VERSIONS) - LIST(GET MSVC_TOOLCHAIN_VERSIONS 0 MSVC_TOOLCHAIN_VERSION) + IF(MSVC_TOOLCHAIN_VERSIONS) + LIST(SORT MSVC_TOOLCHAIN_VERSIONS) + LIST(REVERSE MSVC_TOOLCHAIN_VERSIONS) + ENDIF() - SET(VC_DIR "${VC_DIR}/${MSVC_TOOLCHAIN_VERSION}") + IF(MSVC_TOOLCHAIN_VERSIONS) + LIST(GET MSVC_TOOLCHAIN_VERSIONS 0 MSVC_TOOLCHAIN_VERSION) + ENDIF() + + ENDIF() + IF (MSVC_TOOLCHAIN_VERSION) + SET(VC_DIR "${VC_DIR}/${MSVC_TOOLCHAIN_VERSION}") MESSAGE(STATUS "Found Visual C++ ${_NAME} (${_VERSION} with toolchain ${MSVC_TOOLCHAIN_VERSION}) in ${VC_DIR}") ELSE() MESSAGE(FATAL_ERROR "Unable to find Visual C++ in ${VC_DIR}")