blob: 06c8a5dff987df535f01c7ed21aeadf6fd5b96dc [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")
10load("@rules_proto//proto:defs.bzl", "proto_library")
11load("@rules_python//python:defs.bzl", "py_library")
12load("//:protobuf.bzl", "py_proto_library")
13load("//build_defs:cpp_opts.bzl", "COPTS")
14load(":internal.bzl", "internal_copy_files")
15
16py_library(
17 name = "protobuf_python",
18 data = select({
19 "//conditions:default": [],
20 ":use_fast_cpp_protos": [
21 ":google/protobuf/internal/_api_implementation.so",
22 ":google/protobuf/pyext/_message.so",
23 ],
24 }),
25 visibility = ["//:__pkg__"],
26 deps = [
27 ":python_srcs",
28 ":well_known_types_py_pb2",
29 ],
30)
31
32config_setting(
33 name = "use_fast_cpp_protos",
34 values = {
35 "define": "use_fast_cpp_protos=true",
36 },
37)
38
39py_proto_library(
40 name = "well_known_types_py_pb2",
41 srcs = [":copied_wkt_proto_files"],
42 include = ".",
43 default_runtime = "",
44 protoc = "//:protoc",
45 srcs_version = "PY2AND3",
46 visibility = [
47 "//:__pkg__",
48 "@upb//:__subpackages__",
49 ],
50)
51
52internal_copy_files(
53 name = "copied_wkt_proto_files",
54 srcs = [
David L. Jones171a6b12022-05-18 13:45:22 -070055 "//src/google/protobuf:descriptor_proto_srcs",
David L. Jonesb3cbea12022-05-12 19:48:58 -070056 "//:well_known_type_protos",
57 ],
58 strip_prefix = "src",
59)
60
61cc_binary(
62 name = "google/protobuf/internal/_api_implementation.so",
63 srcs = ["google/protobuf/internal/api_implementation.cc"],
64 copts = COPTS + [
65 "-DPYTHON_PROTO2_CPP_IMPL_V2",
66 ],
67 linkshared = 1,
68 linkstatic = 1,
69 tags = [
70 # Exclude this target from wildcard expansion (//...) because it may
71 # not even be buildable. It will be built if it is needed according
72 # to :use_fast_cpp_protos.
73 # https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
74 "manual",
75 ],
76 deps = select({
77 "//conditions:default": [],
78 ":use_fast_cpp_protos": ["//external:python_headers"],
79 }),
80)
81
82config_setting(
83 name = "allow_oversize_protos",
84 values = {
85 "define": "allow_oversize_protos=true",
86 },
87)
88
89cc_binary(
90 name = "google/protobuf/pyext/_message.so",
91 srcs = glob([
92 "google/protobuf/pyext/*.cc",
93 "google/protobuf/pyext/*.h",
94 ]),
95 copts = COPTS + [
96 "-DGOOGLE_PROTOBUF_HAS_ONEOF=1",
97 ] + select({
98 "//conditions:default": [],
99 ":allow_oversize_protos": ["-DPROTOBUF_PYTHON_ALLOW_OVERSIZE_PROTOS=1"],
100 }),
101 includes = ["."],
102 linkshared = 1,
103 linkstatic = 1,
104 tags = [
105 # Exclude this target from wildcard expansion (//...) because it may
106 # not even be buildable. It will be built if it is needed according
107 # to :use_fast_cpp_protos.
108 # https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes
109 "manual",
110 ],
111 deps = [
112 ":proto_api",
113 "//:protobuf",
114 ] + select({
115 "//conditions:default": [],
116 ":use_fast_cpp_protos": ["//external:python_headers"],
117 }),
118)
119
120py_library(
121 name = "python_srcs",
122 srcs = glob(
123 [
124 "google/protobuf/**/*.py",
125 ],
126 exclude = [
127 "google/protobuf/internal/*_test.py",
128 "google/protobuf/internal/test_util.py",
129 "google/protobuf/internal/import_test_package/__init__.py",
130 ],
131 ),
132 imports = ["python"],
133 srcs_version = "PY2AND3",
134 visibility = [
135 "//:__pkg__",
136 "@upb//:__subpackages__",
137 ],
138)
139
140py_library(
141 name = "python_test_srcs",
142 srcs = glob([
143 "google/protobuf/internal/*_test.py",
144 ]) + [
145 "google/protobuf/internal/test_util.py",
146 "google/protobuf/internal/import_test_package/__init__.py",
147 ],
148 imports = ["python"],
149 srcs_version = "PY3",
150 visibility = [
151 "//:__pkg__",
152 "@upb//:__subpackages__",
153 ],
154)
155
156################################################################################
157# Tests
158################################################################################
159
160internal_copy_files(
161 name = "copied_test_proto_files",
David L. Jones07303d62022-05-17 18:13:22 -0700162 testonly = 1,
163 srcs = [
164 "//:test_proto_srcs",
165 "//src/google/protobuf/util:test_proto_srcs",
166 ],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700167 strip_prefix = "src",
168)
169
170py_proto_library(
171 name = "python_common_test_protos",
David L. Jones07303d62022-05-17 18:13:22 -0700172 testonly = 1,
David L. Jonesb3cbea12022-05-12 19:48:58 -0700173 srcs = [":copied_test_proto_files"],
174 include = ".",
175 default_runtime = "",
176 protoc = "//:protoc",
177 srcs_version = "PY2AND3",
178 visibility = ["//:__pkg__"],
179 deps = [":well_known_types_py_pb2"],
180)
181
182py_proto_library(
183 name = "python_specific_test_protos",
David L. Jones07303d62022-05-17 18:13:22 -0700184 testonly = 1,
David L. Jonesb3cbea12022-05-12 19:48:58 -0700185 srcs = glob([
186 "google/protobuf/internal/*.proto",
187 "google/protobuf/internal/import_test_package/*.proto",
188 ]),
189 include = ".",
190 default_runtime = ":protobuf_python",
191 protoc = "//:protoc",
192 srcs_version = "PY2AND3",
193 visibility = ["//:__pkg__"],
194 deps = [":python_common_test_protos"],
195)
196
197py_library(
198 name = "python_test_lib",
David L. Jones07303d62022-05-17 18:13:22 -0700199 testonly = 1,
David L. Jonesb3cbea12022-05-12 19:48:58 -0700200 srcs = [
201 "google/protobuf/internal/import_test_package/__init__.py",
202 "google/protobuf/internal/test_util.py",
203 ],
204 imports = ["python"],
205 srcs_version = "PY2AND3",
206 deps = [
207 ":protobuf_python",
208 ":python_common_test_protos",
209 ":python_specific_test_protos",
210 ],
211)
212
213py_test(
214 name = "descriptor_database_test",
215 srcs = ["google/protobuf/internal/descriptor_database_test.py"],
216 deps = [":python_test_lib"],
217)
218
219py_test(
220 name = "descriptor_pool_test",
221 srcs = ["google/protobuf/internal/descriptor_pool_test.py"],
222 deps = [":python_test_lib"],
223)
224
225py_test(
226 name = "descriptor_test",
227 srcs = ["google/protobuf/internal/descriptor_test.py"],
228 imports = ["."],
229 deps = [":python_test_lib"],
230)
231
232py_test(
233 name = "generator_test",
234 srcs = ["google/protobuf/internal/generator_test.py"],
235 imports = ["."],
236 deps = [":python_test_lib"],
237)
238
239py_test(
240 name = "json_format_test",
241 srcs = ["google/protobuf/internal/json_format_test.py"],
242 imports = ["."],
243 deps = [":python_test_lib"],
244)
245
246py_test(
247 name = "message_factory_test",
248 srcs = ["google/protobuf/internal/message_factory_test.py"],
249 imports = ["."],
250 deps = [":python_test_lib"],
251)
252
253py_test(
254 name = "message_test",
255 srcs = ["google/protobuf/internal/message_test.py"],
256 data = glob(["testdata/golden_pickle_*"]) + [
David L. Jones171a6b12022-05-18 13:45:22 -0700257 "//src/google/protobuf:testdata",
David L. Jonesb3cbea12022-05-12 19:48:58 -0700258 ],
259 imports = ["."],
260 deps = [":python_test_lib"],
261)
262
263py_test(
264 name = "proto_builder_test",
265 srcs = ["google/protobuf/internal/proto_builder_test.py"],
266 imports = ["."],
267 deps = [":python_test_lib"],
268)
269
270py_test(
271 name = "reflection_test",
272 srcs = ["google/protobuf/internal/reflection_test.py"],
273 imports = ["."],
274 deps = [":python_test_lib"],
275)
276
277py_test(
278 name = "service_reflection_test",
279 srcs = ["google/protobuf/internal/service_reflection_test.py"],
280 imports = ["."],
281 deps = [":python_test_lib"],
282)
283
284py_test(
285 name = "symbol_database_test",
286 srcs = ["google/protobuf/internal/symbol_database_test.py"],
287 imports = ["."],
288 deps = [":python_test_lib"],
289)
290
291py_test(
292 name = "text_encoding_test",
293 srcs = ["google/protobuf/internal/text_encoding_test.py"],
294 imports = ["."],
295 deps = [":python_test_lib"],
296)
297
298py_test(
299 name = "text_format_test",
300 srcs = ["google/protobuf/internal/text_format_test.py"],
David L. Jones171a6b12022-05-18 13:45:22 -0700301 data = ["//src/google/protobuf:testdata"],
David L. Jonesb3cbea12022-05-12 19:48:58 -0700302 imports = ["."],
303 deps = [":python_test_lib"],
304)
305
306py_test(
307 name = "unknown_fields_test",
308 srcs = ["google/protobuf/internal/unknown_fields_test.py"],
309 imports = ["."],
310 deps = [":python_test_lib"],
311)
312
313py_test(
314 name = "wire_format_test",
315 srcs = ["google/protobuf/internal/wire_format_test.py"],
316 imports = ["."],
317 deps = [":python_test_lib"],
318)
319
320cc_library(
321 name = "proto_api",
322 hdrs = ["python/google/protobuf/proto_api.h"],
323 visibility = ["//visibility:public"],
324 deps = [
325 "//external:python_headers",
326 ],
327)
328
329################################################################################
330# Distribution files
331################################################################################
332
333pkg_files(
334 name = "dist_files",
335 srcs = glob([
336 "google/**/*.proto",
337 "google/**/*.py",
338 "google/protobuf/internal/*.cc",
339 "google/protobuf/pyext/*.cc",
340 "google/protobuf/pyext/*.h",
341 ]) + [
342 "BUILD.bazel",
343 "MANIFEST.in",
344 "README.md",
345 "google/protobuf/proto_api.h",
346 "google/protobuf/pyext/README",
347 "google/protobuf/python_protobuf.h",
348 "internal.bzl",
349 "mox.py",
350 "release.sh",
351 "setup.cfg",
352 "setup.py",
353 "stubout.py",
354 "tox.ini",
355 ],
356 strip_prefix = strip_prefix.from_root(""),
357 visibility = ["//pkg:__pkg__"],
358)