From efa18a621917dc6c38f453825cadc76eb793a73c Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Fri, 17 Feb 2023 08:24:56 -0700 Subject: [PATCH] feat: Utilize globally installed gtest if available (#151) --- cpp/cmake/gtest.cmake | 56 +++++++++++++++++++++++++----------------- cpp/cmake/module.cmake | 6 ++--- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/cpp/cmake/gtest.cmake b/cpp/cmake/gtest.cmake index 96e88741e9..19a7783153 100644 --- a/cpp/cmake/gtest.cmake +++ b/cpp/cmake/gtest.cmake @@ -3,38 +3,48 @@ if(TESTING) include(FetchContent) FetchContent_Declare( - googletest + GTest GIT_REPOSITORY https://github.com/google/googletest.git # Version 1.12.1 is not compatible with WASI-SDK 12 - GIT_TAG release-1.10.0 + GIT_TAG release-1.10.0 + FIND_PACKAGE_ARGS ) - FetchContent_GetProperties(googletest) - if(NOT googletest_POPULATED) - FetchContent_Populate(googletest) - add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL) - endif() + set(BUILD_GMOCK OFF CACHE BOOL "Build with gMock disabled") - # Disable all warning when compiling gtest - target_compile_options( - gtest - PRIVATE - -w - ) + FetchContent_MakeAvailable(GTest) - if(WASM) - target_compile_definitions( + if (NOT GTest_FOUND) + # FetchContent_MakeAvailable calls FetchContent_Populate if `find_package` is unsuccessful + # so these variables will be available if we reach this case + set_property(DIRECTORY ${gtest_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL) + set_property(DIRECTORY ${gtest_BINARY_DIR} PROPERTY EXCLUDE_FROM_ALL) + + # Disable all warning when compiling gtest + target_compile_options( gtest PRIVATE - -DGTEST_HAS_EXCEPTIONS=0 - -DGTEST_HAS_STREAM_REDIRECTION=0) - endif() + -w + ) - mark_as_advanced( - BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS - gmock_build_tests gtest_build_samples gtest_build_tests - gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols - ) + if(WASM) + target_compile_definitions( + gtest + PRIVATE + -DGTEST_HAS_EXCEPTIONS=0 + -DGTEST_HAS_STREAM_REDIRECTION=0 + ) + endif() + + mark_as_advanced( + BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS + gmock_build_tests gtest_build_samples gtest_build_tests + gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols + ) + + add_library(GTest::gtest ALIAS gtest) + add_library(GTest::gtest_main ALIAS gtest_main) + endif() enable_testing() endif() diff --git a/cpp/cmake/module.cmake b/cpp/cmake/module.cmake index 9ff808dd3f..4bb7f526ba 100644 --- a/cpp/cmake/module.cmake +++ b/cpp/cmake/module.cmake @@ -54,7 +54,7 @@ function(barretenberg_module MODULE_NAME) target_link_libraries( ${MODULE_NAME}_test_objects PRIVATE - gtest + GTest::gtest ${TBB_IMPORTED_TARGETS} ) @@ -94,8 +94,8 @@ function(barretenberg_module MODULE_NAME) PRIVATE ${MODULE_LINK_NAME} ${ARGN} - gtest - gtest_main + GTest::gtest + GTest::gtest_main ${TBB_IMPORTED_TARGETS} )