name: Linux
on:
  workflow_dispatch:
  pull_request:
  push:
    branches: [master]
concurrency:
  group: ${{ github.workflow }}-${{ github.job }}-${{ github.ref }}
  cancel-in-progress: true
defaults:
  run:
    shell: bash -e -l {0}
jobs:
  build:
    runs-on: ubuntu-24.04
    name: ${{ matrix.sys.compiler }} ${{ matrix.sys.version }} - ${{ matrix.sys.blas }}
    strategy:
      fail-fast: false
      matrix:
        sys:
        - {compiler: gcc, version: '11', blas: OpenBLAS}
        - {compiler: gcc, version: '11', blas: mkl}
        - {compiler: gcc, version: '12', blas: OpenBLAS}
        - {compiler: gcc, version: '12', blas: mkl}
        - {compiler: gcc, version: '13', blas: OpenBLAS}
        - {compiler: gcc, version: '13', blas: mkl}
        - {compiler: gcc, version: '14', blas: OpenBLAS}
        - {compiler: gcc, version: '14', blas: mkl}
        - {compiler: clang, version: '17', blas: OpenBLAS}
        - {compiler: clang, version: '18', blas: mkl}
        - {compiler: clang, version: '19', blas: OpenBLAS}
        - {compiler: clang, version: '20', blas: mkl}

    steps:
    - name: Install GCC
      if: matrix.sys.compiler == 'gcc'
      uses: egor-tensin/setup-gcc@v1
      with:
        version: ${{matrix.sys.version}}
        platform: x64

    - name: Install LLVM and Clang
      if: matrix.sys.compiler == 'clang'
      run: |
        wget https://apt.llvm.org/llvm.sh
        chmod +x llvm.sh
        sudo ./llvm.sh ${{matrix.sys.version}}
        sudo apt-get install -y clang-tools-${{matrix.sys.version}}
        sudo update-alternatives --install /usr/bin/clang++                 clang++                     /usr/bin/clang++-${{matrix.sys.version}} 200
        sudo update-alternatives --install /usr/bin/clang                       clang                         /usr/bin/clang-${{matrix.sys.version}} 200
        sudo update-alternatives --install /usr/bin/clang-scan-deps   clang-scan-deps     /usr/bin/clang-scan-deps-${{matrix.sys.version}} 200
        sudo update-alternatives --set clang /usr/bin/clang-${{matrix.sys.version}}
        sudo update-alternatives --set clang++ /usr/bin/clang++-${{matrix.sys.version}}
        sudo update-alternatives --set clang-scan-deps /usr/bin/clang-scan-deps-${{matrix.sys.version}}


    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set conda environment
      uses: mamba-org/setup-micromamba@main
      with:
        environment-name: myenv
        environment-file: environment-dev.yml
        init-shell: bash
        cache-downloads: true

    - name: Install mkl
      if: ${{ matrix.sys.blas == 'mkl' }}
      run: micromamba install mkl

    - name: Install OpenBLAS
      if: ${{ matrix.sys.blas == 'OpenBLAS' }}
      run: micromamba install 'openblas==0.3.29=pthreads*' blas-devel

    - name: Configure using CMake
      run: cmake -Bbuild -DBUILD_TESTS=ON -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib

    - name: Build
      working-directory: build
      run: cmake --build . --target test_xtensor_blas --parallel 8

    - name: Run tests
      working-directory: build/test
      run: ./test_xtensor_blas
