# 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:
      - /deps-cmake.yaml
      - /deps-rocm.yaml
      - /gpus-rocm.yaml

stages:
  - build
  - test
  - benchmark

variables:
  PACKAGE_DIR: $BUILD_DIR/package

.deps:doc:
  stage: test:doc
  before_script:
    - $SUDO_CMD apt update -qq
    - $SUDO_CMD apt install -y -qq doxygen

.cmake-latest:
  extends:
    - .deps:rocm
    - .deps:cmake-latest
  before_script:
    - !reference [".deps:rocm", before_script]
    - !reference [".deps:cmake-latest", before_script]

.cmake-minimum:
  extends:
    - .deps:rocm
    - .deps:cmake-minimum
  before_script:
    - !reference [".deps:rocm", before_script]
    - !reference [".deps:cmake-minimum", before_script]

.build:common:
  stage: build
  extends: .gpus:rocm-gpus
  script:
    - mkdir -p $BUILD_DIR
    - cd $BUILD_DIR
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_CXX_FLAGS="-Wall -Wextra -Werror"
      -D CMAKE_BUILD_TYPE=Release
      -D BUILD_TEST=ON
      -D BUILD_EXAMPLE=ON
      -D BUILD_BENCHMARK=OFF
      -D AMDGPU_TARGETS=$GPU_TARGETS
      -D AMDGPU_TEST_TARGETS=$GPU_TARGETS
      -S $CI_PROJECT_DIR
      -B $BUILD_DIR
    - cmake --build $BUILD_DIR
  artifacts:
    paths:
    - $BUILD_DIR/test/test_*
    - $BUILD_DIR/test/rocprim/test_*
    - $BUILD_DIR/test/CTestTestfile.cmake
    - $BUILD_DIR/test/rocprim/CTestTestfile.cmake
    - $BUILD_DIR/gtest/
    - $BUILD_DIR/CMakeCache.txt
    - $BUILD_DIR/CTestTestfile.cmake
    expire_in: 2 weeks

build:cmake-latest:
  stage: build
  extends:
    - .cmake-latest
    - .build:common

build:cmake-minimum:
  stage: build
  needs:
    - job: build:cmake-latest
      artifacts: false
  extends:
    - .cmake-minimum
    - .build:common

build:package:
  stage: build
  extends:
    - .cmake-minimum
  script:
    - mkdir -p $PACKAGE_DIR
    - cd $PACKAGE_DIR
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_BUILD_TYPE=Release
      -B $PACKAGE_DIR
      $CI_PROJECT_DIR
    - $SUDO_CMD cmake
      --build $PACKAGE_DIR
      --target install
    - cpack -G "DEB;ZIP"
    - mkdir -p ../package_test && cd ../package_test
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_BUILD_TYPE=Release
      $CI_PROJECT_DIR/test/extra
    - cmake --build .
    - ./test_rocprim_package
    - ctest --output-on-failure --repeat-until-fail 2
  artifacts:
    paths:
      - $PACKAGE_DIR/rocprim*.deb
      - $PACKAGE_DIR/rocprim*.zip
    expire_in: 2 weeks

build:benchmark:
  stage: build
  extends:
    - .cmake-latest
    - .gpus:rocm-gpus
  only:
    - ci-benchmark-extend
    - internal_benchmark
    - develop_stream
    - develop
    - master
  script:
    - mkdir build
    - cd build
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_BUILD_TYPE=Release
      -D BUILD_TEST=OFF
      -D BUILD_EXAMPLE=OFF
      -D BUILD_BENCHMARK=ON
      -D AMDGPU_TARGETS=$GPU_TARGETS
      ..
    - cmake --build .
  artifacts:
    paths:
      - build/benchmark/*
      - build/googlebenchmark/
    expire_in: 2 weeks

test:
  stage: test
  extends:
    - .cmake-latest
    - .gpus:rocm
  needs:
    - build:cmake-latest
  script:
    - cd $BUILD_DIR
    - cmake
      -D CMAKE_PREFIX_PATH=/opt/rocm
      -P $CI_PROJECT_DIR/cmake/GenerateResourceSpec.cmake
    - cat ./resources.json
    - ctest
      --output-on-failure
      --repeat-until-fail 2
      --tests-regex "hip|$GPU_TARGET"
      --resource-spec-file ./resources.json
      --parallel $PARALLEL_JOBS

test:deb:
  stage: test
  needs:
    - build:package
  extends:
    - .cmake-minimum
  script:
    - $SUDO_CMD dpkg -i $PACKAGE_DIR/rocprim*.deb
    - mkdir -p package_test && cd package_test
    - cmake
      -G Ninja
      -D CMAKE_CXX_COMPILER=hipcc
      -D CMAKE_BUILD_TYPE=Release
      $CI_PROJECT_DIR/test/extra
    - cmake --build .
    - ctest --output-on-failure --repeat-until-fail 2
    - $SUDO_CMD dpkg -r rocprim

test:doc:
  stage: test
  needs: []
  before_script:
    - apt-get update -qq
    - apt-get install -y -qq doxygen
  script:
    - cd doc
    - doxygen Doxyfile

benchmark_view:
  stage: benchmark
  extends:
    - .cmake-latest
    - .gpus:rocm
  needs:
    - build:benchmark
  when: manual
  only:
    - ci-benchmark-extend
    - internal_benchmark
    - develop_stream
    - develop
    - master
  script:
    - cmake
      -D BENCHMARK_BINARY_DIR=build/benchmark
      -D BENCHMARK_OUTPUT_DIR=.
      -P ${CI_PROJECT_DIR}/.gitlab/RunBenchmarks.cmake
