name: Build and upload to PyPI

on:
  workflow_dispatch:
  release:
    types: [published]
    
env:
  CIBW_BUILD: cp3*
  CIBW_SKIP: cp38-*
  CIBW_TEST_REQUIRES: pytest
  CIBW_TEST_COMMAND: pytest {project}/test/PyGLM_test.py -v

jobs:
  build_wheels_windows:
    name: Build wheels on Windows (${{ matrix.arch }})
    runs-on: windows-latest
    strategy:
      matrix:
        arch: [AMD64, ARM64]
        
    env:
      CIBW_ARCHS: ${{matrix.arch}}

    steps:
      - uses: actions/checkout@v6
        with:
            submodules: recursive

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.14'

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

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse

      - uses: actions/upload-artifact@v5
        with:
          name: artifact-win-${{matrix.arch}}
          path: ./wheelhouse/*.whl
          
  build_wheels_mac:
    name: Build wheels on MacOS (${{ matrix.arch }})
    runs-on: macos-latest
    strategy:
      matrix:
        arch: [x86_64, arm64]
        
    env:
      CIBW_ARCHS: ${{matrix.arch}}

    steps:
      - uses: actions/checkout@v6
        with:
            submodules: recursive

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.14'

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

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse

      - uses: actions/upload-artifact@v5
        with:
          name: artifact-mac-${{matrix.arch}}
          path: ./wheelhouse/*.whl
          
  build_wheels_manylinux:
    name: Build wheels on ${{ matrix.distro }} for ${{ matrix.arch }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        distro: [manylinux2014, manylinux_2_34]
        arch: [x86_64]
        
    env:
      CIBW_MANYLINUX_X86_64_IMAGE: ${{ matrix.distro }}
      CIBW_MANYLINUX_I686_IMAGE: ${{ matrix.distro }}
      CIBW_MANYLINUX_S390X_IMAGE: ${{ matrix.distro }}
      CIBW_BUILD: cp3*-manylinux*
      CIBW_ARCHS: ${{matrix.arch}}

    steps:
      - uses: actions/checkout@v6
        with:
            submodules: recursive

      - name: Set up QEMU
        if: ${{ matrix.arch != 'x86_64' }}
        uses: docker/setup-qemu-action@v3

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.14'

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

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse

      - uses: actions/upload-artifact@v5
        with:
          name: artifact-${{ matrix.distro }}-${{matrix.arch}}
          path: ./wheelhouse/*.whl

  build_wheels_manylinux_arm:
    name: Build wheels on ${{ matrix.distro }} for ${{ matrix.arch }}
    runs-on: ubuntu-24.04-arm
    strategy:
      matrix:
        distro: [manylinux2014, manylinux_2_34]
        arch: [aarch64]
        #include:
        #  - distro: manylinux_2_31
        #    arch: armv7l
            
        
    env:
      CIBW_MANYLINUX_AARCH64_IMAGE: ${{ matrix.distro }}
      CIBW_MANYLINUX_ARMV7L_IMAGE: ${{ matrix.distro }}
      CIBW_BUILD: cp3*-manylinux*
      CIBW_ARCHS: ${{matrix.arch}}

    steps:
      - uses: actions/checkout@v6
        with:
            submodules: recursive

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.14'

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

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse

      - uses: actions/upload-artifact@v5
        with:
          name: artifact-${{ matrix.distro }}-${{matrix.arch}}
          path: ./wheelhouse/*.whl
          
  build_wheels_musllinux:
    name: Build wheels on musllinux_1_2 for ${{ matrix.arch }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        arch: [x86_64]
        
    env:
      CIBW_MUSLLINUX_X86_64_IMAGE: musllinux_1_2
      CIBW_MUSLLINUX_I686_IMAGE: musllinux_1_2
      CIBW_MUSLLINUX_S390X_IMAGE: musllinux_1_2
      CIBW_BUILD: cp3*-musllinux*
      CIBW_ARCHS: ${{matrix.arch}}

    steps:
      - uses: actions/checkout@v6
        with:
            submodules: recursive

      - name: Set up QEMU
        if: ${{ matrix.arch != 'x86_64' }}
        uses: docker/setup-qemu-action@v3

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.14'

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

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse

      - uses: actions/upload-artifact@v5
        with:
          name: artifact-musllinux-${{matrix.arch}}
          path: ./wheelhouse/*.whl

  build_wheels_musllinux_arm:
    name: Build wheels on musllinux_1_2 for ${{ matrix.arch }}
    runs-on: ubuntu-24.04-arm
    strategy:
      matrix:
        arch: [aarch64]
        
    env:
      CIBW_MUSLLINUX_AARCH64_IMAGE: musllinux_1_2
      CIBW_BUILD: cp3*-musllinux*
      CIBW_ARCHS: ${{matrix.arch}}

    steps:
      - uses: actions/checkout@v6
        with:
            submodules: recursive

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.14'

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

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse

      - uses: actions/upload-artifact@v5
        with:
          name: artifact-musllinux-${{matrix.arch}}
          path: ./wheelhouse/*.whl

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
            submodules: recursive

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.14'

      - name: Install build
        run: pip install build

      - name: Build sdist
        run: python -m build --sdist

      - uses: actions/upload-artifact@v5
        with:
          name: artifact-source
          path: dist/*.tar.gz

  upload_pypi:
    needs: [build_wheels_windows, build_wheels_mac, build_wheels_manylinux, build_wheels_manylinux_arm, build_wheels_musllinux, build_wheels_musllinux_arm, build_sdist]
    runs-on: ubuntu-latest
    
    #if: github.event_name == 'release' && github.event.action == 'published'
    
    steps:
      - uses: actions/download-artifact@v6
        with:
          pattern: artifact-*
          merge-multiple: true
          path: dist

      - uses: pypa/gh-action-pypi-publish@v1.13.0
        with:
          user: __token__
          password: ${{ secrets.pypi_password }}
          # password: ${{ secrets.testpypi_password }}
          # repository_url: https://test.pypi.org/legacy/
