name: Builds

on: [push, pull_request]

jobs:
  tests:
    strategy:
      fail-fast: false
      matrix:
        include:
          - runs-on: ubuntu-latest
            python: 3.6
            arch: x64
          - runs-on: ubuntu-latest
            python: 3.7
            arch: x64
          - runs-on: ubuntu-latest
            python: 3.8
            arch: x64
          - runs-on: ubuntu-latest
            python: 3.9
            arch: x64

          - runs-on: ubuntu-latest
            python: 3.9
            arch: x64
            gcc: 10
            name-extra: ' (GCC 10)'

          - runs-on: ubuntu-latest
            python: 3.6
            arch: x64
            release: true
            name-extra: ' (Release)'
          - runs-on: ubuntu-latest
            python: 3.9
            arch: x64
            release: true
            name-extra: ' (Release)'

          - runs-on: macos-latest
            python: 3.6
            arch: x64
          - runs-on: macos-latest
            python: 3.9
            arch: x64
          - runs-on: macos-latest
            python: 3.9
            arch: x64
            release: true
            name-extra: ' (Release)'

          - runs-on: windows-latest
            python: 3.6
            arch: x64
          - runs-on: windows-latest
            python: 3.9
            arch: x64

    name: Python ${{ matrix.python }}${{ matrix.name-extra }}  ${{ matrix.runs-on }}
    runs-on: ${{ matrix.runs-on }}
    if: startsWith(github.ref, 'refs/heads/docs') != true

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          submodules: recursive

      - name: Setup Python ${{ matrix.python }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python }}
          architecture: ${{ matrix.arch }}

      - name: Install conan
        shell: bash
        run: |
          python3 -m pip install --upgrade pip setuptools
          python3 -m pip install conan

      - name: Setup CMake
        uses: jwlawson/actions-setup-cmake@v1.10

      - name: Install Linux Dependencies
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install libxrandr-dev libxcursor-dev libudev-dev libopenal-dev libflac-dev libvorbis-dev libgl1-mesa-dev libegl1-mesa-dev

      - name: Select GCC
        if: matrix.gcc
        shell: bash
        run: |
          echo "CC=gcc-${{ matrix.gcc }}" >> $GITHUB_ENV
          echo "CXX=g++-${{ matrix.gcc }}" >> $GITHUB_ENV
      - name: Select Clang
        if: matrix.clang
        shell: bash
        run: |
          echo "CC=clang-${{ matrix.clang }}" >> $GITHUB_ENV
          echo "CXX=clang++-${{ matrix.clang }}" >> $GITHUB_ENV

      - name: Add Linux flags
        if: runner.os == 'Linux'
        shell: bash
        run: |
          echo "CFLAGS='-fPIC'" >> $GITHUB_ENV
          echo "CXXFLAGS='-fPIC'" >> $GITHUB_ENV

      - name: Setup pytest annotations
        run: |
          python -m pip install pytest-github-actions-annotate-failures
      - name: Setup GCC annotations
        uses: ammaraskar/gcc-problem-matcher@master

      - name: Setup MSVC annotations
        uses: ammaraskar/msvc-problem-matcher@master

      - name: Configure
        shell: bash
        run: |
          CMAKE_ARCH="${{ (runner.os == 'Windows' && matrix.arch == 'x86') && '-A Win32' || '' }}"
          CMAKE_BUILD_TYPE="${{ matrix.release && 'Release' || 'Debug' }}"
          cmake -S . -B build $CMAKE_ARCH -DENABLE_STRATEGA_BINDINGS=OFF -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE -DPython_EXECUTABLE="$(which python)"
      - name: Compile
        run: |
          cmake --build build -j 2
      - name: Test Mac and Linux
        shell: bash
        if: runner.os == 'Linux' || runner.os == 'macOS'
        run: |
          #GTEST_OUTPUT=xml:test-results/ GTEST_COLOR=1 --test-dir build -C Debug -V ctest
          ctest --test-dir build --verbose
      - name: Test Windows   
        shell: bash
        if: runner.os == 'Windows'
        run: |
          #ctest --test-dir build --GTEST_OUTPUT=xml:test-results --GTEST_COLOR=1 -V -C Debug
          ctest --test-dir build --verbose -C Debug
      - name: Upload test results
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: test_results_xml
          path: build/test-results/**/*.xml