eindir-core 0.6.0

Typed primitives for ND objective functions and bounded sampling.
Documentation
name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write

# Release flow (abi3 wheels + towncrier changelog):
# - Cargo.toml enables abi3-py310: wheels job builds one wheel per
#   platform (no --find-interpreter, no per-Python matrix).
# - Release preparation: run `pixi r towncrier-build` after fragments are
#   merged so CHANGELOG.md is up to date under the marker.
# - GH Release uses --generate-notes (supplements the towncrier history).

jobs:
  native-libs:
    name: Native libs (${{ matrix.os }}, ${{ matrix.target }})
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: linux
            runner: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: eindir-core-linux-x86_64
            static_lib: libeindir_core.a
            shared_lib: libeindir_core.so
          - os: macos
            runner: macos-14
            target: aarch64-apple-darwin
            artifact: eindir-core-macos-aarch64
            static_lib: libeindir_core.a
            shared_lib: libeindir_core.dylib
          # x86_64 mac is cross-compiled from the arm64 runner; the macos-13
          # x86_64 image is retired and queues forever.
          - os: macos
            runner: macos-14
            target: x86_64-apple-darwin
            artifact: eindir-core-macos-x86_64
            static_lib: libeindir_core.a
            shared_lib: libeindir_core.dylib

    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cbindgen
        uses: taiki-e/install-action@v2
        with:
          tool: cbindgen

      - name: Build static + shared library
        run: cargo build --release --target ${{ matrix.target }}

      - name: Generate C header
        run: cbindgen --config cbindgen.toml --crate eindir-core --output eindir-core.h

      - name: Package artifacts
        run: |
          mkdir -p staging/include staging/lib
          cp eindir-core.h staging/include/
          cp include/eindir-core.hpp staging/include/
          cp target/${{ matrix.target }}/release/${{ matrix.static_lib }} staging/lib/
          cp target/${{ matrix.target }}/release/${{ matrix.shared_lib }} staging/lib/
          tar czf ${{ matrix.artifact }}.tar.gz -C staging .
          tar tzf ${{ matrix.artifact }}.tar.gz | grep -Fq "./include/eindir-core.h"
          tar tzf ${{ matrix.artifact }}.tar.gz | grep -Fq "./include/eindir-core.hpp"
          tar tzf ${{ matrix.artifact }}.tar.gz | grep -Fq "./lib/${{ matrix.static_lib }}"
          tar tzf ${{ matrix.artifact }}.tar.gz | grep -Fq "./lib/${{ matrix.shared_lib }}"

      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.artifact }}.tar.gz

  sdist:
    name: Build sdist
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist

      - name: Validate sdist
        run: pipx run twine check --strict dist/*

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

  wheels:
    name: Wheels (${{ matrix.os }}, ${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64
          - os: ubuntu-22.04
            target: aarch64
          - os: macos-14
            target: x86_64
          - os: macos-14
            target: aarch64
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          # abi3 enabled in Cargo.toml (pyo3 with "abi3-py310") produces a
          # single wheel per platform that works on all Python >=3.10.
          args: --release --out dist --features python
          manylinux: auto

      - name: Validate wheels
        if: matrix.target != 'aarch64' || runner.os != 'Linux'
        run: pipx run twine check --strict dist/*

      - uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.os }}-${{ matrix.target }}
          path: dist/*.whl

  publish-pypi:
    name: Publish to PyPI
    needs: [sdist, wheels]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    environment:
      name: pypi
      url: https://pypi.org/p/eindir
    permissions:
      id-token: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: '{sdist,wheels-*}'
          merge-multiple: true
          path: dist/

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  publish-crate:
    name: Publish eindir-core to crates.io
    needs: [native-libs]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          ver=$(grep -m1 '^version' Cargo.toml | sed -E 's/.*"(.*)".*/\1/')
          if curl -fsS -A 'eindir-release-ci' "https://crates.io/api/v1/crates/eindir-core/${ver}" >/dev/null 2>&1; then
            echo "eindir-core ${ver} already on crates.io; nothing to upload."
            exit 0
          fi
          cargo publish --no-verify

  github-release:
    name: GitHub Release
    needs: [native-libs, sdist, wheels]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v6

      - uses: actions/download-artifact@v4
        with:
          path: artifacts/
          merge-multiple: true

      - name: Create release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release create "${{ github.ref_name }}" \
            --title "${{ github.ref_name }}" \
            --generate-notes \
            artifacts/*.tar.gz