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