Florin Crișan | 9ebb317 | 2022-02-08 06:41:29 +0200 | [diff] [blame] | 1 | option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 2 | |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 3 | option(protobuf_REMOVE_INSTALLED_HEADERS |
| 4 | "Remove local headers so that installed ones are used instead" OFF) |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 5 | |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 6 | option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH |
| 7 | "Using absolute test_plugin path in tests" ON) |
Konstantin Podsvirov | 7155629 | 2016-06-01 17:00:08 +0300 | [diff] [blame] | 8 | mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 9 | |
Florin Crișan | 9ebb317 | 2022-02-08 06:41:29 +0200 | [diff] [blame] | 10 | if (protobuf_USE_EXTERNAL_GTEST) |
| 11 | find_package(GTest REQUIRED) |
| 12 | else() |
Adam Cozzette | b20209f | 2022-03-10 18:36:42 +0000 | [diff] [blame] | 13 | if (NOT EXISTS "${protobuf_SOURCE_DIR}/third_party/googletest/CMakeLists.txt") |
Florin Crișan | 9ebb317 | 2022-02-08 06:41:29 +0200 | [diff] [blame] | 14 | message(FATAL_ERROR |
| 15 | "Cannot find third_party/googletest directory that's needed to " |
| 16 | "build tests. If you use git, make sure you have cloned submodules:\n" |
| 17 | " git submodule update --init --recursive\n" |
| 18 | "If instead you want to skip tests, run cmake with:\n" |
| 19 | " cmake -Dprotobuf_BUILD_TESTS=OFF\n") |
| 20 | endif() |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 21 | |
Adam Cozzette | b20209f | 2022-03-10 18:36:42 +0000 | [diff] [blame] | 22 | set(googlemock_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googlemock") |
| 23 | set(googletest_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googletest") |
Florin Crișan | 9ebb317 | 2022-02-08 06:41:29 +0200 | [diff] [blame] | 24 | include_directories( |
| 25 | ${googlemock_source_dir} |
| 26 | ${googletest_source_dir} |
| 27 | ${googletest_source_dir}/include |
| 28 | ${googlemock_source_dir}/include |
| 29 | ) |
| 30 | |
| 31 | add_library(gmock STATIC |
| 32 | "${googlemock_source_dir}/src/gmock-all.cc" |
| 33 | "${googletest_source_dir}/src/gtest-all.cc" |
| 34 | ) |
| 35 | target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT}) |
| 36 | add_library(gmock_main STATIC "${googlemock_source_dir}/src/gmock_main.cc") |
| 37 | target_link_libraries(gmock_main gmock) |
| 38 | |
| 39 | add_library(GTest::gmock ALIAS gmock) |
| 40 | add_library(GTest::gmock_main ALIAS gmock_main) |
| 41 | endif() |
Feng Xiao | 818c5ee | 2015-06-15 21:42:57 -0700 | [diff] [blame] | 42 | |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 43 | include(${protobuf_SOURCE_DIR}/src/file_lists.cmake) |
| 44 | |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 45 | set(lite_test_protos |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 46 | ${protobuf_lite_test_protos_proto_srcs} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 47 | ) |
| 48 | |
| 49 | set(tests_protos |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 50 | ${protobuf_test_protos_proto_srcs} |
| 51 | ${compiler_test_protos_files} |
| 52 | ${util_test_protos_files} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 53 | ) |
| 54 | |
| 55 | macro(compile_proto_file filename) |
David L. Jones | 4e5e8d3 | 2022-04-28 14:01:34 -0700 | [diff] [blame] | 56 | string(REPLACE .proto .pb.cc pb_file ${filename}) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 57 | add_custom_command( |
David L. Jones | 4e5e8d3 | 2022-04-28 14:01:34 -0700 | [diff] [blame] | 58 | OUTPUT ${pb_file} |
| 59 | DEPENDS ${protobuf_PROTOC_EXE} ${filename} |
| 60 | COMMAND ${protobuf_PROTOC_EXE} ${filename} |
Adam Cozzette | b20209f | 2022-03-10 18:36:42 +0000 | [diff] [blame] | 61 | --proto_path=${protobuf_SOURCE_DIR}/src |
| 62 | --cpp_out=${protobuf_SOURCE_DIR}/src |
Joshua Haberman | b99994d | 2020-03-31 16:25:37 -0700 | [diff] [blame] | 63 | --experimental_allow_proto3_optional |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 64 | ) |
| 65 | endmacro(compile_proto_file) |
| 66 | |
| 67 | set(lite_test_proto_files) |
| 68 | foreach(proto_file ${lite_test_protos}) |
| 69 | compile_proto_file(${proto_file}) |
David L. Jones | 4e5e8d3 | 2022-04-28 14:01:34 -0700 | [diff] [blame] | 70 | set(lite_test_proto_files ${lite_test_proto_files} ${pb_file}) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 71 | endforeach(proto_file) |
| 72 | |
| 73 | set(tests_proto_files) |
| 74 | foreach(proto_file ${tests_protos}) |
| 75 | compile_proto_file(${proto_file}) |
David L. Jones | 4e5e8d3 | 2022-04-28 14:01:34 -0700 | [diff] [blame] | 76 | set(tests_proto_files ${tests_proto_files} ${pb_file}) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 77 | endforeach(proto_file) |
| 78 | |
David L. Jones | 9e09343 | 2022-04-14 17:36:14 -0700 | [diff] [blame] | 79 | add_library(protobuf-lite-test-common STATIC |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 80 | ${lite_test_util_srcs} ${lite_test_proto_files}) |
Mike Kruskal | cac9765 | 2022-08-12 16:41:00 -0700 | [diff] [blame] | 81 | target_include_directories(protobuf-lite-test-common PRIVATE ${ABSL_ROOT_DIR}) |
| 82 | target_link_libraries(protobuf-lite-test-common |
| 83 | ${protobuf_LIB_PROTOBUF_LITE} ${protobuf_ABSL_USED_TARGETS} GTest::gmock) |
David L. Jones | a3347c2 | 2022-03-29 11:48:16 -0700 | [diff] [blame] | 84 | |
Joshua Haberman | e5c570b | 2021-09-09 08:21:42 -0700 | [diff] [blame] | 85 | set(common_test_files |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 86 | ${test_util_hdrs} |
| 87 | ${test_util_srcs} |
| 88 | ${mock_code_generator_srcs} |
| 89 | ${testing_srcs} |
Joshua Haberman | e5c570b | 2021-09-09 08:21:42 -0700 | [diff] [blame] | 90 | ) |
| 91 | |
David L. Jones | 9e09343 | 2022-04-14 17:36:14 -0700 | [diff] [blame] | 92 | add_library(protobuf-test-common STATIC |
David L. Jones | a3347c2 | 2022-03-29 11:48:16 -0700 | [diff] [blame] | 93 | ${common_test_files} ${tests_proto_files}) |
Mike Kruskal | cac9765 | 2022-08-12 16:41:00 -0700 | [diff] [blame] | 94 | target_include_directories(protobuf-test-common PRIVATE ${ABSL_ROOT_DIR}) |
| 95 | target_link_libraries(protobuf-test-common |
| 96 | ${protobuf_LIB_PROTOBUF} ${protobuf_ABSL_USED_TARGETS} GTest::gmock) |
David L. Jones | a3347c2 | 2022-03-29 11:48:16 -0700 | [diff] [blame] | 97 | |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 98 | set(tests_files |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 99 | ${protobuf_test_files} |
| 100 | ${compiler_test_files} |
| 101 | ${annotation_test_util_srcs} |
| 102 | ${io_test_files} |
| 103 | ${util_test_files} |
| 104 | ${stubs_test_files} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 105 | ) |
| 106 | |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 107 | if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) |
| 108 | add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>") |
| 109 | endif() |
| 110 | |
Ivan Shynkarenka | feb183d | 2018-05-03 01:47:17 +0300 | [diff] [blame] | 111 | if(MINGW) |
Bo Yang | 509aee4 | 2021-08-17 17:26:34 -0700 | [diff] [blame] | 112 | set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing") |
Roman Popov | a69dfe6 | 2018-07-30 21:55:44 -0700 | [diff] [blame] | 113 | |
| 114 | # required for tests on MinGW Win64 |
| 115 | if (CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 116 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216") |
| 117 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj") |
| 118 | endif() |
| 119 | |
Ivan Shynkarenka | feb183d | 2018-05-03 01:47:17 +0300 | [diff] [blame] | 120 | endif() |
| 121 | |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 122 | if(protobuf_TEST_XML_OUTDIR) |
| 123 | if(NOT "${protobuf_TEST_XML_OUTDIR}" MATCHES "[/\\]$") |
| 124 | string(APPEND protobuf_TEST_XML_OUTDIR "/") |
| 125 | endif() |
| 126 | set(protobuf_GTEST_ARGS "--gtest_output=xml:${protobuf_TEST_XML_OUTDIR}") |
David L. Jones | c4ddd84 | 2022-04-28 15:17:51 -0700 | [diff] [blame] | 127 | else() |
| 128 | set(protobuf_GTEST_ARGS) |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 129 | endif() |
| 130 | |
David L. Jones | a3347c2 | 2022-03-29 11:48:16 -0700 | [diff] [blame] | 131 | add_executable(tests ${tests_files}) |
Stephen Kennedy | 8495372 | 2022-02-02 23:10:15 +0000 | [diff] [blame] | 132 | if (MSVC) |
| 133 | target_compile_options(tests PRIVATE |
| 134 | /wd4146 # unary minus operator applied to unsigned type, result still unsigned |
| 135 | ) |
| 136 | endif() |
Matt Kulukundis | c7b2991 | 2022-08-02 10:20:58 -0400 | [diff] [blame] | 137 | target_link_libraries(tests protobuf-lite-test-common protobuf-test-common ${protobuf_LIB_PROTOC} ${protobuf_LIB_PROTOBUF} GTest::gmock_main) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 138 | |
| 139 | set(test_plugin_files |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 140 | ${test_plugin_files} |
| 141 | ${mock_code_generator_srcs} |
| 142 | ${testing_srcs} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 143 | ) |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 144 | |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 145 | add_executable(test_plugin ${test_plugin_files}) |
Mike Kruskal | 701dd83 | 2022-08-20 14:22:08 -0700 | [diff] [blame^] | 146 | target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR}) |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 147 | target_link_libraries(test_plugin ${protobuf_LIB_PROTOC} ${protobuf_LIB_PROTOBUF} GTest::gmock) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 148 | |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 149 | add_executable(lite-test ${protobuf_lite_test_files}) |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 150 | target_link_libraries(lite-test protobuf-lite-test-common ${protobuf_LIB_PROTOBUF_LITE} GTest::gmock_main) |
Feng Xiao | b17ec3c | 2015-08-23 17:50:38 -0700 | [diff] [blame] | 151 | |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 152 | add_test(NAME lite-test |
| 153 | COMMAND lite-test ${protobuf_GTEST_ARGS}) |
Feng Xiao | b17ec3c | 2015-08-23 17:50:38 -0700 | [diff] [blame] | 154 | |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 155 | add_custom_target(check |
| 156 | COMMAND tests |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 157 | DEPENDS tests lite-test test_plugin |
Adam Cozzette | b20209f | 2022-03-10 18:36:42 +0000 | [diff] [blame] | 158 | WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) |
Florin Crișan | 18c951e | 2022-02-08 03:38:38 +0200 | [diff] [blame] | 159 | |
| 160 | add_test(NAME check |
Mike Kruskal | b5380c3 | 2022-07-26 12:27:46 -0700 | [diff] [blame] | 161 | COMMAND tests ${protobuf_GTEST_ARGS}) |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 162 | |
| 163 | # For test purposes, remove headers that should already be installed. This |
| 164 | # prevents accidental conflicts and also version skew (since local headers take |
| 165 | # precedence over installed headers). |
| 166 | add_custom_target(save-installed-headers) |
| 167 | add_custom_target(remove-installed-headers) |
| 168 | add_custom_target(restore-installed-headers) |
| 169 | |
| 170 | # Explicitly skip the bootstrapping headers as it's directly used in tests |
| 171 | set(_installed_hdrs ${libprotobuf_hdrs} ${libprotoc_hdrs}) |
| 172 | list(REMOVE_ITEM _installed_hdrs |
| 173 | "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h" |
| 174 | "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h") |
| 175 | |
| 176 | foreach(_hdr ${_installed_hdrs}) |
| 177 | string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr}) |
| 178 | set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}") |
| 179 | add_custom_command(TARGET remove-installed-headers PRE_BUILD |
| 180 | COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}") |
| 181 | add_custom_command(TARGET save-installed-headers PRE_BUILD |
| 182 | COMMAND ${CMAKE_COMMAND} -E |
| 183 | copy "${_hdr}" "${_tmp_file}" || true) |
| 184 | add_custom_command(TARGET restore-installed-headers PRE_BUILD |
| 185 | COMMAND ${CMAKE_COMMAND} -E |
| 186 | copy "${_tmp_file}" "${_hdr}") |
| 187 | endforeach() |
| 188 | |
| 189 | add_dependencies(remove-installed-headers save-installed-headers) |
| 190 | if(protobuf_REMOVE_INSTALLED_HEADERS) |
| 191 | add_dependencies(protobuf-lite-test-common remove-installed-headers) |
| 192 | add_dependencies(protobuf-test-common remove-installed-headers) |
| 193 | endif() |