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
- Create toolchain file with e.g.
set(PREFIX arm-none-eabi-)
line in it - Run
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ...
- CMake reports
The CXX compiler identification is GNU
instead ofThe CXX compiler identification is GNU <version>
Repository with reproduction: https://github.com/Novakov/cmake-bug-toolchain-prefix
Details
Generated compiler detection files contains invalid #define
s 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.