Skip to content

CMake is not detecting GCC version when PREFIX variable is defined in toolchain file

Issue

CMake is unable to detect GCC version when PREFIX variable is set in toolchain file. During generation a lot of warnings related CMP0053 is reported but generation runs successfully (unless something depends on compiler version, like target_compile_features).

Steps to reproduce

  1. Create toolchain file with e.g. set(PREFIX arm-none-eabi-) line in it
  2. Run cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ...
  3. CMake reports The CXX compiler identification is GNU instead of The CXX compiler identification is GNU <version>

Repository with reproduction: https://github.com/Novakov/cmake-bug-toolchain-prefix

Details

Generated compiler detection files contains invalid #defines for compiler version. Snippet generation e.g. in Modules/Compiler/GNU-CXX-DetermineCompiler.cmake (https://gitlab.kitware.com/cmake/cmake/-/blob/24d73fa0eb2e9f7cdc8fb8192445ce3ee04cf7d3/Modules/Compiler/GNU-CXX-DetermineCompiler.cmake) uses @PREFIX@ and PREFIX variable holds value set in toolchain file, however common part of detection file does not use prefix: #ifdef COMPILER_VERSION_MAJOR in Modules/CMakeCXXCompilerId.cpp.in (https://gitlab.kitware.com/cmake/cmake/-/blob/24d73fa0eb2e9f7cdc8fb8192445ce3ee04cf7d3/Modules/CMakeCXXCompilerId.cpp.in#L75).

Because of mismatch in the name of #define compiler version is not reported and CMAKE_CXX_COMPILER_VERSION variable is not set.

Affected versions: 3.19 and 3.21 (probably others but I was using these two versions).

Workaround

Do not set PREFIX variable in toolchain file.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information