Skip to content

Commit

Permalink
feat: Utilize globally installed gtest if available (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Feb 17, 2023
1 parent aea3f8d commit efa18a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
56 changes: 33 additions & 23 deletions cpp/cmake/gtest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 3 additions & 3 deletions cpp/cmake/module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function(barretenberg_module MODULE_NAME)
target_link_libraries(
${MODULE_NAME}_test_objects
PRIVATE
gtest
GTest::gtest
${TBB_IMPORTED_TARGETS}
)

Expand Down Expand Up @@ -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}
)

Expand Down

0 comments on commit efa18a6

Please sign in to comment.