1option(protobuf_REMOVE_INSTALLED_HEADERS 2 "Remove local headers so that installed ones are used instead" OFF) 3 4option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH 5 "Using absolute test_plugin path in tests" ON) 6mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) 7 8include(${protobuf_SOURCE_DIR}/src/file_lists.cmake) 9include(${protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake) 10 11set(lite_test_protos 12 ${protobuf_lite_test_protos_files} 13) 14 15set(tests_protos 16 ${protobuf_test_protos_files} 17 ${compiler_test_protos_files} 18 ${util_test_protos_files} 19) 20 21file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/src) 22 23set(lite_test_proto_files) 24foreach(proto_file ${lite_test_protos}) 25 protobuf_generate( 26 PROTOS ${proto_file} 27 LANGUAGE cpp 28 OUT_VAR pb_generated_files 29 IMPORT_DIRS ${protobuf_SOURCE_DIR}/src 30 ) 31 set(lite_test_proto_files ${lite_test_proto_files} ${pb_generated_files}) 32endforeach(proto_file) 33 34set(tests_proto_files) 35foreach(proto_file ${tests_protos}) 36 protobuf_generate( 37 PROTOS ${proto_file} 38 LANGUAGE cpp 39 OUT_VAR pb_generated_files 40 IMPORT_DIRS ${protobuf_SOURCE_DIR}/src 41 ) 42 set(tests_proto_files ${tests_proto_files} ${pb_generated_files}) 43endforeach(proto_file) 44 45set(common_test_files 46 ${test_util_hdrs} 47 ${lite_test_util_srcs} 48 ${test_util_srcs} 49 ${common_test_hdrs} 50 ${common_test_srcs} 51) 52 53set(tests_files 54 ${protobuf_test_files} 55 ${compiler_test_files} 56 ${annotation_test_util_srcs} 57 ${editions_test_files} 58 ${io_test_files} 59 ${util_test_files} 60 ${stubs_test_files} 61) 62 63if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) 64 add_compile_options(-DGOOGLE_PROTOBUF_FAKE_PLUGIN_PATH="$<TARGET_FILE:fake_plugin>") 65 add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>") 66endif() 67 68if(MINGW) 69 set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing") 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 77endif() 78 79if(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}") 84else() 85 set(protobuf_GTEST_ARGS) 86endif() 87 88add_library(libtest_common STATIC 89 ${tests_proto_files} 90) 91target_link_libraries(libtest_common 92 ${protobuf_LIB_PROTOC} 93 ${protobuf_LIB_PROTOBUF} 94 ${protobuf_ABSL_USED_TARGETS} 95 ${protobuf_ABSL_USED_TEST_TARGETS} 96 GTest::gmock 97) 98if (MSVC) 99 target_compile_options(libtest_common PRIVATE /bigobj) 100endif () 101 102add_executable(tests ${tests_files} ${common_test_files}) 103if (MSVC) 104 target_compile_options(tests PRIVATE 105 /wd4146 # unary minus operator applied to unsigned type, result still unsigned 106 /bigobj 107 ) 108endif() 109target_link_libraries(tests 110 libtest_common 111 libtest_common_lite 112 ${protobuf_LIB_PROTOC} 113 ${protobuf_LIB_PROTOBUF} 114 ${protobuf_ABSL_USED_TARGETS} 115 ${protobuf_ABSL_USED_TEST_TARGETS} 116 GTest::gmock_main 117) 118 119add_executable(fake_plugin ${fake_plugin_files} ${common_test_files}) 120target_include_directories(fake_plugin PRIVATE ${ABSL_ROOT_DIR}) 121target_link_libraries(fake_plugin 122 libtest_common 123 libtest_common_lite 124 ${protobuf_LIB_PROTOC} 125 ${protobuf_LIB_PROTOBUF} 126 ${protobuf_ABSL_USED_TARGETS} 127 ${protobuf_ABSL_USED_TEST_TARGETS} 128 GTest::gmock 129) 130 131add_executable(test_plugin ${test_plugin_files} ${common_test_files}) 132target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR}) 133target_link_libraries(test_plugin 134 libtest_common 135 libtest_common_lite 136 ${protobuf_LIB_PROTOC} 137 ${protobuf_LIB_PROTOBUF} 138 ${protobuf_ABSL_USED_TARGETS} 139 ${protobuf_ABSL_USED_TEST_TARGETS} 140 GTest::gmock 141) 142 143add_library(libtest_common_lite STATIC 144 ${lite_test_proto_files} 145) 146target_link_libraries(libtest_common_lite 147 ${protobuf_LIB_PROTOBUF_LITE} 148 ${protobuf_ABSL_USED_TARGETS} 149 GTest::gmock 150) 151 152add_executable(lite-test 153 ${protobuf_lite_test_files} 154 ${lite_test_util_hdrs} 155 ${lite_test_util_srcs} 156) 157target_link_libraries(lite-test 158 libtest_common_lite 159 ${protobuf_LIB_PROTOBUF_LITE} 160 ${protobuf_ABSL_USED_TARGETS} 161 ${protobuf_ABSL_USED_TEST_TARGETS} 162 GTest::gmock_main 163) 164 165add_test(NAME lite-test 166 COMMAND lite-test ${protobuf_GTEST_ARGS} 167 WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) 168 169add_custom_target(full-test 170 COMMAND tests 171 DEPENDS tests lite-test fake_plugin test_plugin 172 WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) 173 174add_test(NAME full-test 175 COMMAND tests ${protobuf_GTEST_ARGS} 176 WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) 177 178if (protobuf_BUILD_LIBUPB) 179 set(upb_test_proto_genfiles) 180 foreach(proto_file ${upb_test_protos_files} ${descriptor_proto_proto_srcs}) 181 foreach(generator upb upbdefs upb_minitable) 182 protobuf_generate( 183 PROTOS ${proto_file} 184 LANGUAGE ${generator} 185 GENERATE_EXTENSIONS .${generator}.h .${generator}.c 186 OUT_VAR pb_generated_files 187 IMPORT_DIRS ${protobuf_SOURCE_DIR}/src 188 IMPORT_DIRS ${protobuf_SOURCE_DIR} 189 PLUGIN protoc-gen-${generator}=$<TARGET_FILE:protobuf::protoc-gen-${generator}> 190 DEPENDENCIES $<TARGET_FILE:protobuf::protoc-gen-${generator}> 191 ) 192 set(upb_test_proto_genfiles ${upb_test_proto_genfiles} ${pb_generated_files}) 193 endforeach() 194 endforeach(proto_file) 195 196 add_executable(upb-test 197 ${upb_test_files} 198 ${upb_test_proto_genfiles} 199 ${upb_test_util_files}) 200 201 target_link_libraries(upb-test 202 ${protobuf_LIB_PROTOBUF} 203 ${protobuf_LIB_UPB} 204 ${protobuf_ABSL_USED_TARGETS} 205 ${protobuf_ABSL_USED_TEST_TARGETS} 206 GTest::gmock_main) 207 208 add_test(NAME upb-test 209 COMMAND upb-test ${protobuf_GTEST_ARGS} 210 WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) 211endif() 212 213# For test purposes, remove headers that should already be installed. This 214# prevents accidental conflicts and also version skew (since local headers take 215# precedence over installed headers). 216add_custom_target(save-installed-headers) 217add_custom_target(remove-installed-headers) 218add_custom_target(restore-installed-headers) 219 220file(GLOB_RECURSE _local_hdrs 221 "${PROJECT_SOURCE_DIR}/src/*.h" 222 "${PROJECT_SOURCE_DIR}/src/*.inc" 223 "${PROJECT_SOURCE_DIR}/upb/*.h" 224) 225 226# Exclude the bootstrapping that are directly used by tests. 227set(_exclude_hdrs 228 "${protobuf_SOURCE_DIR}/src/google/protobuf/cpp_features.pb.h" 229 "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h" 230 "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h" 231 "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/java/java_features.pb.h") 232 233# Exclude test library headers. 234list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs} 235 ${compiler_test_utils_hdrs} ${upb_test_util_files}) 236foreach(_hdr ${_exclude_hdrs}) 237 list(REMOVE_ITEM _local_hdrs ${_hdr}) 238endforeach() 239 240foreach(_hdr ${_local_hdrs}) 241 string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr}) 242 set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}") 243 add_custom_command(TARGET remove-installed-headers PRE_BUILD 244 COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}") 245 add_custom_command(TARGET save-installed-headers PRE_BUILD 246 COMMAND ${CMAKE_COMMAND} -E 247 copy "${_hdr}" "${_tmp_file}" || true) 248 add_custom_command(TARGET restore-installed-headers PRE_BUILD 249 COMMAND ${CMAKE_COMMAND} -E 250 copy "${_tmp_file}" "${_hdr}") 251endforeach() 252 253add_dependencies(remove-installed-headers save-installed-headers) 254if(protobuf_REMOVE_INSTALLED_HEADERS) 255 # Make sure we remove all the headers *before* any codegen occurs. 256 add_dependencies(${protobuf_PROTOC_EXE} remove-installed-headers) 257endif() 258