Skip to content

Commit dc40a95

Browse files
CMake: improved windows SDK directory detection (#467)
Read the Windows Kits roots location from the registry to make the path available at configuration time and not rely on the $(WindowsSdkDir) VS-only env variable
1 parent 15dc2e9 commit dc40a95

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ endif()
101101
message("CMake generator: " ${CMAKE_GENERATOR})
102102

103103
if(WIN32)
104+
# Determine Windows SDK version
104105
if (CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
105106
set(WINDOWS_SDK_VERSION ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} CACHE INTERNAL "Windows SDK version")
106107
elseif(DEFINED ENV{WindowsSDKVersion})
@@ -113,13 +114,56 @@ if(WIN32)
113114
message(WARNING "Unable to determine Windows SDK version: neither the CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION CMake variable nor the WindowsSDKVersion environment variable is set")
114115
endif()
115116

117+
# Get Windows SDK bin directory
118+
if ("${WINDOWS_SDK_VERSION}" VERSION_GREATER_EQUAL "10.0")
119+
get_filename_component(
120+
WINDOWS_KITS_ROOT
121+
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]"
122+
ABSOLUTE
123+
)
124+
if (NOT WINDOWS_KITS_ROOT)
125+
message(WARNING "Unable to read Windows SDK root directory from registry")
126+
elseif(NOT EXISTS "${WINDOWS_KITS_ROOT}")
127+
message(WARNING "Windows SDK root directory does not exist: " ${WINDOWS_KITS_ROOT})
128+
unset(WINDOWS_KITS_ROOT)
129+
endif()
130+
131+
if(WINDOWS_KITS_ROOT)
132+
# NOTE: CMAKE_GENERATOR_PLATFORM is not set for single-configuration generators (e.g. Makefile, Ninja)
133+
if (${CMAKE_SIZEOF_VOID_P} EQUAL 8)
134+
if ("${TARGET_CPU}" STREQUAL "arm")
135+
set(ARCH_SUFFIX "arm64")
136+
else()
137+
set(ARCH_SUFFIX "x64")
138+
endif()
139+
else()
140+
if ("${TARGET_CPU}" STREQUAL "arm")
141+
set(ARCH_SUFFIX "arm")
142+
else()
143+
set(ARCH_SUFFIX "x86")
144+
endif()
145+
endif()
146+
147+
set(WINDOWS_SDK_BIN_DIR "${WINDOWS_KITS_ROOT}/bin/${WINDOWS_SDK_VERSION}/${ARCH_SUFFIX}")
148+
if (EXISTS "${WINDOWS_SDK_BIN_DIR}")
149+
set(WINDOWS_SDK_BIN_DIR "${WINDOWS_SDK_BIN_DIR}" CACHE INTERNAL "Windows SDK bin directory")
150+
else()
151+
message(WARNING "Windows SDK bin directory does not exist: " ${WINDOWS_SDK_BIN_DIR})
152+
unset(WINDOWS_SDK_BIN_DIR)
153+
endif()
154+
endif()
155+
endif()
156+
116157
if(${CMAKE_SYSTEM_NAME} STREQUAL "WindowsStore")
117158
set(PLATFORM_UNIVERSAL_WINDOWS TRUE CACHE INTERNAL "Target platform: Windows Store")
118159
message("Target platform: Universal Windows " ${ARCH} ". Windows SDK Version: " ${WINDOWS_SDK_VERSION})
119160
else()
120161
set(PLATFORM_WIN32 TRUE CACHE INTERNAL "Target platform: Win32") #WIN32 is a variable, so we cannot use string "WIN32"
121162
message("Target platform: Win32 " ${ARCH} ". Windows SDK Version: " ${WINDOWS_SDK_VERSION})
122163
endif()
164+
if(WINDOWS_SDK_BIN_DIR)
165+
message("Windows SDK bin directory: " ${WINDOWS_SDK_BIN_DIR})
166+
endif()
123167
else()
124168
if(${CMAKE_SYSTEM_NAME} STREQUAL "Android")
125169
set(PLATFORM_ANDROID TRUE CACHE INTERNAL "Target platform: Android")

Graphics/GraphicsEngineD3DBase/CMakeLists.txt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,13 @@ if(MSVC)
8181

8282
# Note that CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION is stated to be defined when targeting Windows 10
8383
# and above, however it is also defined when targeting 8.1 and Visual Studio 2019 (but not VS2017)
84-
if("${WINDOWS_SDK_VERSION}" VERSION_GREATER_EQUAL "10.0")
85-
if (DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
86-
# Note that VS_WINDOWS_SDK_BIN_DIR as well as all derived paths can only be used in Visual Studio
87-
# commands and are not valid paths during CMake configuration
88-
set(VS_WINDOWS_SDK_BIN_DIR "$(WindowsSdkDir)\\bin\\${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}\\${ARCH_SUFFIX}")
89-
90-
set(VS_D3D_COMPILER_PATH "\"${VS_WINDOWS_SDK_BIN_DIR}\\D3Dcompiler_47.dll\"" CACHE INTERNAL "D3Dcompiler_47.dll path")
91-
92-
# DXC is only present in Windows SDK starting with version 10.0.17763.0
93-
if(${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} VERSION_GREATER_EQUAL "10.0.17763.0")
94-
set(VS_DXC_COMPILER_PATH "\"${VS_WINDOWS_SDK_BIN_DIR}\\dxcompiler.dll\"" CACHE INTERNAL "dxcompiler.dll path")
95-
set(VS_DXIL_SIGNER_PATH "\"${VS_WINDOWS_SDK_BIN_DIR}\\dxil.dll\"" CACHE INTERNAL "dxil.dll path")
96-
endif()
84+
if("${WINDOWS_SDK_VERSION}" VERSION_GREATER_EQUAL "10.0" AND WINDOWS_SDK_BIN_DIR)
85+
set(VS_D3D_COMPILER_PATH "\"${WINDOWS_SDK_BIN_DIR}\\D3Dcompiler_47.dll\"" CACHE INTERNAL "D3Dcompiler_47.dll path")
86+
87+
# DXC is only present in Windows SDK starting with version 10.0.17763.0
88+
if("${WINDOWS_SDK_VERSION}" VERSION_GREATER_EQUAL "10.0.17763.0")
89+
set(VS_DXC_COMPILER_PATH "\"${WINDOWS_SDK_BIN_DIR}\\dxcompiler.dll\"" CACHE INTERNAL "dxcompiler.dll path")
90+
set(VS_DXIL_SIGNER_PATH "\"${WINDOWS_SDK_BIN_DIR}\\dxil.dll\"" CACHE INTERNAL "dxil.dll path")
9791
endif()
9892
elseif("${WINDOWS_SDK_VERSION}" VERSION_EQUAL "8.1")
9993
# D3Dcompiler_47.dll from Win8.1 SDK is ancient (from 2013) and fails to

0 commit comments

Comments
 (0)