blob: b891db9fe3c75e0ec16ca6c6753e062668a55e48 [file] [log] [blame]
Mike Kruskala9f1ea62023-01-24 21:49:33 -08001option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF)
2
3if (protobuf_USE_EXTERNAL_GTEST)
Mike Kruskal7714d572023-05-04 12:57:03 -07004 find_package(GTest REQUIRED CONFIG)
Mike Kruskala9f1ea62023-01-24 21:49:33 -08005else()
6 if (NOT EXISTS "${protobuf_SOURCE_DIR}/third_party/googletest/CMakeLists.txt")
7 message(FATAL_ERROR
8 "Cannot find third_party/googletest directory that's needed to "
9 "build tests. If you use git, make sure you have cloned submodules:\n"
10 " git submodule update --init --recursive\n"
11 "If instead you want to skip tests, run cmake with:\n"
12 " cmake -Dprotobuf_BUILD_TESTS=OFF\n")
13 endif()
14
15 set(googlemock_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googlemock")
16 set(googletest_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googletest")
17 include_directories(
18 ${googlemock_source_dir}
19 ${googletest_source_dir}
20 ${googletest_source_dir}/include
21 ${googlemock_source_dir}/include
22 )
23
24 add_library(gmock ${protobuf_SHARED_OR_STATIC}
25 "${googlemock_source_dir}/src/gmock-all.cc"
26 "${googletest_source_dir}/src/gtest-all.cc"
27 )
28 if (protobuf_BUILD_SHARED_LIBS)
29 set_target_properties(gmock
30 PROPERTIES
31 COMPILE_DEFINITIONS
32 "GTEST_CREATE_SHARED_LIBRARY=1"
33 )
Mike Kruskal2b4e4262023-02-01 11:32:39 -080034
Mike Kruskal2a3ce312023-05-04 10:57:15 -070035 endif()
36 if (protobuf_INSTALL)
37 set(protobuf_INSTALL_TESTS ON)
Mike Kruskala9f1ea62023-01-24 21:49:33 -080038 endif()
39
40 target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT})
41 add_library(gmock_main STATIC "${googlemock_source_dir}/src/gmock_main.cc")
42 target_link_libraries(gmock_main gmock)
43
44 add_library(GTest::gmock ALIAS gmock)
45 add_library(GTest::gmock_main ALIAS gmock_main)
46 add_library(GTest::gtest ALIAS gmock)
47 add_library(GTest::gtest_main ALIAS gmock_main)
48endif()