Getting CMake to detect a new CXX Compiler (CXX_COMPILER_ID) without modying CMake's source

I wrote a new C++ compiler and am trying to get it to work with CMake. Setting CMAKE_CXX_COMPILER to my compiler’s path is fine: the right compiler gets invoked.

The problem is CMakeDetermineCompilerId always detects my compiler as Clang or GCC (depending on the implicit macro definitions I add to my compiler), even when I have a project toolchain/ file that sets CXX_COMPILER_ID and CMAKE_CXX_COMPILER_ID to my compiler’s name.

How can I force detection of my compiler at project level, or register my compiler for cmake to detect without going in and actually modying cmake’s source, which seems not very scalable…

This is an issue when compiling Bloomberg/BDE, because that project’s build scripts use CXX_COMPILER_ID to retrieve compiler-specific command-line arguments, so it’s invoking my compiler with Clang’s arguments (which aren’t all supported).

A more detailed case of my problem is here:

@brad.king Do you have any tips on getting this to work?

It’s not currently possible to add a compiler id externally to CMake. This hasn’t been a problem historically because people just contribute their compiler id to CMake’s builtin set, and it hasn’t happened often.

The compiler id (and version information) is detected from preprocessor macros by compiling a small source file generated from e.g. Modules/CMakeCCompilerId.c.in. The generated compiler id detection source has a bunch of #if checks. They are collected from per-compiler checks located in Modules/Compiler/*-DetermineCompiler.cmake by logic in Modules/CMakeCompilerIdDetection.cmake. The latter module contains an ordered list of the builtin compiler ids. The order is important because some compilers define the builtin macros for other compilers as well as themselves.

I encourage you to add your compiler to CMake’s builtin set directly. Additionally or alternatively you could look at adding hooks in Modules/CMakeCompilerIdDetection.cmake to add content via externally-controlled settings, e.g. from a toolchain file.

Yes, I added it to the ordered_compilers list in that file, added a determine file, and it all works fine. Thanks.

1 Like