Skip to content

Commit

Permalink
CMAKE_CROSSCOMPILING should not be true, if the build targets the hos…
Browse files Browse the repository at this point in the history
…t processor (#124)
  • Loading branch information
MarkSchofield authored Nov 22, 2024
2 parents 25f97a4 + 35a505e commit 88ebf71
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- windows-msvc-spectre-x64
- windows-msvc-x86
- windows-msvc-arm64
- windows-msvc-host
- windows-clang-x64
- windows-clangcl-x64
- windows-vs-x64
Expand Down
14 changes: 10 additions & 4 deletions Windows.Clang.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# | CMAKE_CXX_COMPILER | The path to the C++ compiler to use. |
# | CMAKE_MT | The path to the 'mt.exe' tool to use. |
# | CMAKE_RC_COMPILER | The path tp the 'rc.exe' tool to use. |
# | CMAKE_SYSTEM_NAME | Windows |
# | CMAKE_SYSTEM_NAME | "Windows", when cross-compiling |
# | WIN32 | 1 |
# | CMAKE_CXX_CLANG_TIDY | The commandline clang-tidy is used if CLANG_TIDY_CHECKS was set. |
#
Expand All @@ -54,22 +54,28 @@ cmake_minimum_required(VERSION 3.20)

include_guard()

# If `CMAKE_HOST_SYSTEM_NAME` is not 'Windows', there's nothing to do.
if(NOT (CMAKE_HOST_SYSTEM_NAME STREQUAL Windows))
return()
endif()

option(TOOLCHAIN_UPDATE_PROGRAM_PATH "Whether the toolchain should update CMAKE_PROGRAM_PATH." ON)

set(UNUSED ${CMAKE_TOOLCHAIN_FILE}) # Note: only to prevent cmake unused variable warninig
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES "CMAKE_SYSTEM_PROCESSOR;CMAKE_CROSSCOMPILING")
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES "CMAKE_SYSTEM_PROCESSOR")
set(WIN32 1)

# If `CMAKE_SYSTEM_PROCESSOR` isn't set, default to `CMAKE_HOST_SYSTEM_PROCESSOR`
if(NOT CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()

# If `CMAKE_SYSTEM_PROCESSOR` is not equal to `CMAKE_HOST_SYSTEM_PROCESSOR`, this is cross-compilation.
# CMake expects `CMAKE_SYSTEM_NAME` to be set to reflect cross-compilation.
if(NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL ${CMAKE_HOST_SYSTEM_PROCESSOR}))
set(CMAKE_SYSTEM_NAME Windows)
endif()

if(NOT CMAKE_VS_VERSION_RANGE)
set(CMAKE_VS_VERSION_RANGE "[16.0,)")
endif()
Expand Down
2 changes: 1 addition & 1 deletion Windows.EWDK.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
# | CMAKE_CXX_COMPILER | The path to the C++ compiler to use. |
# | CMAKE_MT | The path to the 'mt.exe' tool to use. |
# | CMAKE_RC_COMPILER | The path tp the 'rc.exe' tool to use. |
# | CMAKE_SYSTEM_NAME | 'Windows' |
# | CMAKE_SYSTEM_NAME | "Windows", when cross-compiling |
# | CMAKE_SYSTEM_PROCESSOR | The architecture to build for (e.g. ARM64). |
# | CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE | The architecture of the host tooling to use (e.g. x86). |
# | CMAKE_VS_PLATFORM_TOOLSET_VERSION | The version of the MSVC platform toolset to use (e.g. 14.31.31103). |
Expand Down
13 changes: 9 additions & 4 deletions Windows.MSVC.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
# | CMAKE_CXX_COMPILER | The path to the C++ compiler to use. |
# | CMAKE_MT | The path to the 'mt.exe' tool to use. |
# | CMAKE_RC_COMPILER | The path tp the 'rc.exe' tool to use. |
# | CMAKE_SYSTEM_NAME | Windows |
# | CMAKE_SYSTEM_NAME | "Windows", when cross-compiling |
# | CMAKE_VS_PLATFORM_TOOLSET_VERSION | The version of the MSVC toolset being used - e.g. 14.29.30133. |
# | WIN32 | 1 |
# | MSVC | 1 |
Expand All @@ -69,16 +69,15 @@ cmake_minimum_required(VERSION 3.20)

include_guard()

# If `CMAKE_HOST_SYSTEM_NAME` is not 'Windows', there's nothing to do.
if(NOT (CMAKE_HOST_SYSTEM_NAME STREQUAL Windows))
return()
endif()

option(TOOLCHAIN_UPDATE_PROGRAM_PATH "Whether the toolchain should update CMAKE_PROGRAM_PATH." ON)

set(UNUSED ${CMAKE_TOOLCHAIN_FILE}) # Note: only to prevent cmake unused variable warninig
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
CMAKE_CROSSCOMPILING
CMAKE_SYSTEM_PROCESSOR
CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE
CMAKE_VS_PRODUCTS
Expand All @@ -88,16 +87,22 @@ set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
VS_INSTALLATION_VERSION
VS_PLATFORM_TOOLSET_VERSION
)
set(CMAKE_CROSSCOMPILING TRUE)
set(WIN32 1)
set(MSVC 1)

include("${CMAKE_CURRENT_LIST_DIR}/VSWhere.cmake")

# If `CMAKE_SYSTEM_PROCESSOR` isn't set, default to `CMAKE_HOST_SYSTEM_PROCESSOR`
if(NOT CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()

# If `CMAKE_SYSTEM_PROCESSOR` is not equal to `CMAKE_HOST_SYSTEM_PROCESSOR`, this is cross-compilation.
# CMake expects `CMAKE_SYSTEM_NAME` to be set to reflect cross-compilation.
if(NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL ${CMAKE_HOST_SYSTEM_PROCESSOR}))
set(CMAKE_SYSTEM_NAME Windows)
endif()

if(NOT CMAKE_VS_VERSION_RANGE)
set(CMAKE_VS_VERSION_RANGE "[16.0,)")
endif()
Expand Down
4 changes: 3 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ project(WindowsToolchainExample)

# Write properties that are influenced by the toolchain
#
message(STATUS "CMAKE_CROSSCOMPILING = ${CMAKE_CROSSCOMPILING}")
message(STATUS "CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR = ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "MSVC_VERSION = ${MSVC_VERSION}")
message(STATUS "MSVC_TOOLSET_VERSION = ${MSVC_TOOLSET_VERSION}")
message(STATUS "MSVC_VERSION = ${MSVC_VERSION}")

# Demonstrate that toolchain-related tooling can be found
#
Expand Down
10 changes: 10 additions & 0 deletions example/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
"CMAKE_SYSTEM_PROCESSOR": "ARM64"
}
},
{
"name": "windows-msvc-host",
"inherits": "windows-msvc",
"displayName": "Configure for 'windows-msvc-host'",
"binaryDir": "${sourceDir}/__output/${presetName}"
},
{
"name": "windows-clang-x64",
"inherits": "windows-clang",
Expand Down Expand Up @@ -184,6 +190,10 @@
"name": "windows-msvc-arm64",
"configurePreset": "windows-msvc-arm64"
},
{
"name": "windows-msvc-host",
"configurePreset": "windows-msvc-host"
},
{
"name": "windows-clang-x64",
"configurePreset": "windows-clang-x64"
Expand Down

0 comments on commit 88ebf71

Please sign in to comment.