name: Build and Publish Python Package

on:
  # push:
  #   branches: [ "Master" ]
  #   tags:
  #     - 'v*'
  # pull_request:
  #   branches: [ "Master" ]
  workflow_dispatch:

jobs:
  build_wheels:
    name: Build wheel ${{ matrix.python }}-${{ matrix.buildplat[1] }}-${{ matrix.buildplat[2] }}
    runs-on: ${{ matrix.buildplat[0] }}
    strategy:
      # Ensure that a wheel builder finishes even if another fails
      fail-fast: false
      matrix:
        # Github Actions doesn't support pairing matrix values together, let's improvise
        # https://github.com/github/feedback/discussions/7835#discussioncomment-1769026
        buildplat:
          - [ubuntu-22.04, manylinux_x86_64, ""]
          - [ubuntu-22.04, musllinux_x86_64, ""]
          - [macos-13, macosx_x86_64, openblas]
          
          # targeting macos >= 14. Could probably build on macos-14, but it would be a cross-compile
          - [macos-13, macosx_x86_64, accelerate]
          - [macos-14, macosx_arm64, accelerate]  # always use accelerate
          - [windows-2019, win_amd64, ""]
          - [windows-2019, win32, ""]
        python: ["cp310", "cp311", "cp312", "pp310", "cp313", "cp313t"]
        exclude:
          # Don't build PyPy 32-bit windows
          - buildplat: [windows-2019, win32, ""]
            python: "pp310"
          - buildplat: [ ubuntu-22.04, musllinux_x86_64, "" ]
            python: "pp310"
          - buildplat: [ macos-14, macosx_arm64, accelerate ]
            python: "pp310"
          - buildplat: [ windows-2019, win_amd64, "" ]
            python: "cp313t"
          - buildplat: [ windows-2019, win32, "" ]
            python: "cp313t"
          - buildplat: [ macos13, macosx_x86_64, openblas ]
            python: "cp313t"

    steps:
    - uses: actions/checkout@v4      
    - name: Set up Python
      uses: actions/setup-python@v3
      with:
        python-version: '3.10'

    - name: Print Python version info
      run: |
        python --version
        python -c "import sys; print(sys.executable)"

    - name: Install cibuildwheel
      run: |
        python -m pip install --upgrade pip
        python -m pip install cibuildwheel==2.16.2

    - name: Build wheels
      run: |
        python -m cibuildwheel --output-dir wheelhouse
      env:
        CIBW_SKIP: cp36-* pp*
        CIBW_BEFORE_BUILD: >
          pip install cmake setuptools_scm[toml] wheel pybind11

    - uses: actions/upload-artifact@v3
      with:
        path: ./wheelhouse/*.whl
        
  build_sdist:
    name: Build sdist
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v3
      with:
        python-version: '3.10'
        
    - name: Build sdist
      run: |
        python -m pip install -U pip build
        python -m build --sdist -Csetup-args=-Dallow-noblas=true
        
    - name: Check README rendering for PyPI
      run: |
        python -mpip install twine
        twine check dist/*     
        
    - uses: actions/upload-artifact@v3
      with:
        path: ./dist/*
    
  upload_pypi:
    needs: [build_wheels]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/download-artifact@v3
      with:
        name: artifact
        path: dist
    
    - name: List Build contents
      run: ls -l dist
      
    - uses: pypa/gh-action-pypi-publish@release/v1
      with:
        user: __token__
        password: ${{ secrets.PYPI_TOKEN }}
        skip_existing: true
        
