name: Build Python Wheels

on:
  # allow manual runs
  workflow_dispatch:
  
  # run when we tag a release
  #release:
  #  types:
  #    - "created"

env:
  BUILD_TYPE: Release

jobs:
  build_sdist:
    name: Source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-python@v4
        name: Install Python
        with:
          python-version: '3.x'

      - name: Install build package
        run: python -m pip install build --user

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

      - uses: actions/upload-artifact@v3
        with:
          path: dist/*.tar.gz

  build_wheels:
    name: ${{ matrix.config.name }}
    runs-on: ${{ matrix.config.os }}
    strategy:
      matrix:
        config:
        - {
            name: "MacOS Latest (Intel)",
            os: macos-latest,
            cibw-arch: macosx_x86_64
          }
        - {
            name: "MacOS Latest (Apple Silicon)",
            os: macos-latest,
            cibw-arch: macosx_arm64
          }
        - {
            name: "Ubuntu Latest (x86_64)",
            os: ubuntu-latest,
            cibw-arch: manylinux_x86_64
          }
        - {
            name: "Ubuntu Latest (ARM64)",
            os: ubuntu-latest,
            cibw-arch: manylinux_aarch64,
            use-qemu: true
          }
        - {
            name: "Ubuntu Latest (i686)",
            os: ubuntu-latest,
            cibw-arch: manylinux_i686
          }
        - {
            name: "Windows Latest",
            os: windows-latest,
            cibw-arch: win_amd64
          }

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up Python 3.x
        uses: actions/setup-python@v4
        with:
          python-version: '3.x'

      - name: Set up QEMU for linux/arm64 builds
        if: runner.os == 'Linux' && matrix.config.use-qemu == true
        uses: docker/setup-qemu-action@v2
        with:
          platforms: arm64

      - name: Install Python dependencies
        run: python -m pip install cibuildwheel==2.12.0

      - name: Build wheels
        run: python -m cibuildwheel --output-dir dist
        env:
          CIBW_ARCHS_LINUX: "auto aarch64"
          CIBW_ARCHS_MACOS: "x86_64 arm64"
          CIBW_ENVIRONMENT_MACOS: CMAKE_OSX_ARCHITECTURES=${{ matrix.config.cibw-arch == 'macosx_x86_64' && 'x86_64' || matrix.config.cibw-arch == 'macosx_arm64' && 'arm64' || '' }}
          CIBW_BUILD: "*-${{ matrix.config.cibw-arch }}"
          CIBW_BEFORE_BUILD_LINUX: "yum remove -y cmake"
          CIBW_BEFORE_BUILD: "python -m pip install cmake>=3.18"
          CIBW_SKIP: "*-win32 pp*-aarch64 pp*-macosx"

      - uses: actions/upload-artifact@v3
        with:
          path: ./dist/*.whl
