# Do I need to set the CMAKE\_SYSTEM\_NAME when making a toolchain file?

**URL:** https://discourse.cmake.org/t/do-i-need-to-set-the-cmake-system-name-when-making-a-toolchain-file/2861
**Category:** Usage
**Tags:** os:windows
**Created:** [March 1, 2021, 7:24pm UTC](https://discourse.cmake.org/t/do-i-need-to-set-the-cmake-system-name-when-making-a-toolchain-file/2861 "2021-03-01T19:24:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [March 1, 2021, 7:24pm UTC](https://discourse.cmake.org/t/do-i-need-to-set-the-cmake-system-name-when-making-a-toolchain-file/2861/1 "2021-03-01T19:24:49Z")

</div>

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

```auto
    # 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:

```auto
# 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.

```auto
    # 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.

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [March 1, 2021, 7:29pm UTC](https://discourse.cmake.org/t/do-i-need-to-set-the-cmake-system-name-when-making-a-toolchain-file/2861/2 "2021-03-01T19:29:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 1, 2021, 8:32pm UTC](https://discourse.cmake.org/t/do-i-need-to-set-the-cmake-system-name-when-making-a-toolchain-file/2861/3 "2021-03-01T20:32:40Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [March 2, 2021, 2:43pm UTC](https://discourse.cmake.org/t/do-i-need-to-set-the-cmake-system-name-when-making-a-toolchain-file/2861/4 "2021-03-02T14:43:07Z")

</div>

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

```auto
    # 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()

```
