blob: ee74094d50a6fd8c7d4f11814f5ea35086f0afbb [file] [log] [blame]
Mike Kruskal3edec1f2022-07-25 20:36:47 -07001option(protobuf_REMOVE_INSTALLED_HEADERS
2 "Remove local headers so that installed ones are used instead" OFF)
David L. Jones5b2c7b82022-03-30 13:44:42 -07003
Konstantin Podsvirove3019462015-09-17 12:08:47 +03004option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH
5 "Using absolute test_plugin path in tests" ON)
Konstantin Podsvirov71556292016-06-01 17:00:08 +03006mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
Konstantin Podsvirove3019462015-09-17 12:08:47 +03007
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -04008include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
9
Feng Xiao4333edb2015-05-31 02:28:34 -070010set(lite_test_protos
Mike Kruskal81e35132022-09-19 11:08:21 -070011 ${protobuf_lite_test_protos_files}
Feng Xiao4333edb2015-05-31 02:28:34 -070012)
13
14set(tests_protos
Mike Kruskal81e35132022-09-19 11:08:21 -070015 ${protobuf_test_protos_files}
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040016 ${compiler_test_protos_files}
17 ${util_test_protos_files}
Mike Kruskal983fc2d2022-11-02 23:24:26 -070018 ${lite_test_protos}
Feng Xiao4333edb2015-05-31 02:28:34 -070019)
20
21macro(compile_proto_file filename)
Mike Kruskal81e35132022-09-19 11:08:21 -070022 string(REPLACE .proto .pb.h pb_hdr ${filename})
23 string(REPLACE .proto .pb.cc pb_src ${filename})
Feng Xiao4333edb2015-05-31 02:28:34 -070024 add_custom_command(
Mike Kruskal81e35132022-09-19 11:08:21 -070025 OUTPUT ${pb_hdr} ${pb_src}
David L. Jones4e5e8d32022-04-28 14:01:34 -070026 DEPENDS ${protobuf_PROTOC_EXE} ${filename}
27 COMMAND ${protobuf_PROTOC_EXE} ${filename}
Adam Cozzetteb20209f2022-03-10 18:36:42 +000028 --proto_path=${protobuf_SOURCE_DIR}/src
29 --cpp_out=${protobuf_SOURCE_DIR}/src
Joshua Habermanb99994d2020-03-31 16:25:37 -070030 --experimental_allow_proto3_optional
Feng Xiao4333edb2015-05-31 02:28:34 -070031 )
32endmacro(compile_proto_file)
33
34set(lite_test_proto_files)
35foreach(proto_file ${lite_test_protos})
36 compile_proto_file(${proto_file})
Mike Kruskal81e35132022-09-19 11:08:21 -070037 set(lite_test_proto_files ${lite_test_proto_files} ${pb_src} ${pb_hdr})
Feng Xiao4333edb2015-05-31 02:28:34 -070038endforeach(proto_file)
39
40set(tests_proto_files)
41foreach(proto_file ${tests_protos})
42 compile_proto_file(${proto_file})
Mike Kruskal81e35132022-09-19 11:08:21 -070043 set(tests_proto_files ${tests_proto_files} ${pb_src} ${pb_hdr})
Feng Xiao4333edb2015-05-31 02:28:34 -070044endforeach(proto_file)
45
Joshua Habermane5c570b2021-09-09 08:21:42 -070046set(common_test_files
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040047 ${test_util_hdrs}
Adam Cozzette57a88462022-11-04 14:38:11 -070048 ${lite_test_util_srcs}
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040049 ${test_util_srcs}
Mike Kruskal81e35132022-09-19 11:08:21 -070050 ${common_test_hdrs}
51 ${common_test_srcs}
Joshua Habermane5c570b2021-09-09 08:21:42 -070052)
53
Feng Xiao4333edb2015-05-31 02:28:34 -070054set(tests_files
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -040055 ${protobuf_test_files}
56 ${compiler_test_files}
57 ${annotation_test_util_srcs}
58 ${io_test_files}
59 ${util_test_files}
60 ${stubs_test_files}
Feng Xiao4333edb2015-05-31 02:28:34 -070061)
62
Konstantin Podsvirove3019462015-09-17 12:08:47 +030063if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
Adam Cozzette80df4b22023-06-23 13:49:32 -070064 add_compile_options(-DGOOGLE_PROTOBUF_FAKE_PLUGIN_PATH="$<TARGET_FILE:fake_plugin>")
Konstantin Podsvirove3019462015-09-17 12:08:47 +030065 add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
66endif()
67
Ivan Shynkarenkafeb183d2018-05-03 01:47:17 +030068if(MINGW)
Bo Yang509aee42021-08-17 17:26:34 -070069 set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing")
Roman Popova69dfe62018-07-30 21:55:44 -070070
71 # required for tests on MinGW Win64
72 if (CMAKE_SIZEOF_VOID_P EQUAL 8)
73 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216")
74 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj")
75 endif()
76
Ivan Shynkarenkafeb183d2018-05-03 01:47:17 +030077endif()
78
David L. Jones5b2c7b82022-03-30 13:44:42 -070079if(protobuf_TEST_XML_OUTDIR)
80 if(NOT "${protobuf_TEST_XML_OUTDIR}" MATCHES "[/\\]$")
81 string(APPEND protobuf_TEST_XML_OUTDIR "/")
82 endif()
83 set(protobuf_GTEST_ARGS "--gtest_output=xml:${protobuf_TEST_XML_OUTDIR}")
David L. Jonesc4ddd842022-04-28 15:17:51 -070084else()
85 set(protobuf_GTEST_ARGS)
David L. Jones5b2c7b82022-03-30 13:44:42 -070086endif()
87
Mike Kruskal983fc2d2022-11-02 23:24:26 -070088add_executable(tests
89 ${tests_files}
90 ${common_test_files}
91 ${tests_proto_files}
92)
Stephen Kennedy84953722022-02-02 23:10:15 +000093if (MSVC)
94 target_compile_options(tests PRIVATE
95 /wd4146 # unary minus operator applied to unsigned type, result still unsigned
96 )
97endif()
Mike Kruskal983fc2d2022-11-02 23:24:26 -070098target_link_libraries(tests
99 ${protobuf_LIB_PROTOC}
100 ${protobuf_LIB_PROTOBUF}
101 ${protobuf_ABSL_USED_TARGETS}
Mike Kruskala9f1ea62023-01-24 21:49:33 -0800102 ${protobuf_ABSL_USED_TEST_TARGETS}
Mike Kruskal983fc2d2022-11-02 23:24:26 -0700103 GTest::gmock_main
104)
Feng Xiao4333edb2015-05-31 02:28:34 -0700105
Adam Cozzette80df4b22023-06-23 13:49:32 -0700106set(fake_plugin_files
107 ${fake_plugin_files}
108 ${common_test_hdrs}
109 ${common_test_srcs}
Mike Kruskale897bcf2023-08-30 14:15:16 -0700110 ${tests_proto_files}
Adam Cozzette80df4b22023-06-23 13:49:32 -0700111)
Feng Xiao4333edb2015-05-31 02:28:34 -0700112set(test_plugin_files
Elliotte Rusty Harold24dad622022-06-10 16:04:55 -0400113 ${test_plugin_files}
Mike Kruskal81e35132022-09-19 11:08:21 -0700114 ${common_test_hdrs}
115 ${common_test_srcs}
Mike Kruskale897bcf2023-08-30 14:15:16 -0700116 ${tests_proto_files}
Feng Xiao4333edb2015-05-31 02:28:34 -0700117)
Konstantin Podsvirove3019462015-09-17 12:08:47 +0300118
Adam Cozzette80df4b22023-06-23 13:49:32 -0700119add_executable(fake_plugin ${fake_plugin_files})
120target_include_directories(fake_plugin PRIVATE ${ABSL_ROOT_DIR})
121target_link_libraries(fake_plugin
122 ${protobuf_LIB_PROTOC}
123 ${protobuf_LIB_PROTOBUF}
124 ${protobuf_ABSL_USED_TARGETS}
125 ${protobuf_ABSL_USED_TEST_TARGETS}
126 GTest::gmock
127)
128
Feng Xiao4333edb2015-05-31 02:28:34 -0700129add_executable(test_plugin ${test_plugin_files})
Mike Kruskal701dd832022-08-20 14:22:08 -0700130target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR})
Mike Kruskal465653e2022-08-25 13:32:32 -0700131target_link_libraries(test_plugin
132 ${protobuf_LIB_PROTOC}
133 ${protobuf_LIB_PROTOBUF}
134 ${protobuf_ABSL_USED_TARGETS}
Mike Kruskala9f1ea62023-01-24 21:49:33 -0800135 ${protobuf_ABSL_USED_TEST_TARGETS}
Mike Kruskal465653e2022-08-25 13:32:32 -0700136 GTest::gmock
137)
Feng Xiao4333edb2015-05-31 02:28:34 -0700138
Mike Kruskal983fc2d2022-11-02 23:24:26 -0700139add_executable(lite-test
140 ${protobuf_lite_test_files}
141 ${lite_test_util_srcs}
142 ${lite_test_proto_files}
143)
144target_link_libraries(lite-test
145 ${protobuf_LIB_PROTOBUF_LITE}
146 ${protobuf_ABSL_USED_TARGETS}
Mike Kruskala9f1ea62023-01-24 21:49:33 -0800147 ${protobuf_ABSL_USED_TEST_TARGETS}
Mike Kruskal983fc2d2022-11-02 23:24:26 -0700148 GTest::gmock_main
149)
Feng Xiaob17ec3c2015-08-23 17:50:38 -0700150
David L. Jones5b2c7b82022-03-30 13:44:42 -0700151add_test(NAME lite-test
Adam Cozzetteae2531d2023-03-03 12:20:37 -0800152 COMMAND lite-test ${protobuf_GTEST_ARGS}
153 WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
Feng Xiaob17ec3c2015-08-23 17:50:38 -0700154
Mike Kruskal7930cd12023-01-20 21:38:59 -0800155add_custom_target(full-test
Konstantin Podsvirove3019462015-09-17 12:08:47 +0300156 COMMAND tests
Adam Cozzette80df4b22023-06-23 13:49:32 -0700157 DEPENDS tests lite-test fake_plugin test_plugin
Adam Cozzetteb20209f2022-03-10 18:36:42 +0000158 WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
Florin Crișan18c951e2022-02-08 03:38:38 +0200159
Mike Kruskal7930cd12023-01-20 21:38:59 -0800160add_test(NAME full-test
Adam Cozzetteae2531d2023-03-03 12:20:37 -0800161 COMMAND tests ${protobuf_GTEST_ARGS}
162 WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700163
164# For test purposes, remove headers that should already be installed. This
165# prevents accidental conflicts and also version skew (since local headers take
166# precedence over installed headers).
167add_custom_target(save-installed-headers)
168add_custom_target(remove-installed-headers)
169add_custom_target(restore-installed-headers)
170
Mike Kruskal81e35132022-09-19 11:08:21 -0700171file(GLOB_RECURSE _local_hdrs
172 "${PROJECT_SOURCE_DIR}/src/*.h"
173 "${PROJECT_SOURCE_DIR}/src/*.inc")
174
175# Exclude the bootstrapping that are directly used by tests.
176set(_exclude_hdrs
Mike Kruskal4f9e4172023-06-30 20:14:50 -0700177 "${protobuf_SOURCE_DIR}/src/google/protobuf/cpp_features.pb.h"
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700178 "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h"
179 "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h")
180
Mike Kruskal81e35132022-09-19 11:08:21 -0700181# Exclude test library headers.
182list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs}
183 ${compiler_test_utils_hdrs})
184foreach(_hdr ${_exclude_hdrs})
185 list(REMOVE_ITEM _local_hdrs ${_hdr})
186endforeach()
187
188foreach(_hdr ${_local_hdrs})
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700189 string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr})
190 set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}")
191 add_custom_command(TARGET remove-installed-headers PRE_BUILD
192 COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}")
193 add_custom_command(TARGET save-installed-headers PRE_BUILD
194 COMMAND ${CMAKE_COMMAND} -E
195 copy "${_hdr}" "${_tmp_file}" || true)
196 add_custom_command(TARGET restore-installed-headers PRE_BUILD
197 COMMAND ${CMAKE_COMMAND} -E
198 copy "${_tmp_file}" "${_hdr}")
199endforeach()
200
201add_dependencies(remove-installed-headers save-installed-headers)
202if(protobuf_REMOVE_INSTALLED_HEADERS)
Mike Kruskal81e35132022-09-19 11:08:21 -0700203 # Make sure we remove all the headers *before* any codegen occurs.
204 add_dependencies(${protobuf_PROTOC_EXE} remove-installed-headers)
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700205endif()