language: cpp

dist: bionic
sudo: required

install:
  # TRAVIS_BUILD_DIR is set to where the source code is checked
  # out (ie it assumes in-source build)
  - JSON_INSTALL_DIR="${TRAVIS_BUILD_DIR}/../include/nlohmann"
  - mkdir -p "${JSON_INSTALL_DIR}"
  - travis_retry wget --quiet -O "${JSON_INSTALL_DIR}/json.hpp" https://raw.githubusercontent.com/nlohmann/json/v3.7.0/single_include/nlohmann/json.hpp

  - GO_IPFS_DIR="${TRAVIS_BUILD_DIR}/../go-ipfs"
  - GO_IPFS_ARCHIVE_BASENAME="go-ipfs_v0.4.22_linux-amd64.tar.gz"
  - GO_IPFS_ARCHIVE_FULLPATH="${GO_IPFS_DIR}/${GO_IPFS_ARCHIVE_BASENAME}"
  - GO_IPFS_CMD_BASENAME="ipfs"
  - GO_IPFS_CMD="${GO_IPFS_DIR}/go-ipfs/${GO_IPFS_CMD_BASENAME}"
  - |
    if [ -e "${GO_IPFS_ARCHIVE_FULLPATH}" ] ; then
      echo "${GO_IPFS_ARCHIVE_FULLPATH} exists, not downloading"
    else
      echo "${GO_IPFS_ARCHIVE_FULLPATH} does not exist, downloading"
      mkdir -p "${GO_IPFS_DIR}"
      travis_retry wget --quiet -O "${GO_IPFS_ARCHIVE_FULLPATH}" "https://ipfs.io/ipns/dist.ipfs.io/go-ipfs/v0.4.22/${GO_IPFS_ARCHIVE_BASENAME}"
    fi
  - |
    if [ -x "${GO_IPFS_CMD}" ] ; then
      echo "${GO_IPFS_CMD} is present, not extracting"
    else
      echo "${GO_IPFS_CMD} is not present, extracting"
      tar -C "${GO_IPFS_DIR}" -vzxf "${GO_IPFS_ARCHIVE_FULLPATH}"
    fi

cache:
  directories:
    - ${TRAVIS_BUILD_DIR}/../go-ipfs

matrix:
  include:
    - os: linux
      before_install:
        - curl https://apt.llvm.org/llvm-snapshot.gpg.key |sudo apt-key add -
        - sudo add-apt-repository -y "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main"
        - sudo apt-get update -qq
        - sudo apt-get install -y clang-9
      compiler: clang
      env:
        - TAG="Compile with clang 9 in Release mode"
        - CXX_=clang++-9
        - CMAKE_EXTRA_ARGS_="-DCMAKE_BUILD_TYPE=Release"
    - os: linux
      before_install:
        - curl https://apt.llvm.org/llvm-snapshot.gpg.key |sudo apt-key add -
        - sudo add-apt-repository -y "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main"
        - sudo apt-get update -qq
        - sudo apt-get install -y clang-9
      compiler: clang
      env:
        - TAG="Compile with clang 9 and run tests"
        - CXX_=clang++-9
      after_success:
        - "${TRAVIS_BUILD_DIR}/.travis/ipfs_daemon_start.sh"
        - OK="no"
        - CTEST_OUTPUT_ON_FAILURE=1 travis_wait 30 make test && OK="yes"
        - "${TRAVIS_BUILD_DIR}/.travis/ipfs_daemon_stop.sh"
        - test "${OK}" = "yes"
    - os: linux
      before_install:
        - curl https://apt.llvm.org/llvm-snapshot.gpg.key |sudo apt-key add -
        - sudo add-apt-repository -y "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main"
        - sudo apt-get update -qq
        - sudo apt-get install -y clang-9 clang-tools-9
      compiler: clang
      env:
        - TAG="Static analysis with scan-build 9"
      script:
        - cmake --version
        - SOURCE_DIR="${TRAVIS_BUILD_DIR}"
        - BUILD_DIR="${TRAVIS_BUILD_DIR}/../build"
        - mkdir -p "${BUILD_DIR}"
        - cd "${BUILD_DIR}"
        - CCC_CXX=clang++-9 scan-build-9 --status-bugs cmake -DJSON_FOR_MODERN_CXX_INCLUDE_DIR:PATH="${TRAVIS_BUILD_DIR}/../include" -DBUILD_SHARED_LIBS:BOOL=ON "${SOURCE_DIR}"
        - CCC_CXX=clang++-9 scan-build-9 --status-bugs make VERBOSE=1
    - os: linux
      before_install:
        - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
        - sudo apt-get update -qq
        - sudo apt-get install -y g++-9
      compiler: gcc
      env:
        - TAG="Compile with gcc 9"
        - CXX_=g++-9
    - os: linux
      before_install:
        - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
        - sudo apt-get update -qq
        - sudo apt-get install -y g++-6
        - pip install --user cpp-coveralls
      compiler: gcc
      env:
        - TAG="Compile with gcc 6 and assess coverage"
        - CXX_=g++-6
        - CXXFLAGS="-g -O0 --coverage"
        - LDFLAGS="-g -O0 --coverage"
      script:
        - cmake --version
        - CXX=${CXX_:-${CXX}}
        - ${CXX} --version
        - SOURCE_DIR="${TRAVIS_BUILD_DIR}"
        - BUILD_DIR="${TRAVIS_BUILD_DIR}"
        - mkdir -p "${BUILD_DIR}"
        - cd "${BUILD_DIR}"
        - echo "CXXFLAGS='${CXXFLAGS}' LDFLAGS='${LDFLAGS}'"
        - cmake -DJSON_FOR_MODERN_CXX_INCLUDE_DIR:PATH="${TRAVIS_BUILD_DIR}/../include" -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_BUILD_TYPE=Debug "${SOURCE_DIR}"
        - make VERBOSE=1
        - "${TRAVIS_BUILD_DIR}/.travis/ipfs_daemon_start.sh"
        - OK="no"
        - CTEST_OUTPUT_ON_FAILURE=1 travis_wait 30 make test && OK="yes"
        - "${TRAVIS_BUILD_DIR}/.travis/ipfs_daemon_stop.sh"
        - |
          if [ "${OK}" = "yes" ] ; then
            echo "Tests passed, gathering coverage statistics"
            coveralls --verbose --exclude "`find ./ -name CMakeCXXCompilerId.cpp`" --exclude "`find ./ -name feature_tests.cxx`" --exclude test --exclude include/ipfs/test/utils.h --gcov gcov-6 --gcov-options '\-lp' --repo-token hfT2FIRhKeMMqAVnEmUxGSu36wUwnpa0V
          else
            echo "Tests failed, not gathering coverage statistics"
          fi
        - test "${OK}" = "yes"
    - os: osx
      osx_image: xcode8
      env:
        - TAG="OSX xcode 8"

script:
  - cmake --version
  - CXX=${CXX_:-${CXX}}
  - ${CXX} --version
  - SOURCE_DIR="${TRAVIS_BUILD_DIR}"
  - BUILD_DIR="${TRAVIS_BUILD_DIR}/../build"
  - mkdir -p "${BUILD_DIR}"
  - cd "${BUILD_DIR}"
  - echo "CXXFLAGS='${CXXFLAGS}' LDFLAGS='${LDFLAGS}'"
  - cmake -DJSON_FOR_MODERN_CXX_INCLUDE_DIR:PATH="${TRAVIS_BUILD_DIR}/../include" -DBUILD_SHARED_LIBS:BOOL=ON "${CMAKE_EXTRA_ARGS_}" "${SOURCE_DIR}"
  - make VERBOSE=1
