name: CMake Build Matrix

on: [push, pull_request]

env:
  BUILD_TYPE: Release

jobs:
  build:
    name: ${{ matrix.config.name }}
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config:
        - {
            name: "macOS Latest Clang",
            os: macOS,
            cc: "clang", cxx: "clang++"
          }
    steps:
    - uses: actions/checkout@v1

    - name: Configure
      shell: cmake -P {0}
      run: |
        set(ENV{CC} ${{ matrix.config.cc }})
        set(ENV{CXX} ${{ matrix.config.cxx }})
        set(path_separator ":")
        if ("${{ runner.os }}" STREQUAL "Windows")
          set(path_separator ";")
        endif()
        set(ENV{PATH} "$ENV{GITHUB_WORKSPACE}${path_separator}$ENV{PATH}")
        execute_process(
          COMMAND cmake
            -S .
            -B condor_build
            -D CMAKE_BUILD_TYPE=$ENV{BUILD_TYPE}
            -D CMAKE_INSTALL_PREFIX=release_dir
            -G Ninja
            -D CMAKE_MAKE_PROGRAM=ninja
            -D WANT_PYTHON2_BINDINGS=OFF
            -D WITH_VOMS=OFF
          RESULT_VARIABLE result
        )
        if (NOT result EQUAL 0)
          message(FATAL_ERROR "Bad exit status")
        endif()

    - name: Build
      shell: cmake -P {0}
      run: |
        set(ENV{NINJA_STATUS} "[%f/%t %o/sec] ")
        execute_process(
          COMMAND cmake --build condor_build
          RESULT_VARIABLE result
          OUTPUT_VARIABLE output
          ERROR_VARIABLE output
          ECHO_OUTPUT_VARIABLE ECHO_ERROR_VARIABLE
        )
        if (NOT result EQUAL 0)
          string(REGEX MATCH "FAILED:.*$" error_message "${output}")
          string(REPLACE "\n" "%0A" error_message "${error_message}")
          message("::error::${error_message}")
          message(FATAL_ERROR "Build failed")
        endif()

    - name: Install
      run: cmake --install condor_build

    #- name: Run tests
    #  shell: cmake -P {0}
    #  run: |
    #    include(ProcessorCount)
    #    ProcessorCount(N)
    #    set(ENV{CTEST_OUTPUT_ON_FAILURE} "ON")
    #    execute_process(
    #      COMMAND ctest -j ${N}
    #      WORKING_DIRECTORY condor_build
    #      RESULT_VARIABLE result
    #      OUTPUT_VARIABLE output
    #      ERROR_VARIABLE output
    #      ECHO_OUTPUT_VARIABLE ECHO_ERROR_VARIABLE
    #    )
    #    if (NOT result EQUAL 0)
    #      string(REGEX MATCH "[0-9]+% tests.*[0-9.]+ sec.*$" test_results "${output}")
    #      string(REPLACE "\n" "%0A" test_results "${test_results}")
    #      message("::error::${test_results}")
    #      message(FATAL_ERROR "Running tests failed!")
    #    endif()

