---
name: Test & Build

on:
  push:
    branches:
      - '*'
  workflow_dispatch:

# Builds only read the repository; releasing is handled by release.yml.
permissions:
  contents: read

# Cancel superseded runs for the same branch to save CI minutes.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    uses: ./.github/workflows/test.yml

  coverage:
    uses: ./.github/workflows/coverage.yml
    secrets: inherit

  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    needs:
      - test

    strategy:
      fail-fast: false
      matrix:
        include:
          - build: linux
            os: ubuntu-24.04
            target: x86_64-unknown-linux-musl
            platform: linux

          - build: linux-arm
            os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-musl
            platform: linux

          - build: macos
            os: macos-15
            target: x86_64-apple-darwin
            platform: macos

          - build: macos-arm
            os: macos-15
            target: aarch64-apple-darwin
            platform: macos

    steps:
      - name: Checkout
        uses: actions/checkout@v6.0.2

      - name: Get package name from Cargo.toml
        id: package
        shell: bash
        run: |
          PACKAGE_NAME=$(awk -F '"' '/^name = / {print $2; exit}' Cargo.toml)
          echo "name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
          echo "Package name: $PACKAGE_NAME"

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

      - name: Install system deps (musl)
        if: matrix.platform == 'linux'
        run: |
          sudo apt-get update -y
          sudo apt-get install -y musl-dev musl-tools

      - name: Build
        run: cargo build --release --locked --target ${{ matrix.target }}
