CMAKE_TOOLCHAIN_FILE properly updated by profiles but still somehow cached after switching

Hi,

I’m fairly new to CMake but quickly diving into its features.

As we are compiling for MCU’s we’re using arm-none-eabi as the primary toolchain. However, our unit tests are executed on x86, requiring multiple toolchains to be used by our build pipeline.

I tried to incorporate everything into one CMake tree by using the ‘new’ CMake profiles and got pretty far, the only thing I couldn’t get around is switching between toolchains without deep cleaning the CMake build directory (to remove the caches).

Is this by design or am I missing a step? Or is there a better way?

I made sure to switch the CMAKE_TOOLCHAIN_FILE variable before the project() call and I can see that it is set correctly by the profiles but somehow the first entry of the variable sticks. Which presents me with the messages I placed in the toolchain file that was first used.

Thanks!

Kind regards,

Jebo

Yes. This is a decision made long ago: there is only one toolchain per language.

CMake caches the toolchain information and as such, CMAKE_TOOLCHAIN_FILE really only does anything (reliable) on the first configure. If CMake detects a compiler change, it recommends to clean CMakeCache.txt.

I would recommend using an ExternalProject_add setup which configures one build tree for a local build to test and a second for the target platform.

Thank you for your quick reply!

Good to know! Then I don’t have to continue down that road anymore.

You mentioned per language, does that mean C/C++ as a language or are C and C++ seperated? Because our application code is written and compiled in C and our tests are GoogleTest, so C++. Would that make it possible?

I will definitely take a look at ExternalProject_add, thank you for the suggestion!

C and C++ are two different languages to CMake. You can certainly use gcc and clang++ if you like (though it is rare to mix-and-match AFAIK).