---
name: Linux Build

# yamllint disable-line rule:truthy
on:
  - push

jobs:
  build:
    name: ${{matrix.buildname}}
    runs-on: ${{matrix.os}}
    strategy:
      matrix:
        include:
          - os: ubuntu-20.04
            buildname: Linux-GCC
            build_type: Debug
            compiler: g++
            coverage: true
            compiler_flags: >-
              --coverage
              -fprofile-arcs
              -ftest-coverage
              -fprofile-abs-path
            compiler_package: g++

          - os: ubuntu-20.04
            buildname: Linux-Clang
            build_type: Debug
            compiler: clang++
            compiler_package: clang

    steps:
      - name: Checkout source code
        uses: actions/checkout@v2
        with:
          submodules: false
          fetch-depth: 1

      - name: Install dependencies
        uses: nick-invision/retry@v2
        with:
          timeout_minutes: 10
          retry_wait_seconds: 30
          max_attempts: 3
          command: >-
            '${{github.workspace}}/utils/github/install-linux.sh'
            '${{matrix.compiler_package}}'

      - name: Build with CMake
        uses: lukka/run-cmake@v3
        with:
          cmakeListsOrSettingsJson: CMakeListsTxtAdvanced
          cmakeListsTxtPath: '${{github.workspace}}/CMakeLists.txt'
          cmakeBuildType: Release
          cmakeGenerator: Ninja
          buildDirectory: '${{runner.workspace}}/build'
          cmakeAppendedArgs: >-
            -DWITH_TESTS=TRUE
            -DPEDANTIC=TRUE
            -DCMAKE_BUILD_TYPE='${{matrix.build_type}}'
            -DCMAKE_INSTALL_PREFIX='${{runner.workspace}}/install'
            -DCMAKE_CXX_FLAGS='${{matrix.compiler_flags}}'
            -DCMAKE_C_FLAGS='${{matrix.compiler_flags}}'
            -DCMAKE_CXX_COMPILER='${{matrix.compiler}}'

      - name: Install with CMake
        run: cmake --install '${{runner.workspace}}/build'

      - name: Create gnupg directory for tests
        run: mkdir -p ~/.gnupg

      - name: Run tests
        working-directory: '${{runner.workspace}}/install/bin'
        run: '${{github.workspace}}/utils/github/test-linux.sh'

      - name: Update coverage
        if: matrix.coverage
        env:
          COVERALLS_REPO_TOKEN: '${{secrets.COVERALLS_REPO_TOKEN}}'
        run: >-
          '${{github.workspace}}/utils/github/coverage-linux.sh'
          '${{runner.workspace}}/build'
