1option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF) 2 3option(protobuf_TEST_XML_OUTDIR "Output directory for XML logs from tests." "") 4 5option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH 6 "Using absolute test_plugin path in tests" ON) 7mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) 8 9if (protobuf_USE_EXTERNAL_GTEST) 10 find_package(GTest REQUIRED) 11else() 12 if (NOT EXISTS "${protobuf_SOURCE_DIR}/third_party/googletest/CMakeLists.txt") 13 message(FATAL_ERROR 14 "Cannot find third_party/googletest directory that's needed to " 15 "build tests. If you use git, make sure you have cloned submodules:\n" 16 " git submodule update --init --recursive\n" 17 "If instead you want to skip tests, run cmake with:\n" 18 " cmake -Dprotobuf_BUILD_TESTS=OFF\n") 19 endif() 20 21 set(googlemock_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googlemock") 22 set(googletest_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googletest") 23 include_directories( 24 ${googlemock_source_dir} 25 ${googletest_source_dir} 26 ${googletest_source_dir}/include 27 ${googlemock_source_dir}/include 28 ) 29 30 add_library(gmock STATIC 31 "${googlemock_source_dir}/src/gmock-all.cc" 32 "${googletest_source_dir}/src/gtest-all.cc" 33 ) 34 target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT}) 35 add_library(gmock_main STATIC "${googlemock_source_dir}/src/gmock_main.cc") 36 target_link_libraries(gmock_main gmock) 37 38 add_library(GTest::gmock ALIAS gmock) 39 add_library(GTest::gmock_main ALIAS gmock_main) 40endif() 41 42set(lite_test_protos 43 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_lite_unittest.proto 44 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_import_lite.proto 45 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_import_public_lite.proto 46 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_lite.proto 47) 48 49set(tests_protos 50 ${protobuf_SOURCE_DIR}/src/google/protobuf/any_test.proto 51 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/test_bad_identifiers.proto 52 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/test_large_enum_value.proto 53 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_proto2_unittest.proto 54 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_unittest.proto 55 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest.proto 56 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_arena.proto 57 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_custom_options.proto 58 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_drop_unknown_fields.proto 59 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_embed_optimize_for.proto 60 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_empty.proto 61 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_enormous_descriptor.proto 62 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_import.proto 63 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_import_public.proto 64 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_lazy_dependencies.proto 65 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_lazy_dependencies_custom_option.proto 66 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_lazy_dependencies_enum.proto 67 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_lite_imports_nonlite.proto 68 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_mset.proto 69 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_mset_wire_format.proto 70 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_no_field_presence.proto 71 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_no_generic_services.proto 72 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_optimize_for.proto 73 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_preserve_unknown_enum.proto 74 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_preserve_unknown_enum2.proto 75 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_proto3.proto 76 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_proto3_arena.proto 77 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_proto3_arena_lite.proto 78 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_proto3_lite.proto 79 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_proto3_optional.proto 80 ${protobuf_SOURCE_DIR}/src/google/protobuf/unittest_well_known_types.proto 81 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/anys.proto 82 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/books.proto 83 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/default_value.proto 84 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/default_value_test.proto 85 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/field_mask.proto 86 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/maps.proto 87 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/oneofs.proto 88 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/proto3.proto 89 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/struct.proto 90 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/timestamp_duration.proto 91 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/testdata/wrappers.proto 92 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/json_format.proto 93 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/json_format_proto3.proto 94 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/message_differencer_unittest.proto 95) 96 97macro(compile_proto_file filename) 98 string(REPLACE .proto .pb.cc pb_file ${filename}) 99 add_custom_command( 100 OUTPUT ${pb_file} 101 DEPENDS ${protobuf_PROTOC_EXE} ${filename} 102 COMMAND ${protobuf_PROTOC_EXE} ${filename} 103 --proto_path=${protobuf_SOURCE_DIR}/src 104 --cpp_out=${protobuf_SOURCE_DIR}/src 105 --experimental_allow_proto3_optional 106 ) 107endmacro(compile_proto_file) 108 109set(lite_test_proto_files) 110foreach(proto_file ${lite_test_protos}) 111 compile_proto_file(${proto_file}) 112 set(lite_test_proto_files ${lite_test_proto_files} ${pb_file}) 113endforeach(proto_file) 114 115set(tests_proto_files) 116foreach(proto_file ${tests_protos}) 117 compile_proto_file(${proto_file}) 118 set(tests_proto_files ${tests_proto_files} ${pb_file}) 119endforeach(proto_file) 120 121set(common_lite_test_files 122 ${protobuf_SOURCE_DIR}/src/google/protobuf/arena_test_util.cc 123 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_lite_test_util.cc 124 ${protobuf_SOURCE_DIR}/src/google/protobuf/test_util_lite.cc 125) 126 127add_library(protobuf-lite-test-common STATIC 128 ${common_lite_test_files} ${lite_test_proto_files}) 129target_link_libraries(protobuf-lite-test-common libprotobuf-lite GTest::gmock) 130 131set(common_test_files 132 ${common_lite_test_files} 133 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/mock_code_generator.cc 134 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_test_util.inc 135 ${protobuf_SOURCE_DIR}/src/google/protobuf/reflection_tester.cc 136 ${protobuf_SOURCE_DIR}/src/google/protobuf/test_util.cc 137 ${protobuf_SOURCE_DIR}/src/google/protobuf/testing/file.cc 138 ${protobuf_SOURCE_DIR}/src/google/protobuf/testing/googletest.cc 139) 140 141add_library(protobuf-test-common STATIC 142 ${common_test_files} ${tests_proto_files}) 143target_link_libraries(protobuf-test-common libprotobuf GTest::gmock) 144 145set(tests_files 146 ${protobuf_SOURCE_DIR}/src/google/protobuf/any_test.cc 147 ${protobuf_SOURCE_DIR}/src/google/protobuf/arena_unittest.cc 148 ${protobuf_SOURCE_DIR}/src/google/protobuf/arenastring_unittest.cc 149 ${protobuf_SOURCE_DIR}/src/google/protobuf/arenaz_sampler_test.cc 150 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/annotation_test_util.cc 151 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/annotation_test_util.h 152 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/command_line_interface_unittest.cc 153 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/bootstrap_unittest.cc 154 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/message_size_unittest.cc 155 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/metadata_test.cc 156 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/move_unittest.cc 157 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/plugin_unittest.cc 158 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/unittest.cc 159 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/cpp/unittest.inc 160 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/csharp/csharp_bootstrap_unittest.cc 161 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/csharp/csharp_generator_unittest.cc 162 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/importer_unittest.cc 163 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/java/doc_comment_unittest.cc 164 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/java/plugin_unittest.cc 165 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc 166 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/parser_unittest.cc 167 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/python/plugin_unittest.cc 168 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc 169 ${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor_database_unittest.cc 170 ${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor_unittest.cc 171 ${protobuf_SOURCE_DIR}/src/google/protobuf/drop_unknown_fields_test.cc 172 ${protobuf_SOURCE_DIR}/src/google/protobuf/dynamic_message_unittest.cc 173 ${protobuf_SOURCE_DIR}/src/google/protobuf/extension_set_unittest.cc 174 ${protobuf_SOURCE_DIR}/src/google/protobuf/generated_message_reflection_unittest.cc 175 ${protobuf_SOURCE_DIR}/src/google/protobuf/generated_message_tctable_lite_test.cc 176 ${protobuf_SOURCE_DIR}/src/google/protobuf/inlined_string_field_unittest.cc 177 ${protobuf_SOURCE_DIR}/src/google/protobuf/io/coded_stream_unittest.cc 178 ${protobuf_SOURCE_DIR}/src/google/protobuf/io/io_win32_unittest.cc 179 ${protobuf_SOURCE_DIR}/src/google/protobuf/io/printer_unittest.cc 180 ${protobuf_SOURCE_DIR}/src/google/protobuf/io/tokenizer_unittest.cc 181 ${protobuf_SOURCE_DIR}/src/google/protobuf/io/zero_copy_stream_unittest.cc 182 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_field_test.cc 183 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_test.cc 184 ${protobuf_SOURCE_DIR}/src/google/protobuf/map_test.inc 185 ${protobuf_SOURCE_DIR}/src/google/protobuf/message_unittest.cc 186 ${protobuf_SOURCE_DIR}/src/google/protobuf/message_unittest.inc 187 ${protobuf_SOURCE_DIR}/src/google/protobuf/no_field_presence_test.cc 188 ${protobuf_SOURCE_DIR}/src/google/protobuf/preserve_unknown_enum_test.cc 189 ${protobuf_SOURCE_DIR}/src/google/protobuf/proto3_arena_lite_unittest.cc 190 ${protobuf_SOURCE_DIR}/src/google/protobuf/proto3_arena_unittest.cc 191 ${protobuf_SOURCE_DIR}/src/google/protobuf/proto3_lite_unittest.cc 192 ${protobuf_SOURCE_DIR}/src/google/protobuf/proto3_lite_unittest.inc 193 ${protobuf_SOURCE_DIR}/src/google/protobuf/reflection_ops_unittest.cc 194 ${protobuf_SOURCE_DIR}/src/google/protobuf/repeated_field_reflection_unittest.cc 195 ${protobuf_SOURCE_DIR}/src/google/protobuf/repeated_field_unittest.cc 196 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/bytestream_unittest.cc 197 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/common_unittest.cc 198 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/int128_unittest.cc 199 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/status_test.cc 200 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/statusor_test.cc 201 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringpiece_unittest.cc 202 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/stringprintf_unittest.cc 203 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/structurally_valid_unittest.cc 204 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/strutil_unittest.cc 205 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/template_util_unittest.cc 206 ${protobuf_SOURCE_DIR}/src/google/protobuf/stubs/time_test.cc 207 ${protobuf_SOURCE_DIR}/src/google/protobuf/text_format_unittest.cc 208 ${protobuf_SOURCE_DIR}/src/google/protobuf/unknown_field_set_unittest.cc 209 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/delimited_message_util_test.cc 210 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/field_comparator_test.cc 211 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/field_mask_util_test.cc 212 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/default_value_objectwriter_test.cc 213 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/json_objectwriter_test.cc 214 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/json_stream_parser_test.cc 215 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/protostream_objectsource_test.cc 216 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/protostream_objectwriter_test.cc 217 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/internal/type_info_test_helper.cc 218 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/json_util_test.cc 219 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/message_differencer_unittest.cc 220 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/time_util_test.cc 221 ${protobuf_SOURCE_DIR}/src/google/protobuf/util/type_resolver_util_test.cc 222 ${protobuf_SOURCE_DIR}/src/google/protobuf/well_known_types_unittest.cc 223 ${protobuf_SOURCE_DIR}/src/google/protobuf/wire_format_unittest.cc 224 ${protobuf_SOURCE_DIR}/src/google/protobuf/wire_format_unittest.inc 225) 226 227if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) 228 add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>") 229endif() 230 231if(MINGW) 232 set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing") 233 234 # required for tests on MinGW Win64 235 if (CMAKE_SIZEOF_VOID_P EQUAL 8) 236 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216") 237 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj") 238 endif() 239 240endif() 241 242if(protobuf_TEST_XML_OUTDIR) 243 if(NOT "${protobuf_TEST_XML_OUTDIR}" MATCHES "[/\\]$") 244 string(APPEND protobuf_TEST_XML_OUTDIR "/") 245 endif() 246 set(protobuf_GTEST_ARGS "--gtest_output=xml:${protobuf_TEST_XML_OUTDIR}") 247else() 248 set(protobuf_GTEST_ARGS) 249endif() 250 251add_executable(tests ${tests_files}) 252if (MSVC) 253 target_compile_options(tests PRIVATE 254 /wd4146 # unary minus operator applied to unsigned type, result still unsigned 255 ) 256endif() 257target_link_libraries(tests protobuf-lite-test-common protobuf-test-common libprotoc libprotobuf GTest::gmock_main) 258 259set(test_plugin_files 260 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/mock_code_generator.cc 261 ${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/test_plugin.cc 262 ${protobuf_SOURCE_DIR}/src/google/protobuf/testing/file.cc 263 ${protobuf_SOURCE_DIR}/src/google/protobuf/testing/file.h 264) 265 266add_executable(test_plugin ${test_plugin_files}) 267target_link_libraries(test_plugin libprotoc libprotobuf GTest::gmock) 268 269set(lite_test_files 270 ${protobuf_SOURCE_DIR}/src/google/protobuf/lite_unittest.cc 271) 272add_executable(lite-test ${lite_test_files}) 273target_link_libraries(lite-test protobuf-lite-test-common libprotobuf-lite GTest::gmock_main) 274 275add_test(NAME lite-test 276 COMMAND lite-test ${protobuf_GTEST_ARGS}) 277 278set(lite_arena_test_files 279 ${protobuf_SOURCE_DIR}/src/google/protobuf/lite_arena_unittest.cc 280) 281add_executable(lite-arena-test ${lite_arena_test_files}) 282target_link_libraries(lite-arena-test protobuf-lite-test-common libprotobuf-lite GTest::gmock_main) 283 284add_test(NAME lite-arena-test 285 COMMAND lite-arena-test ${protobuf_GTEST_ARGS}) 286 287add_custom_target(check 288 COMMAND tests 289 DEPENDS tests test_plugin 290 WORKING_DIRECTORY ${protobuf_SOURCE_DIR}) 291 292add_test(NAME check 293 COMMAND tests ${protobuf_GTEST_ARGS} 294 WORKING_DIRECTORY "${protobuf_SOURCE_DIR}") 295