name: build-and-test

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  # TODO: add 32-bit builds (g++ and clang++) for ubuntu
  #   (requires g++-multilib and libc6:i386)
  # TODO: add coverage build (requires lcov)
  # TODO: add clang + libc++ builds for ubuntu
  # TODO: add clang + ubsan/asan/msan + libc++ builds for ubuntu
  job:
    name: ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.compiler }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, ubuntu-16.04, ubuntu-20.04, macos-latest]
        build_type: ['Release', 'Debug']
        compiler: [g++, clang++]
        include:
          - displayTargetName: windows-latest-release
            os: windows-latest
            build_type: 'Release'
          - displayTargetName: windows-latest-debug
            os: windows-latest
            build_type: 'Debug'
    steps:
      - uses: actions/checkout@v2

      - name: create build environment
        run: cmake -E make_directory ${{ runner.workspace }}/_build

      - name: configure cmake
        env:
          CXX: ${{ matrix.compiler }}
        shell: bash
        working-directory: ${{ runner.workspace }}/_build
        run: >
          cmake $GITHUB_WORKSPACE
          -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}

      - name: build
        shell: bash
        working-directory: ${{ runner.workspace }}/_build
        run: cmake --build . --config ${{ matrix.build_type }}

      - name: test
        shell: bash
        working-directory: ${{ runner.workspace }}/_build
        run: ctest -C ${{ matrix.build_type }} -VV

  ubuntu-14_04:
    name: ubuntu-14.04.${{ matrix.build_type }}.${{ matrix.compiler }}
    runs-on: [ubuntu-latest]
    strategy:
      fail-fast: false
      matrix:
        build_type: ['Release', 'Debug']
        compiler: [g++-4.8, clang++-3.6]
        include:
          - compiler: g++-6
            build_type: 'Debug'
            run_tests: true
          - compiler: g++-6
            build_type: 'Release'
            run_tests: true
    container: ubuntu:14.04
    steps:
      - uses: actions/checkout@v2

      - name: install required bits
        run: |
          sudo apt update
          sudo apt -y install clang-3.6 cmake3 g++-4.8 git

      - name: install other bits
        if: ${{ matrix.compiler }} == g++-6
        run: |
          sudo apt -y install software-properties-common
          sudo add-apt-repository -y "ppa:ubuntu-toolchain-r/test"
          sudo apt update
          sudo apt -y install g++-6

      - name: create build environment
        run: cmake -E make_directory $GITHUB_WORKSPACE/_build

      - name: configure cmake
        env:
          CXX: ${{ matrix.compiler }}
        shell: bash
        working-directory: ${{ github.workspace }}/_build
        run: >
          cmake $GITHUB_WORKSPACE
          -DBENCHMARK_ENABLE_TESTING=${{ matrix.run_tests }}
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
          -DBENCHMARK_DOWNLOAD_DEPENDENCIES=${{ matrix.run_tests }}

      - name: build
        shell: bash
        working-directory: ${{ github.workspace }}/_build
        run: cmake --build . --config ${{ matrix.build_type }}

      - name: test
        if: ${{ matrix.run_tests }}
        shell: bash
        working-directory: ${{ github.workspace }}/_build
        run: ctest -C ${{ matrix.build_type }} -VV
