|
| 1 | +# Finds Windows SDK and sets the following cache variables: |
| 2 | +# - WINDOWS_SDK_VERSION (e.g. 10.0.22621.0). If not found, set to 0.0 |
| 3 | +# - WINDOWS_SDK_BIN_DIR (e.g. C:/Program Files (x86)/Windows Kits/10/bin/10.0.22621.0/x64) |
| 4 | +# - D3D_COMPILER_PATH (e.g. C:/Program Files (x86)/Windows Kits/10/bin/10.0.22621.0/x64/D3Dcompiler_47.dll) |
| 5 | +# - DXC_COMPILER_PATH (e.g. C:/Program Files (x86)/Windows Kits/10/bin/10.0.22621.0/x64/dxcompiler.dll) |
| 6 | +# - DXIL_SIGNER_PATH (e.g. C:/Program Files (x86)/Windows Kits/10/bin/10.0.22621.0/x64/dxil.dll) |
| 7 | +function(find_windows_sdk) |
| 8 | + # Determine Windows SDK version |
| 9 | + if (CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) |
| 10 | + set(WINDOWS_SDK_VERSION ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} CACHE INTERNAL "Windows SDK version") |
| 11 | + elseif(DEFINED ENV{WindowsSDKVersion}) |
| 12 | + set(WINDOWS_SDK_VERSION $ENV{WindowsSDKVersion}) |
| 13 | + # For unbeknown reason, the value ends with a backslash, so we need to remove it |
| 14 | + string(REPLACE "\\" "" WINDOWS_SDK_VERSION ${WINDOWS_SDK_VERSION}) |
| 15 | + set(WINDOWS_SDK_VERSION ${WINDOWS_SDK_VERSION} CACHE INTERNAL "Windows SDK version") |
| 16 | + else() |
| 17 | + set(WINDOWS_SDK_VERSION "0.0" CACHE INTERNAL "Windows SDK version") |
| 18 | + 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") |
| 19 | + endif() |
| 20 | + |
| 21 | + if ("${WINDOWS_SDK_VERSION}" VERSION_GREATER "0.0") |
| 22 | + message("") |
| 23 | + message("Windows SDK version: ${WINDOWS_SDK_VERSION}") |
| 24 | + endif() |
| 25 | + |
| 26 | + unset(WINDOWS_SDK_BIN_DIR CACHE) |
| 27 | + unset(D3D_COMPILER_PATH CACHE) |
| 28 | + unset(DXC_COMPILER_PATH CACHE) |
| 29 | + unset(DXIL_SIGNER_PATH CACHE) |
| 30 | + |
| 31 | + if ("${WINDOWS_SDK_VERSION}" VERSION_GREATER_EQUAL "10.0") |
| 32 | + # Get Windows SDK root directory from registry |
| 33 | + get_filename_component( |
| 34 | + WINDOWS_KITS_ROOT |
| 35 | + "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]" |
| 36 | + ABSOLUTE |
| 37 | + ) |
| 38 | + if (NOT WINDOWS_KITS_ROOT) |
| 39 | + message(WARNING "Unable to read Windows SDK root directory from registry") |
| 40 | + elseif(NOT EXISTS "${WINDOWS_KITS_ROOT}") |
| 41 | + message(WARNING "Windows SDK root directory does not exist: ${WINDOWS_KITS_ROOT}") |
| 42 | + unset(WINDOWS_KITS_ROOT) |
| 43 | + endif() |
| 44 | + |
| 45 | + if(WINDOWS_KITS_ROOT) |
| 46 | + # NOTE: CMAKE_GENERATOR_PLATFORM is not set for single-configuration generators (e.g. Makefile, Ninja) |
| 47 | + if (${CMAKE_SIZEOF_VOID_P} EQUAL 8) |
| 48 | + if (("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64") OR ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AARCH64")) |
| 49 | + set(ARCH_SUFFIX "arm64") |
| 50 | + else() |
| 51 | + set(ARCH_SUFFIX "x64") |
| 52 | + endif() |
| 53 | + else() |
| 54 | + if (("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm") OR ("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "ARM")) |
| 55 | + set(ARCH_SUFFIX "arm") |
| 56 | + else() |
| 57 | + set(ARCH_SUFFIX "x86") |
| 58 | + endif() |
| 59 | + endif() |
| 60 | + |
| 61 | + set(WINDOWS_SDK_BIN_DIR "${WINDOWS_KITS_ROOT}/bin/${WINDOWS_SDK_VERSION}/${ARCH_SUFFIX}") |
| 62 | + if (EXISTS "${WINDOWS_SDK_BIN_DIR}") |
| 63 | + set(WINDOWS_SDK_BIN_DIR "${WINDOWS_SDK_BIN_DIR}" CACHE INTERNAL "Windows SDK bin directory") |
| 64 | + message(STATUS "Windows SDK bin directory: " ${WINDOWS_SDK_BIN_DIR}) |
| 65 | + |
| 66 | + set(D3D_COMPILER_PATH "${WINDOWS_SDK_BIN_DIR}/D3Dcompiler_47.dll") |
| 67 | + |
| 68 | + # DXC is only present in Windows SDK starting with version 10.0.17763.0 |
| 69 | + if("${WINDOWS_SDK_VERSION}" VERSION_GREATER_EQUAL "10.0.17763.0") |
| 70 | + set(DXC_COMPILER_PATH "${WINDOWS_SDK_BIN_DIR}/dxcompiler.dll") |
| 71 | + set(DXIL_SIGNER_PATH "${WINDOWS_SDK_BIN_DIR}/dxil.dll") |
| 72 | + endif() |
| 73 | + else() |
| 74 | + message(WARNING "Windows SDK bin directory does not exist: ${WINDOWS_SDK_BIN_DIR}") |
| 75 | + unset(WINDOWS_SDK_BIN_DIR) |
| 76 | + endif() |
| 77 | + endif() |
| 78 | + elseif("${WINDOWS_SDK_VERSION}" VERSION_EQUAL "8.1") |
| 79 | + # D3Dcompiler_47.dll from Win8.1 SDK is ancient (from 2013) and fails to |
| 80 | + # compile a number of test shaders. Use the compiler from Visual Studio |
| 81 | + # executable path instead |
| 82 | + if(CMAKE_AR AND IS_ABSOLUTE "${CMAKE_AR}") |
| 83 | + get_filename_component(CMAKE_AR_DIR "${CMAKE_AR}" DIRECTORY) |
| 84 | + set(D3D_COMPILER_PATH "${CMAKE_AR_DIR}/D3Dcompiler_47.dll") |
| 85 | + endif() |
| 86 | + endif() |
| 87 | + |
| 88 | + if(D3D_COMPILER_PATH) |
| 89 | + if(EXISTS "${D3D_COMPILER_PATH}") |
| 90 | + message(STATUS "Found D3Dcompiler_47.dll: ${D3D_COMPILER_PATH}") |
| 91 | + set(D3D_COMPILER_PATH "${D3D_COMPILER_PATH}" CACHE INTERNAL "D3Dcompiler_47.dll path") |
| 92 | + else() |
| 93 | + message(WARNING "Cannot find D3Dcompiler_47.dll. File does not exist: ${D3D_COMPILER_PATH}") |
| 94 | + endif() |
| 95 | + endif() |
| 96 | + |
| 97 | + if (DXC_COMPILER_PATH) |
| 98 | + if (EXISTS "${DXC_COMPILER_PATH}") |
| 99 | + message(STATUS "Found dxcompiler.dll: ${DXC_COMPILER_PATH}") |
| 100 | + set(DXC_COMPILER_PATH "${DXC_COMPILER_PATH}" CACHE INTERNAL "dxcompiler.dll path") |
| 101 | + else() |
| 102 | + message(WARNING "Cannot find dxcompiler.dll. File does not exist: ${DXC_COMPILER_PATH}") |
| 103 | + endif() |
| 104 | + endif() |
| 105 | + |
| 106 | + if (DXIL_SIGNER_PATH) |
| 107 | + if (EXISTS "${DXIL_SIGNER_PATH}") |
| 108 | + message(STATUS "Found dxil.dll: ${DXIL_SIGNER_PATH}") |
| 109 | + set(DXIL_SIGNER_PATH "${DXIL_SIGNER_PATH}" CACHE INTERNAL "dxil.dll path") |
| 110 | + else() |
| 111 | + message(WARNING "Cannot find dxil.dll. File does not exist: ${DXIL_SIGNER_PATH}") |
| 112 | + endif() |
| 113 | + endif() |
| 114 | + |
| 115 | + message("") |
| 116 | + |
| 117 | +endfunction() # find_windows_sdk |
0 commit comments