name: CMake

on:
  pull_request:
  push:

env:
  config: Release
  build_dir: build

defaults:
  run:
    shell: bash

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ['ubuntu-latest', 'windows-latest', 'macos-latest']

    steps:
    - uses: actions/checkout@v2

    - name: Configure
      working-directory: ${{ github.workspace }}/dlib/test
      run: |
        if [ "$RUNNER_OS" == "macOS" ]; then
          # MacOS machines often come with low quality BLAS libraries installed, so don't use those.
          cmake . -B ${{ env.build_dir }} -DDLIB_USE_BLAS=0 -DDLIB_USE_LAPACK=0
        else
          cmake . -B ${{ env.build_dir }}
        fi;

    - name: Build just tests
      working-directory: ${{ github.workspace }}/dlib/test
      run: cmake --build ${{ env.build_dir }} --config ${{ env.config }} --target dtest --parallel 4

    - name: Test
      working-directory: ${{ github.workspace }}/dlib/test/${{ env.build_dir }}
      run: |
        if [ "$RUNNER_OS" == "Windows" ]; then
          ./${{ env.config }}/dtest.exe --runall -q
        elif [ "$RUNNER_OS" == "macOS" ]; then
          ./dtest --runall --no_test_timer -q
        else
          ./dtest --runall -q
        fi;

    - name: Build examples, etc
      if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
      working-directory: ${{ github.workspace }}/dlib/test
      run: cmake --build ${{ env.build_dir }} --config ${{ env.config }} --parallel 2
