• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2009-2022, Google LLC
2# All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
9load("@bazel_skylib//lib:selects.bzl", "selects")
10load("@pip_deps//:requirements.bzl", "requirement")
11load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
12load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
13load("@rules_python//python:packaging.bzl", "py_wheel")
14load("@system_python//:version.bzl", "SYSTEM_PYTHON_VERSION")
15load("//:protobuf_version.bzl", "PROTOBUF_PYTHON_VERSION")
16load(":dist.bzl", "py_dist", "py_dist_module")
17load(":py_proto_library.bzl", "py_proto_library")
18
19licenses(["notice"])
20
21py_dist_module(
22    name = "message_mod",
23    extension = "//python:_message_binary",
24    module_name = "google._upb._message",
25)
26
27py_proto_library(
28    name = "well_known_proto_py_pb2",
29    deps = [
30        "//:any_proto",
31        "//:api_proto",
32        "//:descriptor_proto",
33        "//:duration_proto",
34        "//:empty_proto",
35        "//:field_mask_proto",
36        "//:source_context_proto",
37        "//:struct_proto",
38        "//:timestamp_proto",
39        "//:type_proto",
40        "//:wrappers_proto",
41    ],
42)
43
44py_proto_library(
45    name = "plugin_py_pb2",
46    deps = ["//:compiler_plugin_proto"],
47)
48
49config_setting(
50    name = "linux_aarch64_release",
51    flag_values = {
52        "//toolchain:release": "True",
53    },
54    values = {"cpu": "linux-aarch_64"},
55)
56
57config_setting(
58    name = "linux_aarch64_local",
59    constraint_values = [
60        "@platforms//os:linux",
61        "@platforms//cpu:aarch64",
62    ],
63    flag_values = {
64        "//toolchain:release": "False",
65    },
66)
67
68config_setting(
69    name = "linux_x86_64_release",
70    flag_values = {
71        "//toolchain:release": "True",
72    },
73    values = {"cpu": "linux-x86_64"},
74)
75
76config_setting(
77    name = "linux_x86_64_local",
78    constraint_values = [
79        "@platforms//os:linux",
80        "@platforms//cpu:x86_64",
81    ],
82    flag_values = {
83        "//toolchain:release": "False",
84    },
85)
86
87config_setting(
88    name = "osx_x86_64_release",
89    flag_values = {
90        "//toolchain:release": "True",
91    },
92    values = {"cpu": "osx-x86_64"},
93)
94
95config_setting(
96    name = "osx_x86_64_local",
97    constraint_values = [
98        "@platforms//os:osx",
99        "@platforms//cpu:x86_64",
100    ],
101    flag_values = {
102        "//toolchain:release": "False",
103    },
104)
105
106selects.config_setting_group(
107    name = "osx_x86_64",
108    match_any = [
109        ":osx_x86_64_release",
110        ":osx_x86_64_local",
111    ],
112)
113
114config_setting(
115    name = "osx_aarch64_release",
116    flag_values = {
117        "//toolchain:release": "True",
118    },
119    values = {"cpu": "osx-aarch_64"},
120)
121
122config_setting(
123    name = "osx_aarch64_local",
124    constraint_values = [
125        "@platforms//os:osx",
126        "@platforms//cpu:aarch64",
127    ],
128    flag_values = {
129        "//toolchain:release": "False",
130    },
131)
132
133selects.config_setting_group(
134    name = "osx_aarch64",
135    match_any = [
136        ":osx_aarch64_release",
137        ":osx_aarch64_local",
138    ],
139)
140
141config_setting(
142    name = "osx_universal2",
143    values = {"cpu": "osx-universal2"},
144)
145
146config_setting(
147    name = "windows_x86_32_release",
148    flag_values = {
149        "//toolchain:release": "True",
150    },
151    values = {"cpu": "win32"},
152)
153
154config_setting(
155    name = "windows_x86_32_local",
156    constraint_values = [
157        "@platforms//os:windows",
158        "@platforms//cpu:x86_32",
159    ],
160    flag_values = {
161        "//toolchain:release": "False",
162    },
163)
164
165selects.config_setting_group(
166    name = "windows_x86_32",
167    match_any = [
168        ":windows_x86_32_release",
169        ":windows_x86_32_local",
170    ],
171)
172
173config_setting(
174    name = "windows_x86_64_release",
175    flag_values = {
176        "//toolchain:release": "True",
177    },
178    values = {"cpu": "win64"},
179)
180
181config_setting(
182    name = "windows_x86_64_local",
183    constraint_values = [
184        "@platforms//os:windows",
185        "@platforms//cpu:x86_64",
186    ],
187    flag_values = {
188        "//toolchain:release": "False",
189    },
190)
191
192selects.config_setting_group(
193    name = "windows_x86_64",
194    match_any = [
195        ":windows_x86_64_release",
196        ":windows_x86_64_local",
197    ],
198)
199
200pkg_files(
201    name = "generated_wkt",
202    srcs = [
203        ":well_known_proto_py_pb2",
204        "//src/google/protobuf:descriptor_upb_c_proto",
205        "//src/google/protobuf:descriptor_upb_minitable_proto",
206        "//src/google/protobuf:descriptor_upb_reflection_proto",
207        "//upb/reflection:descriptor_upb_proto",
208    ],
209    prefix = "google/protobuf",
210)
211
212pkg_files(
213    name = "generated_wkt_compiler",
214    srcs = [
215        ":plugin_py_pb2",
216    ],
217    prefix = "google/protobuf/compiler",
218)
219
220pkg_files(
221    name = "utf8_range_source_files",
222    srcs = ["//third_party/utf8_range:utf8_range_srcs"],
223    prefix = "utf8_range",
224)
225
226pkg_files(
227    name = "dist_source_files",
228    srcs = [
229        "MANIFEST.in",
230        "setup.py",
231    ],
232)
233
234# Passing filegroups to pkg_tar directly results in incorrect
235# `protobuf/external/upb/` directory structure when built from the protobuf
236# repo. This can be removed once repositories are merged.
237pkg_files(
238    name = "filegroup_source_files",
239    srcs = [
240        "//:LICENSE",
241        "//python:message_srcs",
242        "//upb:source_files",
243        "//upb/base:source_files",
244        "//upb/hash:source_files",
245        "//upb/lex:source_files",
246        "//upb/mem:source_files",
247        "//upb/message:source_files",
248        "//upb/mini_descriptor:source_files",
249        "//upb/mini_table:source_files",
250        "//upb/port:source_files",
251        "//upb/reflection:source_files",
252        "//upb/text:source_files",
253        "//upb/util:source_files",
254        "//upb/wire:source_files",
255    ],
256    strip_prefix = strip_prefix.from_root(""),
257)
258
259# NOTE: This package currently only works for macos and ubuntu, MSVC users
260# should use a binary wheel.
261pkg_tar(
262    name = "source_tarball",
263    srcs = [
264        ":dist_source_files",
265        ":filegroup_source_files",
266        ":generated_wkt",
267        ":generated_wkt_compiler",
268        ":utf8_range_source_files",
269        "//python:python_source_files",
270    ],
271    extension = "tar.gz",
272    package_dir = "protobuf",
273    package_file_name = "protobuf.tar.gz",
274    strip_prefix = ".",
275    target_compatible_with = select({
276        "@system_python//:none": ["@platforms//:incompatible"],
277        "//conditions:default": [],
278    }),
279)
280
281genrule(
282    name = "source_wheel",
283    srcs = [":source_tarball"],
284    outs = ["protobuf-%s.tar.gz" % PROTOBUF_PYTHON_VERSION],
285    cmd = """
286        export PYTHONPATH=$$PWD/external/pip_deps_setuptools/site-packages
287        set -eux
288        tar -xzvf $(location :source_tarball)
289        cd protobuf/
290        python3 setup.py sdist
291        cd ..
292        mv protobuf/dist/*.tar.gz $@
293    """,
294    target_compatible_with = select({
295        "@system_python//:none": ["@platforms//:incompatible"],
296        "//conditions:default": [],
297    }),
298    tools = [requirement("setuptools")],
299)
300
301py_wheel(
302    name = "binary_wheel",
303    abi = select({
304        "//python:full_api_3.8": "cp38",
305        "//python:full_api_3.9": "cp39",
306        "//conditions:default": "abi3",
307    }),
308    author = "protobuf@googlegroups.com",
309    author_email = "protobuf@googlegroups.com",
310    classifiers = [
311        "Programming Language :: Python",
312        "Programming Language :: Python :: 3",
313        "Programming Language :: Python :: 3.8",
314        "Programming Language :: Python :: 3.9",
315        "Programming Language :: Python :: 3.10",
316        "Programming Language :: Python :: 3.11",
317        "Programming Language :: Python :: 3.12",
318    ],
319    distribution = "protobuf",
320    extra_distinfo_files = {
321        "//:LICENSE": "LICENSE",
322    },
323    homepage = "https://developers.google.com/protocol-buffers/",
324    license = "3-Clause BSD License",
325    platform = select({
326        ":linux_x86_64_local": "linux_x86_64",
327        ":linux_x86_64_release": "manylinux2014_x86_64",
328        ":linux_aarch64_local": "linux_aarch64",
329        ":linux_aarch64_release": "manylinux2014_aarch64",
330        ":osx_universal2": "macosx_10_9_universal2",
331        ":osx_aarch64": "macosx_11_0_arm64",
332        ":windows_x86_32": "win32",
333        ":windows_x86_64": "win_amd64",
334        "//conditions:default": "any",
335    }),
336    python_requires = ">=3.8",
337    # LINT.IfChange(python_tag)
338    python_tag = selects.with_or({
339        ("//python:limited_api_3.8", "//python:full_api_3.8"): "cp38",
340        "//python:full_api_3.9": "cp39",
341        "//python:limited_api_3.10": "cp310",
342        "//conditions:default": "cp" + SYSTEM_PYTHON_VERSION,
343    }),
344    # LINT.ThenChange(
345    #    :full_api_version,
346    #    :limited_api_wheels,
347    # )
348    strip_path_prefixes = [
349        "python/dist/",
350        "python/",
351        "src/",
352    ],
353    target_compatible_with = select({
354        "@system_python//:none": ["@platforms//:incompatible"],
355        "//conditions:default": [],
356    }),
357    version = PROTOBUF_PYTHON_VERSION,
358    deps = [
359        ":message_mod",
360        ":plugin_py_pb2",
361        ":well_known_proto_py_pb2",
362        "//:python_srcs",
363    ],
364)
365
366py_wheel(
367    name = "pure_python_wheel",
368    abi = "none",
369    author = "protobuf@googlegroups.com",
370    author_email = "protobuf@googlegroups.com",
371    classifiers = [
372        "Programming Language :: Python",
373        "Programming Language :: Python :: 3",
374        "Programming Language :: Python :: 3.8",
375        "Programming Language :: Python :: 3.9",
376        "Programming Language :: Python :: 3.10",
377        "Programming Language :: Python :: 3.11",
378        "Programming Language :: Python :: 3.12",
379    ],
380    distribution = "protobuf",
381    extra_distinfo_files = {
382        "//:LICENSE": "LICENSE",
383    },
384    homepage = "https://developers.google.com/protocol-buffers/",
385    license = "3-Clause BSD License",
386    platform = "any",
387    python_requires = ">=3.8",
388    python_tag = "py3",
389    strip_path_prefixes = [
390        "python/",
391        "src/",
392    ],
393    target_compatible_with = select({
394        "@system_python//:none": ["@platforms//:incompatible"],
395        "//conditions:default": [],
396    }),
397    version = PROTOBUF_PYTHON_VERSION,
398    deps = [
399        ":plugin_py_pb2",
400        ":well_known_proto_py_pb2",
401        "//:python_srcs",
402    ],
403)
404
405py_wheel(
406    name = "test_wheel",
407    testonly = True,
408    abi = "none",
409    distribution = "protobuftests",
410    extra_distinfo_files = {
411        "//:LICENSE": "LICENSE",
412    },
413    platform = "any",
414    python_tag = "py3",
415    strip_path_prefixes = [
416        "python/",
417        "src/",
418    ],
419    target_compatible_with = select({
420        "@system_python//:none": ["@platforms//:incompatible"],
421        "//conditions:default": [],
422    }),
423    version = PROTOBUF_PYTHON_VERSION,
424    deps = [
425        "//:python_common_test_protos",
426        "//:python_specific_test_protos",
427        "//:python_test_srcs",
428        "//python/pb_unit_tests:test_files",
429        "//src/google/protobuf:testdata",
430    ],
431)
432
433py_dist(
434    name = "dist",
435    binary_wheel = ":binary_wheel",
436    full_api_cpus = [
437        "win32",
438        "win64",
439    ],
440    # Windows needs version-specific wheels until 3.10.
441    # LINT.IfChange(full_api_version)
442    full_api_versions = [
443        "38",
444        "39",
445    ],
446    # LINT.ThenChange(:python_tag)
447    # Limited API: these wheels will satisfy any Python version >= the
448    # given version.
449    #
450    # Technically the limited API doesn't have the functions we need until
451    # 3.10, but on Linux we can get away with using 3.1+ (see ../python_api.h for
452    # details).
453    # LINT.IfChange(limited_api_wheels)
454    limited_api_wheels = {
455        "win32": "310",
456        "win64": "310",
457        "linux-x86_64": "38",
458        "linux-aarch_64": "38",
459        "osx-universal2": "38",
460    },
461    # LINT.ThenChange(:python_tag)
462    pure_python_wheel = ":pure_python_wheel",
463    tags = ["manual"],
464)
465
466# py_proto_library() is private rule, only intended for internal use by upb.
467# Hopefully py_proto_library() will eventually be available in rules_proto or
468# another upstream package.
469bzl_library(
470    name = "py_proto_library_bzl",
471    srcs = ["py_proto_library.bzl"],
472    deps = [
473        "@rules_python//python:py_info_bzl",
474    ],
475)
476