Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.5 KiB

"""Rules to create python distribution files and properly name them"""
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@system_python//:version.bzl", "SYSTEM_PYTHON_VERSION")
def _get_suffix(limited_api, python_version, cpu):
"""Computes an ABI version tag for an extension module per PEP 3149."""
if "win32" in cpu or "win64" in cpu:
if limited_api:
return ".pyd"
if "win32" in cpu:
abi = "win32"
elif "win64" in cpu:
abi = "win_amd64"
else:
fail("Unsupported CPU: " + cpu)
return ".cp{}-{}.{}".format(python_version, abi, "pyd")
if python_version == "system":
python_version = SYSTEM_PYTHON_VERSION
if int(python_version) < 38:
python_version += "m"
abis = {
"darwin_arm64": "darwin",
Cherry-pick recent changes from the upb repo (#13908) I merged a handful of PRs on the upb repo after upb moved into the protobuf repo. This PR cherry-picks them here so that they will not be lost. ``` commit 7afb426a5ace6f6e76ac2144960363379a1be08d Author: Keith Smiley <keithbsmiley@gmail.com> Date: Thu Sep 7 11:36:01 2023 -0700 [bazel] Fix disallowing dylibs on darwin (#1180) Since this bazel commit https://github.com/bazelbuild/bazel/commit/ec5553352f2f661d39ac4cf665dd9b3c779e614c building dylibs like the ones in this rule on darwin platforms has been unsupported. This feature is a default C++ toolchain feature to indicate this. In bazel 7.x these dylibs will fail to link if they are still built. As far as I can tell in the tests even if they are built they are never used on macOS. Co-authored-by: Adam Cozzette <acozzette@google.com> commit 72decab5eca0110548b0c805275fffab562aebde Author: Keith Smiley <keithbsmiley@gmail.com> Date: Thu Sep 7 09:42:20 2023 -0700 Add missing darwin_x86_64 CPU (#1181) This CPU is often used when cross compiling from M1 machines. I'm also hoping we can remove the legacy 'darwin' CPU. commit ccadaf3196bda975dec5ed2aac8a78e75ab51c1b Author: messense <messense@icloud.com> Date: Fri Sep 8 00:28:54 2023 +0800 Fix `PyUpb_Message_MergeInternal` segfault (#1338) when `PyUpb_Message_MergeFromString` returns `NULL`, currently `PyUpb_Message_MergeInternal` will call `Py_DECREF` on `NULL` which results in a segmentation fault. This patch switches to `Py_XDECREF` to fix the segfault. commit 2a5724d86ea81e0c2a0f8d10db274821c8bb6656 Author: Kevin Greene <kgreenek@gmail.com> Date: Wed Sep 6 16:46:35 2023 -0700 Fix lambda capture compiler error with c++20 (#1502) When compiling with C++20, the following error is produced: ``` upb/mini_table.hpp:63:22: note: add explicit 'this' or '*this' capture upb/mini_table.hpp: In lambda function: upb/mini_table.hpp:71:22: error: implicit capture of 'this' via '[=]' is deprecated in C++20 [-Werror=deprecated] 71 | return appender_([=](char* buf) { ``` In C++20, it is no longer allowed to implicitly capture 'this' in a lambda using [=]. This commit explicitly captures required values in the appropriate lambdas and removes all uses of [=] with lambdas. ``` Closes #13908 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/13908 from acozzette:upb 7afb426a5ace6f6e76ac2144960363379a1be08d PiperOrigin-RevId: 563784513
1 year ago
"darwin_x86_64": "darwin",
"darwin": "darwin",
"osx-x86_64": "darwin",
"osx-aarch_64": "darwin",
"linux-aarch_64": "aarch64-linux-gnu",
"linux-x86_64": "x86_64-linux-gnu",
"k8": "x86_64-linux-gnu",
}
return ".cpython-{}-{}.{}".format(
python_version,
abis[cpu],
"so" if limited_api else "abi3.so",
)
elif limited_api:
return ".abi3.so"
fail("Unsupported combination of flags")
def _declare_module_file(ctx, module_name, python_version, limited_api):
"""Declares an output file for a Python module with this name, version, and limited api."""
base_filename = module_name.replace(".", "/")
suffix = _get_suffix(
python_version = python_version,
limited_api = limited_api,
cpu = ctx.var["TARGET_CPU"],
)
filename = base_filename + suffix
return ctx.actions.declare_file(filename)
# --------------------------------------------------------------------------------------------------
# py_dist_module()
#
# Creates a Python binary extension module that is ready for distribution.
#
# py_dist_module(
# name = "message_mod",
# extension = "//python:_message_binary",
# module_name = "google._upb._message",
# )
#
# In the simple case, this simply involves copying the input file to the proper filename for
# our current configuration (module_name, cpu, python_version, limited_abi).
#
# For multiarch platforms (osx-universal2), we must combine binaries for multiple architectures
# into a single output binary using the "llvm-lipo" tool. A config transition depends on multiple
# architectures to get us the input files we need.
def _py_multiarch_transition_impl(settings, attr):
if settings["//command_line_option:cpu"] == "osx-universal2":
return [{"//command_line_option:cpu": cpu} for cpu in ["osx-aarch_64", "osx-x86_64"]]
else:
return settings
_py_multiarch_transition = transition(
implementation = _py_multiarch_transition_impl,
inputs = ["//command_line_option:cpu"],
outputs = ["//command_line_option:cpu"],
)
def _py_dist_module_impl(ctx):
output_file = _declare_module_file(
ctx = ctx,
module_name = ctx.attr.module_name,
python_version = ctx.attr._python_version[BuildSettingInfo].value,
limited_api = ctx.attr._limited_api[BuildSettingInfo].value,
)
if len(ctx.attr.extension) == 1:
src = ctx.attr.extension[0][DefaultInfo].files.to_list()[0]
ctx.actions.run(
executable = "cp",
arguments = [src.path, output_file.path],
inputs = [src],
outputs = [output_file],
)
return [
DefaultInfo(files = depset([output_file])),
]
else:
srcs = [mod[DefaultInfo].files.to_list()[0] for mod in ctx.attr.extension]
ctx.actions.run(
executable = "/usr/local/bin/llvm-lipo",
arguments = ["-create", "-output", output_file.path] + [src.path for src in srcs],
inputs = srcs,
outputs = [output_file],
)
return [
DefaultInfo(files = depset([output_file])),
]
py_dist_module = rule(
implementation = _py_dist_module_impl,
attrs = {
"module_name": attr.string(mandatory = True),
"extension": attr.label(
mandatory = True,
cfg = _py_multiarch_transition,
),
"_limited_api": attr.label(default = "//python:limited_api"),
"_python_version": attr.label(default = "//python:python_version"),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)
# --------------------------------------------------------------------------------------------------
# py_dist()
#
# A rule that builds a collection of binary wheels, using transitions to depend on many different
# python versions and cpus.
def _py_dist_transition_impl(settings, attr):
_ignore = (settings) # @unused
transitions = []
for cpu, version in attr.limited_api_wheels.items():
transitions.append({
"//command_line_option:cpu": cpu,
"//python:python_version": version,
"//python:limited_api": True,
})
for version in attr.full_api_versions:
for cpu in attr.full_api_cpus:
transitions.append({
"//command_line_option:cpu": cpu,
"//python:python_version": version,
"//python:limited_api": False,
})
return transitions
_py_dist_transition = transition(
implementation = _py_dist_transition_impl,
inputs = [],
outputs = [
"//command_line_option:cpu",
"//python:python_version",
"//python:limited_api",
],
)
def _py_dist_impl(ctx):
binary_files = [dep[DefaultInfo].files for dep in ctx.attr.binary_wheel]
pure_python_files = [ctx.attr.pure_python_wheel[DefaultInfo].files]
return [
DefaultInfo(files = depset(
transitive = binary_files + pure_python_files,
)),
]
py_dist = rule(
implementation = _py_dist_impl,
attrs = {
"binary_wheel": attr.label(
mandatory = True,
cfg = _py_dist_transition,
),
"pure_python_wheel": attr.label(mandatory = True),
"limited_api_wheels": attr.string_dict(),
"full_api_versions": attr.string_list(),
"full_api_cpus": attr.string_list(),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)