name: CI

on: [push, pull_request]

jobs:
  checkers:
    name: Run static checkers
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Run clang-format style check (.c and .h)
      uses: jidicula/clang-format-action@v3.0.0

  build:
    name: ${{ matrix.cmake-build-type }}-build [${{ matrix.compiler }}, cmake-${{ matrix.cmake-version }} sanitizer="${{ matrix.sanitizer }}"]
    runs-on: ubuntu-18.04
    strategy:
      fail-fast: false
      matrix:
        compiler: [gcc-9, clang-10]
        cmake-version: [3.19]
        cmake-build-type: [Release, RelWithDebInfo]
        sanitizer: ["", thread, undefined, leak, address]
        include:
          - compiler: gcc-4.8
            cmake-version: 3.11
            cmake-build-type: Release
            sanitizer: ""
          - compiler: gcc-5
            cmake-version: 3.12
            cmake-build-type: Release
            sanitizer: ""
          - compiler: gcc-6
            cmake-version: 3.13
            cmake-build-type: Release
            sanitizer: ""
          - compiler: gcc-7
            cmake-version: 3.14
            cmake-build-type: Release
            sanitizer: ""
          - compiler: gcc-8
            cmake-version: 3.15
            cmake-build-type: Release
            sanitizer: ""
          - compiler: clang-3.9
            cmake-version: 3.16
            cmake-build-type: Release
            sanitizer: ""
          - compiler: clang-7
            cmake-version: 3.17
            cmake-build-type: Release
            sanitizer: ""
          - compiler: clang-9
            cmake-version: 3.18
            cmake-build-type: Release
            sanitizer: ""

    steps:
    - name: Prepare
      run: sudo apt install libevent-dev libuv1-dev libev-dev libglib2.0-dev ${{ matrix.compiler }}
    - name: Setup cmake
      # Temporary disabled due to actions-setup-cmake/issues/21
      # uses: jwlawson/actions-setup-cmake@v1.6
      # with:
      #   cmake-version: ${{ matrix.cmake-version }}
      run: |
        wget https://cmake.org/files/v${{ matrix.cmake-version }}/cmake-${{ matrix.cmake-version }}.0-Linux-x86_64.sh -O /tmp/cmake.sh
        sudo sh /tmp/cmake.sh --prefix=/usr/local/ --exclude-subdir
        # Make sure we use correct version
        cmake --version | grep -c ${{ matrix.cmake-version }}.0
    - uses: actions/checkout@v2
    - name: Create build folder
      run: cmake -E make_directory build
    - name: Generate makefiles
      shell: bash
      env:
        CC: ${{ matrix.compiler }}
      working-directory: build
      run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DENABLE_SSL=ON -DUSE_SANITIZER=${{ matrix.sanitizer }} ..
    - name: Build
      shell: bash
      working-directory: build
      run: VERBOSE=1 make
    - name: Setup clusters
      shell: bash
      working-directory: build
      run: make start
    - name: Wait for clusters to start..
      uses: kibertoad/wait-action@1.0.1
      with:
        time: '20s'
    - name: Run tests
      shell: bash
      working-directory: build
      run: make CTEST_OUTPUT_ON_FAILURE=1 test
    - name: Teardown clusters
      working-directory: build
      shell: bash
      run: make stop
    - name: Build examples
      shell: bash
      env:
        CC: ${{ matrix.compiler }}
      run: |
        examples/using_cmake_externalproject/build.sh
        examples/using_cmake_separate/build.sh
        examples/using_make/build.sh
