name: github-Linux

on:
  workflow_call:

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

permissions: read-all

jobs:
  CI:
    continue-on-error: true
    strategy:
      matrix:
        arch: ['x64']
        distro: ['fedora:latest', 'ubuntu:latest']
        cxx: ['g++', 'clang++']
        cxx_extra_flags: ['']
        cmake_build_type: ['Release', 'Debug']
        backend: ['OPENMP']
        cmake_extra_opts: ['-DCMAKE_CXX_CLANG_TIDY="clang-tidy;-warnings-as-errors=*"']
        stdcxx: [20]
        include:
          - arch: 'x64'
            distro: 'ubuntu:intel'
            cxx: 'icpx'
            cxx_extra_flags: '-fp-model=precise -Wno-pass-failed -fsanitize=address,undefined -fno-sanitize=function -fno-sanitize=vptr -fno-sanitize-recover=all'
            extra_linker_flags: '-fsanitize=address,undefined -fno-sanitize=function -fno-sanitize=vptr -fno-sanitize-recover=all'
            cmake_build_type: 'Release'
            backend: 'OPENMP'
            stdcxx: '20'
          - arch: 'x64'
            distro: 'ubuntu:intel'
            cxx: 'icpx'
            cxx_extra_flags: '-fp-model=precise -Wno-pass-failed'
            cmake_build_type: 'Debug'
            backend: 'OPENMP'
            stdcxx: '20'
          - arch: 'x64'
            distro: 'ubuntu:latest'
            cxx: 'clang++'
            cxx_extra_flags: '-fsanitize=address,undefined -fno-sanitize=function -fno-sanitize=vptr -fno-sanitize-recover=all'
            extra_linker_flags: '-fsanitize=address,undefined -fno-sanitize=function -fno-sanitize=vptr -fno-sanitize-recover=all'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'THREADS'
            stdcxx: '23'
          - arch: 'x64'
            distro: 'ubuntu:latest'
            cxx: 'clang++'
            cxx_extra_flags: '-ffinite-math-only -Wno-nan-infinity-disabled -fsanitize=address,undefined -fno-sanitize=function -fno-sanitize=vptr -fno-sanitize-recover=all'
            extra_linker_flags: '-fsanitize=address,undefined -fno-sanitize=function -fno-sanitize=vptr -fno-sanitize-recover=all'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'SERIAL'
            stdcxx: '23'
          - arch: 'x64'
            distro: 'ubuntu:latest'
            cxx: 'g++'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'THREADS'
            stdcxx: '23'
          - arch: 'arm64'
            distro: 'ubuntu:latest'
            cxx: 'g++'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'OPENMP'
            stdcxx: '23'
            cmake_extra_opts: '-DKokkos_ARCH_NATIVE=ON -DCMAKE_CXX_CLANG_TIDY="clang-tidy;-warnings-as-errors=*"'
          - arch: 'arm64'
            distro: 'ubuntu:latest'
            cxx: 'g++'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'THREADS'
            stdcxx: '20'
            cmake_extra_opts: '-DKokkos_ARCH_NATIVE=ON -DCMAKE_CXX_CLANG_TIDY="clang-tidy;-warnings-as-errors=*"'
          - arch: 'arm64'
            distro: 'ubuntu:latest'
            cxx: 'g++'
            cmake_build_type: 'Release'
            backend: 'SERIAL'
            stdcxx: '20'
            cmake_extra_opts: '-DKokkos_ARCH_NATIVE=ON -DCMAKE_CXX_CLANG_TIDY="clang-tidy;-warnings-as-errors=*"'
          - arch: 'x86'
            distro: 'ubuntu:latest'
            cxx: 'g++'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'OPENMP'
            stdcxx: '20'
            cxx_extra_flags: '-m32'
            cmake_extra_opts: '-DKokkos_ENABLE_HWLOC=OFF'
          - arch: 'x64'
            distro: 'ubuntu:latest'
            cxx: 'g++-12'
            cmake_build_type: 'RelWithDebInfo'
            backend: 'OPENMP'
            stdcxx: '20'
    runs-on: ${{ matrix.arch == 'arm64' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }}
    container:
      image: ghcr.io/kokkos/ci-containers/${{ matrix.distro }}
    steps:
      - name: Checkout desul
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          repository: desul/desul
          ref: 22931326247c9333cc909628004d75ee5de99fa2
          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@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
      - uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
        with:
          path: ~/.cache/ccache
          key: kokkos-${{ matrix.arch }}-${{ matrix.distro }}-${{ matrix.cxx }}-${{ matrix.stdcxx }}-${{ matrix.cmake_build_type }}-${{ matrix.backend }}-${{ github.ref }}-${{ github.sha }}
          restore-keys: kokkos-${{ matrix.arch }}${{ matrix.distro }}-${{ matrix.cxx }}-${{ matrix.stdcxx }}-${{ matrix.cmake_build_type }}-${{ matrix.backend }}-${{ github.ref }}
      - name: maybe_use_flang
        if: ${{ matrix.cxx == 'clang++' }}
        run: |
          if type flang; then
            echo "flang found"
            echo "FC=flang" >> $GITHUB_ENV
          fi
      - name: Configure Kokkos
        run: |
          cmake -B builddir \
            -DCMAKE_INSTALL_PREFIX=/usr \
            -DBUILD_SHARED_LIBS=ON \
            -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=${{ (matrix.cmake_build_type != 'Debug') && 'ON' || 'OFF' }} \
            -DKokkos_ENABLE_EXAMPLES=ON \
            -DKokkos_ENABLE_DEPRECATED_CODE_4=ON \
            -DKokkos_ENABLE_COMPILER_WARNINGS=ON \
            -DCMAKE_CXX_FLAGS="-Werror ${{ matrix.cxx_extra_flags }}" \
            -DCMAKE_CXX_STANDARD=${{ matrix.stdcxx }} \
            -DCMAKE_EXE_LINKER_FLAGS="${{ matrix.extra_linker_flags }}" \
            -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} \
            -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            ${{ matrix.cmake_extra_opts }} \
            -DCMAKE_BUILD_TYPE=${{ matrix.cmake_build_type }}
      - name: Build
        run: |
          ccache -z
          cmake --build builddir --parallel $(nproc)
          ccache -s
      - name: Tests
        working-directory: builddir
        run: ctest --output-on-failure
      - name: Test linking against build dir
        if: ${{ !contains(matrix.cxx_extra_flags, '-fsanitize=address') && matrix.arch != 'x86' }}
        working-directory: example/build_cmake_installed
        run: |
          cmake -B builddir_buildtree -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DCMAKE_CXX_STANDARD=${{ matrix.stdcxx }} -DKokkos_ROOT=../../builddir
          cmake --build builddir_buildtree
          export CTEST_OUTPUT_ON_FAILURE=1
          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: ${{ !contains(matrix.cxx_extra_flags, '-fsanitize=address') && matrix.arch != 'x86' }}
        working-directory: example/build_cmake_installed
        run: |
          cmake -B builddir -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DCMAKE_CXX_STANDARD=${{ matrix.stdcxx }}
          cmake --build builddir
          export CTEST_OUTPUT_ON_FAILURE=1
          cmake --build builddir --target test
