Skip to content

Commit

Permalink
Find GTest via CONFIG mode to reference gmock (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Mar 30, 2021
1 parent a947a6e commit 3ddda3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,21 @@ if(BUILD_TESTING)
${CMAKE_BINARY_DIR}/lib/libgmock.a)
elseif(WIN32)
# Make sure we are always bootsrapped with vcpkg on Windows
find_package(GTest)
if(NOT GTEST_FOUND)
find_package(GTest CONFIG)
if(NOT GTest_FOUND)
install_windows_deps()
find_package(GTest REQUIRED)
find_package(GTest CONFIG REQUIRED)
endif()
set(GTEST_BOTH_LIBRARIES GTest::gtest GTest::gtest_main GTest::gmock)
else()
# Prefer GTest installed by OS distro, brew or vcpkg package manager
find_package(GTest REQUIRED)
find_package(GTest CONFIG REQUIRED)
set(GTEST_BOTH_LIBRARIES GTest::gtest GTest::gtest_main GTest::gmock)
endif()
if(GTEST_INCLUDE_DIRS)
include_directories(SYSTEM ${GTEST_INCLUDE_DIRS})
message("GTEST_INCLUDE_DIRS = ${GTEST_INCLUDE_DIRS}")
endif()
include_directories(SYSTEM ${GTEST_INCLUDE_DIRS})
message("GTEST_INCLUDE_DIRS = ${GTEST_INCLUDE_DIRS}")
message("GTEST_BOTH_LIBRARIES = ${GTEST_BOTH_LIBRARIES}")
enable_testing()
# Benchmark respects the CMAKE_PREFIX_PATH
Expand Down
31 changes: 8 additions & 23 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,12 @@ if(BUILD_TESTING)
TARGET recordable_test
TEST_PREFIX exporter.otlp.
TEST_LIST recordable_test)
if(MSVC)
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)
endif()
if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug")
find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib)
else()
find_library(GMOCK_LIB gmock PATH_SUFFIXES lib)
endif()
if(GMOCK_LIB)
add_executable(otlp_exporter_test test/otlp_exporter_test.cc)
target_link_libraries(
otlp_exporter_test
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
opentelemetry_exporter_otprotocol
${GMOCK_LIB}
protobuf::libprotobuf
gRPC::grpc++)
gtest_add_tests(
TARGET otlp_exporter_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_exporter_test)
endif()
add_executable(otlp_exporter_test test/otlp_exporter_test.cc)
target_link_libraries(
otlp_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_exporter_otprotocol protobuf::libprotobuf gRPC::grpc++)
gtest_add_tests(
TARGET otlp_exporter_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_exporter_test)
endif() # BUILD_TESTING

1 comment on commit 3ddda3c

@maxgolov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having some odd issues with Visual Studio 2019 build + gtest from vcpkg after this commit ☹️ tests no longer run.. :-/ I suspect it's because it entirely reverts the changes needed for Windows under if(MSVC)

Please sign in to comment.