Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 1 | option(protobuf_REMOVE_INSTALLED_HEADERS |
| 2 | "Remove local headers so that installed ones are used instead" OFF) |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 3 | |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 4 | option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH |
| 5 | "Using absolute test_plugin path in tests" ON) |
Konstantin Podsvirov | 7155629 | 2016-06-01 17:00:08 +0300 | [diff] [blame] | 6 | mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 7 | |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 8 | include(${protobuf_SOURCE_DIR}/src/file_lists.cmake) |
| 9 | |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 10 | set(lite_test_protos |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 11 | ${protobuf_lite_test_protos_files} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | set(tests_protos |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 15 | ${protobuf_test_protos_files} |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 16 | ${compiler_test_protos_files} |
| 17 | ${util_test_protos_files} |
Mike Kruskal | 983fc2d | 2022-11-02 23:24:26 -0700 | [diff] [blame] | 18 | ${lite_test_protos} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | macro(compile_proto_file filename) |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 22 | string(REPLACE .proto .pb.h pb_hdr ${filename}) |
| 23 | string(REPLACE .proto .pb.cc pb_src ${filename}) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 24 | add_custom_command( |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 25 | OUTPUT ${pb_hdr} ${pb_src} |
David L. Jones | 4e5e8d3 | 2022-04-28 14:01:34 -0700 | [diff] [blame] | 26 | DEPENDS ${protobuf_PROTOC_EXE} ${filename} |
| 27 | COMMAND ${protobuf_PROTOC_EXE} ${filename} |
Adam Cozzette | b20209f | 2022-03-10 18:36:42 +0000 | [diff] [blame] | 28 | --proto_path=${protobuf_SOURCE_DIR}/src |
| 29 | --cpp_out=${protobuf_SOURCE_DIR}/src |
Joshua Haberman | b99994d | 2020-03-31 16:25:37 -0700 | [diff] [blame] | 30 | --experimental_allow_proto3_optional |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 31 | ) |
| 32 | endmacro(compile_proto_file) |
| 33 | |
| 34 | set(lite_test_proto_files) |
| 35 | foreach(proto_file ${lite_test_protos}) |
| 36 | compile_proto_file(${proto_file}) |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 37 | set(lite_test_proto_files ${lite_test_proto_files} ${pb_src} ${pb_hdr}) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 38 | endforeach(proto_file) |
| 39 | |
| 40 | set(tests_proto_files) |
| 41 | foreach(proto_file ${tests_protos}) |
| 42 | compile_proto_file(${proto_file}) |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 43 | set(tests_proto_files ${tests_proto_files} ${pb_src} ${pb_hdr}) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 44 | endforeach(proto_file) |
| 45 | |
Joshua Haberman | e5c570b | 2021-09-09 08:21:42 -0700 | [diff] [blame] | 46 | set(common_test_files |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 47 | ${test_util_hdrs} |
Adam Cozzette | 57a8846 | 2022-11-04 14:38:11 -0700 | [diff] [blame] | 48 | ${lite_test_util_srcs} |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 49 | ${test_util_srcs} |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 50 | ${common_test_hdrs} |
| 51 | ${common_test_srcs} |
Joshua Haberman | e5c570b | 2021-09-09 08:21:42 -0700 | [diff] [blame] | 52 | ) |
| 53 | |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 54 | set(tests_files |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 55 | ${protobuf_test_files} |
| 56 | ${compiler_test_files} |
| 57 | ${annotation_test_util_srcs} |
| 58 | ${io_test_files} |
| 59 | ${util_test_files} |
| 60 | ${stubs_test_files} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 61 | ) |
| 62 | |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 63 | if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) |
Adam Cozzette | 80df4b2 | 2023-06-23 13:49:32 -0700 | [diff] [blame] | 64 | add_compile_options(-DGOOGLE_PROTOBUF_FAKE_PLUGIN_PATH="$<TARGET_FILE:fake_plugin>") |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 65 | add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>") |
| 66 | endif() |
| 67 | |
Ivan Shynkarenka | feb183d | 2018-05-03 01:47:17 +0300 | [diff] [blame] | 68 | if(MINGW) |
Bo Yang | 509aee4 | 2021-08-17 17:26:34 -0700 | [diff] [blame] | 69 | set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing") |
Roman Popov | a69dfe6 | 2018-07-30 21:55:44 -0700 | [diff] [blame] | 70 | |
| 71 | # required for tests on MinGW Win64 |
| 72 | if (CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 73 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216") |
| 74 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj") |
| 75 | endif() |
| 76 | |
Ivan Shynkarenka | feb183d | 2018-05-03 01:47:17 +0300 | [diff] [blame] | 77 | endif() |
| 78 | |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 79 | if(protobuf_TEST_XML_OUTDIR) |
| 80 | if(NOT "${protobuf_TEST_XML_OUTDIR}" MATCHES "[/\\]$") |
| 81 | string(APPEND protobuf_TEST_XML_OUTDIR "/") |
| 82 | endif() |
| 83 | set(protobuf_GTEST_ARGS "--gtest_output=xml:${protobuf_TEST_XML_OUTDIR}") |
David L. Jones | c4ddd84 | 2022-04-28 15:17:51 -0700 | [diff] [blame] | 84 | else() |
| 85 | set(protobuf_GTEST_ARGS) |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 86 | endif() |
| 87 | |
Mike Kruskal | 983fc2d | 2022-11-02 23:24:26 -0700 | [diff] [blame] | 88 | add_executable(tests |
| 89 | ${tests_files} |
| 90 | ${common_test_files} |
| 91 | ${tests_proto_files} |
| 92 | ) |
Stephen Kennedy | 8495372 | 2022-02-02 23:10:15 +0000 | [diff] [blame] | 93 | if (MSVC) |
| 94 | target_compile_options(tests PRIVATE |
| 95 | /wd4146 # unary minus operator applied to unsigned type, result still unsigned |
| 96 | ) |
| 97 | endif() |
Mike Kruskal | 983fc2d | 2022-11-02 23:24:26 -0700 | [diff] [blame] | 98 | target_link_libraries(tests |
| 99 | ${protobuf_LIB_PROTOC} |
| 100 | ${protobuf_LIB_PROTOBUF} |
| 101 | ${protobuf_ABSL_USED_TARGETS} |
Mike Kruskal | a9f1ea6 | 2023-01-24 21:49:33 -0800 | [diff] [blame] | 102 | ${protobuf_ABSL_USED_TEST_TARGETS} |
Mike Kruskal | 983fc2d | 2022-11-02 23:24:26 -0700 | [diff] [blame] | 103 | GTest::gmock_main |
| 104 | ) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 105 | |
Adam Cozzette | 80df4b2 | 2023-06-23 13:49:32 -0700 | [diff] [blame] | 106 | set(fake_plugin_files |
| 107 | ${fake_plugin_files} |
| 108 | ${common_test_hdrs} |
| 109 | ${common_test_srcs} |
Mike Kruskal | e897bcf | 2023-08-30 14:15:16 -0700 | [diff] [blame] | 110 | ${tests_proto_files} |
Adam Cozzette | 80df4b2 | 2023-06-23 13:49:32 -0700 | [diff] [blame] | 111 | ) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 112 | set(test_plugin_files |
Elliotte Rusty Harold | 24dad62 | 2022-06-10 16:04:55 -0400 | [diff] [blame] | 113 | ${test_plugin_files} |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 114 | ${common_test_hdrs} |
| 115 | ${common_test_srcs} |
Mike Kruskal | e897bcf | 2023-08-30 14:15:16 -0700 | [diff] [blame] | 116 | ${tests_proto_files} |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 117 | ) |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 118 | |
Adam Cozzette | 80df4b2 | 2023-06-23 13:49:32 -0700 | [diff] [blame] | 119 | add_executable(fake_plugin ${fake_plugin_files}) |
| 120 | target_include_directories(fake_plugin PRIVATE ${ABSL_ROOT_DIR}) |
| 121 | target_link_libraries(fake_plugin |
| 122 | ${protobuf_LIB_PROTOC} |
| 123 | ${protobuf_LIB_PROTOBUF} |
| 124 | ${protobuf_ABSL_USED_TARGETS} |
| 125 | ${protobuf_ABSL_USED_TEST_TARGETS} |
| 126 | GTest::gmock |
| 127 | ) |
| 128 | |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 129 | add_executable(test_plugin ${test_plugin_files}) |
Mike Kruskal | 701dd83 | 2022-08-20 14:22:08 -0700 | [diff] [blame] | 130 | target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR}) |
Mike Kruskal | 465653e | 2022-08-25 13:32:32 -0700 | [diff] [blame] | 131 | target_link_libraries(test_plugin |
| 132 | ${protobuf_LIB_PROTOC} |
| 133 | ${protobuf_LIB_PROTOBUF} |
| 134 | ${protobuf_ABSL_USED_TARGETS} |
Mike Kruskal | a9f1ea6 | 2023-01-24 21:49:33 -0800 | [diff] [blame] | 135 | ${protobuf_ABSL_USED_TEST_TARGETS} |
Mike Kruskal | 465653e | 2022-08-25 13:32:32 -0700 | [diff] [blame] | 136 | GTest::gmock |
| 137 | ) |
Feng Xiao | 4333edb | 2015-05-31 02:28:34 -0700 | [diff] [blame] | 138 | |
Mike Kruskal | 983fc2d | 2022-11-02 23:24:26 -0700 | [diff] [blame] | 139 | add_executable(lite-test |
| 140 | ${protobuf_lite_test_files} |
| 141 | ${lite_test_util_srcs} |
| 142 | ${lite_test_proto_files} |
| 143 | ) |
| 144 | target_link_libraries(lite-test |
| 145 | ${protobuf_LIB_PROTOBUF_LITE} |
| 146 | ${protobuf_ABSL_USED_TARGETS} |
Mike Kruskal | a9f1ea6 | 2023-01-24 21:49:33 -0800 | [diff] [blame] | 147 | ${protobuf_ABSL_USED_TEST_TARGETS} |
Mike Kruskal | 983fc2d | 2022-11-02 23:24:26 -0700 | [diff] [blame] | 148 | GTest::gmock_main |
| 149 | ) |
Feng Xiao | b17ec3c | 2015-08-23 17:50:38 -0700 | [diff] [blame] | 150 | |
David L. Jones | 5b2c7b8 | 2022-03-30 13:44:42 -0700 | [diff] [blame] | 151 | add_test(NAME lite-test |
Adam Cozzette | ae2531d | 2023-03-03 12:20:37 -0800 | [diff] [blame] | 152 | COMMAND lite-test ${protobuf_GTEST_ARGS} |
| 153 | WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) |
Feng Xiao | b17ec3c | 2015-08-23 17:50:38 -0700 | [diff] [blame] | 154 | |
Mike Kruskal | 7930cd1 | 2023-01-20 21:38:59 -0800 | [diff] [blame] | 155 | add_custom_target(full-test |
Konstantin Podsvirov | e301946 | 2015-09-17 12:08:47 +0300 | [diff] [blame] | 156 | COMMAND tests |
Adam Cozzette | 80df4b2 | 2023-06-23 13:49:32 -0700 | [diff] [blame] | 157 | DEPENDS tests lite-test fake_plugin 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 | |
Mike Kruskal | 7930cd1 | 2023-01-20 21:38:59 -0800 | [diff] [blame] | 160 | add_test(NAME full-test |
Adam Cozzette | ae2531d | 2023-03-03 12:20:37 -0800 | [diff] [blame] | 161 | COMMAND tests ${protobuf_GTEST_ARGS} |
| 162 | WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 163 | |
| 164 | # For test purposes, remove headers that should already be installed. This |
| 165 | # prevents accidental conflicts and also version skew (since local headers take |
| 166 | # precedence over installed headers). |
| 167 | add_custom_target(save-installed-headers) |
| 168 | add_custom_target(remove-installed-headers) |
| 169 | add_custom_target(restore-installed-headers) |
| 170 | |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 171 | file(GLOB_RECURSE _local_hdrs |
| 172 | "${PROJECT_SOURCE_DIR}/src/*.h" |
| 173 | "${PROJECT_SOURCE_DIR}/src/*.inc") |
| 174 | |
| 175 | # Exclude the bootstrapping that are directly used by tests. |
| 176 | set(_exclude_hdrs |
Mike Kruskal | 4f9e417 | 2023-06-30 20:14:50 -0700 | [diff] [blame] | 177 | "${protobuf_SOURCE_DIR}/src/google/protobuf/cpp_features.pb.h" |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 178 | "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h" |
| 179 | "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h") |
| 180 | |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 181 | # Exclude test library headers. |
| 182 | list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs} |
| 183 | ${compiler_test_utils_hdrs}) |
| 184 | foreach(_hdr ${_exclude_hdrs}) |
| 185 | list(REMOVE_ITEM _local_hdrs ${_hdr}) |
| 186 | endforeach() |
| 187 | |
| 188 | foreach(_hdr ${_local_hdrs}) |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 189 | string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr}) |
| 190 | set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}") |
| 191 | add_custom_command(TARGET remove-installed-headers PRE_BUILD |
| 192 | COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}") |
| 193 | add_custom_command(TARGET save-installed-headers PRE_BUILD |
| 194 | COMMAND ${CMAKE_COMMAND} -E |
| 195 | copy "${_hdr}" "${_tmp_file}" || true) |
| 196 | add_custom_command(TARGET restore-installed-headers PRE_BUILD |
| 197 | COMMAND ${CMAKE_COMMAND} -E |
| 198 | copy "${_tmp_file}" "${_hdr}") |
| 199 | endforeach() |
| 200 | |
| 201 | add_dependencies(remove-installed-headers save-installed-headers) |
| 202 | if(protobuf_REMOVE_INSTALLED_HEADERS) |
Mike Kruskal | 81e3513 | 2022-09-19 11:08:21 -0700 | [diff] [blame] | 203 | # Make sure we remove all the headers *before* any codegen occurs. |
| 204 | add_dependencies(${protobuf_PROTOC_EXE} remove-installed-headers) |
Mike Kruskal | 3edec1f | 2022-07-25 20:36:47 -0700 | [diff] [blame] | 205 | endif() |