Pierre
Pierre

Reputation: 6172

Cmake: How to force a compiler only if present?

I would like Cmake to use Intel C++ Compiler only if it is present. I found this snippet on here, but, as the name suggests, it forces Cmake to use Intel compiler. And will complain if not present. Period.

include(CMakeForceCompiler)
CMAKE_FORCE_C_COMPILER(icc "Intel C Compiler")
CMAKE_FORCE_CXX_COMPILER(icpc "Intel C++ Compiler")

Is there any way to achieve this kind of behaviour (ideally only displaying a warning to the user if not present)?

Upvotes: 1

Views: 564

Answers (2)

xoolive
xoolive

Reputation: 366

Here is how I do it:

find_program (INTEL_COMPILER icc)
if (INTEL_COMPILER)
  include (CMakeForceCompiler)
  cmake_force_c_compiler (icc "Intel C Compiler")
  cmake_force_cxx_compiler (icpc "Intel C++ Compiler")
endif (INTEL_COMPILER)

So here, CMake doesn't say anything if you don't have Intel compiler, but you could easily add a message("You don't have icc in your PATH") in the else() branch!

Upvotes: 0

ComicSansMS
ComicSansMS

Reputation: 54589

This is not possible from inside CMake.

CMake expects the user to setup and select the correct toolchain. Then on the first configure run CMake performs a couple of tests to verify that the selected compiler is present and works as expected. This check can only be performed once per configure, so a clean switch of compilers from within CMake always requires wiping the CMakeCache.txt and restarting with a fresh configure run.

You will need an external script that selects the correct compiler and then invokes CMake with the correct parameters.

Upvotes: 3

Related Questions