name: Build wheels

on:
  pull_request:
  push:
    branches:
      - master
    tags:
      - 'openbabel-*'
  workflow_dispatch:
    inputs:
      version:
        description: 'PEP 440 version to bake into artifacts (e.g. 3.2.0). Leave blank for dev build.'
        required: false
        default: ''

env:
  # Build CPython 3.10 - 3.14, skip 32-bit and musllinux variants
  CIBW_BUILD: "cp310-* cp311-* cp312-* cp313-* cp314-*"
  CIBW_SKIP: "*-manylinux_i686 *-win32 *-musllinux*"
  CIBW_BUILD_VERBOSITY: 1
  # setuptools_scm doesn't recognize the 'openbabel-X-Y-Z' tag format, so
  # explicit override is required to publish a real release.
  SETUPTOOLS_SCM_PRETEND_VERSION_FOR_OPENBABEL: ${{ inputs.version }}
  CIBW_ENVIRONMENT: ${{ inputs.version && format('SETUPTOOLS_SCM_PRETEND_VERSION_FOR_OPENBABEL={0}', inputs.version) || '' }}

jobs:
  build_wheels:
    name: Build wheels for ${{ matrix.os }}
    runs-on: ${{ matrix.runs-on }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: linux-intel
            runs-on: ubuntu-latest
          - os: linux-arm
            runs-on: ubuntu-24.04-arm
          - os: macos-intel
            runs-on: macos-15-intel
          - os: macos-arm
            runs-on: macos-14
          - os: windows-intel
            runs-on: windows-latest

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
      - uses: lukka/get-cmake@latest

      - name: Build wheels
        uses: pypa/cibuildwheel@8d2b08b68458a16aeb24b64e68a09ab1c8e82084 # v3.4.1

      - name: Uploading wheels
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
      - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: '3.14'
      - run: python -m pip install build
      - name: Build sdist
        run: python -m build --sdist
      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: cibw-sdist
          path: dist/*.tar.gz

  upload_pypi:
    needs: [build_wheels, build_sdist]
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/openbabel-')
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true
      - uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0
