Skip to content

Installing Compilers

Graham Lopez edited this page Dec 20, 2018 · 1 revision

When developing for QMCPACK, various tools can be helpful. This page describes getting certain up-to-date versions of different toolchains installed.

Contents:

Installing GCC- and LLVM-based toolchains

To get C++17 supporting compilers, along with the recommended versions of clang-format for automatic source formatting, one can use the following scripts.

Centos (tested on 7.3)

For Centos, there are no convenient packages to get the updated versions of GCC/LLVM, so we'll build them for ourselves. Below is a script that will download and build both toolchains from source. Note that it also works for Ubuntu building from source is desired over packages.

Prerequisites for building

shell> yum -y group install 'Development Tools'
shell> yum -y install wget

A script to download and build GCC/LLVM from source

#!/bin/bash

# Script for installing a minimal, yet up-to-date development environment
#
# It's assumed that cmake, wget, and a C/C++ compiler are installed. Use e.g
#
#   centos> yum -y group install 'Development Tools'
#   centos> yum -y install epel-release && yum -y install wget cmake3
#
#   ubuntu> apt -y install build-essential cmake python
#
# Beware: some distros install cmake 3.x as 'cmake3', so
#         modify below or make a symlink in your path
#
# Configuration is done by setting internal variables (see the first section)
#   - where to build (tmpfs recommended)
#   - how many procs to use for parallel make
#   - where to install
#   - which compilers/versions to install
#
# GCC 7.3.0, LLVM 6.0.0 tested on:
#   - Centos 7
#   - Ubuntu 17.10

# exit on error
set -e

###########################
# configuration information

build_path=/tmp/build

par_build=8

install_path=${HOME}/local/apps

gcc_version=7.3.0
llvm_release_version=6.0.0

BUILD_GCC=true
BUILD_LLVM=true


################################
# setup - create our directories

mkdir -p ${build_path}/src ${install_path}



#########################
####### Now GCC #########
#########################

if [[ $BUILD_GCC = 'true' ]] ; then

  echo "*************************************"
  echo "  installing gcc-${gcc_version} "
  echo "*************************************"
  sleep 5

  #cd ${build_path}/src
  mkdir -p ${build_path}/gcc/src
  cd ${build_path}/gcc/src

  wget http://www.netgull.com/gcc/releases/gcc-${gcc_version}/gcc-${gcc_version}.tar.gz
  tar xvzf gcc-${gcc_version}.tar.gz

  cd gcc-${gcc_version}
  ./contrib/download_prerequisites

  mkdir ../build
  cd ../build

    ../gcc-${gcc_version}/configure \
      --prefix=${install_path}/gcc-${gcc_version} \
      --enable-languages=c,c++,fortran \
      --enable-checking=release \
      --disable-multilib

  make -j${par_build}

  make install

  cd gmp # fixes 'gmp.h' not found message
  make install

  cd ../../
  #rm -rf build gcc-${gcc_version} gcc-${gcc_version}.tar.gz

fi


##########################
####### Now LLVM #########
##########################

if [[ $BUILD_LLVM = 'true' ]] ; then

  echo "*************************************"
  echo "  installing llvm-${llvm_release_version} "
  echo "*************************************"
  sleep 5

  mkdir -p ${build_path}/llvm/src
  cd ${build_path}/llvm/src

  # contain our mess
  mkdir -p llvm-build/tarballs
  cd llvm-build/tarballs

  wget http://releases.llvm.org/${llvm_release_version}/llvm-${llvm_release_version}.src.tar.xz
  wget http://releases.llvm.org/${llvm_release_version}/cfe-${llvm_release_version}.src.tar.xz
  wget http://releases.llvm.org/${llvm_release_version}/clang-tools-extra-${llvm_release_version}.src.tar.xz
  wget http://releases.llvm.org/${llvm_release_version}/openmp-${llvm_release_version}.src.tar.xz
  wget http://releases.llvm.org/${llvm_release_version}/compiler-rt-${llvm_release_version}.src.tar.xz
  wget http://releases.llvm.org/${llvm_release_version}/libcxx-${llvm_release_version}.src.tar.xz
  wget http://releases.llvm.org/${llvm_release_version}/libcxxabi-${llvm_release_version}.src.tar.xz
  wget http://releases.llvm.org/${llvm_release_version}/lld-${llvm_release_version}.src.tar.xz

  cd ..

  tar xvf tarballs/llvm-${llvm_release_version}.src.tar.xz
  tar xvf tarballs/cfe-${llvm_release_version}.src.tar.xz
  tar xvf tarballs/clang-tools-extra-${llvm_release_version}.src.tar.xz
  tar xvf tarballs/openmp-${llvm_release_version}.src.tar.xz
  tar xvf tarballs/compiler-rt-${llvm_release_version}.src.tar.xz
  tar xvf tarballs/libcxx-${llvm_release_version}.src.tar.xz
  tar xvf tarballs/libcxxabi-${llvm_release_version}.src.tar.xz
  tar xvf tarballs/lld-${llvm_release_version}.src.tar.xz

  mv cfe-${llvm_release_version}.src llvm-${llvm_release_version}.src/tools/clang
  mv clang-tools-extra-${llvm_release_version}.src llvm-${llvm_release_version}.src/tools/clang/tools/extra
  mv openmp-${llvm_release_version}.src llvm-${llvm_release_version}.src/projects/openmp
  mv compiler-rt-${llvm_release_version}.src llvm-${llvm_release_version}.src/projects/compiler-rt
  mv libcxx-${llvm_release_version}.src llvm-${llvm_release_version}.src/projects/libcxx
  mv libcxxabi-${llvm_release_version}.src llvm-${llvm_release_version}.src/projects/libcxxabi
  mv lld-${llvm_release_version}.src llvm-${llvm_release_version}.src/tools/lld


  mkdir -p build
  cd build

  cmake -DCMAKE_C_COMPILER=gcc \
        -DCMAKE_CXX_COMPILER=g++ \
        -DCMAKE_INSTALL_PREFIX=${install_path}/llvm-${llvm_release_version} \
        -DCMAKE_BUILD_TYPE=Release \
        -DCOMPILER_RT_INCLUDE_TESTS=OFF \
        -DLLVM_ENABLE_ASSERTIONS=OFF \
        -G "Unix Makefiles" \
        ../llvm-${llvm_release_version}.src

  make -j${par_build}

  make install

  cd ${build_path}/src
  #rm -rf llvm-build

fi

Ubuntu (tested on 16.04, 17.10, 18.04)

This script shows how to install multiple compiler versions and manage them in Ubuntu. This script is for 18.04 "bionic", but if you're using it for another version, substitute "xenial" or "artful" for 16.04, 17.10, respectively.

A script to install GCC/LLVM from packages

#!/bin/bash

## only needed for 16.04
# apt-get install -y software-properties-common python-software-properties
# add-apt-repository ppa:ubuntu-toolchain-r/test
# apt update
# apt upgrade
##

apt -y install \
  gcc-7 \
  g++-7 \
  gcc-6 \
  g++-6 \
  gcc-5 \
  g++-5

update-alternatives \
  --install /usr/bin/gcc gcc /usr/bin/gcc-7 50 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-7

update-alternatives \
  --install /usr/bin/gcc gcc /usr/bin/gcc-6 40 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-6

update-alternatives \
  --install /usr/bin/gcc gcc /usr/bin/gcc-5 30 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-5

mkdir -p /etc/apt/sources.list.d

apt -y install wget

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -

echo "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-5.0 main" >> /etc/apt/sources.list.d/llvm.list
echo "deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-5.0 main" >> /etc/apt/sources.list.d/llvm.list

echo "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-6.0 main" >> /etc/apt/sources.list.d/llvm.list
echo "deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-6.0 main" >> /etc/apt/sources.list.d/llvm.list

apt update

apt -y install \
  clang-6.0 \
  clang-format-6.0 \
  libclang-6.0-dev \
  clang-tidy-6.0 \
  clang-5.0 \
  clang-format-5.0 \
  libclang-5.0-dev \
  clang-tidy-5.0 \
  clang-4.0 \
  clang-format-4.0 \
  libclang-4.0-dev \
  clang-tidy-4.0

update-alternatives \
  --install /usr/bin/clang clang /usr/bin/clang-6.0 60 \
  --slave /usr/bin/clang++ clang++ /usr/bin/clang++-6.0 \
  --slave /usr/bin/clang-format clang-format /usr/bin/clang-format-6.0 \
  --slave /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-6.0 \
  --slave /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-6.0

update-alternatives \
  --install /usr/bin/clang clang /usr/bin/clang-5.0 50 \
  --slave /usr/bin/clang++ clang++ /usr/bin/clang++-5.0 \
  --slave /usr/bin/clang-format clang-format /usr/bin/clang-format-5.0 \
  --slave /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-5.0 \
  --slave /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-5.0

update-alternatives \
  --install /usr/bin/clang clang /usr/bin/clang-4.0 40 \
  --slave /usr/bin/clang++ clang++ /usr/bin/clang++-4.0 \
  --slave /usr/bin/clang-format clang-format /usr/bin/clang-format-4.0 \
  --slave /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-4.0 \
  --slave /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-4.0

# the use with 'update-alternatives --config gcc/clang'

Now the various versions can be selected for use by using update-alternatives --config gcc and update-alternatives --config clang.

Mac OS X

(under construction)