Mike Kruskal | a9f1ea6 | 2023-01-24 21:49:33 -0800 | [diff] [blame] | 1 | option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF) |
| 2 | |
| 3 | if (protobuf_USE_EXTERNAL_GTEST) |
Mike Kruskal | 7714d57 | 2023-05-04 12:57:03 -0700 | [diff] [blame] | 4 | find_package(GTest REQUIRED CONFIG) |
Mike Kruskal | a9f1ea6 | 2023-01-24 21:49:33 -0800 | [diff] [blame] | 5 | else() |
| 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 Kruskal | 2b4e426 | 2023-02-01 11:32:39 -0800 | [diff] [blame] | 34 | |
Mike Kruskal | 2a3ce31 | 2023-05-04 10:57:15 -0700 | [diff] [blame] | 35 | endif() |
| 36 | if (protobuf_INSTALL) |
| 37 | set(protobuf_INSTALL_TESTS ON) |
Mike Kruskal | a9f1ea6 | 2023-01-24 21:49:33 -0800 | [diff] [blame] | 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) |
| 48 | endif() |