name: build-and-test-perfcounters

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  job:
    # TODO(dominic): Extend this to include compiler and set through env: CC/CXX.
    name: ${{ matrix.os }}.${{ matrix.build_type }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, ubuntu-20.04]
        build_type: ['Release', 'Debug']
    steps:
    - uses: actions/checkout@v2

    - name: install libpfm
      run: sudo apt -y install libpfm4-dev

    - name: setup cmake
      uses: jwlawson/actions-setup-cmake@v1.9
      with:
        cmake-version: '3.5.1'

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

    - name: configure cmake
      shell: bash
      working-directory: ${{ runner.workspace }}/_build
      run: >
        cmake $GITHUB_WORKSPACE
        -DBENCHMARK_ENABLE_LIBPFM=1
        -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 }}

    # Skip testing, for now. It seems perf_event_open does not succeed on the
    # hosting machine, very likely a permissions issue.
    # TODO(mtrofin): Enable test.
    # - name: test
    #   shell: bash
    #   working-directory: ${{ runner.workspace }}/_build
    #   run: ctest -C ${{ matrix.build_type }} --rerun-failed --output-on-failure

  ubuntu-16_04:
    name: ubuntu-16.04.${{ matrix.build_type }}
    runs-on: [ubuntu-latest]
    strategy:
      fail-fast: false
      matrix:
        build_type: ['Release', 'Debug']
    container: ubuntu:16.04
    steps:
      - uses: actions/checkout@v2

      - name: install required bits
        run: |
          apt update
          apt -y install clang cmake g++ git

      - name: install libpfm
        run: apt -y install libpfm4-dev

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

      - name: configure cmake
        shell: bash
        working-directory: ${{ github.workspace }}/_build
        run: >
          cmake $GITHUB_WORKSPACE
          -DBENCHMARK_ENABLE_LIBPFM=1
          -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}

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

    # Skip testing, for now. It seems perf_event_open does not succeed on the
    # hosting machine, very likely a permissions issue.
    # TODO(mtrofin): Enable test.
    # - name: test
    #   shell: bash
    #   working-directory: ${{ runner.workspace }}/_build
    #   run: ctest -C ${{ matrix.build_type }} --rerun-failed --output-on-failure
