# This file is autogenerated by maturin v1.7.0
# To update, run
#
#    maturin generate-ci github
#
name: CI

on:
  workflow_dispatch:

permissions:
  contents: read

jobs:
  sdist:
    name: build sdist
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.13'
      - uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
          rust-toolchain: stable
      - uses: actions/upload-artifact@v4
        with:
          name: pypi_files_sdist
          path: dist

  build:
    name: build on ${{ matrix.os }} (${{ matrix.target }} - ${{ matrix.interpreter || 'all' }}${{ matrix.os == 'linux' && format(' - {0}', matrix.manylinux == 'auto' && 'manylinux' || matrix.manylinux) || '' }})
    if: startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main'
    strategy:
      fail-fast: false
      matrix:
        os: [linux, macos, windows]
        target: [x86_64, aarch64]
        manylinux: [auto]
        include:
          # manylinux
          - os: linux
            manylinux: auto
            target: i686
          - os: linux
            manylinux: auto
            target: aarch64
          - os: linux
            manylinux: auto
            target: armv7
            interpreter: 3.8 3.9 3.10 3.11 3.12 3.13
          - os: linux
            manylinux: auto
            target: ppc64le
            interpreter: 3.8 3.9 3.10 3.11 3.12 3.13
          - os: linux
            manylinux: auto
            target: s390x
            interpreter: 3.8 3.9 3.10 3.11 3.12 3.13
          - os: linux
            manylinux: auto
            target: x86_64
            interpreter: pypy3.9 pypy3.10

          # musllinux
          - os: linux
            manylinux: musllinux_1_1
            target: x86_64
          - os: linux
            manylinux: musllinux_1_1
            target: aarch64
          - os: linux
            manylinux: musllinux_1_1
            target: armv7

          # macos
          - os: macos
            target: x86_64
          - os: macos
            target: aarch64
            interpreter: 3.8 3.9 pypy3.9 pypy3.10

          # windows
          - os: windows
            target: x86_64
            interpreter: pypy3.9 pypy3.10
          - os: windows
            target: i686
            python-architecture: x86
            interpreter: 3.8 3.9 3.10 3.11 3.12 3.13

        exclude:
          - os: windows
            target: aarch64

    runs-on: ${{ (matrix.os == 'linux' && 'ubuntu') || matrix.os }}-latest
    steps:
      - uses: actions/checkout@v4

      - name: set up python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ (matrix.os == 'windows' && matrix.target == 'x86_64' && '>=3.9 <=3.10') || '3.13' }}
          architecture: ${{ matrix.python-architecture || 'x64' }}

      - run: pip install -U twine

      - name: build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          manylinux: ${{ matrix.manylinux }}
          args: --release --out dist --interpreter ${{ matrix.interpreter || '3.8 3.9 3.10 3.11 3.12 3.13 pypy3.9 pypy3.10' }}
          rust-toolchain: ${{ (matrix.os == 'windows' && '1.77') || 'stable' }}
          sccache: 'true'
          docker-options: -e CI

      - run: ${{ (matrix.os == 'windows' && 'dir') || 'ls -lh' }} dist/

      - run: twine check --strict dist/*

      - uses: actions/upload-artifact@v4
        with:
          name: pypi_files_${{ matrix.os }}_${{ matrix.target }}_${{ matrix.interpreter || 'all' }}_${{ matrix.manylinux }}
          path: dist

  release:
    needs: [build, sdist]
    if: success() && startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: set up python
        uses: actions/setup-python@v5
        with:
          python-version: '3.13'

      - run: pip install -U twine

      - name: get dist artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: pypi_files_*
          merge-multiple: true
          path: dist

      - run: twine check --strict dist/*

      - name: upload to pypi
        run: twine upload dist/*
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}