name: CMake build on Kunpeng

on:
  #push:
  schedule:
    - cron: '0 18 * * 6'  # 16h Saturday
  workflow_dispatch:
    inputs:
      git-ref:
        description: Git Ref (Optional)
        required: false

# Show the git ref in the workflow name if it is invoked manually.
run-name: ${{ github.event_name == 'workflow_dispatch' && format('Manual run {0}', inputs.git-ref) || '' }}


permissions:
  contents: read

jobs:

  cmake-main:
    runs-on: [self-hosted, kp]
    strategy:
      fail-fast: false
      matrix:
        toolchain:
          - {compiler: gcc, cflags: '-Wall -Wextra -Werror', fflags: '-Wall -Wextra -Wpedantic -Werror -pedantic -fimplicit-none -frecursive -fcheck=all -fstack-check -Wno-function-elimination'}

    steps:

      - name: Clone Repository (Latest)
        uses: actions/checkout@v4
        if: github.event.inputs.git-ref == ''
        with:
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY_ACT }} # This forces checkout to use SSH, not HTTPS
          submodules: recursive
      - name: Clone Repository (Custom Ref)
        uses: actions/checkout@v4
        if: github.event.inputs.git-ref != ''
        with:
          ref: ${{ github.event.inputs.git-ref }}
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY_ACT }} # This forces checkout to use SSH, not HTTPS
          submodules: recursive

      - name: Build
        run: |
          cmake --version
          cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=. -LAH -DCMAKE_C_FLAGS="${{ matrix.toolchain.cflags }}" -DCMAKE_Fortran_FLAGS="${{ matrix.toolchain.fflags }}" .
          cmake --build . --target install --parallel 4
          cmake --build . --target examples --parallel 4
          ctest --output-on-failure -V -j4 -R example
        env:
          FC: ${{ steps.setup-fortran.outputs.fc }}


  cmake-other:
    runs-on: [self-hosted, kp]
    strategy:
      fail-fast: false
      matrix:
        toolchain:
          - {compiler: nvfortran, cflags: '-Wall', fflags: '-C -Wall -Wextra -Minform=warn -Mstandard -Mrecursive -Mbounds -Mchkstk -Mchkptr'}
          - {compiler: flang, cflags: '-Wall', fflags: '-pedantic -Weverything -Wall -Wextra'}

    steps:

      - name: Clone Repository (Latest)
        uses: actions/checkout@v4
        if: github.event.inputs.git-ref == ''
        with:
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY_ACT }} # This forces checkout to use SSH, not HTTPS
          submodules: recursive
      - name: Clone Repository (Custom Ref)
        uses: actions/checkout@v4
        if: github.event.inputs.git-ref != ''
        with:
          ref: ${{ github.event.inputs.git-ref }}
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY_ACT }} # This forces checkout to use SSH, not HTTPS
          submodules: recursive

      - name: Build
        run: |
          cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=. -LAH -DCMAKE_C_FLAGS="${{ matrix.toolchain.cflags }}" -DCMAKE_Fortran_FLAGS="${{ matrix.toolchain.fflags }}" .
          cmake --build . --target install --parallel 4
          cmake --build . --target examples --parallel 4
          # cobyla test does not pass on AOCC: https://github.com/libprima/prima/issues/41
          ctest --output-on-failure -V -j4 -R example -E cobyla
        env:
          FC: ${{ matrix.toolchain.compiler }}
