File: tests.cmake

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (309 lines) | stat: -rw-r--r-- 9,709 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
option(protobuf_REMOVE_INSTALLED_HEADERS
  "Remove local headers so that installed ones are used instead" OFF)

option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH
  "Using absolute test_plugin path in tests" ON)
mark_as_advanced(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)

include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
include(${protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake)

set(lite_test_protos
  ${protobuf_lite_test_protos_files}
)

set(tests_protos
  ${protobuf_test_protos_files}
  ${compiler_test_protos_files}
  ${util_test_protos_files}
)

file(MAKE_DIRECTORY ${protobuf_BINARY_DIR}/src)

set(lite_test_proto_files)
foreach(proto_file ${lite_test_protos})
  protobuf_generate(
    PROTOS ${proto_file}
    LANGUAGE cpp
    OUT_VAR pb_generated_files
    IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
  )
  set(lite_test_proto_files ${lite_test_proto_files} ${pb_generated_files})
endforeach(proto_file)

set(tests_proto_files)
foreach(proto_file ${tests_protos})
  protobuf_generate(
    PROTOS ${proto_file}
    LANGUAGE cpp
    OUT_VAR pb_generated_files
    IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
  )
  set(tests_proto_files ${tests_proto_files} ${pb_generated_files})
endforeach(proto_file)

set(common_test_files
  ${test_util_hdrs}
  ${lite_test_util_srcs}
  ${test_util_srcs}
  ${common_test_hdrs}
  ${common_test_srcs}
)

set(tests_files
  ${protobuf_test_files}
  ${compiler_test_files}
  ${annotation_test_util_srcs}
  ${editions_test_files}
  ${io_test_files}
  ${util_test_files}
  ${stubs_test_files}
)

if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
  add_compile_options(-DGOOGLE_PROTOBUF_FAKE_PLUGIN_PATH="$<TARGET_FILE:fake_plugin>")
  add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
endif()

if(MINGW)
  set_source_files_properties(${tests_files} PROPERTIES COMPILE_FLAGS "-Wno-narrowing")

  # required for tests on MinGW Win64
  if (CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj")
  endif()

endif()

if(protobuf_TEST_XML_OUTDIR)
  if(NOT "${protobuf_TEST_XML_OUTDIR}" MATCHES "[/\\]$")
    string(APPEND protobuf_TEST_XML_OUTDIR "/")
  endif()
  set(protobuf_GTEST_ARGS "--gtest_output=xml:${protobuf_TEST_XML_OUTDIR}")
else()
  set(protobuf_GTEST_ARGS)
endif()

add_library(libtest_common STATIC
  ${tests_proto_files}
)
target_link_libraries(libtest_common
  ${protobuf_LIB_PROTOC}
  ${protobuf_LIB_PROTOBUF}
  ${protobuf_ABSL_USED_TARGETS}
  ${protobuf_ABSL_USED_TEST_TARGETS}
  GTest::gmock
)
if (MSVC)
  target_compile_options(libtest_common PRIVATE /bigobj)
endif ()

add_executable(tests ${tests_files} ${common_test_files})
if (MSVC)
  target_compile_options(tests PRIVATE
    /wd4146 # unary minus operator applied to unsigned type, result still unsigned
    /bigobj
  )
endif()
target_link_libraries(tests
  libtest_common
  libtest_common_lite
  ${protobuf_LIB_PROTOC}
  ${protobuf_LIB_PROTOBUF}
  ${protobuf_ABSL_USED_TARGETS}
  ${protobuf_ABSL_USED_TEST_TARGETS}
  GTest::gmock_main
)

add_executable(fake_plugin ${fake_plugin_files} ${common_test_files})
target_include_directories(fake_plugin PRIVATE ${ABSL_ROOT_DIR})
target_link_libraries(fake_plugin
  libtest_common
  libtest_common_lite
  ${protobuf_LIB_PROTOC}
  ${protobuf_LIB_PROTOBUF}
  ${protobuf_ABSL_USED_TARGETS}
  ${protobuf_ABSL_USED_TEST_TARGETS}
  GTest::gmock
)

add_executable(test_plugin ${test_plugin_files} ${common_test_files})
target_include_directories(test_plugin PRIVATE ${ABSL_ROOT_DIR})
target_link_libraries(test_plugin
  libtest_common
  libtest_common_lite
  ${protobuf_LIB_PROTOC}
  ${protobuf_LIB_PROTOBUF}
  ${protobuf_ABSL_USED_TARGETS}
  ${protobuf_ABSL_USED_TEST_TARGETS}
  GTest::gmock
)

add_library(libtest_common_lite STATIC
  ${lite_test_proto_files}
)
target_link_libraries(libtest_common_lite
  ${protobuf_LIB_PROTOBUF_LITE}
  ${protobuf_ABSL_USED_TARGETS}
  GTest::gmock
)

add_executable(lite-test
  ${protobuf_lite_test_files}
  ${lite_test_util_hdrs}
  ${lite_test_util_srcs}
)
target_link_libraries(lite-test
  libtest_common_lite
  ${protobuf_LIB_PROTOBUF_LITE}
  ${protobuf_ABSL_USED_TARGETS}
  ${protobuf_ABSL_USED_TEST_TARGETS}
  GTest::gmock_main
)

add_test(NAME lite-test
  COMMAND lite-test ${protobuf_GTEST_ARGS}
  WORKING_DIRECTORY ${protobuf_SOURCE_DIR})

add_custom_target(full-test
  COMMAND tests
  DEPENDS tests lite-test fake_plugin test_plugin
  WORKING_DIRECTORY ${protobuf_SOURCE_DIR})

add_test(NAME full-test
  COMMAND tests ${protobuf_GTEST_ARGS}
  WORKING_DIRECTORY ${protobuf_SOURCE_DIR})

# This test involves observing and modifying the internal state of the global
# descriptor pool, so it needs to be in its own binary to avoid conflicting
# with other tests.
add_executable(lazily-build-dependencies-test
  ${lazily_build_dependencies_test_files}
)

target_link_libraries(lazily-build-dependencies-test
  libtest_common
  libtest_common_lite
  ${protobuf_LIB_PROTOBUF}
  ${protobuf_ABSL_USED_TARGETS}
  ${protobuf_ABSL_USED_TEST_TARGETS}
  GTest::gmock_main
)

add_test(NAME lazily-build-dependencies-test
  COMMAND lazily-build-dependencies-test ${protobuf_GTEST_ARGS}
  WORKING_DIRECTORY ${protobuf_SOURCE_DIR})

if (protobuf_BUILD_LIBUPB)
  set(upb_test_proto_genfiles)
  foreach(proto_file ${upb_test_protos_files} ${descriptor_proto_proto_srcs})
    foreach(generator upb upbdefs)
      protobuf_generate(
        PROTOS ${proto_file}
        LANGUAGE ${generator}
        GENERATE_EXTENSIONS .${generator}.h .${generator}.c
        OUT_VAR pb_generated_files
        IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
        IMPORT_DIRS ${protobuf_SOURCE_DIR}
        PLUGIN protoc-gen-${generator}=$<TARGET_FILE:protobuf::protoc-gen-${generator}>
        DEPENDENCIES $<TARGET_FILE:protobuf::protoc-gen-${generator}>
      )
      set(upb_test_proto_genfiles ${upb_test_proto_genfiles} ${pb_generated_files})
    endforeach()

    # The minitable generator has a slightly different setup from the other upb
    # generators, since it is built into protoc.
    protobuf_generate(
      PROTOS ${proto_file}
      LANGUAGE upb_minitable
      GENERATE_EXTENSIONS .upb_minitable.h .upb_minitable.c
      OUT_VAR pb_generated_files
      IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
      IMPORT_DIRS ${protobuf_SOURCE_DIR}
    )
    set(upb_test_proto_genfiles ${upb_test_proto_genfiles} ${pb_generated_files})
  endforeach(proto_file)

  add_executable(upb-test
    ${upb_test_files}
    ${upb_test_proto_genfiles}
    ${upb_test_util_files})

  target_link_libraries(upb-test
    ${protobuf_LIB_PROTOBUF}
    ${protobuf_LIB_UPB}
    ${protobuf_ABSL_USED_TARGETS}
    ${protobuf_ABSL_USED_TEST_TARGETS}
    GTest::gmock_main)

  add_test(NAME upb-test
    COMMAND upb-test ${protobuf_GTEST_ARGS}
    WORKING_DIRECTORY ${protobuf_SOURCE_DIR})
endif()

# For test purposes, remove headers that should already be installed.  This
# prevents accidental conflicts and also version skew (since local headers take
# precedence over installed headers).
add_custom_target(save-installed-headers)
add_custom_target(remove-installed-headers)
add_custom_target(restore-installed-headers)

file(GLOB_RECURSE _local_hdrs
  "${PROJECT_SOURCE_DIR}/src/*.h"
  "${PROJECT_SOURCE_DIR}/src/*.inc"
)
file(GLOB_RECURSE _local_upb_hdrs
  "${PROJECT_SOURCE_DIR}/upb/*.h"
)

# Exclude test library headers.
list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs}
  ${compiler_test_utils_hdrs} ${upb_test_util_files} ${libprotoc_hdrs})
foreach(_hdr ${_exclude_hdrs})
  list(REMOVE_ITEM _local_hdrs ${_hdr})
  list(REMOVE_ITEM _local_upb_hdrs ${_hdr})
endforeach()
list(APPEND _local_hdrs ${libprotoc_public_hdrs})

# Exclude the bootstrapping that are directly used by tests.
set(_exclude_hdrs
  "${protobuf_SOURCE_DIR}/src/google/protobuf/cpp_features.pb.h"
  "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h"
  "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h"
  "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/java/java_features.pb.h")
foreach(_hdr ${_exclude_hdrs})
  list(REMOVE_ITEM _local_hdrs ${_hdr})
endforeach()

foreach(_hdr ${_local_hdrs})
  string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr})
  set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}")
  add_custom_command(TARGET remove-installed-headers PRE_BUILD
                     COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}")
  add_custom_command(TARGET save-installed-headers PRE_BUILD
                     COMMAND ${CMAKE_COMMAND} -E
                        copy "${_hdr}" "${_tmp_file}" || true)
  add_custom_command(TARGET restore-installed-headers PRE_BUILD
                     COMMAND ${CMAKE_COMMAND} -E
                        copy "${_tmp_file}" "${_hdr}")
endforeach()

foreach(_hdr ${_local_upb_hdrs})
  string(REPLACE "${protobuf_SOURCE_DIR}/upb" "" _file ${_hdr})
  set(_tmp_file "${CMAKE_BINARY_DIR}/tmp-install-test/${_file}")
  add_custom_command(TARGET remove-installed-headers PRE_BUILD
                     COMMAND ${CMAKE_COMMAND} -E remove -f "${_hdr}")
  add_custom_command(TARGET save-installed-headers PRE_BUILD
                     COMMAND ${CMAKE_COMMAND} -E
                        copy "${_hdr}" "${_tmp_file}" || true)
  add_custom_command(TARGET restore-installed-headers PRE_BUILD
                     COMMAND ${CMAKE_COMMAND} -E
                        copy "${_tmp_file}" "${_hdr}")
endforeach()

add_dependencies(remove-installed-headers save-installed-headers)
if(protobuf_REMOVE_INSTALLED_HEADERS)
  # Make sure we remove all the headers *before* any codegen occurs.
  add_dependencies(${protobuf_PROTOC_EXE} remove-installed-headers)
endif()