name: C++

on:
  push:
    branches:
    - master
    paths:
    - ".github/workflows/build_cpp.yml"
    - "**.cpp"
    - "**.h"
    - "**.c"
    - "**.cu"
    - "**.cmake"
    - "**CMakeLists.txt"
  pull_request:
    branches:
    - master
    paths:
    - ".github/workflows/build_cpp.yml"
    - "**.cpp"
    - "**.h"
    - "**.c"
    - "**.cu"
    - "**.cmake"
    - "**CMakeLists.txt"

defaults:
  run:
    shell: bash
    working-directory: dlib/test

jobs:
  ubuntu-22-04-gcc-default-cmake-3-10-ffmpeg5:
    runs-on: 'ubuntu-22.04'
    steps:
    - uses: actions/checkout@v2

    - name: Install dependencies
      run: |
        sudo apt update
        sudo apt install libwebp-dev make yasm
        
    - name: Cache cmake 3.10.0
      uses: actions/cache@v3
      id: cache-cmake-download
      with:
        # cache this folder:
        path: ~/cmake-3.10.0-Linux-x86_64
        key: cmake-3.10.0_try3
        
    - run: |
        # Get the minimum version of cmake dlib supports
        wget https://cmake.org/files/v3.10/cmake-3.10.0-Linux-x86_64.tar.gz
        tar -xf cmake-3.10.0-Linux-x86_64.tar.gz -C ~
      if: steps.cache-cmake-download.outputs.cache-hit != 'true'

    - name: Cache FFmpeg 5
      uses: actions/cache@v3
      id: cache-ffmpeg5
      with:
        path: /home/runner/ffmpeg-n5.1.3_installation
        key: ffmpeg-n5.1.3_try4
        
    - name: Build FFmpeg 5
      if: steps.cache-ffmpeg5.outputs.cache-hit != 'true'
      run: |
        wget https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n5.1.3.tar.gz
        tar -xf n5.1.3.tar.gz
        cd FFmpeg-n5.1.3
        ./configure --prefix=/home/runner/ffmpeg-n5.1.3_installation --disable-doc --disable-programs
        make -j4
        make install
        cd ..
        
    - name: Configure
      run: |
        mkdir build
        cd build
        ~/cmake-3.10.0-Linux-x86_64/bin/cmake -DCMAKE_PREFIX_PATH=/home/runner/ffmpeg-n5.1.3_installation .. 
    
    - name: Build just tests
      run: |
        cd build
        make -j4 dtest
        
    - name: Test
      run: build/dtest --runall -q
      
    - name: Build examples, etc
      run: |
        cd build
        make -j2

  ubuntu-latest-gcc-11-blas:
    runs-on: 'ubuntu-latest'
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: |
        sudo apt update
        sudo apt install libwebp-dev libavformat-dev libavcodec-dev libavdevice-dev libavfilter-dev libswresample-dev libswscale-dev libavutil-dev 
        sudo apt install libopenblas-dev liblapack-dev
    - name: Install gcc 11
      run: |
        sudo apt install gcc-11 g++-11
        sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11
    - name: Configure
      run: cmake ${{ github.workspace }}/dlib/test -B build
    - name: Build just tests
      run: cmake --build build --config Release --target dtest --parallel 4
    - name: Test
      run: build/dtest --runall -q
    - name: Build examples, etc
      run: cmake --build build --config Release --parallel 2

    # Test the BLAS bindings
    - name: Configure BLAS binding tests
      run: cmake ${{ github.workspace }}/dlib/test/blas_bindings -B build_blas_bindings
    - name: Build blas binding tests
      run: cmake --build build_blas_bindings --config Debug --parallel 4
    - name: Test BLAS bindings
      run: build_blas_bindings/dtest --runall -q

  ubuntu-latest-clang-default-avx:
    runs-on: 'ubuntu-latest'
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
      run: |
        sudo apt update
        sudo apt install libwebp-dev libavformat-dev libavcodec-dev libavdevice-dev libavfilter-dev libswresample-dev libswscale-dev libavutil-dev 
    - name: Configure
      run: |
        export CC=/usr/bin/clang
        export CXX=/usr/bin/clang++
        cmake ${{ github.workspace }}/dlib/test -B build -DUSE_AVX_INSTRUCTIONS=1
    - name: Build just tests
      run: cmake --build build --config Release --target dtest --parallel 4
    - name: Test
      run: build/dtest --runall -q
    - name: Build examples, etc
      run: cmake --build build --config Release --parallel 2

  ubuntu-22-04-ffmpeg7:
    runs-on: 'ubuntu-22.04'
    steps:
    - uses: actions/checkout@v2

    - name: Install dependencies
      run: |
        sudo apt update
        sudo apt install make yasm 

    - name: Cache FFmpeg 7
      uses: actions/cache@v3
      id: cache-ffmpeg7
      with:
        path: /home/runner/ffmpeg-n7.0.1_installation
        key: ffmpeg-n7.0.1_try2
        
    - name: Build FFmpeg 7
      if: steps.cache-ffmpeg7.outputs.cache-hit != 'true'
      run: |
        wget https://github.com/FFmpeg/FFmpeg/archive/refs/tags/n7.0.1.tar.gz
        tar -xf n7.0.1.tar.gz
        cd FFmpeg-n7.0.1
        ./configure --prefix=/home/runner/ffmpeg-n7.0.1_installation --disable-doc --disable-programs
        make -j4
        make install
        cd ..
        
    - name: Configure
      run: cmake . -B build -DCMAKE_PREFIX_PATH=/home/runner/ffmpeg-n7.0.1_installation
    - name: Build ffmpeg example
      run: cmake --build build --config Release --target ffmpeg_video_muxing_ex --parallel 4

  windows-latest:
    runs-on: 'windows-latest'
    steps:
    - uses: actions/checkout@v2
    - name: Configure
      run: |
        # don't use CMake 3.25.0 https://gitlab.kitware.com/cmake/cmake/-/issues/23975
        pip3 install cmake==3.24.0
        cmake . -B build
    - name: Build just tests
      run: cmake --build build --config Release --target dtest --parallel 4
    - name: Test
      run: build/Release/dtest.exe --runall -q
    - name: Build ancillary tools
      run: cmake --build build --config Release --target imglab htmlify dtoc --parallel 4

  windows-2019:
    runs-on: 'windows-2019'
    steps:
    - uses: actions/checkout@v2
    - name: Configure
      run: |
        # don't use CMake 3.25.0 https://gitlab.kitware.com/cmake/cmake/-/issues/23975
        pip3 install cmake==3.24.0
        cmake . -B build
    - name: Build just tests
      run: cmake --build build --config Debug --target dtest --parallel 4
    - name: Build ancillary tools
      run: cmake --build build --config Release --target imglab htmlify dtoc --parallel 4
  
  # Disable this because macos targets aren't working on github actions right now.
  #macos-latest:
  #  runs-on: 'macos-latest'
  #  steps:
  #  - uses: actions/checkout@v2
  #  - name: Configure
  #    # MacOS machines often come with low quality BLAS libraries installed, so don't use those.
  #    run: cmake ${{ github.workspace }}/dlib/test -B build -DDLIB_USE_BLAS=0 -DDLIB_USE_LAPACK=0
  #  - name: Build just tests
  #    run: cmake --build build --config Release --target dtest --parallel 4
  #  - name: Test
  #    run: build/dtest --runall --no_test_timer -q
  #  - name: Build examples, etc
  #    run: cmake --build build --config Release --parallel 2
