blob: e67422b4ac9372774e309de7b147bb5b98927624 [file] [log] [blame]
David L. Jonesb3cbea12022-05-12 19:48:58 -07001# Protobuf Python runtime
2#
3# See also code generation logic under /src/google/protobuf/compiler/python.
4#
5# Most users should depend upon public aliases in the root:
6# //:protobuf_python
7# //:well_known_types_py_pb2
8
9load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
David L. Jonesb3cbea12022-05-12 19:48:58 -070010load("@rules_python//python:defs.bzl", "py_library")
zhangskzedb7ef92022-10-05 21:45:16 +000011load("@pip_deps//:requirements.bzl", "requirement")
Mike Kruskaled5c57a2022-08-10 22:51:29 -070012load("//:protobuf.bzl", "internal_py_proto_library")
Mike Kruskal9baae6a2022-12-14 21:51:34 -080013load("//build_defs:arch_tests.bzl", "aarch64_test", "x86_64_test")
David L. Jonesb3cbea12022-05-12 19:48:58 -070014load("//build_defs:cpp_opts.bzl", "COPTS")
Mike Kruskaled5c57a2022-08-10 22:51:29 -070015load("//conformance:defs.bzl", "conformance_test")
Mike Kruskal3bc50492022-12-01 13:34:12 -080016load(":internal.bzl", "internal_copy_files", "internal_py_test")
David L. Jonesb3cbea12022-05-12 19:48:58 -070017
18py_library(
19 name = "protobuf_python",
20 data = select({
21 "//conditions:default": [],
22 ":use_fast_cpp_protos": [
23 ":google/protobuf/internal/_api_implementation.so",
24 ":google/protobuf/pyext/_message.so",
25 ],
26 }),
27 visibility = ["//:__pkg__"],
28 deps = [
29 ":python_srcs",
30 ":well_known_types_py_pb2",
31 ],
32)
33
34config_setting(
35 name = "use_fast_cpp_protos",
36 values = {
37 "define": "use_fast_cpp_protos=true",
38 },
39)
40
Mike Kruskaled5c57a2022-08-10 22:51:29 -070041internal_py_proto_library(
David L. Jonesb3cbea12022-05-12 19:48:58 -070042 name = "well_known_types_py_pb2",
43 srcs = [":copied_wkt_proto_files"],
44 include = ".",
45 default_runtime = "",
46 protoc = "//:protoc",
47 srcs_version = "PY2AND3",
48 visibility = [
49 "//:__pkg__",
50 "@upb//:__subpackages__",
51 ],
52)
53
54internal_copy_files(
55 name = "copied_wkt_proto_files",
56 srcs = [
David L. Jonesb3cbea12022-05-12 19:48:58 -070057 "//:well_known_type_protos",
Mike Kruskalca4b0632022-08-11 20:55:01 -070058 "//src/google/protobuf:descriptor_proto_srcs",
David L. Jonesb3cbea12022-05-12 19:48:58 -070059 ],
60 strip_prefix = "src",
61)
62
63cc_binary(
64 name = "google/protobuf/internal/_api_implementation.so",
65 srcs = ["google/protobuf/internal/api_implementation.cc"],
66 copts = COPTS + [
67 "-DPYTHON_PROTO2_CPP_IMPL_V2",
68 ],
69 linkshared = 1,
70 linkstatic = 1,
71 tags = [
72 # Exclude this target from wildcard expansion (//...) because it may
73 # not even be buildable. It will be built if it is needed according
74 # to :use_fast_cpp_protos.
75 # https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
76 "manual",
77 ],
78 deps = select({
79 "//conditions:default": [],
80 ":use_fast_cpp_protos": ["//external:python_headers"],
81 }),
82)
83
84config_setting(
85 name = "allow_oversize_protos",
86 values = {
87 "define": "allow_oversize_protos=true",
88 },
89)
90
91cc_binary(
92 name = "google/protobuf/pyext/_message.so",
93 srcs = glob([
94 "google/protobuf/pyext/*.cc",
95 "google/protobuf/pyext/*.h",
96 ]),
97 copts = COPTS + [
98 "-DGOOGLE_PROTOBUF_HAS_ONEOF=1",
99 ] + select({
100 "//conditions:default": [],
101 ":allow_oversize_protos": ["-DPROTOBUF_PYTHON_ALLOW_OVERSIZE_PROTOS=1"],
102 }),
103 includes = ["."],
104 linkshared = 1,
105 linkstatic = 1,
106 tags = [
107 # Exclude this target from wildcard expansion (//...) because it may
108 # not even be buildable. It will be built if it is needed according
109 # to :use_fast_cpp_protos.
110 # https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
111 "manual",
112 ],
113 deps = [
114 ":proto_api",
115 "//:protobuf",
116 ] + select({
117 "//conditions:default": [],
118 ":use_fast_cpp_protos": ["//external:python_headers"],
119 }),
120)
121
Mike Kruskal9baae6a2022-12-14 21:51:34 -0800122aarch64_test(
123 name = "aarch64_test",
124 bazel_binaries = [
125 "google/protobuf/internal/_api_implementation.so",
126 "google/protobuf/pyext/_message.so",
127 ],
128)
129
130x86_64_test(
131 name = "x86_64_test",
132 bazel_binaries = [
133 "google/protobuf/internal/_api_implementation.so",
134 "google/protobuf/pyext/_message.so",
135 ],
136)
137
David L. Jonesb3cbea12022-05-12 19:48:58 -0700138py_library(
139 name = "python_srcs",
140 srcs = glob(
141 [
142 "google/protobuf/**/*.py",
143 ],
144 exclude = [
145 "google/protobuf/internal/*_test.py",
146 "google/protobuf/internal/test_util.py",
147 "google/protobuf/internal/import_test_package/__init__.py",
148 ],
149 ),
150 imports = ["python"],
151 srcs_version = "PY2AND3",
152 visibility = [
153 "//:__pkg__",
154 "@upb//:__subpackages__",
155 ],
156)
157
158py_library(
159 name = "python_test_srcs",
160 srcs = glob([
161 "google/protobuf/internal/*_test.py",
162 ]) + [
163 "google/protobuf/internal/test_util.py",
164 "google/protobuf/internal/import_test_package/__init__.py",
165 ],
166 imports = ["python"],
167 srcs_version = "PY3",
168 visibility = [
169 "//:__pkg__",
170 "@upb//:__subpackages__",
171 ],
172)
173
174################################################################################
175# Tests
176################################################################################
177
178internal_copy_files(
179 name = "copied_test_proto_files",
David L. Jones07303d62022-05-17 18:13:22 -0700180 testonly = 1,
181 srcs = [
182 "//:test_proto_srcs",
183 "//src/google/protobuf/util:test_proto_srcs",
184 ],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700185 strip_prefix = "src",
186)
187
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700188internal_copy_files(
189 name = "copied_test_messages_proto2_files",
190 testonly = 1,
191 srcs = [
192 "//src/google/protobuf:test_messages_proto2.proto",
193 ],
194 strip_prefix = "src",
195)
196
197internal_copy_files(
198 name = "copied_test_messages_proto3_files",
199 testonly = 1,
200 srcs = [
201 "//src/google/protobuf:test_messages_proto3.proto",
202 ],
203 strip_prefix = "src",
204)
205
206internal_py_proto_library(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700207 name = "python_common_test_protos",
David L. Jones07303d62022-05-17 18:13:22 -0700208 testonly = 1,
David L. Jonesb3cbea12022-05-12 19:48:58 -0700209 srcs = [":copied_test_proto_files"],
210 include = ".",
211 default_runtime = "",
212 protoc = "//:protoc",
213 srcs_version = "PY2AND3",
214 visibility = ["//:__pkg__"],
215 deps = [":well_known_types_py_pb2"],
216)
217
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700218internal_py_proto_library(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700219 name = "python_specific_test_protos",
David L. Jones07303d62022-05-17 18:13:22 -0700220 testonly = 1,
David L. Jonesb3cbea12022-05-12 19:48:58 -0700221 srcs = glob([
222 "google/protobuf/internal/*.proto",
223 "google/protobuf/internal/import_test_package/*.proto",
224 ]),
225 include = ".",
226 default_runtime = ":protobuf_python",
227 protoc = "//:protoc",
228 srcs_version = "PY2AND3",
229 visibility = ["//:__pkg__"],
230 deps = [":python_common_test_protos"],
231)
232
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700233internal_py_proto_library(
234 name = "test_messages_proto2_py_proto",
235 testonly = 1,
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700236 srcs = [":copied_test_messages_proto2_files"],
237 include = ".",
238 default_runtime = "//:protobuf_python",
239 protoc = "//:protoc",
Mike Kruskalca4b0632022-08-11 20:55:01 -0700240 visibility = [
241 "//conformance:__pkg__",
242 "//python:__subpackages__",
243 ],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700244)
245
246internal_py_proto_library(
247 name = "test_messages_proto3_py_proto",
248 testonly = 1,
Mike Kruskalca4b0632022-08-11 20:55:01 -0700249 srcs = [":copied_test_messages_proto3_files"],
250 include = ".",
251 default_runtime = "//:protobuf_python",
252 protoc = "//:protoc",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700253 visibility = [
254 "//conformance:__pkg__",
255 "//python:__subpackages__",
256 ],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700257 deps = [":well_known_types_py_pb2"],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700258)
259
David L. Jonesb3cbea12022-05-12 19:48:58 -0700260py_library(
261 name = "python_test_lib",
David L. Jones07303d62022-05-17 18:13:22 -0700262 testonly = 1,
David L. Jonesb3cbea12022-05-12 19:48:58 -0700263 srcs = [
264 "google/protobuf/internal/import_test_package/__init__.py",
265 "google/protobuf/internal/test_util.py",
266 ],
267 imports = ["python"],
268 srcs_version = "PY2AND3",
269 deps = [
270 ":protobuf_python",
271 ":python_common_test_protos",
272 ":python_specific_test_protos",
273 ],
274)
275
Mike Kruskal3bc50492022-12-01 13:34:12 -0800276internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700277 name = "descriptor_database_test",
278 srcs = ["google/protobuf/internal/descriptor_database_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700279)
280
Mike Kruskal3bc50492022-12-01 13:34:12 -0800281internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700282 name = "descriptor_pool_test",
283 srcs = ["google/protobuf/internal/descriptor_pool_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700284)
285
Mike Kruskal3bc50492022-12-01 13:34:12 -0800286internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700287 name = "descriptor_test",
288 srcs = ["google/protobuf/internal/descriptor_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700289)
290
Mike Kruskal3bc50492022-12-01 13:34:12 -0800291internal_py_test(
Mike Kruskal23f14812022-10-10 21:05:53 -0700292 name = "field_mask_test",
293 srcs = ["google/protobuf/internal/field_mask_test.py"],
Mike Kruskal23f14812022-10-10 21:05:53 -0700294)
295
Mike Kruskal3bc50492022-12-01 13:34:12 -0800296internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700297 name = "generator_test",
298 srcs = ["google/protobuf/internal/generator_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700299)
300
Mike Kruskal3bc50492022-12-01 13:34:12 -0800301internal_py_test(
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700302 name = "import_test",
303 srcs = ["google/protobuf/internal/import_test.py"],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700304)
305
Mike Kruskal3bc50492022-12-01 13:34:12 -0800306internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700307 name = "json_format_test",
308 srcs = ["google/protobuf/internal/json_format_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700309)
310
Mike Kruskal3bc50492022-12-01 13:34:12 -0800311internal_py_test(
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700312 name = "keywords_test",
313 srcs = ["google/protobuf/internal/keywords_test.py"],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700314)
315
Mike Kruskal3bc50492022-12-01 13:34:12 -0800316internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700317 name = "message_factory_test",
318 srcs = ["google/protobuf/internal/message_factory_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700319)
320
Mike Kruskal3bc50492022-12-01 13:34:12 -0800321internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700322 name = "message_test",
323 srcs = ["google/protobuf/internal/message_test.py"],
Xavier Bonaventurac8ebeb12023-02-07 17:14:15 -0800324 data = ["//src/google/protobuf:testdata"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700325)
326
Mike Kruskal3bc50492022-12-01 13:34:12 -0800327internal_py_test(
zhangskzedb7ef92022-10-05 21:45:16 +0000328 name = "numpy_test",
329 srcs = ["google/protobuf/internal/numpy_test.py"],
zhangskzedb7ef92022-10-05 21:45:16 +0000330 deps = [
zhangskzedb7ef92022-10-05 21:45:16 +0000331 requirement("numpy"),
332 ],
333)
334
Mike Kruskal3bc50492022-12-01 13:34:12 -0800335internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700336 name = "proto_builder_test",
337 srcs = ["google/protobuf/internal/proto_builder_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700338)
339
Mike Kruskal3bc50492022-12-01 13:34:12 -0800340internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700341 name = "reflection_test",
342 srcs = ["google/protobuf/internal/reflection_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700343)
344
Mike Kruskal3bc50492022-12-01 13:34:12 -0800345internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700346 name = "service_reflection_test",
347 srcs = ["google/protobuf/internal/service_reflection_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700348)
349
Mike Kruskal3bc50492022-12-01 13:34:12 -0800350internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700351 name = "symbol_database_test",
352 srcs = ["google/protobuf/internal/symbol_database_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700353)
354
Mike Kruskal3bc50492022-12-01 13:34:12 -0800355internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700356 name = "text_encoding_test",
357 srcs = ["google/protobuf/internal/text_encoding_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700358)
359
Mike Kruskal3bc50492022-12-01 13:34:12 -0800360internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700361 name = "text_format_test",
362 srcs = ["google/protobuf/internal/text_format_test.py"],
David L. Jones171a6b12022-05-18 13:45:22 -0700363 data = ["//src/google/protobuf:testdata"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700364)
365
Mike Kruskal3bc50492022-12-01 13:34:12 -0800366internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700367 name = "unknown_fields_test",
368 srcs = ["google/protobuf/internal/unknown_fields_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700369)
370
Mike Kruskal3bc50492022-12-01 13:34:12 -0800371internal_py_test(
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700372 name = "well_known_types_test",
373 srcs = ["google/protobuf/internal/well_known_types_test.py"],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700374)
375
Mike Kruskal3bc50492022-12-01 13:34:12 -0800376internal_py_test(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700377 name = "wire_format_test",
378 srcs = ["google/protobuf/internal/wire_format_test.py"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700379)
380
381cc_library(
382 name = "proto_api",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700383 hdrs = ["google/protobuf/proto_api.h"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700384 visibility = ["//visibility:public"],
385 deps = [
386 "//external:python_headers",
387 ],
388)
389
Mike Kruskal3bc50492022-12-01 13:34:12 -0800390internal_py_test(
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700391 name = "python_version",
392 srcs = ["python_version.py"],
393)
394
395conformance_test(
396 name = "conformance_test",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700397 env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "python"},
Mike Kruskalca4b0632022-08-11 20:55:01 -0700398 failure_list = "//conformance:failure_list_python.txt",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700399 target_compatible_with = select({
Mike Kruskal3bc50492022-12-01 13:34:12 -0800400 "@system_python//:none": ["@platforms//:incompatible"],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700401 ":use_fast_cpp_protos": ["@platforms//:incompatible"],
402 "//conditions:default": [],
403 }),
Mike Kruskalca4b0632022-08-11 20:55:01 -0700404 testee = "//conformance:conformance_python",
405 text_format_failure_list = "//conformance:text_format_failure_list_python.txt",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700406)
407
408# Note: this requires --define=use_fast_cpp_protos=true
409conformance_test(
410 name = "conformance_test_cpp",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700411 env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "cpp"},
Mike Kruskalca4b0632022-08-11 20:55:01 -0700412 failure_list = "//conformance:failure_list_python.txt",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700413 target_compatible_with = select({
Mike Kruskal3bc50492022-12-01 13:34:12 -0800414 "@system_python//:none": ["@platforms//:incompatible"],
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700415 ":use_fast_cpp_protos": [],
416 "//conditions:default": ["@platforms//:incompatible"],
417 }),
Mike Kruskalca4b0632022-08-11 20:55:01 -0700418 testee = "//conformance:conformance_python",
419 text_format_failure_list = "//conformance:text_format_failure_list_python_cpp.txt",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700420)
421
David L. Jonesb3cbea12022-05-12 19:48:58 -0700422################################################################################
423# Distribution files
424################################################################################
425
426pkg_files(
Deanna Garciad0169ff2022-12-16 11:13:03 -0800427 name = "python_source_files",
428 srcs = glob(
429 [
430 "google/protobuf/**/*.py",
431 ],
432 exclude = [
433 "google/protobuf/internal/*_test.py",
434 "google/protobuf/internal/test_util.py",
435 "google/protobuf/internal/import_test_package/__init__.py",
436 ],
Deanna Garciac59cc4d2022-12-22 15:04:17 -0800437 ) + [
438 "README.md",
439 "google/__init__.py",
440 "setup.cfg",
441 "tox.ini",
442 ],
Deanna Garciad0169ff2022-12-16 11:13:03 -0800443 visibility = ["@upb//:__subpackages__"],
444 strip_prefix = "",
445)
446
447pkg_files(
David L. Jonesb3cbea12022-05-12 19:48:58 -0700448 name = "dist_files",
449 srcs = glob([
450 "google/**/*.proto",
451 "google/**/*.py",
452 "google/protobuf/internal/*.cc",
453 "google/protobuf/pyext/*.cc",
454 "google/protobuf/pyext/*.h",
455 ]) + [
456 "BUILD.bazel",
457 "MANIFEST.in",
458 "README.md",
459 "google/protobuf/proto_api.h",
460 "google/protobuf/pyext/README",
461 "google/protobuf/python_protobuf.h",
462 "internal.bzl",
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700463 "python_version.py",
David L. Jonesb3cbea12022-05-12 19:48:58 -0700464 "release.sh",
465 "setup.cfg",
466 "setup.py",
David L. Jonesb3cbea12022-05-12 19:48:58 -0700467 "tox.ini",
468 ],
469 strip_prefix = strip_prefix.from_root(""),
470 visibility = ["//pkg:__pkg__"],
471)