stages:
  - canary
  - build

.build:
  retry:
    max: 2
  variables:
    CCACHE_DIR: "${CI_PROJECT_DIR}/ccache"
    CXXFLAGS: "-Wall -Werror"
    DISTRO: "latest"
  image: votca/buildenv:${DISTRO}
  stage: build
  cache:
    paths:
      - ccache/
  script:
    - ccache -z
    - j="$(grep -c processor /proc/cpuinfo 2>/dev/null)" || j=0; ((j++))
    - git branch commit_of_build_${CI_BUILD_ID} ${CI_COMMIT_SHA}
    - cd $HOME
    - git clone --recursive https://github.com/votca/votca
    - pushd votca
    - if [[ ( ${CI_COMMIT_REF_NAME} =~ ^for/([^/]*)/.* || ${CI_COMMIT_REF_NAME} =~ ^(stable)$ ) && ${BASH_REMATCH[1]} != master ]]; then
        git checkout -b ${BASH_REMATCH[1]} origin/${BASH_REMATCH[1]} && git submodule update --recursive --init;
      fi
    - git -C ${CI_PROJECT_NAME} fetch ${CI_PROJECT_DIR} commit_of_build_${CI_BUILD_ID}
    - git -C ${CI_PROJECT_DIR} branch -d commit_of_build_${CI_BUILD_ID}
    - git -C ${CI_PROJECT_NAME} checkout -f ${CI_COMMIT_SHA}
    - mkdir -p build
    - pushd build
    - cmake .. -DENABLE_TESTING=ON -DBUILD_XTP=ON -DBUILD_CTP=ON -DBUILD_XTP_MANUAL=ON -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    - make -O -k -j${j} -l${j} VERBOSE=1
    - make test CTEST_OUTPUT_ON_FAILURE=1
    - make install DESTDIR=${PWD}/install && rm -rf ${PWD}/install/usr && rmdir ${PWD}/install
    - sudo make install
    - ccache -s

Debug GCC:
  stage: canary
  variables:
    CC: "gcc"
    CXX: "g++"
    CMAKE_BUILD_TYPE: "Debug"
  extends: .build

None GCC:
  variables:
    CC: "gcc"
    CXX: "g++"
    CMAKE_BUILD_TYPE: ""
  extends: .build

None Clang:
  variables:
    CC: "clang"
    CXX: "clang++"
    CMAKE_BUILD_TYPE: ""
  extends: .build

Debug Clang:
  variables:
    CC: "clang"
    CXX: "clang++"
    CMAKE_BUILD_TYPE: "Debug"
  extends: .build

Release GCC:
  variables:
    CC: "gcc"
    CXX: "g++"
    CMAKE_BUILD_TYPE: "Release"
  extends: .build

Release Clang:
  variables:
    CC: "clang"
    CXX: "clang++"
    CMAKE_BUILD_TYPE: "Release"
  extends: .build

Debug GCC Ubuntu:
  variables:
    CC: "gcc"
    CXX: "g++"
    CMAKE_BUILD_TYPE: "Debug"
    DISTRO: "ubuntu"
  extends: .build

Debug Clang Ubuntu:
  variables:
    CC: "clang"
    CXX: "clang++"
    CMAKE_BUILD_TYPE: "Debug"
    DISTRO: "ubuntu"
  extends: .build

Release GCC Ubuntu:
  variables:
    CC: "gcc"
    CXX: "g++"
    CMAKE_BUILD_TYPE: "Release"
    DISTRO: "ubuntu"
  extends: .build

Release Clang Ubuntu:
  variables:
    CC: "clang"
    CXX: "clang++"
    CMAKE_BUILD_TYPE: "Release"
    DISTRO: "ubuntu"
  extends: .build
