1option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF) 2 3if (protobuf_USE_EXTERNAL_GTEST) 4 find_package(GTest REQUIRED CONFIG) 5else() 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 ) 34 35 endif() 36 if (protobuf_INSTALL) 37 set(protobuf_INSTALL_TESTS ON) 38 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() 49