# MIT License
#
# Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

include:
    - project: amd/ci-templates
      ref: main
      file:
        - /defaults.yaml
        - /deps-cmake.yaml
        - /deps-rocm.yaml
        - /deps-nvcc.yaml
        - /gpus-rocm.yaml
        - /gpus-nvcc.yaml

stages:
  - build
  - test
  - benchmark

# hipCUB with rocPRIM backend
.rocm:
  variables:
    ROCPRIM_GIT_BRANCH: "develop_stream"
  extends:
    - .deps:rocm
    - .deps:cmake-minimum
  before_script:
    - !reference [".deps:rocm", before_script]
    - !reference [".deps:cmake-minimum", before_script]
    # Install rocPRIM from git
    - BRANCH_NAME="$ROCPRIM_GIT_BRANCH"
    - if [ "$CI_COMMIT_BRANCH" = develop -o "$CI_COMMIT_BRANCH" = master ]; then BRANCH_NAME="$CI_COMMIT_BRANCH"
    - fi;
    - git clone -b "$BRANCH_NAME" --depth 1 https://gitlab-ci-token:${CI_JOB_TOKEN}@${ROCPRIM_GIT_URL}
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_BUILD_TYPE=Release
      -D BUILD_TEST=OFF
      -D BUILD_EXAMPLE=OFF
      -D ROCM_DEP_ROCMCORE=OFF
      -B rocPRIM/build
      -S rocPRIM
    - cd rocPRIM/build
    - cpack -G "DEB"
    - $SUDO_CMD dpkg -i rocprim*.deb
    - cd ../..

build:rocm:
  extends:
    - .rocm
    - .gpus:rocm-gpus
  stage: build
  script:
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_BUILD_TYPE=Release
      -D BUILD_TEST=ON
      -D BUILD_EXAMPLE=ON
      -D AMDGPU_TARGETS="$GPU_TARGETS"
      -B build
      -S ./
    - cmake --build build
    - cd build
    - cpack -G "DEB;ZIP"
  artifacts:
    paths:
      - build/test/hipcub/test_*
      - build/test/CTestTestfile.cmake
      - build/test/hipcub/CTestTestfile.cmake
      - build/gtest/
      - build/CMakeCache.txt
      - build/CTestTestfile.cmake
      - build/hipcub*.deb
      - build/hipcub*.zip
    expire_in: 2 weeks

build:rocm-benchmark:
  extends:
    - .rocm
    - .gpus:rocm-gpus
  stage: build
  only:
    - develop_stream
    - develop
    - master
  script:
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_BUILD_TYPE=Release
      -D BUILD_BENCHMARK=ON
      -D AMDGPU_TARGETS="$GPU_TARGETS"
      -B build
      -S ./
    - cmake --build build
  artifacts:
    paths:
      - build/benchmark/*
      - build/googlebenchmark/
    expire_in: 2 weeks

test:rocm:
  stage: test
  needs:
    - build:rocm
  extends:
    - .rocm
    - .gpus:rocm
  script:
    - cd build
    - ctest --output-on-failure --repeat-until-fail 2

.benchmark:
  stage: benchmark
  when: manual
  only:
    - develop_stream
    - develop
    - master
  script:
    - cmake
      -D BENCHMARK_BINARY_DIR=build/benchmark
      -P ${CI_PROJECT_DIR}/.gitlab/cmake-run-benchmarks.txt

benchmark:rocm:
  extends:
    - .rocm
    - .benchmark
    - .gpus:rocm
  needs:
    - build:rocm-benchmark

.test_package:
  script:
    - cmake
      -G Ninja
      -D rocprim_DIR="/opt/rocm/rocprim"
      -S test/extra
      -B ../package_test
    - cmake --build ../package_test
    - cd ../package_test
    - ctest --output-on-failure --repeat-until-fail 2

.test:package:
  script:
    - cd build
    - $SUDO_CMD dpkg -i hipcub*.deb
    - cd ..
    - export CXX
    - !reference [".test_package", script]
    - $SUDO_CMD dpkg -r rocprim-dev hipcub-dev

.test:install:
  script:
    - export CXX
    - cmake
      -G Ninja
      -D BUILD_TEST=OFF
      -S .
      -B build_only_install
    # Preserve $PATH when sudoing
    - $SUDO_CMD env PATH="$PATH" cmake --install build_only_install
    - !reference [".test_package", script]

test:rocm_package:
  stage: test
  needs:
    - build:rocm
  variables:
    CXX: hipcc
  extends:
    - .rocm
    - .test:package

test:rocm_install:
  stage: test
  needs: []
  variables:
    CXX: hipcc
  extends:
   - .rocm
   - .test:install

# hipCUB with CUB backend
.nvcc:
  extends:
    - .deps:nvcc
    - .deps:cmake-minimum
  before_script:
    - !reference [".deps:nvcc", before_script]
    - !reference [".deps:cmake-minimum", before_script]

build:nvcc:
  stage: build
  extends: .nvcc
  script:
    - cmake
      -G Ninja
      -D CMAKE_BUILD_TYPE=Release
      -D BUILD_TEST=ON
      -D BUILD_EXAMPLE=ON
      -B build
      -S .
    - cmake --build build
    - cd build
    - cpack -G "DEB;ZIP"
  artifacts:
    paths:
      - build/test/hipcub/test_*
      - build/test/CTestTestfile.cmake
      - build/test/hipcub/CTestTestfile.cmake
      - build/gtest/
      - build/CMakeCache.txt
      - build/CTestTestfile.cmake
      - build/hipcub*.deb
      - build/hipcub*.zip
    expire_in: 2 weeks

build:nvcc-benchmark:
  stage: build
  extends: .nvcc
  only:
    - develop_stream
    - develop
    - master
  script:
    - cmake
      -G Ninja
      -D CMAKE_BUILD_TYPE=Release
      -D BUILD_BENCHMARK=ON
      -D CMAKE_CXX_COMPILER=g++-8
      -D CMAKE_C_COMPILER=gcc-8
      -B build
      -S .
    - cmake --build build
  artifacts:
    paths:
      - build/benchmark/*
      - build/googlebenchmark/
    expire_in: 2 weeks

test:nvcc:
  stage: test
  needs:
    - build:nvcc
  extends:
    - .nvcc
    - .gpus:nvcc
  before_script:
    # Must duplicate before_script of .nvcc to add the before_script of .gpus:nvcc
    - !reference [".deps:nvcc", before_script]
    - !reference [".deps:cmake-minimum", before_script]
    - !reference [".gpus:nvcc", before_script]
  script:
    - cd build
    - ctest --output-on-failure --repeat-until-fail 2

benchmark:nvcc:
  needs:
    - build:nvcc-benchmark
  extends:
    - test:nvcc
    - .benchmark

test:nvcc_package:
  stage: test
  needs:
    - build:nvcc
  extends:
    - .nvcc
    - .test:package

test:nvcc_install:
  stage: test
  needs: []
  extends:
    - .nvcc
    - .test:install

test:doc:
  stage: test
  image: sphinxdoc/sphinx-latexpdf
  needs: []
  before_script:
    - apt-get update
    - apt-get install -y -qq doxygen
    - pip3 install sphinx_rtd_theme breathe exhale
  script:
    - ./docs/run_doc.sh
