name: Build Wheels for PyPI

# run every Sunday at 10 AM
on:
  workflow_dispatch:
  schedule:
    - cron: "0 10 * * 0"

jobs:
  build_wheels:
    strategy:
      fail-fast: false
      matrix:
        os: [
            {
              name: "Ubuntu 22.04",
              family: "linux",
              runner: "ubuntu-22.04",
              archs: "x86_64",
            },
            {
              name: "Ubuntu 22.04",
              family: "linux",
              runner: "ubuntu-22.04-arm",
              archs: "aarch64",
            },
            {
              name: "macOS 15 x64",
              family: "macos",
              runner: "macos-15-intel",
              archs: "x86_64",
            },
            {
              name: "macOS 15 arm64",
              family: "macos",
              runner: "macos-15",
              archs: "arm64",
            },
            ## Windows is disabled because of an issue with compiling FFI as
            ## under MinGW in the GitHub Actions environment (SHELL variable has
            ## whitespace.)
            # {
            #   name: "Windows Server 2019",
            #   family: "windows",
            #   runner: "windows-2019",
            #   archs: "AMD64",
            # },
          ]
    name: Build Wheels | ${{ matrix.os.name }} | ${{ matrix.os.archs }}
    runs-on: ${{ matrix.os.runner }}
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: true
          persist-credentials: false
      - uses: actions/setup-python@v5
      - name: Get FFI
        shell: bash
        run: |
          mkdir -p ffi
          curl -L https://github.com/libffi/libffi/releases/download/v3.4.8/libffi-3.4.8.tar.gz | tar --strip-components=1 -xzC ffi
      - if: ${{ matrix.os.family == 'linux' }}
        name: "[Linux] Bison 3.8.2"
        shell: bash
        run: |
          mkdir -p bison
          curl -L https://ftpmirror.gnu.org/gnu/bison/bison-3.8.2.tar.gz | tar --strip-components=1 -xzC bison
      ## Software installed by default in GitHub Action Runner VMs:
      ##  https://github.com/actions/runner-images
      - if: ${{ matrix.os.family == 'macos' }}
        name: "[macOS] Flex/Bison"
        run: |
          brew install flex bison
          echo "PATH=$(brew --prefix flex)/bin:$(brew --prefix bison)/bin:$PATH" >> $GITHUB_ENV
      - if: ${{ matrix.os.family == 'windows' }}
        name: "[Windows] Flex/Bison"
        run: |
          choco install winflexbison3
      - if: ${{ matrix.os.family == 'macos' && matrix.os.archs == 'arm64' }}
        name: "[macOS/arm64] Install Python 3.8 (see: https://cibuildwheel.pypa.io/en/stable/faq/#macos-building-cpython-38-wheels-on-arm64)"
        uses: actions/setup-python@v5
        with:
          python-version: 3.8
      - name: Build wheels
        uses: pypa/cibuildwheel@v2.21.1
        env:
          # * APIs not supported by PyPy
          # * Musllinux disabled because it increases build time from 48m to ~3h
          CIBW_SKIP: >
            pp*
            *musllinux*
          CIBW_ARCHS: ${{ matrix.os.archs }}
          CIBW_BUILD_VERBOSITY: "1"
          # manylinux2014 (default) does not have a modern enough C++ compiler for Yosys
          CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_28
          CIBW_MANYLINUX_AARCH64_IMAGE: manylinux_2_28
          CIBW_BEFORE_ALL: bash ./.github/workflows/wheels/cibw_before_all.sh
          CIBW_ENVIRONMENT: >
            OPTFLAGS=-O3
            PKG_CONFIG_PATH=./ffi/pfx/lib/pkgconfig
            PATH="$PWD/bison/src:$PATH"
          CIBW_ENVIRONMENT_MACOS: >
            OPTFLAGS=-O3
            PKG_CONFIG_PATH=./ffi/pfx/lib/pkgconfig
            MACOSX_DEPLOYMENT_TARGET=11
            makeFlags='CONFIG=clang'
            PATH="$PWD/bison/src:$PATH"
          CIBW_BEFORE_BUILD: bash ./.github/workflows/wheels/cibw_before_build.sh
          CIBW_TEST_COMMAND: python3 {project}/tests/pyosys/run_tests.py
      - uses: actions/upload-artifact@v7
        with:
          name: python-wheels-${{ matrix.os.runner }}
          path: ./wheelhouse/*.whl
  upload_wheels:
    name: Upload Wheels
    if: (github.repository == 'YosysHQ/yosys') && (github.event_name == 'workflow_dispatch')
    runs-on: ubuntu-latest
    # Specifying a GitHub environment is optional, but strongly encouraged
    environment: pypi
    permissions:
      # IMPORTANT: this permission is mandatory for Trusted Publishing
      id-token: write
    needs: build_wheels
    steps:
      - uses: actions/download-artifact@v8
        with:
          path: "."
          pattern: python-wheels-*
          merge-multiple: true
      - run: |
          ls
          mkdir -p ./dist
          mv *.whl ./dist
      - name: Publish
        uses: pypa/gh-action-pypi-publish@release/v1
