name: Rust

on:
  push:
    branches:
      - main
      - master
    tags:
      - '*'
  pull_request:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  build_test:
    strategy:
      fail-fast: false
      matrix:
        lapack: ["lapack-backend,openblas-system",
                 "lapack-backend,openblas-static",
                 "lapack-backend,netlib-system",
                 "lapack-backend,netlib-static",
                 "lapack-backend,intel-mkl-system",
                 ""]
        nalgebra: [nalgebra-backend, ""]
        python: [python, ""]
        
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Install OpenBLAS
      if: matrix.lapack == 'lapack-backend,openblas-system'
      run: |
        sudo apt-get update
        sudo apt-get install libopenblas-dev
    - name: Install libatlas
      if: matrix.lapack == 'lapack-backend,netlib-system'
      run: |
        sudo apt-get update
        sudo apt-get install libatlas-base-dev
    - name: Install Intel MKL
      if: matrix.lapack == 'lapack-backend,intel-mkl-system'
      run: |
        sudo apt-get update
        sudo apt-get install intel-mkl
    - name: Format
      run: cargo fmt --all -- --check
    - name: Clippy
      run: cargo clippy --all-targets --features ${{matrix.lapack}},${{matrix.nalgebra}},${{matrix.python}}, --no-default-features -- -D warnings
    - name: Doc
      # lapack-backend and num-bigfloat are needed to build docs, since otherwise there
      # are warnings due to missing references to these crates
      run: RUSTDOCFLAGS="-D warnings" cargo doc --features lapack-backend,num-bigfloat
    - name: Build
      run: cargo build --verbose --features ${{matrix.lapack}},${{matrix.nalgebra}},${{matrix.python}}, --no-default-features
    - name: Run tests
      env:
        # Opt out of LLD when using intel-mkl. See https://github.com/rust-lang/rust/issues/147156
        # Also explicitly link against the intel-mkl libraries, since without this I was
        # still getting undefined symbol errors when linking (potentially due to -Wl,--as-needed)
        RUSTFLAGS: ${{ matrix.lapack == 'lapack-backend,intel-mkl-system' && '-Clinker-features=-lld -lmkl_intel_lp64 -lmkl_sequential -lmkl_core' || '' }}
        RUSTDOCFLAGS: ${{ matrix.lapack == 'lapack-backend,intel-mkl-system' && '-Clinker-features=-lld -Clink-arg=-lmkl_intel_lp64 -Clink-arg=-lmkl_sequential -Clink-arg=-lmkl_core' || '' }}
      run: cargo test --verbose --features ${{matrix.lapack}},${{matrix.nalgebra}},${{matrix.python}}, --no-default-features

  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    if: "startsWith(github.ref, 'refs/tags/')"
    needs: build_test
    steps:
      - uses: actions/checkout@v4
      - uses: katyo/publish-crates@v2
        with:
          registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
