name: C++/CUDA

on:
  pull_request:
    branches:
      - V3
      - yshekel/V3     # TODO remove when merged to V3
  push:
    branches:
      - V3

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  check-changed-files:
    uses: ./.github/workflows/check-changed-files.yml

  check-format:
    name: Check Code Format
    runs-on: ubuntu-22.04
    needs: check-changed-files
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Check clang-format
      if: needs.check-changed-files.outputs.cpp_cuda == 'true'
      run: if [[ $(find ./ \( -path ./icicle/build -prune -o -path ./**/target -prune -o -path ./examples -prune \) -iname *.h -or -iname *.cuh -or -iname *.cu -or -iname *.c -or -iname *.cpp | xargs clang-format --dry-run -ferror-limit=1 -style=file 2>&1) ]]; then echo "Please run clang-format"; exit 1; fi

  extract-cuda-backend-branch:
    name: Extract cuda branch name
    runs-on: ubuntu-22.04
    outputs:
      cuda-backend-branch: ${{ steps.extract.outputs.cuda-backend-branch }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Extract Private Branch from PR Description
        id: extract
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          DESCRIPTION=$(gh pr view ${{ github.event.pull_request.number }} --json body -q '.body')
          echo "PR Description: $DESCRIPTION"
          CUDA_BE_BRANCH=$(echo "$DESCRIPTION" | grep -oP 'cuda-backend-branch:\s*\K[^\s]+') || true
          if [ -z "$CUDA_BE_BRANCH" ]; then
            CUDA_BE_BRANCH="main" # Default branch if not specified
          fi
          echo "Extracted CUDA Backend Branch: $CUDA_BE_BRANCH"
          echo "::set-output name=cuda-backend-branch::$CUDA_BE_BRANCH"

  test-linux-curve:
    name: Test on Linux
    runs-on: [self-hosted, Linux, X64, icicle]
    needs: [check-changed-files, check-format, extract-cuda-backend-branch]
    strategy:
      matrix:
        curve:
          - name: bn254
            build_args: -DG2=ON -DECNTT=ON
          - name: bls12_381
            build_args: -DG2=ON -DECNTT=ON
          - name: bls12_377
            build_args: -DG2=ON -DECNTT=ON
          - name: bw6_761
            build_args: -DG2=ON -DECNTT=ON
          - name: grumpkin
            build_args:

    steps:
    - name: Checkout Repo
      uses: actions/checkout@v4
    - name: Checkout CUDA Backend
      uses: actions/checkout@v4
      with:
        repository: ingonyama-zk/icicle-cuda-backend
        path: ./icicle_v3/backend/cuda
        token: ${{ secrets.GITHUB_TOKEN }}
        ssh-key: ${{ secrets.CUDA_PULL_KEY }}
        ref: ${{ needs.extract-branch.outputs.cuda-backend-branch }}
    - name: Build curve
      working-directory: ./icicle_v3
      if: needs.check-changed-files.outputs.cpp_cuda == 'true'
      run: |
        mkdir -p build && rm -rf build/*
        cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=ON -DCURVE=${{ matrix.curve.name }} ${{ matrix.curve.build_args }} -DCUDA_BACKEND=local -S . -B build
        cmake --build build -j
    - name: Run C++ curve Tests
      working-directory: ./icicle_v3/build/tests
      if: needs.check-changed-files.outputs.cpp_cuda == 'true'
      run: ctest --verbose

  test-linux-field:
    name: Test on Linux
    runs-on: [self-hosted, Linux, X64, icicle]
    needs: [check-changed-files, check-format, extract-cuda-backend-branch]
    strategy:
      matrix:
        field:
          - name: babybear
            build_args: -DEXT_FIELD=ON
          - name: stark252
            build_args: -DEXT_FIELD=OFF
    steps:
    - name: Checkout Repo
      uses: actions/checkout@v4
    - name: Checkout CUDA Backend
      uses: actions/checkout@v4
      with:
        repository: ingonyama-zk/icicle-cuda-backend
        path: ./icicle_v3/backend/cuda
        token: ${{ secrets.GITHUB_TOKEN }}
        ssh-key: ${{ secrets.CUDA_PULL_KEY }}
        ref: ${{ needs.extract-branch.outputs.cuda-backend-branch }}
    - name: Build field
      working-directory: ./icicle_v3
      if: needs.check-changed-files.outputs.cpp_cuda == 'true'
      run: |
        mkdir -p build && rm -rf build/*
        cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=ON -DFIELD=${{ matrix.field.name }} ${{ matrix.field.build_args }} -DCUDA_BACKEND=local -S . -B build
        cmake --build build -j
    - name: Run C++ field Tests
      working-directory: ./icicle_v3/build/tests
      if: needs.check-changed-files.outputs.cpp_cuda == 'true'
      run: ctest --verbose