tests.cmake 15 KB

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