name: Build and run tests with Verific (Linux)

on:
  pull_request:
  merge_group:
  #push:
  #  branches: [ main ]
  workflow_dispatch:

jobs:
  pre_job:
    runs-on: ubuntu-latest
    outputs:
      should_skip: ${{ steps.set_output.outputs.should_skip }}
    steps:
      - id: skip_check
        if: ${{ github.event_name != 'merge_group' }}
        uses: fkirc/skip-duplicate-actions@v5
        with:
          # don't run on documentation changes
          paths_ignore: '["**/README.md", "docs/**", "guidelines/**"]'
          # cancel previous builds if a new commit is pushed
          # but never cancel main
          cancel_others: ${{ github.ref != 'refs/heads/main' }}

      - id: set_output
        run: |
          if [ "${{ github.event_name }}" = "merge_group" ]; then
            echo "should_skip=false" >> $GITHUB_OUTPUT
          else
            echo "should_skip=${{ steps.skip_check.outputs.should_skip }}" >> $GITHUB_OUTPUT
          fi

  test-verific:
    needs: pre_job
    if: ${{ needs.pre_job.outputs.should_skip != 'true' && github.repository_owner == 'YosysHQ' }}
    runs-on: [self-hosted, linux, x64, fast]
    steps:
      - name: Checkout Yosys
        uses: actions/checkout@v4
        with:
          persist-credentials: false
          submodules: true
      - name: Runtime environment
        run: |
          echo "procs=$(nproc)" >> $GITHUB_ENV

      - name: Build Yosys
        run: |
          make config-clang
          echo "ENABLE_VERIFIC := 1" >> Makefile.conf
          echo "ENABLE_VERIFIC_EDIF := 1" >> Makefile.conf
          echo "ENABLE_VERIFIC_LIBERTY := 1" >> Makefile.conf
          echo "ENABLE_VERIFIC_YOSYSHQ_EXTENSIONS := 1" >> Makefile.conf
          echo "ENABLE_CCACHE := 1" >> Makefile.conf
          echo "ENABLE_FUNCTIONAL_TESTS := 1" >> Makefile.conf
          make -j$procs ENABLE_LTO=1

      - name: Install Yosys
        run: |
          make install DESTDIR=${GITHUB_WORKSPACE}/.local PREFIX=

      - name: Checkout SBY
        uses: actions/checkout@v4
        with:
          repository: 'YosysHQ/sby'
          path: 'sby'
          persist-credentials: false

      - name: Build SBY
        run: |
          make -C sby install DESTDIR=${GITHUB_WORKSPACE}/.local PREFIX=

      - name: Run Yosys tests
        run: |
          make -j$procs vanilla-test

      - name: Run Verific specific Yosys tests
        run: |
          make -C tests/sva
          cd tests/svtypes && bash run-test.sh

      - name: Run SBY tests
        if: ${{ github.event_name == 'merge_group' || github.event_name == 'workflow_dispatch' }}
        run: |
          make -C sby run_ci

  test-pyosys:
    needs: pre_job
    if: ${{ needs.pre_job.outputs.should_skip != 'true' && github.repository_owner == 'YosysHQ' }}
    runs-on: [self-hosted, linux, x64, fast]
    steps:
      - name: Checkout Yosys
        uses: actions/checkout@v4
        with:
          persist-credentials: false
          submodules: true
      - name: Install UV
        run: |
          curl -LsSf https://astral.sh/uv/install.sh | sh
      - name: Runtime environment
        run: |
          echo "procs=$(nproc)" >> $GITHUB_ENV
          echo "${{ github.workspace }}/.local/bin" >> $GITHUB_PATH

      - name: Build pyosys
        run: |
          make config-clang
          echo "ENABLE_VERIFIC := 1" >> Makefile.conf
          echo "ENABLE_VERIFIC_EDIF := 1" >> Makefile.conf
          echo "ENABLE_VERIFIC_LIBERTY := 1" >> Makefile.conf
          echo "ENABLE_VERIFIC_YOSYSHQ_EXTENSIONS := 1" >> Makefile.conf
          echo "ENABLE_CCACHE := 1" >> Makefile.conf
          echo "ENABLE_PYOSYS := 1" >> Makefile.conf
          echo "PYTHON_DESTDIR := /usr/lib/python3/site-packages" >> Makefile.conf
          make -j$procs

      - name: Install pyosys
        run: |
          make install DESTDIR=${GITHUB_WORKSPACE}/.local PREFIX=

      - name: Run pyosys tests
        run: |
          export PYTHONPATH=${GITHUB_WORKSPACE}/.local/usr/lib/python3/site-packages:$PYTHONPATH
          python3 tests/pyosys/run_tests.py

  test-verific-result:
    runs-on: ubuntu-latest
    needs:
      - test-verific
      - test-pyosys
    if: always()
    steps:
      - name: Check results
        run: |
          echo "Needs results: ${{ join(needs.*.result, ',') }}"
          if [[ "${{ join(needs.*.result, ',') }}" == *failure* ]] || \
            [[ "${{ join(needs.*.result, ',') }}" == *cancelled* ]]; then
            echo "Some jobs failed or were cancelled"
            exit 1
          fi
      - run: echo "All good"
