language: cpp
os: linux

# Blacklist
branches:
  only:
    - master
  except: gh-pages

env:
  global:
    - GH_REPO_NAME: ComPWA
    - GH_REPO_REF: github.com/ComPWA/ComPWA.git
    - OMP_NUM_THREADS: 1
    - LANG: en_US.UTF-8
    - BASEPATH: $TRAVIS_BUILD_DIR
    - ROOTSYS: $BASEPATH/root
      # The next declaration is the encrypted COVERITY_SCAN_TOKEN, created
      # via the "travis encrypt" command using the project repo's public key
    - secure: "I1LwO65P4q89f31HYnFlrlJ9yHyJ/QIAOXR5CtPfMo5FnestBtlQg6fX3p1vLNi5XdYXP4QpP51FFGJ5suz1aigC45JRaMzO98WQmMvIqYufrhkYej6lNevL0z/tHeI0aURH7N1SfSZaLwkepErssOsdjJAqP538ldA3Zz9g2gM="
    - CODECOV_TOKEN: "2cf5be81-a194-49a5-a8ab-49123b181e2c"

addons:
  apt:
    update: true
    sources:
      - ubuntu-toolchain-r-test # gcc up to 8.0
      - llvm-toolchain-xenial-8
    packages:
      - gcc-7
      - g++-7
      - libboost-all-dev # Version 1.58 for xenial

install:
  - cd $BASEPATH
  - cmake --version
  # ROOT
  - wget https://root.cern.ch/download/root_${ROOTBIN}.tar.gz
  - tar xpvfz root_*.tar.gz > /dev/null 2>&1
  - source root/bin/thisroot.sh

jobs:
  include:
    - os: linux # coverity scan
      if: type != pull_request
      dist: xenial
      compiler: gcc # Xenial: gcc-5.4
      env:
        - TASK="gcc5, ROOT v6.16 + coverity scan"
        - ROOTBIN="v6.16.00.Linux-ubuntu16-x86_64-gcc5.4"
      before_install:
        # For some reason we have because travisCI
        # does not execute the APT plugin for this instance.
        - sudo apt-get update && sudo apt-get install -y libboost-all-dev
        - echo -n | openssl s_client -connect scan.coverity.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sudo tee -a /etc/ssl/certs/ca-;
      addons:
        coverity_scan:
          project:
            name: "ComPWA/ComPWA"
            description: "Build submitted via Travis CI"
          notification_email: peter.weidenkaff@mailbox.org
          build_command_prepend: "mkdir $BASEPATH/build-coverity; cd $BASEPATH/build-coverity; cmake -DCMAKE_BUILD_TYPE=Debug $BASEPATH;"
          build_command: "cmake --build . -- -j 2"
          branch_pattern: master
      script: True

    - os: linux # test Doxygen
      dist: xenial
      env:
        - TASK="Build documentation"
      addons:
        apt:
          packages:
            - doxygen
            - doxygen-doc
            - doxygen-gui
            - doxygen-latex
            - graphviz
      install: true
      script:
        - cd $BASEPATH
        - doxygen $BASEPATH/doc/Doxyfile
      before_deploy:
        - cd $BASEPATH
        - touch doc/html/.nojekyll
        - openssl aes-256-cbc -K $encrypted_b276c14399d1_key -iv $encrypted_b276c14399d1_iv -in github_deploy_key.enc -out github_deploy_key -d
      deploy:
        provider: pages
        keep_history: true
        deploy_key: github_deploy_key
        cleanup: false
        edge: true
        repo: ComPWA/ComPWA
        target_branch: gh-pages
        local_dir: "$BASEPATH/doc/html"
        verbose: true
        on:
          branch: master

    - os: osx # test XCode build
      osx_image: xcode10.2
      env:
        - TASK="clang, ROOT v6.18"
        - ROOTBIN="v6.18.04.macosx64-10.14-clang100"
      addons:
        homebrew:
          update: false
          packages:
            - boost
      script:
        - mkdir $BASEPATH/build
        - cd $BASEPATH/build
        - cmake -DCMAKE_INSTALL_PREFIX=$BASEPATH/../install $BASEPATH
        - cmake --build . -- -j 2
        # Do not run tests for osx since it will fail. The reason is that
        # the precompiled ROOT libraries do not have RPATH set properly.
        - make install

    - os: linux # test gcc7 build
      dist: xenial
      compiler: gcc
      env:
        - TASK="gcc7, ROOT v6.16"
        - CC=gcc-7
        - CXX=g++-7
        - ROOTBIN="v6.16.00.Linux-ubuntu16-x86_64-gcc5.4"

    - os: linux # test gcc5 build + code coverage
      dist: xenial
      compiler: gcc # Xenial: gcc-5.4
      env:
        - TASK="gcc5, ROOT v6.16 + code coverage"
        - ROOTBIN="v6.16.00.Linux-ubuntu16-x86_64-gcc5.4"
      addons:
        apt:
          packages:
            - libboost-all-dev # Version 1.58 for xenial
            - lcov
      script:
        - mkdir $BASEPATH/build
        - cd $BASEPATH/build
        - cmake -DCODE_COVERAGE=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=$BASEPATH/../install $BASEPATH
        - cmake --build . -- -j 2
        - ctest -j 2 --output-on-failure
        - make install
      after_success:
        - if [ -f "$BASEPATH/build-coverity/cov-int/scm_log.txt" ]; then
          cat $BASEPATH/build-coverity/cov-int/scm_log.txt
          fi # Print coverity log if exists
        - cd $BASEPATH
        # Create lcov report
        - lcov --capture --directory . --output-file $BASEPATH/coverage.info
        # ignore certain files
        - lcov --remove $BASEPATH/coverage.info '/usr/*' '*/test/*' '*/ThirdParty/*' '*/root/include/*' --output-file $BASEPATH/coverage-filtered.info # filter system-files
        - lcov --list $BASEPATH/coverage-filtered.info # debug info
        # Uploading report to CodeCov
        - bash <(curl -s https://codecov.io/bash) -f $BASEPATH/coverage-filtered.info || echo "Codecov did not collect coverage reports"

    - os: linux # test clang7 build
      dist: xenial
      compiler: clang
      env:
        - TASK="clang7, ROOT v6.16"
        - ROOTBIN="v6.16.00.Linux-ubuntu16-x86_64-gcc5.4"

    - os: linux # test build in CMake Debug mode (clang)
      dist: xenial
      compiler: clang
      env:
        - TASK="clang7, ROOT v6.16, DEBUG"
        - ROOTBIN="v6.16.00.Linux-ubuntu16-x86_64-gcc5.4"
      script:
        - mkdir $BASEPATH/build
        - cd $BASEPATH/build
        - cmake -DCMAKE_BUILD_TYPE=Debug $BASEPATH
        - cmake --build . -- -j 2

script:
  - mkdir $BASEPATH/build
  - cd $BASEPATH/build
  - cmake -DCMAKE_INSTALL_PREFIX=$BASEPATH/../install $BASEPATH
  - cmake --build . -- -j 2
  - ctest -j 2 --output-on-failure
  - make install
