name: PyO3-Wheels

on:
  push:
    branches:
      - main
    tags:
      - '*'
    paths:
      - candle-pyo3/**
  pull_request:
    paths:
      - candle-pyo3/**
  workflow_dispatch:

permissions:
  contents: read

env:
  PROTOC_VERSION: '25.0'
  FEATURES_FLAG: '--features onnx'

jobs:
  linux:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        target: [x86_64, x86, aarch64, s390x, ppc64le]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter
          sccache: 'true'
          manylinux: auto
          working-directory: ./candle-pyo3
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels
          path: ./candle-pyo3/dist

  windows:
    runs-on: windows-latest
    strategy:
      matrix:
        target: [x64, x86]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
          architecture: ${{ matrix.target }}
      - name: Install Protoc
        uses: arduino/setup-protoc@v2
        with:
          version: ${{ env.PROTOC_VERSION }}
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter ${{ env.FEATURES_FLAG }}
          sccache: 'true'
          working-directory: ./candle-pyo3
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels
          path: ./candle-pyo3/dist

  macos:
    runs-on: macos-latest
    strategy:
      matrix:
        target: [x86_64, aarch64]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: Install Protoc
        uses: arduino/setup-protoc@v2
        with:
            version: ${{ env.PROTOC_VERSION }}
            repo-token: ${{ secrets.GITHUB_TOKEN }}
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter ${{ env.FEATURES_FLAG }}
          sccache: 'true'
          working-directory: ./candle-pyo3
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels
          path: ./candle-pyo3/dist

  sdist:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Protoc
        uses: arduino/setup-protoc@v2
        with:
          version: ${{ env.PROTOC_VERSION }}
          repo-token: ${{ secrets.GITHUB_TOKEN }}
      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
          working-directory: ./candle-pyo3
      - name: Upload sdist
        uses: actions/upload-artifact@v4
        with:
          name: wheels
          path: ./candle-pyo3/dist
