22

How can I configure travis-ci such that my project builds with more than one version of a compiler?

Say, I want to build it with gcc-4.8, gcc-4.9, clang-3.4, clang-3.5 and clang-3.6.

I know how to build on both gcc and clang, but not on more than one version of them.

To give a little bit more context, my project is a C++ library and I want to ensure compatibility with those compilers.

bst
  • 426
  • 3
  • 10

1 Answers1

37

It was (and still is) quite painful to figure out what is possible/allowed with Travis CI and what isn't. My current solution looks like this:

language: cpp
matrix:
  include:
    - os: linux
      compiler: gcc
      addons:
        apt:
          sources: ['ubuntu-toolchain-r-test']
          packages: ['g++-4.8']
      env: COMPILER=g++-4.8

    - os: linux
      compiler: gcc
      addons:
        apt:
          sources: ['ubuntu-toolchain-r-test']
          packages: ['g++-4.9']
      env: COMPILER=g++-4.9

    - os: linux
      compiler: gcc
      addons:
        apt:
          sources: ['ubuntu-toolchain-r-test']
          packages: ['g++-5']
      env: COMPILER=g++-5

    - os: linux
      compiler: clang
      env: COMPILER=clang++

    - os: linux
      compiler: clang
      addons:
        apt:
          sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.5']
          packages: ['clang-3.5']
      env: COMPILER=clang++-3.5

    - os: linux
      compiler: clang
      addons:
        apt:
          sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6']
          packages: ['clang-3.6']
      env: COMPILER=clang++-3.6

# Activate when 3.7 is released and the repository is available
#    - os: linux
#      compiler: clang
#      addons:
#        apt:
#          sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.7']
#          packages: ['clang-3.7']
#      env: COMPILER=clang++-3.7

# Activate when we are allowed to use MacOS X
#    - os: osx
#      compiler: clang
#      env: COMPILER=clang++

script:
  make CXX=$COMPILER -j3

Some remarks:

  • The above uses the container-based infrastructure
  • Only one compiler is installed per container - speeds up the build and avoids problems as you often can not install several packages/compilers in parallel
  • You can not set CXX directly as Travis CI will overwrite it. You need an intermediate variable like COMPILER
  • The clang++ without extension is currently Clang 3.4
  • Clang 3.7 is not yet available, but should be shortly
  • Clang 3.8 (the development version) from the llvm-toolchain-precise repository is currently black-listed

(Note that the above will change/improve over time, now (2016-01-11) Clang 3.7 is available, as is MacOS X. The above is meant as a starting point, adapt as needed)

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 7
    It's a nice thing to get something off the ground, but for serious projects you also need libc++ and Boost and stuff gets complicated quickly. Boost.Hana has a well-maintained [.yaml file](https://github.com/ldionne/hana/blob/master/.travis.yml) – TemplateRex Aug 20 '15 at 21:19
  • 2
    @TemplateRex They seem to only use Clang, not GCC. Anyways, it is a good example once you've managed the basics. My answer was mostly meant for others to get started because I've seen so many version which are either overly complicated or they don't work or both. My hope is that knowledge spreads throughout as many other repositories as possible so that wherever a new user is looking he can find some good examples. – Daniel Frey Aug 20 '15 at 21:25
  • More as a side remark, half the pain is in knowing how to build the different stuff. I just wished that the gcc folks would put up a nightly build repo where one could download .rpm / .deb packages (just like Clang does). I'd also like to have the same for Boost/libc++, all that installing and compilation on a *per user* basis is just insane. – TemplateRex Aug 20 '15 at 21:37
  • @TemplateRex There seems to be a white-listed [`boost-latest`](https://github.com/travis-ci/apt-source-whitelist/blob/master/ubuntu.json) repository (second entry as of writing this comment). I haven't tried it, though, I have no idea how recent/well maintained it is. – Daniel Frey Aug 20 '15 at 21:41
  • You can trim this down a bit using YAML references for the `apt` sources. See [this issue comment](https://github.com/travis-ci/travis-ci/issues/3505#issuecomment-92764855). – 一二三 Aug 20 '15 at 23:26
  • The `boost-lastest` has version 1.55, so it's not a trunk snapshot. For modern C++14 libraries, having Travis CI with clang/g++ trunk builds (including libc++ and libstdc++, and the latest Boost) is both a must and still a serious pain. – TemplateRex Aug 21 '15 at 06:22
  • 2
    Thanks a bunch. That's precisely what I'm looking for. – bst Aug 21 '15 at 20:36
  • 1
    @TemplateRex thanks so much for the pointer to Hana's .travis.yml – zbeekman Dec 13 '16 at 17:21