4

Suppose I have a system with multiple C/C++ compilers - various versions of GCC, clang and ICC. Also suppose I have a CMake C/C++ project which has certain requirements and certain preferences regarding the C/C++ compiler to use; and to complicate things, suppose these requirements and preferences and generated dynamically based on the combination of project options I've set (with ccmake or otherwise).

Now, other answers about using a compiler other than the default suggest setting the CC or CXX environment variables - but this is clearly inappropriate here.

Is there a way to get CMake to:

  1. Detect the available compilers.
  2. Choose the one it likes based on some rules/ranking mechanism?

Notes:

  • CMake 3.0 . You may assume a newer CMake version, but make that explicit please.
  • The choice of C or C++ in this question is motivated by my own needs, but it could of course be some other language, if that solution is adaptable.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • you can specify generators for cmake in commnad line – Alexey Andronov Apr 28 '18 at 11:39
  • @AlexeyAndronov : I'm not following, please elaborate. – einpoklum Apr 28 '18 at 11:43
  • take a look here: [https://stackoverflow.com/questions/25941536/what-is-a-cmake-generator](https://stackoverflow.com/questions/25941536/what-is-a-cmake-generator). Basically, you can specify in command line for `cmake` commane for which ide generate solution files. Seems like it's what you need – Alexey Andronov Apr 28 '18 at 11:46
  • @AlexeyAndronov: That does not seem to have anything to do with what I need. – einpoklum Apr 28 '18 at 11:49
  • 1
    It will be hard to detect at cmake-time which compilers are available and even harder to run multiple. That is simply not how cmake was designed. It assumes on generation a fixed compiler. But you can nowadays test for certain c++11/14/17 features and their availability (see [cmake-compile-features](https://cmake.org/cmake/help/v3.8/manual/cmake-compile-features.7.html)). Alternatively, you could write "tests" and use [try_compile](https://cmake.org/cmake/help/latest/command/try_compile.html). That does not allow you to select a compiler, but at least error out when it is insufficient. – André Apr 28 '18 at 12:18
  • @André: Why is it hard to detect which compilers are available, when CMake is pretty good at finding all sorts of libraries and tools? And even compilers, e.g. finding CUDA's compiler? – einpoklum Apr 28 '18 at 12:20
  • @einpoklum. I am not saying that you can't, only that it will be hard... CMake at first run, immediately detects "one" compiler and tests for its features. This is then put in the cache and it assumes that that is "the one" compiler. I agree that you could probably use find_path and find_program to find other compilers, and use execute_process and try to replicate everything CMake had already done at the start. Your CMakeLists are "supposed" to work on any other platform/environment, which may be hard to guarantee when you are writing lots of checks specific to your environment. – André Apr 28 '18 at 12:27
  • @André: I was hoping one could change the search order for the "one" compiler before that happens. – einpoklum Apr 28 '18 at 13:24
  • "_Now, other answers about using a compiler other than the default suggest setting the CC or CXX environment variables - but this is clearly inappropriate here._". It's not clear to me. Can you spell it out? What's wrong with setting an environmetnt variable for the "lifetime" a specific command? Also, why not use the [`CMAKE_CXX_COMPILER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html)? – starball Nov 27 '22 at 09:56
  • @starball: Since I want _CMake_ to choose, not the user to choose. – einpoklum Nov 27 '22 at 11:42

1 Answers1

1

Historically, and probably also technically, the C compiler is very basic to the CMake run. Many commands rely on having a compiler, like detecting symbols or trying to compile a piece of code.

As far as I know, there is no way to tests multiple compilers and chose one. To get this, you have to

  • either wrap the CMake calls and have some logic outside which adds the different compilers to the CMake calls
  • or have to re-write a bunch of CMake functions for yourself.

My advice: Accept the way CMake works and teach it to your users.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • I'm my only user actually. But - ok, I guess. – einpoklum Apr 28 '18 at 20:31
  • I know guys having multiple shell script with different sets of compilers and related arguments passed via -D. With accordingly named directories for out-of-source builds, they work quite effectively. Not sure whether you can profit from such approach. – usr1234567 Apr 28 '18 at 20:58