name: Build

on:
  workflow_dispatch: {}  # allows triggering this workflow manually
  push:
    branches: # trigger on commits to main branch
      - main
  pull_request:  # trigger on pull requests affecting relevant files
    branches:
      - main
    paths:
      - '**workflows/wheels.yml'
      - 'pyproject.toml'
  release:  # trigger on published release
    types:
      - published

permissions: {}

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

jobs:
  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-22.04, ubuntu-22.04-arm, macos-14, windows-2022]
        cibw_build: ["cp39-* cp310-* cp311-* cp312-* cp313-* cp313t-* cp314-* cp314t-*"]
        include:
          - os: windows-11-arm
            cibw_build: "cp311-* cp312-* cp313-* cp313t-* cp314-* cp314t-*"

    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  # v4.2.2
        with:
          submodules: true
          persist-credentials: false
      # Used to host cibuildwheel
      - uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c  # v6.0.0
        with:
          python-version: "3.12"

      - name: Install cibuildwheel
        run: python -m pip install cibuildwheel==3.1.2

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse
        env:
          CIBW_ARCHS_LINUX: auto
          CIBW_ARCHS_MACOS: universal2
          CIBW_BUILD: ${{ matrix.cibw_build }}
          CIBW_ENABLE: cpython-freethreading
          CIBW_PRERELEASE_PYTHONS: True
          CIBW_SKIP: "*musllinux* *i686* *win32*"
          CIBW_TEST_REQUIRES: absl-py pytest pytest-xdist
          CIBW_TEST_COMMAND: pytest -n auto {project}
          CIBW_BUILD_VERBOSITY: 1

      - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4  # v5.0.0
        with:
          name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
          path: ./wheelhouse/*.whl

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  # v4.2.2
        with:
          submodules: true
          persist-credentials: false
      - name: Build sdist
        run: pipx run build --sdist

      - uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4  # v5.0.0
        with:
          name: cibw-sdist
          path: dist/*.tar.gz

  download_and_list_artifacts:
    # Helps debug issues like https://github.com/jax-ml/ml_dtypes/issues/196
    name: Download and list artifacts
    needs: [build_sdist, build_wheels]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53  # v6.0.0
        with:
          # unpacks all CIBW artifacts into dist/
          pattern: cibw-*
          path: dist
          merge-multiple: true

      - name: List files
        run: ls -l dist/

  upload_pypi:
    name: Release & Upload to PyPI
    needs: [build_sdist, build_wheels]
    runs-on: ubuntu-latest
    environment: release
    permissions:
      id-token: write
    # Only publish release to PyPI when a github release is created.
    if: github.event_name == 'release' && github.event.action == 'published'
    steps:
      - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53  # v6.0.0
        with:
          # unpacks all CIBW artifacts into dist/
          pattern: cibw-*
          path: dist
          merge-multiple: true

      - name: List files
        run: ls -l dist/

      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e  # v1.13.0
