Debian Codegraph @

extra/protobuf/protobuf-24.4/cmake/tests.cmake

raw
1 option(protobuf_REMOVE_INSTALLED_HEADERS
2 "Remove local headers so that installed ones are used instead" OFF)
3
4 option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH
5 "Using absolute test_plugin path in tests" ON)
6 mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
7
8 include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
9
10 set(lite_test_protos
11 ${protobuf_lite_test_protos_files}
12 )
13
14 set(tests_protos
15 ${protobuf_test_protos_files}
16 ${compiler_test_protos_files}
17 ${util_test_protos_files}
18 ${lite_test_protos}
19 )
20
21 macro(compile_proto_file filename)
22 string(REPLACE .proto .pb.h pb_hdr ${filename})
23 string(REPLACE .proto .pb.cc pb_src ${filename})
24 add_custom_command(
25 OUTPUT ${pb_hdr} ${pb_src}
26 DEPENDS ${protobuf_PROTOC_EXE} ${filename}
27 COMMAND ${protobuf_PROTOC_EXE} ${filename}
28 --proto_path=${protobuf_SOURCE_DIR}/src
29 --cpp_out=${protobuf_SOURCE_DIR}/src
30 --experimental_allow_proto3_optional
31 )
32 endmacro(compile_proto_file)
33
34 set(lite_test_proto_files)
35 foreach(proto_file ${lite_test_protos})
36 compile_proto_file(${proto_file})
37 set(lite_test_proto_files ${lite_test_proto_files} ${pb_src} ${pb_hdr})
38 endforeach(proto_file)
39
40 set(tests_proto_files)
41 foreach(proto_file ${tests_protos})
42 compile_proto_file(${proto_file})
43 set(tests_proto_files ${tests_proto_files} ${pb_src} ${pb_hdr})
44 endforeach(proto_file)
45
46 set(common_test_files
47 ${test_util_hdrs}
48 ${lite_test_util_srcs}
49 ${test_util_srcs}
50 ${common_test_hdrs}
51 ${common_test_srcs}
52 )
53
54 set(tests_files
55 ${protobuf_test_files}
56 ${compiler_test_files}
57 ${annotation_test_util_srcs}
58 ${io_test_files}
59 ${util_test_files}
60 ${stubs_test_files}
61 )
62
63 if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
64 add_compile_options(-DGOOGLE_PROTOBUF_FAKE_PLUGIN_PATH="$<TARGET_FILE:fake_plugin>")
65 add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
66 endif()
67
68 if(MINGW)
69 set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing")
70
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
77 endif()
78
79 if(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}")
84 else()
85 set(protobuf_GTEST_ARGS)
86 endif()
87
88 add_executable(tests
89 ${tests_files}
90 ${common_test_files}
91 ${tests_proto_files}
92 )
93 if (MSVC)
94 target_compile_options(tests PRIVATE
95 /wd4146 # unary minus operator applied to unsigned type, result still unsigned
96 )
97 endif()
98 target_link_libraries(tests
99 ${protobuf_LIB_PROTOC}
100 ${protobuf_LIB_PROTOBUF}
101 ${protobuf_ABSL_USED_TARGETS}
102 ${protobuf_ABSL_USED_TEST_TARGETS}
103 GTest::gmock_main
104 )
105
106 set(fake_plugin_files
107 ${fake_plugin_files}
108 ${common_test_hdrs}
109 ${common_test_srcs}
110 )
111 set(test_plugin_files
112 ${test_plugin_files}
113 ${common_test_hdrs}
114 ${common_test_srcs}
115 )
116
117 add_executable(fake_plugin ${fake_plugin_files})
118 target_include_directories(fake_plugin PRIVATE ${ABSL_ROOT_DIR})
119 target_link_libraries(fake_plugin
120 ${protobuf_LIB_PROTOC}
121 ${protobuf_LIB_PROTOBUF}
122 ${protobuf_ABSL_USED_TARGETS}
123 ${protobuf_ABSL_USED_TEST_TARGETS}
124 GTest::gmock
125 )
126
127 add_executable(test_plugin ${test_plugin_files})
128 target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR})
129 target_link_libraries(test_plugin
130 ${protobuf_LIB_PROTOC}
131 ${protobuf_LIB_PROTOBUF}
132 ${protobuf_ABSL_USED_TARGETS}
133 ${protobuf_ABSL_USED_TEST_TARGETS}
134 GTest::gmock
135 )
136
137 add_executable(lite-test
138 ${protobuf_lite_test_files}
139 ${lite_test_util_srcs}
140 ${lite_test_proto_files}
141 )
142 target_link_libraries(lite-test
143 ${protobuf_LIB_PROTOBUF_LITE}
144 ${protobuf_ABSL_USED_TARGETS}
145 ${protobuf_ABSL_USED_TEST_TARGETS}
146 GTest::gmock_main
147 )
148
149 add_test(NAME lite-test
150 COMMAND lite-test ${protobuf_GTEST_ARGS}
151 WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
152
153 add_custom_target(full-test
154 COMMAND tests
155 DEPENDS tests lite-test fake_plugin test_plugin
156 WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
157
158 add_test(NAME full-test
159 COMMAND tests ${protobuf_GTEST_ARGS}
160 WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
161
162 # For test purposes, remove headers that should already be installed. This
163 # prevents accidental conflicts and also version skew (since local headers take
164 # precedence over installed headers).
165 add_custom_target(save-installed-headers)
166 add_custom_target(remove-installed-headers)
167 add_custom_target(restore-installed-headers)
168
169 file(GLOB_RECURSE _local_hdrs
170 "${PROJECT_SOURCE_DIR}/src/*.h"
171 "${PROJECT_SOURCE_DIR}/src/*.inc")
172
173 # Exclude the bootstrapping that are directly used by tests.
174 set(_exclude_hdrs
175 "${protobuf_SOURCE_DIR}/src/google/protobuf/cpp_features.pb.h"
176 "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h"
177 "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h")
178
179 # Exclude test library headers.
180 list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs}
181 ${compiler_test_utils_hdrs})
182 foreach(_hdr ${_exclude_hdrs})
183 list(REMOVE_ITEM _local_hdrs ${_hdr})
184 endforeach()
185
186 foreach(_hdr ${_local_hdrs})
187 string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr})
188 set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}")
189 add_custom_command(TARGET remove-installed-headers PRE_BUILD
190 COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}")
191 add_custom_command(TARGET save-installed-headers PRE_BUILD
192 COMMAND ${CMAKE_COMMAND} -E
193 copy "${_hdr}" "${_tmp_file}" || true)
194 add_custom_command(TARGET restore-installed-headers PRE_BUILD
195 COMMAND ${CMAKE_COMMAND} -E
196 copy "${_tmp_file}" "${_hdr}")
197 endforeach()
198
199 add_dependencies(remove-installed-headers save-installed-headers)
200 if(protobuf_REMOVE_INSTALLED_HEADERS)
201 # Make sure we remove all the headers *before* any codegen occurs.
202 add_dependencies(${protobuf_PROTOC_EXE} remove-installed-headers)
203 endif()