language: cpp
sudo: required
dist: trusty

env:
  global:
    - BUILD_CMD='make VERBOSE=1'
    - TEST_CMD='ctest -T test --output-on-failure -V'
    - OMP_NUM_THREADS=4
    - MPI_CONFIG='CMAKE_CMD_EXTRA="-DMPIEXEC:FILEPATH=$(which mpirun) -DMPIEXEC_MAX_NUMPROCS=${MPI_PROCS}"'


addons:
  homebrew:
    packages: &osx_sources
      - gcc@7
    update: true

cache:
  directories:
    - $HOME/spack

matrix:
  fast_finish: true

  include:
    - os: osx
      osx_image: xcode10.1
      env:
        - TITLE="XCode 10.1 - GCC"
        - MPI=OFF
        - openMP=OFF
        - DOCS=OFF

    - os: osx
      osx_image: xcode10.1
      env:
        - TITLE="XCode 10.1 - GCC - OMP"
        - MPI=OFF
        - openMP=ON
        - DOCS=OFF

    - os: osx
      osx_image: xcode10.1
      env:
        - TITLE="XCode 10.1 - GCC - MPI"
        - MPI=ON
        - openMP=ON
        - DOCS=OFF
        - MPI_PROCS=2
        - CI_HELPERS='spack'
      addons:
        homebrew:
          packages:
            - *osx_sources
            - modules
            - lmod

    - os: osx
      osx_image: xcode10.1
      env:
        - TITLE="XCode 10.1 - Clang"
        - MPI=OFF
        - openMP=OFF
        - DOCS=OFF

    - os: linux
      addons:
        apt:
          sources: &basic_sources
            - ubuntu-toolchain-r-test
            - llvm-toolchain-trusty-5.0
          packages:
            - &native_deps [ g++-7, clang-5.0, libtiff5-dev, libgomp1]
            # libtiff5-dev version 5 ?; libgomp1 # version 4.8.4-2
            - &all_deps [ libfftw3-dev, libboost-all-dev, libeigen3-dev ]
            # libfftw3 version 3.3.3; libboost-all-dev version 1.54; libeigen3-dev version 3.2.0

      env:
        - TITLE="All dependencies - GCC"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=OFF
        - openMP=OFF
        - DOCS=OFF

    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
            - *all_deps

      env:
        - TITLE="All dependencies - GCC - OMP"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=OFF
        - openMP=ON
        - DOCS=OFF

    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
            - *all_deps
            - openmpi-bin # Version 1.6.5-8
            - lcov

      env:
        - TITLE="All dependencies - GCC - MPI"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=ON
        - openMP=ON
        - DOCS=OFF
        - MPI_PROCS=4
        - COVERAGE=ON

    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
            - *all_deps
            - mpich # version 3.0.4-6

      env:
        - TITLE="All dependencies - GCC - MPI - mpich"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=ON
        - openMP=ON
        - DOCS=OFF
        - MPI_PROCS=4


    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
            - *all_deps
      env:
        - TITLE="All dependencies - Clang"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=OFF
        - openMP=OFF
        - DOCS=OFF

    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
            - openmpi-bin # Version 1.6.5-8

      env:
        - TITLE="No dependencies - GCC - MPI"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=ON
        - openMP=ON
        - DOCS=OFF
        - MPI_PROCS=4

    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
            - openmpi-bin # Version 1.6.5-8

      env:
        - TITLE="Benchmark (no deps) - GCC - MPI"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=ON
        - openMP=ON
        - DOCS=OFF
        - MPI_PROCS=4
        - Benchmarks=ON
        - TEST_CMD='cd cpp/benchmarks; ./wavelets; ./conjugate_gradient; ./l1_proximal'
        - TRAVIS_ALLOW_FAILURE=true

    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
            - openmpi-bin # Version 1.6.5-8

      env:
        - TITLE="Examples (no deps) - GCC - MPI"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=ON
        - openMP=ON
        - DOCS=OFF
        - MPI_PROCS=4
        - EXAMPLES=ON
        - TEST_CMD='cd cpp/examples; find ./ -type f -perm /u=x -printf "Executing %p\n" -exec  {} \; '
        - TRAVIS_ALLOW_FAILURE=true

    - os: linux
      services:
        - docker
      env:
        - TITLE="Documentation - GCC"
        - DOCS=ON
        - BUILD_CMD='make docweb VERBOSE=1'
      script:
        - chmod o+w . # To allow docker to create the build directory
        - docker run --rm -v $(pwd):/mydata uclrits/sopt /bin/sh -c "mkdir build; cd build; cmake .. -Ddocs=${DOCS} -Dweb=ON; eval ${BUILD_CMD}"

    - os: linux
      addons:
        apt:
          sources:
            - *basic_sources
          packages:
            - *native_deps
      env:
        - TITLE="Lint"
        - DEBUG=True
        - LDFLAGS='-pthread'
        - MPI=OFF
        - openMP=OFF
        - DOCS=OFF
        - BUILD_CMD="find ../ -regex '.*\.\(cc\|h\)' -not -iname '*.in.h' | xargs -I{} -P 10 clang-format-7 -i -style=file {}; git diff"
        - TEST_CMD="git diff --exit-code || (echo '## NOTE: your code is not linted properly - fix the above'; false)"

deploy:
  provider: pages
  skip-cleanup: true
  github-token: $GITHUB_TOKEN  # Set in the settings page of your repository, as a secure variable
  keep-history: true
  local-dir: build/cpp/docs/html
  verbose: true # FIXME: just at first to see logs
  on:
    branch: development
    condition: $TITLE = "Documentation - GCC"

before_install:
  - echo "Travis Run!"
  - if [ $TRAVIS_OS_NAME = linux ]; then ClangVer="-5.0"; fi
  - if [[ ${TITLE} = *GCC* ]]; then CC=gcc-7; else CC=clang${ClangVer}; fi
  - if [[ ${TITLE} = *GCC* ]]; then CXX=g++-7; else CXX=clang++${ClangVer}; fi
  - echo "==================== ${TITLE} ====================="
  - if [[ ${TITLE} = *Documentation* ]]; then docker pull uclrits/sopt; fi


install:
  # Add CI-helpers and install the dependencies we need from them.
  - |
    if [ ! -z ${CI_HELPERS} ]; then
        git clone --depth 1 git://github.com/UCL-RITS/ci-helpers.git
        source ci-helpers/travis/setup.sh ${CI_HELPERS}
    fi
  # As MPI is not installed already, this variable is not set properly; this
  # way is set up for all the jobs in the same manner.
  - if [[ $MPI = ON ]]; then eval $MPI_CONFIG; echo $CMAKE_CMD_EXTRA;  fi

before_script:
  - |
    if [[ ${TITLE} = *Lint* ]]; then
        curl https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
        echo "deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-7 main" | sudo tee -a /etc/apt/sources.list
        sudo apt-get update -y -qq
        sudo apt-get install clang-format-7
    fi

script:
  - pwd
  - mkdir build
  - cd build
  - |
    cmake .. -DCMAKE_BUILD_TYPE=Release \
    -Ddompi=${MPI} \
    -Dexamples=${EXAMPLES-OFF} \
    -Dopenmp=${openMP} \
    -Ddocs=${DOCS} \
    -Dbenchmarks=${Benchmarks-OFF} \
    -Dcoverage=${COVERAGE-OFF} \
    ${CMAKE_CMD_EXTRA}
  - eval $BUILD_CMD
  - eval $TEST_CMD

after_success:
  - if [ "${TITLE}" = "All dependencies - GCC - MPI" ]; then cd ..; bash .ci/coverage.sh; fi
