# Copyright 2024-2026, Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

# Runs YARPGEN
name: Yarpgen fuzzing

permissions: read-all

on:
  workflow_dispatch:
    inputs:
      timeout:
        description: "Timeout for test system in minutes. -1 means infinity (default: 1)"
        required: true
        type: number
        default: 1

  schedule:
    # Run every Saturday at 23:00 UTC
    - cron: '0 23 * * 6'

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

jobs:
  linux-build-ispc:
    runs-on: ubuntu-22.04
    # Disabling this workflow for non ispc/ispc repo as it needs to run on releases only.
    if: github.repository == 'ispc/ispc'

    steps:
    - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
      with:
        submodules: true

    - name: Install dependencies
      run: |
        .github/workflows/scripts/install-build-deps.sh

    - name: Check environment
      run: |
        which -a clang
        cat /proc/cpuinfo

    - name: Build package
      run: |
        .github/workflows/scripts/build-ispc.sh

    - name: Sanity testing (make check-all, make test)
      run: |
        .github/workflows/scripts/check-ispc.sh

    - name: Upload package
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      with:
        name: ispc_linux_package
        path: build/ispc-*-linux.tar.gz

  yarpgen-build-and-run:
      runs-on: ubuntu-22.04
      needs: [linux-build-ispc]
      steps:

      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          submodules: true

      - name: Download package
        uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
        with:
          name: ispc_linux_package

      - name: Install dependencies and unpack artifacts
        run: |
          .github/workflows/scripts/install-test-deps.sh

      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          repository: 'intel/yarpgen'
          submodules: true
          path: 'yarpgen'

      - name: Build YARPGEN
        run: |
          cmake -B build && cmake --build build
        working-directory: yarpgen

      - name: Set up PATH
        run: |
          echo "YARPGEN_HOME=$GITHUB_WORKSPACE/yarpgen" >> "$GITHUB_ENV"
          echo "$GITHUB_WORKSPACE/yarpgen/scripts" >> "$GITHUB_PATH"
          echo "$GITHUB_WORKSPACE/yarpgen/build" >> "$GITHUB_PATH"

      - name: Set timeout for dispatched run
        if: github.event_name == 'workflow_dispatch'
        run: echo "TIMEOUT=${GITHUB_EVENT_INPUTS_TIMEOUT}" >> "$GITHUB_ENV"
        env:
          GITHUB_EVENT_INPUTS_TIMEOUT: ${{ github.event.inputs.timeout }}

      - name: Set timeout for scheduled run
        if: github.event_name == 'schedule'
        run: echo "TIMEOUT=240" >> "$GITHUB_ENV"

      - name: Run YARPGEN
        run: |
          ispc --version
          "$YARPGEN_HOME"/scripts/run_gen.py --std ispc --target ispc --timeout "$TIMEOUT"
          tar -czvf yarpgen.tar.gz testing
        working-directory: yarpgen

      - name: Print seeds
        run: |
          ls -al testing/*/*/*
        working-directory: yarpgen

      - name: Upload YARPGEN results
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: yarpgen
          path: yarpgen/yarpgen.tar.gz

