tests.cmake 12 KB

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