name: github-Linux

on:
  push:
    branches:
      - develop
  pull_request:
    paths-ignore:
    - '**/*.md'
    types: [ opened, reopened, synchronize ]

concurrency:
  group: ${ {github.event_name }}-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{github.event_name == 'pull_request'}}

permissions: read-all

jobs:
  CI:
    continue-on-error: true
    strategy:
      matrix:
        distro: ['fedora:latest', 'fedora:rawhide', 'ubuntu:latest']
        cxx: ['g++', 'clang++']
        cxx_extra_flags: ['']
        cmake_build_type: ['Release', 'Debug']
        backend: ['OPENMP']
        clang-tidy: ['']
        include:
          - distro: 'ubuntu:intel'
            cxx: 'icpc'
            cxx_extra_flags: '-diag-disable=177,10441'
            cmake_build_type: 'Release'
            backend: 'OPENMP'
          - distro: 'ubuntu:intel'
            cxx: 'icpc'
            cxx_extra_flags: '-diag-disable=177,10441'
            cmake_build_type: 'Debug'
            backend: 'OPENMP'
          - distro: 'ubuntu:intel'
            cxx: 'icpx'
            cxx_extra_flags: '-fp-model=precise -Wno-pass-failed'
            cmake_build_type: 'Release'
            backend: 'OPENMP'
          - distro: 'ubuntu:intel'
            cxx: 'icpx'
            cxx_extra_flags: '-fp-model=precise -Wno-pass-failed'
            cmake_build_type: 'Debug'
            backend: 'OPENMP'
          - distro: 'ubuntu:latest'
            cxx: 'clang++'
            cxx_extra_flags: '-fsanitize=address'
            extra_linker_flags: '-fsanitize=address'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'THREADS'
            clang-tidy: '-DCMAKE_CXX_CLANG_TIDY="clang-tidy;-warnings-as-errors=*"'
          - distro: 'ubuntu:latest'
            cxx: 'clang++'
            cxx_extra_flags: '-fsanitize=address'
            extra_linker_flags: '-fsanitize=address'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'SERIAL'
          - distro: 'ubuntu:latest'
            cxx: 'g++'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'THREADS'
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/kokkos/ci-containers/${{ matrix.distro }}
    steps:
      - name: Checkout desul
        uses: actions/checkout@v4
        with:
          repository: desul/desul
          ref: 477da9c8f40f8db369c28dd3f93a67e376d8511b
          path: desul
      - name: Install desul
        working-directory: desul
        run: |
          git submodule init
          git submodule update
          mkdir build
          cd build
          cmake -DDESUL_ENABLE_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/desul-install ..
          sudo cmake --build . --target install --parallel 2
      - name: Checkout code
        uses: actions/checkout@v4
      - uses: actions/cache@v4
        with:
          path: ~/.cache/ccache
          key: kokkos-${{ matrix.distro }}-${{ matrix.cxx }}-${{ matrix.cmake_build_type }}-${{ matrix.openmp }}-${{ github.ref }}-${{ github.sha }}
          restore-keys: kokkos-${{ matrix.distro }}-${{ matrix.cxx }}-${{ matrix.cmake_build_type }}-${{ matrix.openmp }}-${{ github.ref }}
      - name: maybe_use_flang_new
        if: ${{ matrix.cxx == 'clang++' && startsWith(matrix.distro,'fedora:') }}
        run: echo "FC=flang-new" >> $GITHUB_ENV
      - name: maybe_use_external_gtest
        if: ${{ matrix.distro == 'ubuntu:latest' }}
        run: sudo apt-get update && sudo apt-get install -y libgtest-dev
      - name: maybe_install_clang_tidy
        if: ${{ matrix.clang-tidy != '' }}
        run: sudo apt-get update && sudo apt-get install -y clang-tidy
      - name: Configure Kokkos
        run: |
          cmake -B builddir \
            -DCMAKE_INSTALL_PREFIX=/usr \
            ${{ matrix.clang-tidy }} \
            -Ddesul_ROOT=/usr/desul-install/ \
            -DKokkos_ENABLE_DESUL_ATOMICS_EXTERNAL=ON \
            -DKokkos_ENABLE_HWLOC=ON \
            -DKokkos_ENABLE_${{ matrix.backend }}=ON \
            -DKokkos_ENABLE_TESTS=ON \
            -DKokkos_ENABLE_BENCHMARKS=ON \
            -DKokkos_ENABLE_EXAMPLES=ON \
            -DKokkos_ENABLE_DEPRECATED_CODE_4=ON \
            -DKokkos_ENABLE_DEPRECATION_WARNINGS=OFF \
            -DKokkos_ENABLE_COMPILER_WARNINGS=ON \
            -DKokkos_ENABLE_IMPL_MDSPAN=ON \
            -DCMAKE_CXX_FLAGS="-Werror ${{ matrix.cxx_extra_flags }}" \
            -DCMAKE_EXE_LINKER_FLAGS="${{ matrix.extra_linker_flags }}" \
            -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} \
            -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            -DCMAKE_BUILD_TYPE=${{ matrix.cmake_build_type }}
      - name: Build
        run: |
          ccache -z
          cmake --build builddir --parallel 2
          ccache -s
      - name: Tests
        working-directory: builddir
        run: ctest --output-on-failure
      - name: Test linking against build dir
        if: ${{ matrix.cxx_extra_flags != '-fsanitize=address' }}
        working-directory: example/build_cmake_installed
        run: |
          cmake -B builddir_buildtree -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DKokkos_ROOT=../../builddir
          cmake --build builddir_buildtree
          cmake --build builddir_buildtree --target test
      - name: Test DESTDIR Install
        run: DESTDIR=${PWD}/install cmake --build builddir --target install && rm -rf ${PWD}/install/usr && rmdir ${PWD}/install
      - name: Install
        run: sudo cmake --build builddir --target install
      - name: Test install
        if: ${{ matrix.cxx_extra_flags != '-fsanitize=address' }}
        working-directory: example/build_cmake_installed
        run: |
          cmake -B builddir -DCMAKE_CXX_COMPILER=${{ matrix.cxx }}
          cmake --build builddir
          cmake --build builddir --target test
