name: Compiler testing

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-compile:
    runs-on: ${{ matrix.os }}
    needs: pre_job
    if: (github.event_name == 'merge_group' || github.event_name == 'workflow_dispatch') && needs.pre_job.outputs.should_skip != 'true'
    env:
      CXXFLAGS: ${{ startsWith(matrix.compiler, 'gcc') && '-Wp,-D_GLIBCXX_ASSERTIONS' || ''}}
      CC_SHORT: ${{ startsWith(matrix.compiler, 'gcc') && 'gcc' || 'clang' }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
        compiler:
          # oldest supported
          - 'clang-10'
          - 'gcc-10'
          # newest, make sure to update maximum standard step to match
          - 'clang-19'
          - 'gcc-14'
        include:
          # macOS x86
          - os: macos-15-intel
            compiler: 'clang-19'
          # macOS arm
          - os: macos-latest
            compiler: 'clang-19'
      fail-fast: false
    steps:
      - name: Checkout Yosys
        uses: actions/checkout@v5
        with:
          submodules: true
          persist-credentials: false

      - name: Setup environment
        uses: ./.github/actions/setup-build-env
        with:
          runs-on: ${{ matrix.os }}
          get-build-deps: true

      - name: Setup Cpp
        uses: aminya/setup-cpp@v1
        with:
          compiler: ${{ matrix.compiler }}

      - name: Tool versions
        shell: bash
        run: |
          $CC --version
          $CXX --version

      # minimum standard
      - name: Build C++17
        shell: bash
        run: |
          make config-$CC_SHORT
          make -j$procs CXXSTD=c++17 compile-only

      # maximum standard, only on newest compilers
      - name: Build C++20
        if: ${{ matrix.compiler == 'clang-19' || matrix.compiler == 'gcc-14' }}
        shell: bash
        run: |
          make config-$CC_SHORT
          make -j$procs CXXSTD=c++20 compile-only

  test-compile-result:
    runs-on: ubuntu-latest
    needs:
      - test-compile
    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"
