Add CMake option YAML_USE_SYSTEM_GTEST to use system googletest if available. (#1035)

There is no need to use the embedded gtest code copy in Linux systems, if they already provide the googletest framework system-wide.
Search for it, and fallback to the embedded one if the system one is not detected.

This patch has been also contributed by Simon Quigley <tsimonq2@debian.org>
This commit is contained in:
Gianfranco Costamagna 2023-10-22 07:06:39 +02:00 committed by GitHub
parent f791b955d8
commit 145eec5f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View File

@ -17,6 +17,7 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest] os: [ubuntu-latest, windows-latest, macos-latest]
cxx_standard: [11, 17, 20] cxx_standard: [11, 17, 20]
build: [static, shared] build: [static, shared]
googletest: [build, system]
generator: ["Default Generator", "MinGW Makefiles"] generator: ["Default Generator", "MinGW Makefiles"]
exclude: exclude:
- os: macos-latest - os: macos-latest
@ -25,14 +26,26 @@ jobs:
generator: "MinGW Makefiles" generator: "MinGW Makefiles"
- os: ubuntu-latest - os: ubuntu-latest
generator: "MinGW Makefiles" generator: "MinGW Makefiles"
- os: macos-latest
googletest: system
- os: windows-latest
googletest: system
env: env:
YAML_BUILD_SHARED_LIBS: ${{ matrix.build == 'shared' && 'ON' || 'OFF' }} YAML_BUILD_SHARED_LIBS: ${{ matrix.build == 'shared' && 'ON' || 'OFF' }}
YAML_USE_SYSTEM_GTEST: ${{ matrix.googletest == 'system' && 'ON' || 'OFF' }}
CMAKE_GENERATOR: >- CMAKE_GENERATOR: >-
${{format(matrix.generator != 'Default Generator' && '-G "{0}"' || '', matrix.generator)}} ${{format(matrix.generator != 'Default Generator' && '-G "{0}"' || '', matrix.generator)}}
CMAKE_INSTALL_PREFIX: "${{ github.workspace }}/install-prefix" CMAKE_INSTALL_PREFIX: "${{ github.workspace }}/install-prefix"
CMAKE_BUILD_TYPE: Debug CMAKE_BUILD_TYPE: Debug
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- uses: awalsh128/cache-apt-pkgs-action@latest
if: matrix.os == 'ubuntu-latest'
with:
packages: googletest libgmock-dev libgtest-dev
version: 1.0
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Configure - name: Configure
@ -45,6 +58,7 @@ jobs:
-D CMAKE_INSTALL_PREFIX="${{ env.CMAKE_INSTALL_PREFIX }}" \ -D CMAKE_INSTALL_PREFIX="${{ env.CMAKE_INSTALL_PREFIX }}" \
-D CMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} \ -D CMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} \
-D YAML_BUILD_SHARED_LIBS=${{ env.YAML_BUILD_SHARED_LIBS }} \ -D YAML_BUILD_SHARED_LIBS=${{ env.YAML_BUILD_SHARED_LIBS }} \
-D YAML_USE_SYSTEM_GTEST=${{ env.YAML_USE_SYSTEM_GTEST }} \
-D YAML_CPP_BUILD_TESTS=ON -D YAML_CPP_BUILD_TESTS=ON
- name: Build - name: Build

View File

@ -27,6 +27,7 @@ option(YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${BUILD_SHARED_LIB
option(YAML_CPP_INSTALL "Enable generation of yaml-cpp install targets" ${YAML_CPP_MAIN_PROJECT}) option(YAML_CPP_INSTALL "Enable generation of yaml-cpp install targets" ${YAML_CPP_MAIN_PROJECT})
option(YAML_CPP_FORMAT_SOURCE "Format source" ${YAML_CPP_MAIN_PROJECT}) option(YAML_CPP_FORMAT_SOURCE "Format source" ${YAML_CPP_MAIN_PROJECT})
option(YAML_CPP_DISABLE_UNINSTALL "Disable uninstallation of yaml-cpp" OFF) option(YAML_CPP_DISABLE_UNINSTALL "Disable uninstallation of yaml-cpp" OFF)
option(YAML_USE_SYSTEM_GTEST "Use system googletest if found" OFF)
cmake_dependent_option(YAML_CPP_BUILD_TESTS cmake_dependent_option(YAML_CPP_BUILD_TESTS
"Enable yaml-cpp tests" OFF "Enable yaml-cpp tests" OFF

View File

@ -4,11 +4,17 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(BUILD_MOCK ON CACHE BOOL "" FORCE) set(BUILD_MOCK ON CACHE BOOL "" FORCE)
set(CMAKE_POLICY_DEFAULT_CMP0048 NEW) set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
add_subdirectory( if(YAML_USE_SYSTEM_GTEST)
"${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0" find_package(GTest)
"${CMAKE_CURRENT_BINARY_DIR}/prefix") if (NOT GTEST_FOUND)
message(FATAL_ERROR "system googletest was requested but not found")
include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0/googletest/include") endif()
else()
add_subdirectory(
"${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0"
"${CMAKE_CURRENT_BINARY_DIR}/prefix")
include_directories(SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.11.0/googletest/include")
endif()
set(test-new-api-pattern "new-api/*.cpp") set(test-new-api-pattern "new-api/*.cpp")
set(test-source-pattern "*.cpp" "integration/*.cpp" "node/*.cpp") set(test-source-pattern "*.cpp" "integration/*.cpp" "node/*.cpp")
@ -38,6 +44,7 @@ target_link_libraries(yaml-cpp-tests
PRIVATE PRIVATE
Threads::Threads Threads::Threads
yaml-cpp yaml-cpp
gtest
gmock) gmock)
set_property(TARGET yaml-cpp-tests PROPERTY CXX_STANDARD_REQUIRED ON) set_property(TARGET yaml-cpp-tests PROPERTY CXX_STANDARD_REQUIRED ON)