From a3cdd9af21f1e810925e2505118f37ebbd450160 Mon Sep 17 00:00:00 2001 From: Luca Weiss Date: Sun, 4 Apr 2021 14:52:45 +0200 Subject: [PATCH 1/2] GMock: Extract version detection code into function --- src/GMock/GMockConfig.cmake | 72 +++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/GMock/GMockConfig.cmake b/src/GMock/GMockConfig.cmake index 514c775..8cd44e8 100644 --- a/src/GMock/GMockConfig.cmake +++ b/src/GMock/GMockConfig.cmake @@ -37,6 +37,43 @@ # NOTE: Due to the way this package finder is implemented, do not attempt # to find the GMock package more than once. +macro(add_gtest_version_defines) + # get a gtest version string that the regex will match + pkg_check_modules(GTEST_PKGCONFIG "gtest") + if (GTEST_PKGCONFIG_FOUND) + set(GTEST_VERSION_STR ${GTEST_PKGCONFIG_VERSION}) + set(GTEST_DETECTION_METHOD "pkg-config") + elseif (DEFINED ENV{GTEST_VERSION}) + set(GTEST_VERSION_STR $ENV{GTEST_VERSION}) + set(GTEST_DETECTION_METHOD "GTEST_VERSION environment variable") + else() + message(WARNING "Could not detect GTest version, Assuming v${GTEST_VERSION_STR} (or compatible) and hoping for the best") + add_definitions(-DGTEST_VERSION_UNKNOWN) + set(GTEST_DETECTION_METHOD "best guess, could not detect") + endif() + + string(REGEX MATCH + "([0-9]+)\\.([0-9]+)\\.([0-9]+)" GTEST_VERSION_PARSED + ${GTEST_VERSION_STR}) + if (GTEST_VERSION_PARSED) + set(GTEST_VERSION_MAJOR ${CMAKE_MATCH_1}) + set(GTEST_VERSION_MINOR ${CMAKE_MATCH_2}) + set(GTEST_VERSION_PATCH ${CMAKE_MATCH_3}) + message("-- Using GTest v${GTEST_VERSION_MAJOR}.${GTEST_VERSION_MINOR}.${GTEST_VERSION_PATCH} (parsed from ${GTEST_DETECTION_METHOD})") + else() + # fallback to 1.8.0 + message(WARNING "Could not parse GTest version: ${GTEST_VERSION_STR} (${GTEST_DETECTION_METHOD}), Assuming v1.8.0 (or compatible) and hoping for the best") + set(GTEST_VERSION_MAJOR 1) + set(GTEST_VERSION_MINOR 8) + set(GTEST_VERSION_PATCH 0) + add_definitions(-DGTEST_VERSION_UNKNOWN) + endif() + + add_definitions(-DGTEST_VERSION_MAJOR=${GTEST_VERSION_MAJOR}) + add_definitions(-DGTEST_VERSION_MINOR=${GTEST_VERSION_MINOR}) + add_definitions(-DGTEST_VERSION_PATCH=${GTEST_VERSION_PATCH}) +endmacro() + find_package(Threads) find_package(PkgConfig) @@ -54,40 +91,7 @@ else() endif() message("-- Using ${GMOCK_SOURCE_DIR}/ as gmock source directory") -# get a gtest version string that the regex will match -pkg_check_modules(GTEST_PKGCONFIG "gtest") -if (GTEST_PKGCONFIG_FOUND) - set(GTEST_VERSION_STR ${GTEST_PKGCONFIG_VERSION}) - set(GTEST_DETECTION_METHOD "pkg-config") -elseif (DEFINED ENV{GTEST_VERSION}) - set(GTEST_VERSION_STR $ENV{GTEST_VERSION}) - set(GTEST_DETECTION_METHOD "GTEST_VERSION environment variable") -else() - message(WARNING "Could not detect GTest version, Assuming v${GTEST_VERSION_STR} (or compatible) and hoping for the best") - add_definitions(-DGTEST_VERSION_UNKNOWN) - set(GTEST_DETECTION_METHOD "best guess, could not detect") -endif() - -string(REGEX MATCH - "([0-9]+)\\.([0-9]+)\\.([0-9]+)" GTEST_VERSION_PARSED - ${GTEST_VERSION_STR}) -if (GTEST_VERSION_PARSED) - set(GTEST_VERSION_MAJOR ${CMAKE_MATCH_1}) - set(GTEST_VERSION_MINOR ${CMAKE_MATCH_2}) - set(GTEST_VERSION_PATCH ${CMAKE_MATCH_3}) - message("-- Using GTest v${GTEST_VERSION_MAJOR}.${GTEST_VERSION_MINOR}.${GTEST_VERSION_PATCH} (parsed from ${GTEST_DETECTION_METHOD})") -else() - # fallback to 1.8.0 - message(WARNING "Could not parse GTest version: ${GTEST_VERSION_STR} (${GTEST_DETECTION_METHOD}), Assuming v1.8.0 (or compatible) and hoping for the best") - set(GTEST_VERSION_MAJOR 1) - set(GTEST_VERSION_MINOR 8) - set(GTEST_VERSION_PATCH 0) - add_definitions(-DGTEST_VERSION_UNKNOWN) -endif() - -add_definitions(-DGTEST_VERSION_MAJOR=${GTEST_VERSION_MAJOR}) -add_definitions(-DGTEST_VERSION_MINOR=${GTEST_VERSION_MINOR}) -add_definitions(-DGTEST_VERSION_PATCH=${GTEST_VERSION_PATCH}) +add_gtest_version_defines() # We add -g so we get debug info for the gtest stack frames with gdb. # The warnings are suppressed so we get a noise-free build for gtest and gmock if the caller -- 2.32.0