name: cibuildwheel
permissions: write-all

on:
  push:
    branches: [latest]
    tags: v*
  pull_request:
  schedule:
    - cron: "0 0 * * *" # daily

jobs:
  build_wheels:
    name: Build wheels for ${{ matrix.os }}-${{ matrix.arch }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        build: [
          linux-x86_64,
          macos-x86_64,
          macos-arm64,
        ]
        include:
          - build: linux-x86_64
            os: ubuntu-latest
            arch: x86_64
            macos_target: ''
          - build: macos-x86_64
            os: macos-latest
            arch: x86_64
            macos_target: 'MACOSX_DEPLOYMENT_TARGET=11.0 CARGO_BUILD_TARGET=x86_64-apple-darwin'
          - build: macos-arm64
            os: macos-latest
            arch: arm64
            macos_target: 'MACOSX_DEPLOYMENT_TARGET=11.0 CARGO_BUILD_TARGET=aarch64-apple-darwin'
      fail-fast: false

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

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

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.3.1
        env:
          CIBW_ENVIRONMENT_MACOS: ${{ matrix.macos_target }}
          CIBW_ARCHS_LINUX: ${{ matrix.arch }}
          CIBW_ARCHS_MACOS: ${{ matrix.arch }}

      - uses: actions/upload-artifact@v6
        with:
          name: wheel-${{ matrix.build }}-${{ matrix.os }}
          path: './wheelhouse/sourmash*.whl'

  build_wasm:
    runs-on: ubuntu-latest
    if: false  # disable for now, need to fix emsdk with Rust: https://github.com/emscripten-core/emscripten/pull/24359
    steps:
      - uses: actions/checkout@v6

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.3.1
        env:
          CIBW_PLATFORM: pyodide
          CIBW_BUILD: 'cp313-pyodide_wasm32'

      - uses: actions/upload-artifact@v6
        with:
          name: wheel-wasm
          path: './wheelhouse/sourmash*.whl'

  release:
    name: Publish wheels
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
#    needs: [build_wheels, build_wasm]
    needs: [build_wheels]

    steps:
      - name: Fetch wheels from artifacts
        id: fetch_artifacts
        uses: actions/download-artifact@v6
        with:
          path: 'wheels/'

      # if it matches a Python release tag, upload to github releases
      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ${{steps.fetch_artifacts.outputs.download-path}}/wheel-*/*
