# Adapted from various sources, including:
# - Louis Dionne's Hana: https://github.com/ldionne/hana
# - Paul Fultz II's FIT: https://github.com/pfultz2/Fit
# - Eric Niebler's range-v3: https://github.com/ericniebler/range-v3
# - Gabi Melman spdlog: https://github.com/gabime/spdlog

sudo: required
language: cpp

addons: &gcc7
  apt:
    packages:
      - g++-7
      - libboost-all-dev
      - libhdf5-openmpi-dev
      - libeigen3-dev
      - ninja-build
    sources:
      - ubuntu-toolchain-r-test

matrix:
  include:
    # Older linux (trusty) with default gcc
    # Install serial hdf5 + build serial
    - os: linux
      dist: trusty
      env:
        - HIGHFIVE_USE_XTENSOR=False
        - HIGHFIVE_USE_OPENCV=False
        - HIGHFIVE_PARALLEL_HDF5=False
        - IS_BASE_ENVIRON=1
      addons:
        apt:
          packages:
            - libboost-all-dev
            - libeigen3-dev
            - libhdf5-serial-dev
            - ninja-build

    # Linux gcc-7
    # Install parallel hdf5 + build parallel
    - os: linux
      dist: xenial
      env:
        - GCC_VERSION=7
        - HIGHFIVE_USE_XTENSOR=True
        - HIGHFIVE_USE_OPENCV=False
        - HIGHFIVE_PARALLEL_HDF5=True
      addons: *gcc7

    # Mac OSX XCode 10
    - os: osx
      osx_image: xcode10.3
      env:
        - HIGHFIVE_USE_XTENSOR=True
        - HIGHFIVE_USE_OPENCV=True
        - HIGHFIVE_PARALLEL_HDF5=False

    # Windows
    - os: windows
      env:
        - HIGHFIVE_USE_XTENSOR=True
        - HIGHFIVE_USE_OPENCV=True
        - HIGHFIVE_PARALLEL_HDF5=False

env:
  global:
    - MINCONDA_VERSION="latest"
    - MINCONDA_LINUX="Linux-x86_64"
    - MINCONDA_OSX="MacOSX-x86_64"

install:
  - export HOMEBREW_NO_AUTO_UPDATE=1  # for reproducibility, dont autoupdate

  - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
      MINCONDA_OS=$MINCONDA_LINUX;
    elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
      if [ "$BREW_USE_LATEST" ]; then
        brew update;
        brew install hdf5; brew upgrade hdf5;
      fi;
      brew install boost hdf5 eigen ninja;
      MINCONDA_OS=$MINCONDA_OSX;
    fi

  - if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then
      export CMAKE_GENERATOR="Visual Studio 15 2017 Win64" ;
      export TESTS_TARGET="RUN_TESTS";
      choco install --yes miniconda3 ;
      source C:/Tools/miniconda3/Scripts/activate ;
    else
      export CMAKE_GENERATOR="Ninja" ;
      export TESTS_TARGET="test";
      wget "http://repo.continuum.io/miniconda/Miniconda3-$MINCONDA_VERSION-$MINCONDA_OS.sh" -O miniconda.sh;
      bash miniconda.sh -b -p $HOME/miniconda ;
      source $HOME/miniconda/bin/activate;
      hash -r ;
    fi
  - conda config --set always_yes yes --set changeps1 no
  - conda update -q conda
  - conda install -c conda-forge mamba
  - if [[ "$HIGHFIVE_USE_XTENSOR" == "True" ]]; then
      mamba install -c conda-forge xtl xsimd xtensor;
    fi
  - if [[ "$HIGHFIVE_USE_OPENCV" == "True" ]]; then
      mamba install -c conda-forge libopencv opencv;
    fi
  - if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then
      mamba install -c conda-forge boost-cpp hdf5 eigen;
    fi

before_script:
  - if [ -n "$GCC_VERSION" ]; then export CXX="g++-${GCC_VERSION}" CC="gcc-${GCC_VERSION}"; fi
  - if [ -n "$CLANG_VERSION" ]; then export CXX="clang++-${CLANG_VERSION}" CC="clang-${CLANG_VERSION}"; fi
  - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then export CXX="clang++" CC="clang"; fi
  - which $CXX
  - which $CC
  - $CXX --version
  - cmake --version

script:
  - cd ${TRAVIS_BUILD_DIR}
  - mkdir -p build && pushd build
  - >
    cmake --warn-uninitialized --debug-output
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
    -DHIGHFIVE_TEST_SINGLE_INCLUDES:BOOL=ON
    -DHIGHFIVE_PARALLEL_HDF5:BOOL=${HIGHFIVE_PARALLEL_HDF5}
    -DHIGHFIVE_USE_EIGEN:BOOL=ON
    -DHIGHFIVE_USE_XTENSOR:BOOL=${HIGHFIVE_USE_XTENSOR}
    -DHIGHFIVE_USE_OPENCV:BOOL=${HIGHFIVE_USE_OPENCV}
    -G "${CMAKE_GENERATOR}" ../
  - cmake --build .
  - CTEST_OUTPUT_ON_FAILURE=1 cmake --build . --target ${TESTS_TARGET}
  - popd
  - if [ $IS_BASE_ENVIRON ]; then
      bash tests/test_project_integration.sh;
    fi
