|
| 1 | +#---------------------------------------------------------------------------------------------------------------------- |
| 2 | +# MIT License |
| 3 | +# |
| 4 | +# Copyright (c) 2021 Mark Schofield |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +#---------------------------------------------------------------------------------------------------------------------- |
| 24 | +# |
| 25 | +# | CMake Variable | Description | |
| 26 | +# |-----------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------| |
| 27 | +# | CMAKE_SYSTEM_VERSION | The version of the operating system for which CMake is to build. Defaults to the host version. | |
| 28 | +# | CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE | The architecture of the tooling to use. Defaults to 'arm64' on ARM64 systems, otherwise 'x64'. | |
| 29 | +# | CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION | The version of the Windows SDK to use. Defaults to the highest installed, that is no higher than CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM | |
| 30 | +# | CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM | The maximum version of the Windows SDK to use, for example '10.0.19041.0'. Defaults to nothing | |
| 31 | +# | CMAKE_WINDOWS_KITS_10_DIR | The location of the root of the Windows Kits 10 directory. | |
| 32 | +# |
| 33 | +# The following variables will be set: |
| 34 | +# |
| 35 | +# | CMake Variable | Description | |
| 36 | +# |---------------------------------------------|-------------------------------------------------------------------------------------------------------| |
| 37 | +# | CMAKE_MT | The path to the 'mt' tool. | |
| 38 | +# | CMAKE_RC_COMPILER | The path to the 'rc' tool. | |
| 39 | +# | CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION | The version of the Windows SDK to be used. | |
| 40 | +# | MDMERGE_TOOL | The path to the 'mdmerge' tool. | |
| 41 | +# | MIDL_COMPILER | The path to the 'midl' compiler. | |
| 42 | +# | WINDOWS_KITS_BIN_PATH | The path to the folder containing the Windows Kits binaries. | |
| 43 | +# | WINDOWS_KITS_INCLUDE_PATH | The path to the folder containing the Windows Kits include files. | |
| 44 | +# | WINDOWS_KITS_LIB_PATH | The path to the folder containing the Windows Kits library files. | |
| 45 | +# | WINDOWS_KITS_REFERENCES_PATH | The path to the folder containing the Windows Kits references. | |
| 46 | +# |
| 47 | +include_guard() |
| 48 | + |
| 49 | +if(NOT CMAKE_SYSTEM_VERSION) |
| 50 | + set(CMAKE_SYSTEM_VERSION ${CMAKE_HOST_SYSTEM_VERSION}) |
| 51 | +endif() |
| 52 | + |
| 53 | +if(NOT CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE) |
| 54 | + if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL ARM64) |
| 55 | + set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE arm64) |
| 56 | + else() |
| 57 | + set(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE x64) |
| 58 | + endif() |
| 59 | +endif() |
| 60 | + |
| 61 | +if(NOT CMAKE_WINDOWS_KITS_10_DIR) |
| 62 | + get_filename_component(CMAKE_WINDOWS_KITS_10_DIR "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\v10.0;InstallationFolder]" ABSOLUTE CACHE) |
| 63 | + if ("${CMAKE_WINDOWS_KITS_10_DIR}" STREQUAL "/registry") |
| 64 | + unset(CMAKE_WINDOWS_KITS_10_DIR) |
| 65 | + endif() |
| 66 | +endif() |
| 67 | + |
| 68 | +if(NOT CMAKE_WINDOWS_KITS_10_DIR) |
| 69 | + message(FATAL_ERROR "Unable to find an installed Windows SDK, and one wasn't specified.") |
| 70 | +endif() |
| 71 | + |
| 72 | +# If a CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION wasn't specified, find the highest installed version that is no higher |
| 73 | +# than the host version |
| 74 | +if(NOT CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) |
| 75 | + file(GLOB WINDOWS_KITS_VERSIONS RELATIVE "${CMAKE_WINDOWS_KITS_10_DIR}/lib" "${CMAKE_WINDOWS_KITS_10_DIR}/lib/*") |
| 76 | + list(FILTER WINDOWS_KITS_VERSIONS INCLUDE REGEX "10\\.0\\.") |
| 77 | + list(SORT WINDOWS_KITS_VERSIONS COMPARE NATURAL ORDER DESCENDING) |
| 78 | + while(WINDOWS_KITS_VERSIONS) |
| 79 | + list(POP_FRONT WINDOWS_KITS_VERSIONS CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) |
| 80 | + if(NOT CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM) |
| 81 | + message(VERBOSE "Windows.Kits: Defaulting version: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") |
| 82 | + break() |
| 83 | + endif() |
| 84 | + |
| 85 | + if(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION VERSION_LESS_EQUAL CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION_MAXIMUM) |
| 86 | + message(VERBOSE "Windows.Kits: Choosing version: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") |
| 87 | + break() |
| 88 | + endif() |
| 89 | + |
| 90 | + message(VERBOSE "Windows.Kits: Not suitable: ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") |
| 91 | + set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) |
| 92 | + endwhile() |
| 93 | +endif() |
| 94 | + |
| 95 | +if(NOT CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) |
| 96 | + message(FATAL_ERROR "A Windows SDK could not be found.") |
| 97 | +endif() |
| 98 | + |
| 99 | +set(WINDOWS_KITS_BIN_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/bin/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" CACHE PATH "" FORCE) |
| 100 | +set(WINDOWS_KITS_INCLUDE_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/include/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" CACHE PATH "" FORCE) |
| 101 | +set(WINDOWS_KITS_LIB_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/lib/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" CACHE PATH "" FORCE) |
| 102 | +set(WINDOWS_KITS_REFERENCES_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/References" CACHE PATH "" FORCE) |
| 103 | +set(WINDOWS_KITS_PLATFORM_PATH "${CMAKE_WINDOWS_KITS_10_DIR}/Platforms/UAP/${CMAKE_SYSTEM_VERSION}/Platform.xml" CACHE PATH "" FORCE) |
| 104 | + |
| 105 | +if(NOT EXISTS ${WINDOWS_KITS_BIN_PATH}) |
| 106 | + message(FATAL_ERROR "Windows SDK ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} cannot be found: Folder '${WINDOWS_KITS_BIN_PATH}' does not exist.") |
| 107 | +endif() |
| 108 | + |
| 109 | +if(NOT EXISTS ${WINDOWS_KITS_INCLUDE_PATH}) |
| 110 | + message(FATAL_ERROR "Windows SDK ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} cannot be found: Folder '${WINDOWS_KITS_INCLUDE_PATH}' does not exist.") |
| 111 | +endif() |
| 112 | + |
| 113 | +if(NOT EXISTS ${WINDOWS_KITS_LIB_PATH}) |
| 114 | + message(FATAL_ERROR "Windows SDK ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} cannot be found: Folder '${WINDOWS_KITS_LIB_PATH}' does not exist.") |
| 115 | +endif() |
| 116 | + |
| 117 | +set(CMAKE_MT "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/mt.exe") |
| 118 | +set(CMAKE_RC_COMPILER_INIT "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/rc.exe") |
| 119 | +set(CMAKE_RC_FLAGS_INIT "/nologo") |
| 120 | + |
| 121 | +set(MIDL_COMPILER "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/midl.exe") |
| 122 | +set(MDMERGE_TOOL "${WINDOWS_KITS_BIN_PATH}/${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}/mdmerge.exe") |
| 123 | + |
| 124 | +# Windows SDK |
| 125 | +if(CMAKE_SYSTEM_PROCESSOR STREQUAL AMD64) |
| 126 | + set(WINDOWS_KITS_TARGET_ARCHITECTURE x64) |
| 127 | +elseif((CMAKE_SYSTEM_PROCESSOR STREQUAL ARM) |
| 128 | + OR (CMAKE_SYSTEM_PROCESSOR STREQUAL ARM64) |
| 129 | + OR (CMAKE_SYSTEM_PROCESSOR STREQUAL X86)) |
| 130 | + set(WINDOWS_KITS_TARGET_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR}) |
| 131 | +elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL x64) |
| 132 | + message(WARNING "CMAKE_SYSTEM_PROCESSOR should be 'AMD64', not 'x64'. WindowsToolchain will stop recognizing 'x64' in a future release.") |
| 133 | + set(WINDOWS_KITS_TARGET_ARCHITECTURE x64) |
| 134 | +elseif((CMAKE_SYSTEM_PROCESSOR STREQUAL arm) |
| 135 | + OR (CMAKE_SYSTEM_PROCESSOR STREQUAL arm64) |
| 136 | + OR (CMAKE_SYSTEM_PROCESSOR STREQUAL x86)) |
| 137 | + message(WARNING "CMAKE_SYSTEM_PROCESSOR (${CMAKE_SYSTEM_PROCESSOR}) should be upper-case. WindowsToolchain will stop recognizing non-upper-case forms in a future release.") |
| 138 | + set(WINDOWS_KITS_TARGET_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR}) |
| 139 | +else() |
| 140 | + message(FATAL_ERROR "Unable identify Windows Kits architecture for CMAKE_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR}") |
| 141 | +endif() |
| 142 | + |
| 143 | +foreach(LANG C CXX RC ASM_MASM) |
| 144 | + list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/ucrt") |
| 145 | + list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/shared") |
| 146 | + list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/um") |
| 147 | + list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/winrt") |
| 148 | + list(APPEND CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${WINDOWS_KITS_INCLUDE_PATH}/cppwinrt") |
| 149 | +endforeach() |
| 150 | + |
| 151 | +link_directories("${WINDOWS_KITS_LIB_PATH}/ucrt/${WINDOWS_KITS_TARGET_ARCHITECTURE}") |
| 152 | +link_directories("${WINDOWS_KITS_LIB_PATH}/um/${WINDOWS_KITS_TARGET_ARCHITECTURE}") |
| 153 | +link_directories("${WINDOWS_KITS_REFERENCES_PATH}/${WINDOWS_KITS_TARGET_ARCHITECTURE}") |
0 commit comments