##============================================================================
##  The contents of this file are covered by the Viskores license. See
##  LICENSE.txt for details.
##
##  By contributing to this file, all contributors agree to the Developer
##  Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
##============================================================================

##=============================================================================
##
##  Copyright (c) Kitware, Inc.
##  All rights reserved.
##  See LICENSE.txt for details.
##
##  This software is distributed WITHOUT ANY WARRANTY; without even
##  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
##  PURPOSE.  See the above copyright notice for more information.
##
##=============================================================================

.docker_image: &docker_image
  variables:
    CCACHE_DIR: "$CI_PROJECT_DIR/.ccache"
    CCACHE_RESHARE: "true"
    CTEST_TIMEOUT: 1200
    GIT_SUBMODULE_STRATEGY: recursive
  image:
    name: ubuntu
    pull_policy: always

.almalinux8: &almalinux8
  image:
    name: "ghcr.io/viskores/viskores:ci-almalinux8-20250321"
  extends:
    - .docker_image

.ubuntu2004: &ubuntu2004
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2004-20250320"
  extends:
    - .docker_image

.ubuntu2004_doxygen: &ubuntu2004_doxygen
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2004_doxygen-20250320"
  extends:
    - .docker_image

.ubuntu2004_cuda118: &ubuntu2004_cuda118
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2004_cuda11.8-20250320"
  extends:
    - .docker_image

.ubuntu2004_kokkos: &ubuntu2004_kokkos
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2004_kokkos-20250320"
  extends:
    - .docker_image

.ubuntu2204: &ubuntu2204
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2204-20250320"
  extends:
    - .docker_image

.ubuntu2204_cuda122: &ubuntu2204_cuda122
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2204_cuda12.2-20250320"
  extends:
    - .docker_image

.ubuntu2204_cuda122_kokkos: &ubuntu2204_cuda122_kokkos
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2204_kokkos_cuda-20250320"
  extends:
    - .docker_image

.ubuntu2204_hip_kokkos: &ubuntu2204_hip_kokkos
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2204_kokkos_hip-20250320"
  extends:
    - .docker_image

.ubuntu2404: &ubuntu2404
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2404-20250320"
  extends:
    - .docker_image

.ubuntu2404_cuda130: &ubuntu2404_cuda130
  image:
    name: "ghcr.io/viskores/viskores:ci-ubuntu2404_cuda13.0-20250820"
  extends:
    - .docker_image

.opensuse: &opensuse
  image:
    name: "ghcr.io/viskores/viskores:ci-opensuse-20250320"
  extends:
    - .docker_image

.run_automatically: &run_automatically
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - if: '$CI_MERGE_REQUEST_ID'
      when: on_success
    - if: '$CI_PROJECT_PATH == "viskores/viskores"'
      when: on_success
    - when: never

.run_only_merge_requests: &run_only_merge_requests
  rules:
    - if: '$CI_MERGE_REQUEST_ID && $CI_PROJECT_PATH == "viskores/viskores"'
      when: on_success
    - when: never

.run_scheduled: &run_scheduled
  rules:
    - if: '$CI_PROJECT_PATH == "viskores/viskores" && $CI_COMMIT_TAG'
      when: on_success
    - if: '$CI_PROJECT_PATH == "viskores/viskores" && $CI_PIPELINE_SOURCE == "schedule"'
      when: on_success
    - when: never

.run_upstream_branches: &run_upstream_branches
  rules:
    - if: '$CI_PROJECT_PATH == "viskores/viskores" && $CI_MERGE_REQUEST_ID == null'
      when: on_success
    - when: never

.uo_cpu_tags: &uo_cpu_tags
  tags:
    - hpsf
    - hpsf-x86_64

# General Longer Term Tasks:
# - Setup clang tidy as sub-pipeline
# - Setup a machine to replicate the issue in https://gitlab.kitware.com/viskores/viskores/-/issues/447
#   Note: Centos7 doesn't work as they ship separate standard library
#   headers for each version. We will have to figure out something else
#   like using spack or building llvm/clang from source

stages:
  - build
  - test

.use_minimum_supported_cmake:
  variables:
    CMAKE_VERSION: "3.13.5"

.warning_policy:
  allow_failure:
    exit_codes: [ 47 ]

.install_cmake: &install_cmake |
  export PATH=$PWD/.gitlab/cmake/bin:$PATH
  .gitlab/ci/config/cmake.sh "$CMAKE_VERSION"
  cmake --version

.cmake_build_linux: &cmake_build_linux
  stage: build
  timeout:  2 hours
  interruptible: true
  cache:
    key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
    fallback_keys:
      - "$CI_JOB_NAME-main"
    paths:
      - "$CI_PROJECT_DIR/.ccache/"
  before_script:
    - *install_cmake
    - "cmake -VV -P .gitlab/ci/config/ninja.cmake"
    - export PATH="${PWD}/.gitlab:${PATH}"
    - "cmake -VV -P .gitlab/ci/config/gitlab-bridge-set-vars.cmake"
    - "CCACHE_INSTALL_DIR=${PWD}/.gitlab cmake -V -P .gitlab/ci/config/ccache.cmake"
    - export PATH="${PWD}/.gitlab/ccache:${PATH}"
    - ccache -z
    - ccache -p
    - ccache -s
    - .gitlab/ci/config/google_benchmarks.sh
    - "cmake --version"
    - "cmake -V -P .gitlab/ci/config/fetch_vtkm_tags.cmake"
    - "cmake -V -P .gitlab/ci/config/gitlab_ci_setup.cmake"
    - "ctest -VV -S .gitlab/ci/ctest_configure.cmake"
  script:
    - "ctest -VV -S .gitlab/ci/ctest_build.cmake"
    - "ctest -VV -S .gitlab/ci/ctest_build_notes.cmake"
    - "[ -z $NO_TESTING ] && ctest $CTEST_TIMEOUT -VV -S .gitlab/ci/ctest_test.cmake"
    - cmake -P .gitlab/ci/check_warnings.cmake || exit 47
  after_script:
    - export PATH="${PWD}/.gitlab/ccache:${PATH}"
    - ccache -s
  extends:
    - .warning_policy

.cmake_memcheck_linux: &cmake_memcheck_linux
  stage: test
  timeout:  2 hours
  interruptible: true
  before_script:
    - *install_cmake
  script:
    - "ctest -VV -S .gitlab/ci/ctest_memcheck.cmake"
  artifacts:
    expire_in: 24 hours
    when: always
    paths:
      # The generated regression testing images
      - build/*.png
      - build/*.pnm
      - build/*.pmm
      - build/junit.xml
    reports:
      junit: build/junit.xml

.cmake_build_artifacts: &cmake_build_artifacts
  artifacts:
    expire_in: 24 hours
    when: always
    paths:
      # The artifacts of the build.
      - build/bin/
      - build/include/
      - build/viskores/thirdparty/viskores/viskoresdiy/
      - build/lib/
      - build/config/

      # CTest and CMake install files.
      # Note: this also captures our CIState.cmake file
      - build/CMakeCache.txt
      - build/compile_num_warnings.log
      - build/**/*.cmake
      - build/Testing/

      # CDash files.
      - build/DartConfiguration.tcl

      # Users guide
      - build/docs/users-guide/html
      - build/docs/users-guide/latex

.cmake_test_artifacts: &cmake_test_artifacts
  artifacts:
    expire_in: 24 hours
    when: always
    paths:
      # The generated regression testing images
      - build/**/*.png
      - build/**/*.pnm
      - build/**/*.pmm
      - build/junit.xml
    reports:
      junit: build/junit.xml

include:
  - local: '/.gitlab/ci/doxygen.yml'
  - local: '/.gitlab/ci/almalinux8.yml'
  - local: '/.gitlab/ci/sync-spack.yml'
  - local: '/.gitlab/ci/ubuntu2004.yml'
  - local: '/.gitlab/ci/ubuntu2204.yml'
  - local: '/.gitlab/ci/ubuntu2404.yml'
