#
# stage-1 take only a few minutes and generally run on the cloud; they do not run the full test suite or external packages.
#
# stage-2 runs on MCS systems and may take 10 to 15 minutes. They run the full test suite but with limited mixture of external packages
#
# stage-3 runs on MCS systems and may take an hour or more. They run the full test suite and heavily test external packages, utilize valgrind etc
#         (not yet implemented)
#
# The stage-(n) tests are only started if all of the stage-(n-1) tests run without error
#   You can limit the testing by using the variable STAGE with value 1 or 2
#
# By default the test branch is merged to master before testing. (not yet implemented)
#   You can limite this by using the variable MERGETOMASTER with value 0 (not yet implemented)
#

stages:
  - stage-1
  - stage-2
  - stage-3
variables:
  GIT_STRATEGY: fetch
  GIT_CLEAN_FLAGS: -ffdxq
  PETSC_OPTIONS: -check_pointer_intensity 0 -error_output_stdout -nox -nox_warning -malloc_dump

#
# The most basic template that most tests will expand upon. In particular merge requests and branch pushes DO NOT trigger testing
#

.test:
  only:
    refs:
#     Set with CI/CD Shedules - New Schedule
      - schedules
#     Set with CI/CD Pipelines - Run Pipeline
      - web

#
#  This provides the basic order of operations and options template for cloud based stage 1 tests.
#  Not all test-short need to follow this template but most will.
#

.stage-1:
  extends: .test
  stage: stage-1
  image: jedbrown/mpich-ccache
  before_script:
    - echo nproc=$(nproc)
    - cat /proc/cpuinfo
    - export CCACHE_COMPILERCHECK=content
    - export CCACHE_DIR=$(pwd)/.ccache
    - ccache --show-stats
    - ccache --zero-stats
  script:
    - ${PYTHON} ./configure --with-debugging=0 COPTFLAGS='-O -march=native' CXXOPTFLAGS='-O -march=native' FOPTFLAGS='-O -march=native' ${CONFIG_OPTS}
    - make CFLAGS=-Werror
    - make allgtests-tap search="${TEST_SEARCH}" TIMEOUT=300
  after_script:
    - CCACHE_DIR=$(pwd)/.ccache ccache --show-stats
  artifacts:
    reports:
      junit: arch-*/tests/testresults.xml
  cache:
    paths:
      - .ccache/
    key: "${CI_JOB_NAME}"

#
# The following tests run on the cloud as part of test-short.
#

mpich-cxx-py3:
  extends: .stage-1
  variables:
    PYTHON: python3
    CONFIG_OPTS: --with-mpi-dir=/usr/local --with-clanguage=cxx --with-fc=0
    TEST_SEARCH: snes_tutorials-ex48%

uni-complex-float-int64:
  extends: .stage-1
  variables:
    PYTHON: python3
    CONFIG_OPTS: <
      --with-mpi=0
      --with-scalar-type=complex --with-precision=single --with-64-bit-indices
    TEST_SEARCH: ts_tutorials-ex11_adv_2d_quad_%

c89-mlib-static-py2:
  extends: .stage-1
  image: jedbrown/mpich-ccache:python2
  variables:
    PYTHON: python2
    CONFIG_OPTS: <
      --with-mpi-dir=/usr/local
      --with-single-library=0 --with-shared-libraries=0 'CFLAGS=-std=c89 -pedantic -Wno-long-long -Wno-overlength-strings'
    TEST_SEARCH: snes_tutorials-ex48%

checksource:
  extends: .test
  image: jedbrown/mpich-ccache
  stage: stage-1
  script:
    - python3 ./configure --with-mpi=0 --with-fc=0 --with-cxx=0
    - make checkbadSource SHELL=bash

#
# This provides the basic order of operations and options template for stage-2 tests.
# Not all stage-2 need to follow this template, but most will.
#
.stage-2:
  extends: .test
  stage: stage-2
  only:
    variables:
      - $STAGE != "1"
  script:
    - ./config/examples/${TEST_ARCH}.py
    - make
    - make check
    - make cleantest allgtests-tap TIMEOUT=300
  artifacts:
    reports:
      junit: ${TEST_ARCH}/tests/testresults.xml

#
# The following tests run on MCS systems as part of stage-2.
#
# The tags variable used in the tests below connects the particular test with the runners
# listed on the left hand side of https://gitlab.com/petsc/petsc/-/settings/ci_cd.
# For example the test linux-gcc-complex-opt which extends .linux_mcs_test will run on
# any runner that has the tag (in a blue box beneath it) of mcs-linux.
#

.mcs_test:
  extends: .stage-2

.linux_mcs_test:
  extends: .mcs_test
  tags:
    - os:linux,name:pj02
  before_script:
    - hostname
    - grep PRETTY_NAME /etc/os-release
    - echo $(nproc)
    - ccache --zero-stats
  after_script:
    - ccache --show-stats

.freebsd_mcs_test:
  extends: .mcs_test
  tags:
    - os:fbsd,name:petsc-fbsd
  before_script:
    - hostname
    - freebsd-version
    - echo $(sysctl -n hw.ncpu)
    - ccache --zero-stats
  after_script:
    - ccache --show-stats

.osx_mcs_test:
  extends: .mcs_test
  tags:
    - os:osx,name:jpro
  variables:
#  the stages definition defines the first line of PETSC_OPTIONS below. Do they have to be repeated here or can one use
#  $PETSC_OPTIONS or another option so that they do not need to be repeated?
    PETSC_OPTIONS: <
      -check_pointer_intensity 0 -error_output_stdout -nox -nox_warning -malloc_dump
      -saws_port_auto_select -saws_port_auto_select_silent -vecscatter_mpi1 false -options_left false
  before_script:
    - hostname
    - sw_vers -productVersion
    - echo $(sysctl -n hw.ncpu)
    - ccache --zero-stats
  after_script:
    - ccache --show-stats

.opensolaris_mcs_test:
  extends: .mcs_test
  tags:
    - os:opensolaris,name:n-gage
  before_script:
    - hostname
    - uname -a
    - echo $(nproc)

#####         These are the specific tests that map to the scripts in config/examples

linux-gcc-complex-opt:
  extends: .linux_mcs_test
  variables:
    TEST_ARCH: arch-ci-linux-gcc-complex-opt

linux-gcc-pkgs-opt:
  extends: .linux_mcs_test
  variables:
    TEST_ARCH: arch-ci-linux-gcc-pkgs-opt

linux-gcc-quad-64idx-dbg:
  extends: .linux_mcs_test
  variables:
    TEST_ARCH: arch-ci-linux-gcc-quad-64idx-dbg

freebsd-c-single-opt:
  extends: .freebsd_mcs_test
  variables:
    TEST_ARCH: arch-ci-freebsd-c-single-opt

freebsd-cxx-cmplx-64idx-dbg:
  extends: .freebsd_mcs_test
  variables:
    TEST_ARCH: arch-ci-freebsd-cxx-cmplx-64idx-dbg

osx-cxx-pkgs-opt:
  extends: .osx_mcs_test
  variables:
    TEST_ARCH: arch-ci-osx-cxx-pkgs-opt

opensolaris-cmplx-pkgs-dbg:
  extends: .opensolaris_mcs_test
  variables:
    TEST_ARCH: arch-ci-opensolaris-cmplx-pkgs-dbg

osx-barry:
  extends: .mcs_test
  tags:
    - os:osx
  variables:
#  the stages definition defines the first line of PETSC_OPTIONS below. Do they have to be repeated here or can one use
#  $PETSC_OPTIONS or another option so that they do not need to be repeated?
    PETSC_OPTIONS: <
      -check_pointer_intensity 0 -error_output_stdout -nox -nox_warning -malloc_dump
      -vecscatter_mpi1 false -options_left false
    TEST_ARCH: arch-ci-osx-prefix
  before_script:
    - hostname
    - sw_vers -productVersion
    - echo $(sysctl -n hw.ncpu)
    - ccache --zero-stats
  after_script:
    - ccache --show-stats

#
# The following tests are experimental; more tests by users at other sites may be added below this.  Experimental test
# that fail produce a warning, but do not block execution of a pipeline.
#

.test-experimental:
  extends: .test
  allow_failure: true

