• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Bazel (https://bazel.build/) BUILD file for Protobuf.
2
3load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
4load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test", "objc_library", native_cc_proto_library = "cc_proto_library")
5load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
6load("@rules_python//python:defs.bzl", "py_library")
7load(":cc_proto_blacklist_test.bzl", "cc_proto_blacklist_test")
8
9licenses(["notice"])
10
11exports_files(["LICENSE"])
12
13################################################################################
14# build configuration
15################################################################################
16
17# TODO(yannic): Remove in 3.14.0.
18string_flag(
19    name = "incompatible_use_com_google_googletest",
20    build_setting_default = "true",
21    values = ["true", "false"]
22)
23
24config_setting(
25    name = "use_com_google_googletest",
26    flag_values = {
27        "//:incompatible_use_com_google_googletest": "true"
28    },
29)
30
31GTEST = select({
32    "//:use_com_google_googletest": [
33        "@com_google_googletest//:gtest",
34    ],
35    "//conditions:default": [
36        "//external:gtest",
37    ],
38})
39
40GTEST_MAIN = select({
41    "//:use_com_google_googletest": [
42        "@com_google_googletest//:gtest_main",
43    ],
44    "//conditions:default": [
45        "//external:gtest_main",
46    ],
47})
48
49################################################################################
50# ZLIB configuration
51################################################################################
52
53ZLIB_DEPS = ["@zlib//:zlib"]
54
55################################################################################
56# Protobuf Runtime Library
57################################################################################
58
59MSVC_COPTS = [
60    "/DHAVE_PTHREAD",
61    "/wd4018",  # -Wno-sign-compare
62    "/wd4065",  # switch statement contains 'default' but no 'case' labels
63    "/wd4146",  # unary minus operator applied to unsigned type, result still unsigned
64    "/wd4244",  # 'conversion' conversion from 'type1' to 'type2', possible loss of data
65    "/wd4251",  # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
66    "/wd4267",  # 'var' : conversion from 'size_t' to 'type', possible loss of data
67    "/wd4305",  # 'identifier' : truncation from 'type1' to 'type2'
68    "/wd4307",  # 'operator' : integral constant overflow
69    "/wd4309",  # 'conversion' : truncation of constant value
70    "/wd4334",  # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
71    "/wd4355",  # 'this' : used in base member initializer list
72    "/wd4506",  # no definition for inline function 'function'
73    "/wd4514",  # -Wno-unused-function
74    "/wd4800",  # 'type' : forcing value to bool 'true' or 'false' (performance warning)
75    "/wd4996",  # The compiler encountered a deprecated declaration.
76]
77
78COPTS = select({
79    ":msvc": MSVC_COPTS,
80    "//conditions:default": [
81        "-DHAVE_PTHREAD",
82        "-DHAVE_ZLIB",
83        "-Woverloaded-virtual",
84        "-Wno-sign-compare",
85        "-Wno-unused-function",
86        # Prevents ISO C++ const string assignment warnings for pyext sources.
87        "-Wno-write-strings",
88        "-Wno-deprecated-declarations",
89    ],
90})
91
92load(":compiler_config_setting.bzl", "create_compiler_config_setting")
93
94create_compiler_config_setting(
95    name = "msvc",
96    value = "msvc-cl",
97    visibility = [
98        # Public, but Protobuf only visibility.
99        "//:__subpackages__",
100    ],
101)
102
103config_setting(
104    name = "android",
105    values = {
106        "crosstool_top": "//external:android/crosstool",
107    },
108    visibility = [
109        # Public, but Protobuf only visibility.
110        "//:__subpackages__",
111    ],
112)
113
114config_setting(
115    name = "android-libcpp",
116    values = {
117        "crosstool_top": "@androidndk//:toolchain-libcpp",
118    },
119    visibility = [
120        # Public, but Protobuf only visibility.
121        "//:__subpackages__",
122    ],
123)
124
125config_setting(
126    name = "android-gnu-libstdcpp",
127    values = {
128        "crosstool_top": "@androidndk//:toolchain-gnu-libstdcpp",
129    },
130    visibility = [
131        # Public, but Protobuf only visibility.
132        "//:__subpackages__",
133    ],
134)
135
136# Android and MSVC builds do not need to link in a separate pthread library.
137LINK_OPTS = select({
138    ":android": [],
139    ":android-libcpp": [],
140    ":android-gnu-libstdcpp": [],
141    ":msvc": [
142        # Suppress linker warnings about files with no symbols defined.
143        "-ignore:4221",
144    ],
145    "//conditions:default": [
146        "-lpthread",
147        "-lm",
148    ],
149})
150
151load(
152    ":protobuf.bzl",
153    "adapt_proto_library",
154    "cc_proto_library",
155    "internal_copied_filegroup",
156    "internal_gen_well_known_protos_java",
157    "internal_protobuf_py_tests",
158    "py_proto_library",
159)
160
161cc_library(
162    name = "protobuf_lite",
163    srcs = [
164        # AUTOGEN(protobuf_lite_srcs)
165        "src/google/protobuf/any_lite.cc",
166        "src/google/protobuf/arena.cc",
167        "src/google/protobuf/arenastring.cc",
168        "src/google/protobuf/extension_set.cc",
169        "src/google/protobuf/generated_enum_util.cc",
170        "src/google/protobuf/generated_message_table_driven_lite.cc",
171        "src/google/protobuf/generated_message_util.cc",
172        "src/google/protobuf/implicit_weak_message.cc",
173        "src/google/protobuf/io/coded_stream.cc",
174        "src/google/protobuf/io/io_win32.cc",
175        "src/google/protobuf/io/strtod.cc",
176        "src/google/protobuf/io/zero_copy_stream.cc",
177        "src/google/protobuf/io/zero_copy_stream_impl.cc",
178        "src/google/protobuf/io/zero_copy_stream_impl_lite.cc",
179        "src/google/protobuf/map.cc",
180        "src/google/protobuf/message_lite.cc",
181        "src/google/protobuf/parse_context.cc",
182        "src/google/protobuf/repeated_field.cc",
183        "src/google/protobuf/stubs/bytestream.cc",
184        "src/google/protobuf/stubs/common.cc",
185        "src/google/protobuf/stubs/int128.cc",
186        "src/google/protobuf/stubs/status.cc",
187        "src/google/protobuf/stubs/statusor.cc",
188        "src/google/protobuf/stubs/stringpiece.cc",
189        "src/google/protobuf/stubs/stringprintf.cc",
190        "src/google/protobuf/stubs/structurally_valid.cc",
191        "src/google/protobuf/stubs/strutil.cc",
192        "src/google/protobuf/stubs/time.cc",
193        "src/google/protobuf/wire_format_lite.cc",
194    ],
195    hdrs = glob([
196        "src/google/protobuf/**/*.h",
197        "src/google/protobuf/**/*.inc",
198    ]),
199    copts = COPTS,
200    includes = ["src/"],
201    linkopts = LINK_OPTS,
202    visibility = ["//visibility:public"],
203)
204
205PROTOBUF_DEPS = select({
206    ":msvc": [],
207    "//conditions:default": ZLIB_DEPS,
208})
209
210cc_library(
211    name = "protobuf",
212    srcs = [
213        # AUTOGEN(protobuf_srcs)
214        "src/google/protobuf/any.cc",
215        "src/google/protobuf/any.pb.cc",
216        "src/google/protobuf/api.pb.cc",
217        "src/google/protobuf/compiler/importer.cc",
218        "src/google/protobuf/compiler/parser.cc",
219        "src/google/protobuf/descriptor.cc",
220        "src/google/protobuf/descriptor.pb.cc",
221        "src/google/protobuf/descriptor_database.cc",
222        "src/google/protobuf/duration.pb.cc",
223        "src/google/protobuf/dynamic_message.cc",
224        "src/google/protobuf/empty.pb.cc",
225        "src/google/protobuf/extension_set_heavy.cc",
226        "src/google/protobuf/field_mask.pb.cc",
227        "src/google/protobuf/generated_message_reflection.cc",
228        "src/google/protobuf/generated_message_table_driven.cc",
229        "src/google/protobuf/io/gzip_stream.cc",
230        "src/google/protobuf/io/printer.cc",
231        "src/google/protobuf/io/tokenizer.cc",
232        "src/google/protobuf/map_field.cc",
233        "src/google/protobuf/message.cc",
234        "src/google/protobuf/reflection_ops.cc",
235        "src/google/protobuf/service.cc",
236        "src/google/protobuf/source_context.pb.cc",
237        "src/google/protobuf/struct.pb.cc",
238        "src/google/protobuf/stubs/substitute.cc",
239        "src/google/protobuf/text_format.cc",
240        "src/google/protobuf/timestamp.pb.cc",
241        "src/google/protobuf/type.pb.cc",
242        "src/google/protobuf/unknown_field_set.cc",
243        "src/google/protobuf/util/delimited_message_util.cc",
244        "src/google/protobuf/util/field_comparator.cc",
245        "src/google/protobuf/util/field_mask_util.cc",
246        "src/google/protobuf/util/internal/datapiece.cc",
247        "src/google/protobuf/util/internal/default_value_objectwriter.cc",
248        "src/google/protobuf/util/internal/error_listener.cc",
249        "src/google/protobuf/util/internal/field_mask_utility.cc",
250        "src/google/protobuf/util/internal/json_escaping.cc",
251        "src/google/protobuf/util/internal/json_objectwriter.cc",
252        "src/google/protobuf/util/internal/json_stream_parser.cc",
253        "src/google/protobuf/util/internal/object_writer.cc",
254        "src/google/protobuf/util/internal/proto_writer.cc",
255        "src/google/protobuf/util/internal/protostream_objectsource.cc",
256        "src/google/protobuf/util/internal/protostream_objectwriter.cc",
257        "src/google/protobuf/util/internal/type_info.cc",
258        "src/google/protobuf/util/internal/type_info_test_helper.cc",
259        "src/google/protobuf/util/internal/utility.cc",
260        "src/google/protobuf/util/json_util.cc",
261        "src/google/protobuf/util/message_differencer.cc",
262        "src/google/protobuf/util/time_util.cc",
263        "src/google/protobuf/util/type_resolver_util.cc",
264        "src/google/protobuf/wire_format.cc",
265        "src/google/protobuf/wrappers.pb.cc",
266    ],
267    hdrs = glob([
268        "src/**/*.h",
269        "src/**/*.inc",
270    ]),
271    copts = COPTS,
272    includes = ["src/"],
273    linkopts = LINK_OPTS,
274    visibility = ["//visibility:public"],
275    deps = [":protobuf_lite"] + PROTOBUF_DEPS,
276)
277
278# This provides just the header files for use in projects that need to build
279# shared libraries for dynamic loading. This target is available until Bazel
280# adds native support for such use cases.
281# TODO(keveman): Remove this target once the support gets added to Bazel.
282cc_library(
283    name = "protobuf_headers",
284    hdrs = glob([
285        "src/**/*.h",
286        "src/**/*.inc",
287    ]),
288    includes = ["src/"],
289    visibility = ["//visibility:public"],
290)
291
292# Map of all well known protos.
293# name => (include path, imports)
294WELL_KNOWN_PROTO_MAP = {
295    "any": ("src/google/protobuf/any.proto", []),
296    "api": (
297        "src/google/protobuf/api.proto",
298        [
299            "source_context",
300            "type",
301        ],
302    ),
303    "compiler_plugin": (
304        "src/google/protobuf/compiler/plugin.proto",
305        ["descriptor"],
306    ),
307    "descriptor": ("src/google/protobuf/descriptor.proto", []),
308    "duration": ("src/google/protobuf/duration.proto", []),
309    "empty": ("src/google/protobuf/empty.proto", []),
310    "field_mask": ("src/google/protobuf/field_mask.proto", []),
311    "source_context": ("src/google/protobuf/source_context.proto", []),
312    "struct": ("src/google/protobuf/struct.proto", []),
313    "timestamp": ("src/google/protobuf/timestamp.proto", []),
314    "type": (
315        "src/google/protobuf/type.proto",
316        [
317            "any",
318            "source_context",
319        ],
320    ),
321    "wrappers": ("src/google/protobuf/wrappers.proto", []),
322}
323
324WELL_KNOWN_PROTOS = [value[0] for value in WELL_KNOWN_PROTO_MAP.values()]
325
326filegroup(
327    name = "well_known_protos",
328    srcs = WELL_KNOWN_PROTOS,
329    visibility = ["//visibility:public"],
330)
331
332adapt_proto_library(
333    name = "cc_wkt_protos_genproto",
334    deps = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()],
335    visibility = ["//visibility:public"],
336)
337
338cc_library(
339    name = "cc_wkt_protos",
340    deprecation = "Only for backward compatibility. Do not use.",
341    visibility = ["//visibility:public"],
342)
343
344################################################################################
345# Well Known Types Proto Library Rules
346#
347# These proto_library rules can be used with one of the language specific proto
348# library rules i.e. java_proto_library:
349#
350# java_proto_library(
351#   name = "any_java_proto",
352#   deps = ["@com_google_protobuf//:any_proto],
353# )
354################################################################################
355
356[proto_library(
357    name = proto[0] + "_proto",
358    srcs = [proto[1][0]],
359    strip_import_prefix = "src",
360    visibility = ["//visibility:public"],
361    deps = [dep + "_proto" for dep in proto[1][1]],
362) for proto in WELL_KNOWN_PROTO_MAP.items()]
363
364[native_cc_proto_library(
365    name = proto + "_cc_proto",
366    deps = [proto + "_proto"],
367    visibility = ["//visibility:private"],
368) for proto in WELL_KNOWN_PROTO_MAP.keys()]
369
370cc_proto_blacklist_test(
371    name = "cc_proto_blacklist_test",
372    deps = [proto + "_cc_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()],
373    tags = [
374        # Exclude this target from wildcard expansion (//...). Due to
375        # https://github.com/bazelbuild/bazel/issues/10590, this test has to
376        # be nominated using the `@com_google_protobuf//` prefix. We do that,
377        # e.g., in kokoro/linux/bazel/build.sh.
378        # See also https://github.com/protocolbuffers/protobuf/pull/7096.
379        "manual",
380    ],
381)
382
383################################################################################
384# Protocol Buffers Compiler
385################################################################################
386
387cc_library(
388    name = "protoc_lib",
389    srcs = [
390        # AUTOGEN(protoc_lib_srcs)
391        "src/google/protobuf/compiler/code_generator.cc",
392        "src/google/protobuf/compiler/command_line_interface.cc",
393        "src/google/protobuf/compiler/cpp/cpp_enum.cc",
394        "src/google/protobuf/compiler/cpp/cpp_enum_field.cc",
395        "src/google/protobuf/compiler/cpp/cpp_extension.cc",
396        "src/google/protobuf/compiler/cpp/cpp_field.cc",
397        "src/google/protobuf/compiler/cpp/cpp_file.cc",
398        "src/google/protobuf/compiler/cpp/cpp_generator.cc",
399        "src/google/protobuf/compiler/cpp/cpp_helpers.cc",
400        "src/google/protobuf/compiler/cpp/cpp_map_field.cc",
401        "src/google/protobuf/compiler/cpp/cpp_message.cc",
402        "src/google/protobuf/compiler/cpp/cpp_message_field.cc",
403        "src/google/protobuf/compiler/cpp/cpp_padding_optimizer.cc",
404        "src/google/protobuf/compiler/cpp/cpp_primitive_field.cc",
405        "src/google/protobuf/compiler/cpp/cpp_service.cc",
406        "src/google/protobuf/compiler/cpp/cpp_string_field.cc",
407        "src/google/protobuf/compiler/csharp/csharp_doc_comment.cc",
408        "src/google/protobuf/compiler/csharp/csharp_enum.cc",
409        "src/google/protobuf/compiler/csharp/csharp_enum_field.cc",
410        "src/google/protobuf/compiler/csharp/csharp_field_base.cc",
411        "src/google/protobuf/compiler/csharp/csharp_generator.cc",
412        "src/google/protobuf/compiler/csharp/csharp_helpers.cc",
413        "src/google/protobuf/compiler/csharp/csharp_map_field.cc",
414        "src/google/protobuf/compiler/csharp/csharp_message.cc",
415        "src/google/protobuf/compiler/csharp/csharp_message_field.cc",
416        "src/google/protobuf/compiler/csharp/csharp_primitive_field.cc",
417        "src/google/protobuf/compiler/csharp/csharp_reflection_class.cc",
418        "src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc",
419        "src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc",
420        "src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc",
421        "src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc",
422        "src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc",
423        "src/google/protobuf/compiler/java/java_context.cc",
424        "src/google/protobuf/compiler/java/java_doc_comment.cc",
425        "src/google/protobuf/compiler/java/java_enum.cc",
426        "src/google/protobuf/compiler/java/java_enum_field.cc",
427        "src/google/protobuf/compiler/java/java_enum_field_lite.cc",
428        "src/google/protobuf/compiler/java/java_enum_lite.cc",
429        "src/google/protobuf/compiler/java/java_extension.cc",
430        "src/google/protobuf/compiler/java/java_extension_lite.cc",
431        "src/google/protobuf/compiler/java/java_field.cc",
432        "src/google/protobuf/compiler/java/java_file.cc",
433        "src/google/protobuf/compiler/java/java_generator.cc",
434        "src/google/protobuf/compiler/java/java_generator_factory.cc",
435        "src/google/protobuf/compiler/java/java_helpers.cc",
436        "src/google/protobuf/compiler/java/java_map_field.cc",
437        "src/google/protobuf/compiler/java/java_map_field_lite.cc",
438        "src/google/protobuf/compiler/java/java_message.cc",
439        "src/google/protobuf/compiler/java/java_message_builder.cc",
440        "src/google/protobuf/compiler/java/java_message_builder_lite.cc",
441        "src/google/protobuf/compiler/java/java_message_field.cc",
442        "src/google/protobuf/compiler/java/java_message_field_lite.cc",
443        "src/google/protobuf/compiler/java/java_message_lite.cc",
444        "src/google/protobuf/compiler/java/java_name_resolver.cc",
445        "src/google/protobuf/compiler/java/java_primitive_field.cc",
446        "src/google/protobuf/compiler/java/java_primitive_field_lite.cc",
447        "src/google/protobuf/compiler/java/java_service.cc",
448        "src/google/protobuf/compiler/java/java_shared_code_generator.cc",
449        "src/google/protobuf/compiler/java/java_string_field.cc",
450        "src/google/protobuf/compiler/java/java_string_field_lite.cc",
451        "src/google/protobuf/compiler/js/js_generator.cc",
452        "src/google/protobuf/compiler/js/well_known_types_embed.cc",
453        "src/google/protobuf/compiler/objectivec/objectivec_enum.cc",
454        "src/google/protobuf/compiler/objectivec/objectivec_enum_field.cc",
455        "src/google/protobuf/compiler/objectivec/objectivec_extension.cc",
456        "src/google/protobuf/compiler/objectivec/objectivec_field.cc",
457        "src/google/protobuf/compiler/objectivec/objectivec_file.cc",
458        "src/google/protobuf/compiler/objectivec/objectivec_generator.cc",
459        "src/google/protobuf/compiler/objectivec/objectivec_helpers.cc",
460        "src/google/protobuf/compiler/objectivec/objectivec_map_field.cc",
461        "src/google/protobuf/compiler/objectivec/objectivec_message.cc",
462        "src/google/protobuf/compiler/objectivec/objectivec_message_field.cc",
463        "src/google/protobuf/compiler/objectivec/objectivec_oneof.cc",
464        "src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc",
465        "src/google/protobuf/compiler/php/php_generator.cc",
466        "src/google/protobuf/compiler/plugin.cc",
467        "src/google/protobuf/compiler/plugin.pb.cc",
468        "src/google/protobuf/compiler/python/python_generator.cc",
469        "src/google/protobuf/compiler/ruby/ruby_generator.cc",
470        "src/google/protobuf/compiler/subprocess.cc",
471        "src/google/protobuf/compiler/zip_writer.cc",
472    ],
473    copts = COPTS,
474    includes = ["src/"],
475    linkopts = LINK_OPTS,
476    visibility = ["//visibility:public"],
477    deps = [":protobuf"],
478)
479
480cc_binary(
481    name = "protoc",
482    srcs = ["src/google/protobuf/compiler/main.cc"],
483    linkopts = LINK_OPTS,
484    visibility = ["//visibility:public"],
485    deps = [":protoc_lib"],
486)
487
488################################################################################
489# Tests
490################################################################################
491
492RELATIVE_LITE_TEST_PROTOS = [
493    # AUTOGEN(lite_test_protos)
494    "google/protobuf/map_lite_unittest.proto",
495    "google/protobuf/unittest_import_lite.proto",
496    "google/protobuf/unittest_import_public_lite.proto",
497    "google/protobuf/unittest_lite.proto",
498]
499
500LITE_TEST_PROTOS = ["src/" + s for s in RELATIVE_LITE_TEST_PROTOS]
501
502RELATIVE_TEST_PROTOS = [
503    # AUTOGEN(test_protos)
504    "google/protobuf/any_test.proto",
505    "google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto",
506    "google/protobuf/compiler/cpp/cpp_test_large_enum_value.proto",
507    "google/protobuf/map_proto2_unittest.proto",
508    "google/protobuf/map_unittest.proto",
509    "google/protobuf/unittest.proto",
510    "google/protobuf/unittest_arena.proto",
511    "google/protobuf/unittest_custom_options.proto",
512    "google/protobuf/unittest_drop_unknown_fields.proto",
513    "google/protobuf/unittest_embed_optimize_for.proto",
514    "google/protobuf/unittest_empty.proto",
515    "google/protobuf/unittest_enormous_descriptor.proto",
516    "google/protobuf/unittest_import.proto",
517    "google/protobuf/unittest_import_public.proto",
518    "google/protobuf/unittest_lazy_dependencies.proto",
519    "google/protobuf/unittest_lazy_dependencies_custom_option.proto",
520    "google/protobuf/unittest_lazy_dependencies_enum.proto",
521    "google/protobuf/unittest_lite_imports_nonlite.proto",
522    "google/protobuf/unittest_mset.proto",
523    "google/protobuf/unittest_mset_wire_format.proto",
524    "google/protobuf/unittest_no_field_presence.proto",
525    "google/protobuf/unittest_no_generic_services.proto",
526    "google/protobuf/unittest_optimize_for.proto",
527    "google/protobuf/unittest_preserve_unknown_enum.proto",
528    "google/protobuf/unittest_preserve_unknown_enum2.proto",
529    "google/protobuf/unittest_proto3.proto",
530    "google/protobuf/unittest_proto3_arena.proto",
531    "google/protobuf/unittest_proto3_arena_lite.proto",
532    "google/protobuf/unittest_proto3_lite.proto",
533    "google/protobuf/unittest_proto3_optional.proto",
534    "google/protobuf/unittest_well_known_types.proto",
535    "google/protobuf/util/internal/testdata/anys.proto",
536    "google/protobuf/util/internal/testdata/books.proto",
537    "google/protobuf/util/internal/testdata/default_value.proto",
538    "google/protobuf/util/internal/testdata/default_value_test.proto",
539    "google/protobuf/util/internal/testdata/field_mask.proto",
540    "google/protobuf/util/internal/testdata/maps.proto",
541    "google/protobuf/util/internal/testdata/oneofs.proto",
542    "google/protobuf/util/internal/testdata/proto3.proto",
543    "google/protobuf/util/internal/testdata/struct.proto",
544    "google/protobuf/util/internal/testdata/timestamp_duration.proto",
545    "google/protobuf/util/internal/testdata/wrappers.proto",
546    "google/protobuf/util/json_format.proto",
547    "google/protobuf/util/json_format_proto3.proto",
548    "google/protobuf/util/message_differencer_unittest.proto",
549]
550
551TEST_PROTOS = ["src/" + s for s in RELATIVE_TEST_PROTOS]
552
553cc_proto_library(
554    name = "cc_test_protos",
555    srcs = LITE_TEST_PROTOS + TEST_PROTOS,
556    include = "src",
557    default_runtime = ":protobuf",
558    protoc = ":protoc",
559    deps = [":cc_wkt_protos"],
560)
561
562COMMON_TEST_SRCS = [
563    # AUTOGEN(common_test_srcs)
564    "src/google/protobuf/arena_test_util.cc",
565    "src/google/protobuf/map_test_util.inc",
566    "src/google/protobuf/test_util.cc",
567    "src/google/protobuf/test_util.inc",
568    "src/google/protobuf/testing/file.cc",
569    "src/google/protobuf/testing/googletest.cc",
570]
571
572cc_binary(
573    name = "test_plugin",
574    srcs = [
575        # AUTOGEN(test_plugin_srcs)
576        "src/google/protobuf/compiler/mock_code_generator.cc",
577        "src/google/protobuf/compiler/test_plugin.cc",
578        "src/google/protobuf/testing/file.cc",
579    ],
580    deps = [
581        ":protobuf",
582        ":protoc_lib",
583    ] + GTEST,
584)
585
586cc_test(
587    name = "win32_test",
588    srcs = ["src/google/protobuf/io/io_win32_unittest.cc"],
589    tags = [
590        "manual",
591        "windows",
592    ],
593    deps = [
594        ":protobuf_lite",
595    ] + GTEST_MAIN,
596)
597
598cc_test(
599    name = "protobuf_test",
600    srcs = COMMON_TEST_SRCS + [
601        # AUTOGEN(test_srcs)
602        "src/google/protobuf/any_test.cc",
603        "src/google/protobuf/arena_unittest.cc",
604        "src/google/protobuf/arenastring_unittest.cc",
605        "src/google/protobuf/compiler/annotation_test_util.cc",
606        "src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc",
607        "src/google/protobuf/compiler/cpp/cpp_move_unittest.cc",
608        "src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc",
609        "src/google/protobuf/compiler/cpp/cpp_unittest.cc",
610        "src/google/protobuf/compiler/cpp/cpp_unittest.inc",
611        "src/google/protobuf/compiler/cpp/metadata_test.cc",
612        "src/google/protobuf/compiler/csharp/csharp_bootstrap_unittest.cc",
613        "src/google/protobuf/compiler/csharp/csharp_generator_unittest.cc",
614        "src/google/protobuf/compiler/importer_unittest.cc",
615        "src/google/protobuf/compiler/java/java_doc_comment_unittest.cc",
616        "src/google/protobuf/compiler/java/java_plugin_unittest.cc",
617        "src/google/protobuf/compiler/mock_code_generator.cc",
618        "src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc",
619        "src/google/protobuf/compiler/parser_unittest.cc",
620        "src/google/protobuf/compiler/python/python_plugin_unittest.cc",
621        "src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc",
622        "src/google/protobuf/descriptor_database_unittest.cc",
623        "src/google/protobuf/descriptor_unittest.cc",
624        "src/google/protobuf/drop_unknown_fields_test.cc",
625        "src/google/protobuf/dynamic_message_unittest.cc",
626        "src/google/protobuf/extension_set_unittest.cc",
627        "src/google/protobuf/generated_message_reflection_unittest.cc",
628        "src/google/protobuf/io/coded_stream_unittest.cc",
629        "src/google/protobuf/io/io_win32_unittest.cc",
630        "src/google/protobuf/io/printer_unittest.cc",
631        "src/google/protobuf/io/tokenizer_unittest.cc",
632        "src/google/protobuf/io/zero_copy_stream_unittest.cc",
633        "src/google/protobuf/map_field_test.cc",
634        "src/google/protobuf/map_test.cc",
635        "src/google/protobuf/message_unittest.cc",
636        "src/google/protobuf/message_unittest.inc",
637        "src/google/protobuf/no_field_presence_test.cc",
638        "src/google/protobuf/preserve_unknown_enum_test.cc",
639        "src/google/protobuf/proto3_arena_lite_unittest.cc",
640        "src/google/protobuf/proto3_arena_unittest.cc",
641        "src/google/protobuf/proto3_lite_unittest.cc",
642        "src/google/protobuf/proto3_lite_unittest.inc",
643        "src/google/protobuf/reflection_ops_unittest.cc",
644        "src/google/protobuf/repeated_field_reflection_unittest.cc",
645        "src/google/protobuf/repeated_field_unittest.cc",
646        "src/google/protobuf/stubs/bytestream_unittest.cc",
647        "src/google/protobuf/stubs/common_unittest.cc",
648        "src/google/protobuf/stubs/int128_unittest.cc",
649        "src/google/protobuf/stubs/status_test.cc",
650        "src/google/protobuf/stubs/statusor_test.cc",
651        "src/google/protobuf/stubs/stringpiece_unittest.cc",
652        "src/google/protobuf/stubs/stringprintf_unittest.cc",
653        "src/google/protobuf/stubs/structurally_valid_unittest.cc",
654        "src/google/protobuf/stubs/strutil_unittest.cc",
655        "src/google/protobuf/stubs/template_util_unittest.cc",
656        "src/google/protobuf/stubs/time_test.cc",
657        "src/google/protobuf/text_format_unittest.cc",
658        "src/google/protobuf/unknown_field_set_unittest.cc",
659        "src/google/protobuf/util/delimited_message_util_test.cc",
660        "src/google/protobuf/util/field_comparator_test.cc",
661        "src/google/protobuf/util/field_mask_util_test.cc",
662        "src/google/protobuf/util/internal/default_value_objectwriter_test.cc",
663        "src/google/protobuf/util/internal/json_objectwriter_test.cc",
664        "src/google/protobuf/util/internal/json_stream_parser_test.cc",
665        "src/google/protobuf/util/internal/protostream_objectsource_test.cc",
666        "src/google/protobuf/util/internal/protostream_objectwriter_test.cc",
667        "src/google/protobuf/util/internal/type_info_test_helper.cc",
668        "src/google/protobuf/util/json_util_test.cc",
669        "src/google/protobuf/util/message_differencer_unittest.cc",
670        "src/google/protobuf/util/time_util_test.cc",
671        "src/google/protobuf/util/type_resolver_util_test.cc",
672        "src/google/protobuf/well_known_types_unittest.cc",
673        "src/google/protobuf/wire_format_unittest.cc",
674    ] + select({
675        "//conditions:default": [
676            # AUTOGEN(non_msvc_test_srcs)
677            "src/google/protobuf/compiler/command_line_interface_unittest.cc",
678        ],
679        ":msvc": [],
680    }),
681    copts = COPTS,
682    data = [
683        ":test_plugin",
684    ] + glob([
685        "src/google/protobuf/**/*",
686        # Files for csharp_bootstrap_unittest.cc.
687        "conformance/**/*",
688        "csharp/src/**/*",
689    ]),
690    includes = [
691        "src/",
692    ],
693    linkopts = LINK_OPTS,
694    deps = [
695        ":cc_test_protos",
696        ":protobuf",
697        ":protoc_lib",
698    ] + PROTOBUF_DEPS + GTEST_MAIN,
699)
700
701################################################################################
702# Java support
703################################################################################
704
705internal_gen_well_known_protos_java(
706    name = "gen_well_known_protos_java",
707    deps = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()],
708    visibility = [
709        "//java:__subpackages__",
710    ],
711)
712
713alias(
714    name = "protobuf_java",
715    actual = "//java/core",
716    visibility = ["//visibility:public"],
717)
718
719alias(
720    name = "protobuf_javalite",
721    actual = "//java/lite",
722    visibility = ["//visibility:public"],
723)
724
725alias(
726    name = "protobuf_java_util",
727    actual = "//java/util",
728    visibility = ["//visibility:public"],
729)
730
731alias(
732    name = "java_toolchain",
733    actual = "//java/core:toolchain",
734    visibility = ["//visibility:public"],
735)
736
737alias(
738    name = "javalite_toolchain",
739    actual = "//java/lite:toolchain",
740    visibility = ["//visibility:public"],
741)
742
743################################################################################
744# Python support
745################################################################################
746
747py_library(
748    name = "python_srcs",
749    srcs = glob(
750        [
751            "python/google/protobuf/**/*.py",
752        ],
753        exclude = [
754            "python/google/protobuf/internal/*_test.py",
755            "python/google/protobuf/internal/test_util.py",
756        ],
757    ),
758    imports = ["python"],
759    srcs_version = "PY2AND3",
760)
761
762cc_binary(
763    name = "python/google/protobuf/internal/_api_implementation.so",
764    srcs = ["python/google/protobuf/internal/api_implementation.cc"],
765    copts = COPTS + [
766        "-DPYTHON_PROTO2_CPP_IMPL_V2",
767    ],
768    tags = [
769        # Exclude this target from wildcard expansion (//...) because it may
770        # not even be buildable. It will be built if it is needed according
771        # to :use_fast_cpp_protos.
772        # https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
773        "manual",
774    ],
775    linkshared = 1,
776    linkstatic = 1,
777    deps = select({
778        "//conditions:default": [],
779        ":use_fast_cpp_protos": ["//external:python_headers"],
780    }),
781)
782
783cc_binary(
784    name = "python/google/protobuf/pyext/_message.so",
785    srcs = glob([
786        "python/google/protobuf/pyext/*.cc",
787        "python/google/protobuf/pyext/*.h",
788    ]),
789    copts = COPTS + [
790        "-DGOOGLE_PROTOBUF_HAS_ONEOF=1",
791    ] + select({
792        "//conditions:default": [],
793        ":allow_oversize_protos": ["-DPROTOBUF_PYTHON_ALLOW_OVERSIZE_PROTOS=1"],
794    }),
795    includes = [
796        "python/",
797        "src/",
798    ],
799    tags = [
800        # Exclude this target from wildcard expansion (//...) because it may
801        # not even be buildable. It will be built if it is needed according
802        # to :use_fast_cpp_protos.
803        # https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
804        "manual",
805    ],
806    linkshared = 1,
807    linkstatic = 1,
808    deps = [
809        ":protobuf",
810        ":proto_api",
811    ] + select({
812        "//conditions:default": [],
813        ":use_fast_cpp_protos": ["//external:python_headers"],
814    }),
815)
816
817config_setting(
818    name = "use_fast_cpp_protos",
819    values = {
820        "define": "use_fast_cpp_protos=true",
821    },
822    visibility = [
823        # Public, but Protobuf only visibility.
824        "//:__subpackages__",
825    ],
826)
827
828config_setting(
829    name = "allow_oversize_protos",
830    values = {
831        "define": "allow_oversize_protos=true",
832    },
833    visibility = [
834        # Public, but Protobuf only visibility.
835        "//:__subpackages__",
836    ],
837)
838
839# Copy the builtin proto files from src/google/protobuf to
840# python/google/protobuf. This way, the generated Python sources will be in the
841# same directory as the Python runtime sources. This is necessary for the
842# modules to be imported correctly since they are all part of the same Python
843# package.
844internal_copied_filegroup(
845    name = "protos_python",
846    srcs = WELL_KNOWN_PROTOS,
847    dest = "python",
848    strip_prefix = "src",
849)
850
851# TODO(dzc): Remove this once py_proto_library can have labels in srcs, in
852# which case we can simply add :protos_python in srcs.
853COPIED_WELL_KNOWN_PROTOS = ["python/" + s[4:] for s in WELL_KNOWN_PROTOS]
854
855py_proto_library(
856    name = "protobuf_python",
857    srcs = COPIED_WELL_KNOWN_PROTOS,
858    include = "python",
859    data = select({
860        "//conditions:default": [],
861        ":use_fast_cpp_protos": [
862            ":python/google/protobuf/internal/_api_implementation.so",
863            ":python/google/protobuf/pyext/_message.so",
864        ],
865    }),
866    default_runtime = "",
867    protoc = ":protoc",
868    py_libs = [
869        ":python_srcs",
870        "@six//:six",
871    ],
872    srcs_version = "PY2AND3",
873    visibility = ["//visibility:public"],
874)
875
876# Copy the test proto files from src/google/protobuf to
877# python/google/protobuf. This way, the generated Python sources will be in the
878# same directory as the Python runtime sources. This is necessary for the
879# modules to be imported correctly by the tests since they are all part of the
880# same Python package.
881internal_copied_filegroup(
882    name = "protos_python_test",
883    srcs = LITE_TEST_PROTOS + TEST_PROTOS,
884    dest = "python",
885    strip_prefix = "src",
886)
887
888# TODO(dzc): Remove this once py_proto_library can have labels in srcs, in
889# which case we can simply add :protos_python_test in srcs.
890COPIED_LITE_TEST_PROTOS = ["python/" + s for s in RELATIVE_LITE_TEST_PROTOS]
891
892COPIED_TEST_PROTOS = ["python/" + s for s in RELATIVE_TEST_PROTOS]
893
894py_proto_library(
895    name = "python_common_test_protos",
896    srcs = COPIED_LITE_TEST_PROTOS + COPIED_TEST_PROTOS,
897    include = "python",
898    default_runtime = "",
899    protoc = ":protoc",
900    srcs_version = "PY2AND3",
901    deps = [":protobuf_python"],
902)
903
904py_proto_library(
905    name = "python_specific_test_protos",
906    srcs = glob([
907        "python/google/protobuf/internal/*.proto",
908        "python/google/protobuf/internal/import_test_package/*.proto",
909    ]),
910    include = "python",
911    default_runtime = ":protobuf_python",
912    protoc = ":protoc",
913    srcs_version = "PY2AND3",
914    deps = [":python_common_test_protos"],
915)
916
917py_library(
918    name = "python_tests",
919    srcs = glob(
920        [
921            "python/google/protobuf/internal/*_test.py",
922            "python/google/protobuf/internal/test_util.py",
923            "python/google/protobuf/internal/import_test_package/__init__.py",
924        ],
925    ),
926    imports = ["python"],
927    srcs_version = "PY2AND3",
928    deps = [
929        ":protobuf_python",
930        ":python_common_test_protos",
931        ":python_specific_test_protos",
932    ],
933)
934
935internal_protobuf_py_tests(
936    name = "python_tests_batch",
937    data = glob([
938        "src/google/protobuf/**/*",
939    ]),
940    modules = [
941        "descriptor_database_test",
942        "descriptor_pool_test",
943        "descriptor_test",
944        "generator_test",
945        "json_format_test",
946        "message_factory_test",
947        "message_test",
948        "proto_builder_test",
949        "reflection_test",
950        "service_reflection_test",
951        "symbol_database_test",
952        "text_encoding_test",
953        "text_format_test",
954        "unknown_fields_test",
955        "wire_format_test",
956    ],
957    deps = [":python_tests"],
958)
959
960cc_library(
961    name = "proto_api",
962    hdrs = ["python/google/protobuf/proto_api.h"],
963    visibility = ["//visibility:public"],
964    deps = [
965        "//external:python_headers",
966    ],
967)
968
969proto_lang_toolchain(
970    name = "cc_toolchain",
971    blacklisted_protos = [proto + "_proto" for proto in WELL_KNOWN_PROTO_MAP.keys()],
972    command_line = "--cpp_out=$(OUT)",
973    runtime = ":protobuf",
974    visibility = ["//visibility:public"],
975)
976
977alias(
978    name = "objectivec",
979    actual = "//objectivec",
980    visibility = ["//visibility:public"],
981)
982
983alias(
984    name = "protobuf_objc",
985    actual = "//objectivec",
986    visibility = ["//visibility:public"],
987)
988
989################################################################################
990# Test generated proto support
991################################################################################
992
993genrule(
994    name = "generated_protos",
995    srcs = ["src/google/protobuf/unittest_import.proto"],
996    outs = ["unittest_gen.proto"],
997    cmd = "cat $(SRCS) | sed 's|google/|src/google/|' >  $(OUTS)",
998)
999
1000proto_library(
1001    name = "generated_protos_proto",
1002    srcs = [
1003        "src/google/protobuf/unittest_import_public.proto",
1004        "unittest_gen.proto",
1005    ],
1006)
1007
1008py_proto_library(
1009    name = "generated_protos_py",
1010    srcs = [
1011        "src/google/protobuf/unittest_import_public.proto",
1012        "unittest_gen.proto",
1013    ],
1014    default_runtime = "",
1015    protoc = ":protoc",
1016)
1017
1018################################################################################
1019# Conformance tests
1020################################################################################
1021
1022proto_library(
1023    name = "test_messages_proto2_proto",
1024    srcs = ["src/google/protobuf/test_messages_proto2.proto"],
1025    visibility = ["//visibility:public"],
1026)
1027
1028proto_library(
1029    name = "test_messages_proto3_proto",
1030    srcs = ["src/google/protobuf/test_messages_proto3.proto"],
1031    visibility = ["//visibility:public"],
1032    deps = [
1033        ":any_proto",
1034        ":duration_proto",
1035        ":field_mask_proto",
1036        ":struct_proto",
1037        ":timestamp_proto",
1038        ":wrappers_proto",
1039    ],
1040)
1041
1042cc_proto_library(
1043    name = "test_messages_proto2_proto_cc",
1044    srcs = ["src/google/protobuf/test_messages_proto2.proto"],
1045)
1046
1047cc_proto_library(
1048    name = "test_messages_proto3_proto_cc",
1049    srcs = ["src/google/protobuf/test_messages_proto3.proto"],
1050    deps = [
1051        ":cc_wkt_protos",
1052    ],
1053)
1054
1055proto_library(
1056    name = "conformance_proto",
1057    srcs = ["conformance/conformance.proto"],
1058    visibility = ["//visibility:public"],
1059)
1060
1061cc_proto_library(
1062    name = "conformance_proto_cc",
1063    srcs = ["conformance/conformance.proto"],
1064)
1065
1066cc_library(
1067    name = "jsoncpp",
1068    srcs = ["conformance/third_party/jsoncpp/jsoncpp.cpp"],
1069    hdrs = ["conformance/third_party/jsoncpp/json.h"],
1070    includes = ["conformance"],
1071)
1072
1073cc_library(
1074    name = "conformance_test",
1075    srcs = [
1076        "conformance/conformance_test.cc",
1077        "conformance/conformance_test_runner.cc",
1078    ],
1079    hdrs = [
1080        "conformance/conformance_test.h",
1081    ],
1082    includes = [
1083        "conformance",
1084        "src",
1085    ],
1086    deps = [":conformance_proto_cc"],
1087)
1088
1089cc_library(
1090    name = "binary_json_conformance_suite",
1091    srcs = ["conformance/binary_json_conformance_suite.cc"],
1092    hdrs = ["conformance/binary_json_conformance_suite.h"],
1093    deps = [
1094        ":conformance_test",
1095        ":jsoncpp",
1096        ":test_messages_proto2_proto_cc",
1097        ":test_messages_proto3_proto_cc",
1098    ],
1099)
1100
1101cc_library(
1102    name = "text_format_conformance_suite",
1103    srcs = ["conformance/text_format_conformance_suite.cc"],
1104    hdrs = ["conformance/text_format_conformance_suite.h"],
1105    deps = [
1106        ":conformance_test",
1107        ":test_messages_proto2_proto_cc",
1108        ":test_messages_proto3_proto_cc",
1109    ],
1110)
1111
1112cc_binary(
1113    name = "conformance_test_runner",
1114    srcs = ["conformance/conformance_test_main.cc"],
1115    visibility = ["//visibility:public"],
1116    deps = [
1117        ":binary_json_conformance_suite",
1118        ":conformance_test",
1119        ":text_format_conformance_suite",
1120    ],
1121)
1122
1123sh_test(
1124    name = "build_files_updated_unittest",
1125    srcs = [
1126        "build_files_updated_unittest.sh",
1127    ],
1128    data = [
1129        "BUILD",
1130        "cmake/extract_includes.bat.in",
1131        "cmake/libprotobuf.cmake",
1132        "cmake/libprotobuf-lite.cmake",
1133        "cmake/libprotoc.cmake",
1134        "cmake/tests.cmake",
1135        "src/Makefile.am",
1136        "update_file_lists.sh",
1137    ],
1138)
1139