Do I need to set the CMAKE_SYSTEM_NAME when making a toolchain file?

We have a MSVC toolchain at work. In the toolchain we have the following code:

    # Define the target system and version
    set(CMAKE_SYSTEM_NAME     "Windows" CACHE INTERNAL "")
    set(CMAKE_SYSTEM_VERSION  "10.0.${AMD_WINDOWS_VERSION}.0" CACHE INTERNAL "")

However, this causes us issues since it enables CMAKE_CROSSCOMPILING:
“This variable will be set to true by CMake if the CMAKE_SYSTEM_NAME variable has been set manually (i.e. in a toolchain file or as a cache entry from the cmake command line).”

The reason this is an issue is because it enables the NATIVE builds for our dependency LLVM:

# https://github.com/llvm/llvm-project/blob/main/llvm/CMakeLists.txt
if(CMAKE_CROSSCOMPILING OR (LLVM_OPTIMIZED_TABLEGEN AND (LLVM_ENABLE_ASSERTIONS OR CMAKE_CONFIGURATION_TYPES)))
  set(LLVM_USE_HOST_TOOLS ON)
endif()

...

# when crosscompiling import the executable targets from a file
if(LLVM_USE_HOST_TOOLS)
  include(CrossCompile)
  llvm_create_cross_target(LLVM NATIVE "" Release)
endif(LLVM_USE_HOST_TOOLS)

LLVM will generate a native build if CMAKE_CROSSCOMPILING is set to true.

However, this is unfavorable because it isn’t necessary and drastically increases the configuration/build time for our building situation. Since our CMAKE_SYSTEM_NAME == CMAKE_HOST_SYSTEM_NAME and our CMAKE_SYSTEM_PROCESSOR == CMAKE_HOST_SYSTEM_PROCESSOR.

A possible workaround is to disable the CMAKE_SYSTEM_NAME code setting.

    # Define the target system and version
    # Commenting out the CMAKE_SYSTEM_NAME
    #    set(CMAKE_SYSTEM_NAME     "Windows" CACHE INTERNAL "")
    set(CMAKE_SYSTEM_VERSION  "10.0.${AMD_WINDOWS_VERSION}.0" CACHE INTERNAL "")

This way CMAKE_CROSSCOMPILING will be set to FALSE. And LLVM won’t generate native builds.

However, I’m unsure if it’s legal to not specify the CMAKE_SYSTEM_NAME in a toolchain file.

@craig.scott (https://gitlab.kitware.com/cmake/cmake/-/issues/21744)

It is your choice what you include in a toolchain file. You are not required to set CMAKE_SYSTEM_NAME, or any other variable for that matter. You only need to set those things that are required for what you want the toolchain file to achieve. That doesn’t always mean you are cross-compiling, sometimes it might just be selecting a different compiler for the host machine.

But yes, this is an example of where CMAKE_CROSSCOMPILING can lie to you.

1 Like

I changed my toolchain logic to not set the system name unless it is neccessary. To avoid this CMAKE_CROSSCOMPILING problem.

    # Define the target system and version:
    # Only set CMAKE_SYSTEM_NAME when it's different than the host system name.
    # Otherwise CMAKE_CROSSCOMPILING will be set to true unneccessarily.
    # Which can cause longer builds for developers in some situations.
    if (NOT (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") )
        set(CMAKE_SYSTEM_NAME     "Windows" CACHE INTERNAL "")
    endif()