8
votes

As far I know the best way to change the target C++ compiler used by cmake is to change the CXX environment variable just before the call to cmake:

$ export CXX="clang++" && cmake --build

The Travis CI build sets the CXX and CC accordingly to the settings of the build. So if you have this in your .travis.yml:

language: cpp
compiler: 
  - gcc
  - clang

script:
  - cmake --build
  - ./bin/foo

The first time cmake should use GCC and Clang on the latter isn't?

Instead, the GCC build compiles just fine (Its the default compiler used by cmake), but the Clang version uses GCC too:

0.01s$ echo $CC $CXX
clang clang++
The command "echo $CC $CXX" exited with 0.

0.02s$ $CXX --version
clang version 3.4 (tags/RELEASE_34/final) Target: x86_64-unknown-linux-gnu Thread model: posix

Running: cmake -G "Unix Makefiles" -Wno-dev ../cmake
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done

I even tried to set those variables again just before the call:

- if [ "$CXX" == "clang++" ]; then export CXX="clang++" && cmake --build; fi
- if [ "$CXX" == "g++" ];     then export CXX="g++"     && cmake --build; fi

Which has no much sense I think...

Here is the successful build using GCC, and here the (Supposed to be) clang build.

My question is: How I can change the compiler used by cmake under Travis CI?

Here is my .travis.yml.

2
The first time cmake should use GCC and Clang on the latter isn't? this approach should work fine. However I prefer to use cmake toolchains, it's more flexible. Example: linux and macuser2288008
@ruslo but doesn't work. As you can see here, cmake gets rid completely from the value of CXX, which is clang++Manu343726

2 Answers

11
votes

Explanation

In your .travis.yml we see:

- if [ "$CXX" == "clang++" ]; then export CXX="clang++" && bii cpp:build; fi
- if [ "$CXX" == "g++" ];     then export CXX="g++"     && bii cpp:build; fi

biicode's command bii cpp:build runs CMake with biicode's default generator which is "Unix Makefiles" for UNIX platform (GNU toolchain). Read about it: Biicode C++ documentation - Generators and IDEs.

This behaviour is seen in your logs: Running: cmake -G "Unix Makefiles" -Wno-dev ../cmake (https://travis-ci.org/Manu343726/Turbo/jobs/33889114, line 257)

CMake not always looks for environment variables CXX/CC (as stated in CMake FAQ). It depends on selected generator. It works for me when I call CMake with no generator (my travis file: https://github.com/quepas/Graph-ene/blob/master/.travis.yml).

Solutions

1
votes

You can use a matrix. This is especially useful if you have many different compilers (eg. gcc5, gcc6, clang3.7, clang3.8):

language: generic # Works better in most cases

matrix:
    include:
    - env: CXX=g++ CC=gcc
      # additional apt settings if needed
    - env: CXX=clang++ CC=clang

script:
    - ...

If you have different versions of a compiler this can be extended:

[…]
matrix:
    include:
    - env: CXX=g++-5 CC=gcc-5
    - env: CXX=g++-6 CC=gcc-6
    - env: CXX=clang++-3.8 CC=clang-3.8
[…]

It also doesn't duplicate the script steps and keeps the file clean of if… else… parts.

https://travis-ci.org/RhysU/descendu/builds/158055261