blob: 8838b4c4de040c524664199dedfbfb41c32d25fb [file] [log] [blame]
Florin Crișan9ebb3172022-02-08 06:41:29 +02001option(protobuf_USE_EXTERNAL_GTEST "Use external Google Test (i.e. not the one in third_party/googletest)" OFF)
Feng Xiao4333edb2015-05-31 02:28:34 -07002
Mike Kruskal3edec1f2022-07-25 20:36:47 -07003option(protobuf_REMOVE_INSTALLED_HEADERS
4 "Remove local headers so that installed ones are used instead" OFF)
David L. Jones5b2c7b82022-03-30 13:44:42 -07005
Konstantin Podsvirove3019462015-09-17 12:08:47 +03006option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH
7 "Using absolute test_plugin path in tests" ON)
Konstantin Podsvirov71556292016-06-01 17:00:08 +03008mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
Konstantin Podsvirove3019462015-09-17 12:08:47 +03009
Florin Crișan9ebb3172022-02-08 06:41:29 +020010if (protobuf_USE_EXTERNAL_GTEST)
11 find_package(GTest REQUIRED)
12else()
Adam Cozzetteb20209f2022-03-10 18:36:42 +000013 if (NOT EXISTS "${protobuf_SOURCE_DIR}/third_party/googletest/CMakeLists.txt")
Florin Crișan9ebb3172022-02-08 06:41:29 +020014 message(FATAL_ERROR
15 "Cannot find third_party/googletest directory that's needed to "
16 "build tests. If you use git, make sure you have cloned submodules:\n"
17 " git submodule update --init --recursive\n"
18 "If instead you want to skip tests, run cmake with:\n"
19 " cmake -Dprotobuf_BUILD_TESTS=OFF\n")
20 endif()
Feng Xiao4333edb2015-05-31 02:28:34 -070021
Adam Cozzetteb20209f2022-03-10 18:36:42 +000022 set(googlemock_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googlemock")
23 set(googletest_source_dir "${protobuf_SOURCE_DIR}/third_party/googletest/googletest")
Florin Crișan9ebb3172022-02-08 06:41:29 +020024 include_directories(
25 ${googlemock_source_dir}
26 ${googletest_source_dir}
27 ${googletest_source_dir}/include
28 ${googlemock_source_dir}/include
29 )
30
31 add_library(gmock STATIC
32 "${googlemock_source_dir}/src/gmock-all.cc"
33 "${googletest_source_dir}/src/gtest-all.cc"
34 )
35 target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT})
36 add_library(gmock_main STATIC "${googlemock_source_dir}/src/gmock_main.cc")
37 target_link_libraries(gmock_main gmock)
38
39 add_library(GTest::gmock ALIAS gmock)
40 add_library(GTest::gmock_main ALIAS gmock_main)
41endif()
Feng Xiao818c5ee2015-06-15 21:42:57 -070042
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040043include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
44
Feng Xiao4333edb2015-05-31 02:28:34 -070045set(lite_test_protos
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040046 ${protobuf_lite_test_protos_proto_srcs}
Feng Xiao4333edb2015-05-31 02:28:34 -070047)
48
49set(tests_protos
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040050 ${protobuf_test_protos_proto_srcs}
51 ${compiler_test_protos_files}
52 ${util_test_protos_files}
Feng Xiao4333edb2015-05-31 02:28:34 -070053)
54
55macro(compile_proto_file filename)
David L. Jones4e5e8d32022-04-28 14:01:34 -070056 string(REPLACE .proto .pb.cc pb_file ${filename})
Feng Xiao4333edb2015-05-31 02:28:34 -070057 add_custom_command(
David L. Jones4e5e8d32022-04-28 14:01:34 -070058 OUTPUT ${pb_file}
59 DEPENDS ${protobuf_PROTOC_EXE} ${filename}
60 COMMAND ${protobuf_PROTOC_EXE} ${filename}
Adam Cozzetteb20209f2022-03-10 18:36:42 +000061 --proto_path=${protobuf_SOURCE_DIR}/src
62 --cpp_out=${protobuf_SOURCE_DIR}/src
Joshua Habermanb99994d2020-03-31 16:25:37 -070063 --experimental_allow_proto3_optional
Feng Xiao4333edb2015-05-31 02:28:34 -070064 )
65endmacro(compile_proto_file)
66
67set(lite_test_proto_files)
68foreach(proto_file ${lite_test_protos})
69 compile_proto_file(${proto_file})
David L. Jones4e5e8d32022-04-28 14:01:34 -070070 set(lite_test_proto_files ${lite_test_proto_files} ${pb_file})
Feng Xiao4333edb2015-05-31 02:28:34 -070071endforeach(proto_file)
72
73set(tests_proto_files)
74foreach(proto_file ${tests_protos})
75 compile_proto_file(${proto_file})
David L. Jones4e5e8d32022-04-28 14:01:34 -070076 set(tests_proto_files ${tests_proto_files} ${pb_file})
Feng Xiao4333edb2015-05-31 02:28:34 -070077endforeach(proto_file)
78
David L. Jones9e093432022-04-14 17:36:14 -070079add_library(protobuf-lite-test-common STATIC
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040080 ${lite_test_util_srcs} ${lite_test_proto_files})
Mike Kruskalcac97652022-08-12 16:41:00 -070081target_include_directories(protobuf-lite-test-common PRIVATE ${ABSL_ROOT_DIR})
82target_link_libraries(protobuf-lite-test-common
83 ${protobuf_LIB_PROTOBUF_LITE} ${protobuf_ABSL_USED_TARGETS} GTest::gmock)
David L. Jonesa3347c22022-03-29 11:48:16 -070084
Joshua Habermane5c570b2021-09-09 08:21:42 -070085set(common_test_files
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040086 ${test_util_hdrs}
87 ${test_util_srcs}
88 ${mock_code_generator_srcs}
89 ${testing_srcs}
Joshua Habermane5c570b2021-09-09 08:21:42 -070090)
91
David L. Jones9e093432022-04-14 17:36:14 -070092add_library(protobuf-test-common STATIC
David L. Jonesa3347c22022-03-29 11:48:16 -070093 ${common_test_files} ${tests_proto_files})
Mike Kruskalcac97652022-08-12 16:41:00 -070094target_include_directories(protobuf-test-common PRIVATE ${ABSL_ROOT_DIR})
95target_link_libraries(protobuf-test-common
96 ${protobuf_LIB_PROTOBUF} ${protobuf_ABSL_USED_TARGETS} GTest::gmock)
David L. Jonesa3347c22022-03-29 11:48:16 -070097
Feng Xiao4333edb2015-05-31 02:28:34 -070098set(tests_files
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040099 ${protobuf_test_files}
100 ${compiler_test_files}
101 ${annotation_test_util_srcs}
102 ${io_test_files}
103 ${util_test_files}
104 ${stubs_test_files}
Feng Xiao4333edb2015-05-31 02:28:34 -0700105)
106
Konstantin Podsvirove3019462015-09-17 12:08:47 +0300107if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
108 add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
109endif()
110
Ivan Shynkarenkafeb183d2018-05-03 01:47:17 +0300111if(MINGW)
Bo Yang509aee42021-08-17 17:26:34 -0700112 set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing")
Roman Popova69dfe62018-07-30 21:55:44 -0700113
114 # required for tests on MinGW Win64
115 if (CMAKE_SIZEOF_VOID_P EQUAL 8)
116 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216")
117 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj")
118 endif()
119
Ivan Shynkarenkafeb183d2018-05-03 01:47:17 +0300120endif()
121
David L. Jones5b2c7b82022-03-30 13:44:42 -0700122if(protobuf_TEST_XML_OUTDIR)
123 if(NOT "${protobuf_TEST_XML_OUTDIR}" MATCHES "[/\\]$")
124 string(APPEND protobuf_TEST_XML_OUTDIR "/")
125 endif()
126 set(protobuf_GTEST_ARGS "--gtest_output=xml:${protobuf_TEST_XML_OUTDIR}")
David L. Jonesc4ddd842022-04-28 15:17:51 -0700127else()
128 set(protobuf_GTEST_ARGS)
David L. Jones5b2c7b82022-03-30 13:44:42 -0700129endif()
130
David L. Jonesa3347c22022-03-29 11:48:16 -0700131add_executable(tests ${tests_files})
Stephen Kennedy84953722022-02-02 23:10:15 +0000132if (MSVC)
133 target_compile_options(tests PRIVATE
134 /wd4146 # unary minus operator applied to unsigned type, result still unsigned
135 )
136endif()
Matt Kulukundisc7b29912022-08-02 10:20:58 -0400137target_link_libraries(tests protobuf-lite-test-common protobuf-test-common ${protobuf_LIB_PROTOC} ${protobuf_LIB_PROTOBUF} GTest::gmock_main)
Feng Xiao4333edb2015-05-31 02:28:34 -0700138
139set(test_plugin_files
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -0400140 ${test_plugin_files}
141 ${mock_code_generator_srcs}
142 ${testing_srcs}
Feng Xiao4333edb2015-05-31 02:28:34 -0700143)
Konstantin Podsvirove3019462015-09-17 12:08:47 +0300144
Feng Xiao4333edb2015-05-31 02:28:34 -0700145add_executable(test_plugin ${test_plugin_files})
Mike Kruskal701dd832022-08-20 14:22:08 -0700146target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR})
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700147target_link_libraries(test_plugin ${protobuf_LIB_PROTOC} ${protobuf_LIB_PROTOBUF} GTest::gmock)
Feng Xiao4333edb2015-05-31 02:28:34 -0700148
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -0400149add_executable(lite-test ${protobuf_lite_test_files})
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700150target_link_libraries(lite-test protobuf-lite-test-common ${protobuf_LIB_PROTOBUF_LITE} GTest::gmock_main)
Feng Xiaob17ec3c2015-08-23 17:50:38 -0700151
David L. Jones5b2c7b82022-03-30 13:44:42 -0700152add_test(NAME lite-test
153 COMMAND lite-test ${protobuf_GTEST_ARGS})
Feng Xiaob17ec3c2015-08-23 17:50:38 -0700154
Konstantin Podsvirove3019462015-09-17 12:08:47 +0300155add_custom_target(check
156 COMMAND tests
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700157 DEPENDS tests lite-test test_plugin
Adam Cozzetteb20209f2022-03-10 18:36:42 +0000158 WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
Florin Crișan18c951e2022-02-08 03:38:38 +0200159
160add_test(NAME check
Mike Kruskalb5380c32022-07-26 12:27:46 -0700161 COMMAND tests ${protobuf_GTEST_ARGS})
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700162
163# For test purposes, remove headers that should already be installed. This
164# prevents accidental conflicts and also version skew (since local headers take
165# precedence over installed headers).
166add_custom_target(save-installed-headers)
167add_custom_target(remove-installed-headers)
168add_custom_target(restore-installed-headers)
169
170# Explicitly skip the bootstrapping headers as it's directly used in tests
171set(_installed_hdrs ${libprotobuf_hdrs} ${libprotoc_hdrs})
172list(REMOVE_ITEM _installed_hdrs
173 "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h"
174 "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h")
175
176foreach(_hdr ${_installed_hdrs})
177 string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr})
178 set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}")
179 add_custom_command(TARGET remove-installed-headers PRE_BUILD
180 COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}")
181 add_custom_command(TARGET save-installed-headers PRE_BUILD
182 COMMAND ${CMAKE_COMMAND} -E
183 copy "${_hdr}" "${_tmp_file}" || true)
184 add_custom_command(TARGET restore-installed-headers PRE_BUILD
185 COMMAND ${CMAKE_COMMAND} -E
186 copy "${_tmp_file}" "${_hdr}")
187endforeach()
188
189add_dependencies(remove-installed-headers save-installed-headers)
190if(protobuf_REMOVE_INSTALLED_HEADERS)
191 add_dependencies(protobuf-lite-test-common remove-installed-headers)
192 add_dependencies(protobuf-test-common remove-installed-headers)
193endif()