Using an alternative compiler for projects of the Travis-CI R project

The Travis-CI official build of the R project supports the use of Ubuntu (at the time of this question) gcc version 4.6.

CRAN uses gcc 4.9, and some packages that build on CRAN will not build on Travis with gcc 4.6.

How to change the default gcc compiler for a project / R package assembly to more accurately reflect CRAN assemblies?

+5
source share
2 answers

I really wanted to use Travis to test my ndjson package, but the C ++ library I use will not compile under gcc 4.6.

The ndjson package is on CRAN, and the CRAN builds are excellent (with the exception of r-oldrel on Windows, which doesn't bother me) a bit), so I needed a way to change the compiler that R uses on Travis.

I use gcc 5 in the example below, but you can use any version available in instrumental tests . Ideally, you need to emulate the CRAN gcc version, and this may be something that Travis people might consider creating a default for R-builds.

.travis.yml begins with the same:

 language: r warnings_are_errors: true sudo: required env: global: - CRAN: http://cran.rstudio.com 

I added a matrix build configuration to add a new package source, as well as specify the packages (packages) that need to be installed. I left it in the matrix configuration, since I will try (in the end) to add clang .

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

Then I made sure that the auto default compiler is installed on this new gcc , and I am doubly sure that R will use it by creating a local Makevars :

 before_install: - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 100 - mkdir -p ~/.R - echo "VkVSPS01CkNDPWdjYyQoVkVSKSAtc3RkPWMxMSAKQ1hYPWcrKyQoVkVSKQpTSExJQl9DWFhMRD1nKyskKFZFUikKRkM9Z2ZvcnRyYW4KRjc3PWdmb3J0cmFuCg==" | base64 -d > ~/.R/Makevars - cat ~/.R/Makevars 

The base64 string matches:

 VER=-5 CC=gcc$(VER) -std=c11 CXX=g++$(VER) SHLIB_CXXLD=g++$(VER) FC=gfortran F77=gfortran 

and is just (IMO) clean that way.

In theory, all I would have to do was create Makevars (i.e. do not change the default gcc value using update-alternatives ), but it turned out that Travis used the Makevars gcc setting when installing the dependencies, but not for the build itself package. Thus, update-alternatives is required. I also had to add -std=c11 to make sure some of the compiled dependencies (build error not indicated).

After these changes to the configuration .travis.yml , ndjson built perfectly.

+5
source

To give an alternative approach, I use a custom shell script in one of my sourcetools packages. My goal was to ensure that the package was built using the (now ancient) gcc-4.4 compiler. In .travis.yml I have:

 language: r cache: packages sudo: false warnings_are_errors: true before_install: - source travis/before-install.sh addons: apt: packages: - gcc-4.4 - g++-4.4 - clang r: - oldrel - release - devel env: - COMPILER=gcc-4.4 - COMPILER=gcc - COMPILER=clang 

And in travis/before-install.sh I have:

 #!/usr/bin/env sh mkdir -p ~/.R if [ "${COMPILER}" = "gcc-4.4" ]; then echo "CC=gcc-4.4 -std=gnu99" >> ~/.R/Makevars echo "CXX=g++-4.4" >> ~/.R/Makevars echo "CXX1X=g++-4.4 -std=c++0x" >> ~/.R/Makevars fi if [ "${COMPILER}" = "gcc" ]; then echo "CC=gcc -std=gnu99" >> ~/.R/Makevars echo "CXX=g++" >> ~/.R/Makevars echo "CXX1X=g++ -std=c++0x" >> ~/.R/Makevars fi if [ "${COMPILER}" = "clang" ]; then echo "CC=clang -std=gnu99" >> ~/.R/Makevars echo "CXX=clang++" >> ~/.R/Makevars echo "CXX1X=clang++ -std=c++0x" >> ~/.R/Makevars fi 

The end result is basically the same, but IMHO it is slightly cleaner to separate the setup logic to its own script, which is completely disconnected from the environment variables. It also simplifies the construction of R-matrix constructions, since Travis automatically combines the permutations of r and env here (no need to do this “manually”).

I believe that using before-install.sh script can be cleaned up / made more general, but I haven’t needed to do this yet.

+4
source

All Articles