Update upb's _message target and Copybara config to use OSS rules_python py_extension

Updates the Copybara configuration for upb to load py_extension from @rules_python instead of the local path. Changes the _message target to use the new py_extension.

PiperOrigin-RevId: 963822572
This commit is contained in:
Protobuf Team Bot 2026-08-12 20:50:45 -07:00 committed by Copybara-Service
parent 7b091ee68e
commit 06bc39dc0e
10 changed files with 106 additions and 42 deletions

View file

@ -37,6 +37,12 @@ bazel_dep(name = "rules_kotlin", version = "2.3.20")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "rules_python", version = "2.3.0")
git_override(
module_name = "rules_python",
commit = "84874d514e343a32ce1c611165b1f26952dedd79",
remote = "https://github.com/rsartor-cmd/rules_python.git",
)
bazel_dep(name = "rules_rust", version = "0.69.0")
bazel_dep(name = "rules_ruby", version = "0.20.1", dev_dependency = True)
@ -153,33 +159,15 @@ pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip", dev_depen
use_repo(pip, "protobuf_pip_deps")
local_runtime_repo = use_repo_rule(
"@rules_python//python/local_toolchains:repos.bzl",
"local_runtime_repo",
)
python = use_extension("@rules_python//python/extensions:python.bzl", "python", dev_dependency = True)
local_runtime_toolchains_repo = use_repo_rule(
"@rules_python//python/local_toolchains:repos.bzl",
"local_runtime_toolchains_repo",
)
local_runtime_repo(
name = "local_python3",
dev_dependency = True,
interpreter_path = "python3",
on_failure = "fail",
)
local_runtime_toolchains_repo(
name = "local_toolchains",
dev_dependency = True,
runtimes = ["local_python3"],
)
register_toolchains(
"@local_toolchains//:all",
dev_dependency = True,
)
[
python.toolchain(
is_default = (python_version == "3.10"),
python_version = python_version,
)
for python_version in SUPPORTED_PYTHON_VERSIONS
]
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust", dev_dependency = True)

View file

@ -522,8 +522,8 @@ py_binary(
"//conformance/test_protos:test_messages_edition_unstable_py_pb2",
"//editions:test_messages_proto2_editions_py_pb2",
"//editions:test_messages_proto3_editions_py_pb2",
"//python:_message", # Make upb visible if we need it.
"//python:conformance_test_py_proto",
"//python:google/_upb/_message", # Make upb visible if we need it.
],
)

View file

@ -23,3 +23,8 @@ bazel_dep(name = "rules_cc", version = "0.2.18")
bazel_dep(name = "rules_java", version = "8.6.1")
bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "rules_python", version = "2.3.0")
git_override(
module_name = "rules_python",
commit = "84874d514e343a32ce1c611165b1f26952dedd79",
remote = "https://github.com/rsartor-cmd/rules_python.git",
)

View file

@ -6,8 +6,8 @@
# https://developers.google.com/open-source/licenses/bsd
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
load("@rules_python//python/cc:py_extension.bzl", "py_extension")
load("//python:build_targets.bzl", "build_targets")
load("//python:py_extension.bzl", "py_extension")
load("//upb/bazel:copts.bzl", "UPB_DEFAULT_COPTS", "UPB_DEFAULT_FEATURES")
build_targets(name = "python")
@ -108,11 +108,15 @@ filegroup(
)
py_extension(
name = "_message",
name = "google/_upb/_message",
srcs = [":message_srcs"],
copts = UPB_DEFAULT_COPTS + select(LIMITED_API_FLAG_SELECT),
features = UPB_DEFAULT_FEATURES,
target_compatible_with = select(_message_target_compatible_with),
visibility = [
":__subpackages__",
"//conformance:__pkg__",
],
deps = [
":breaking_changes",
"//src/google/protobuf:descriptor_upb_minitable_proto",

View file

@ -21,7 +21,7 @@ licenses(["notice"])
py_dist_module(
name = "message_mod",
extension = "//python:_message_binary",
extension = "//python:google/_upb/_message",
module_name = "google._upb._message",
)

View file

@ -77,7 +77,7 @@ def _declare_module_file(ctx, module_name, python_version, limited_api):
#
# py_dist_module(
# name = "message_mod",
# extension = "//python:_message_binary",
# extension = "//python:google/_upb/_message",
# module_name = "google._upb._message",
# )
#

View file

@ -4,6 +4,7 @@ load("@bazel_skylib//lib:selects.bzl", "selects")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_python//python:py_library.bzl", "py_library")
# TODO: Remove this file once _message has successfully switched to OSS rules_python's py_extension.
def py_extension(name, srcs, copts, deps = [], **kwargs):
"""Creates a C++ library to extend python

View file

@ -6,6 +6,7 @@ load(
"flag_group",
"flag_set",
"tool_path",
"variable_with_value",
"with_feature_set",
)
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
@ -176,10 +177,51 @@ def _impl(ctx):
],
)
archiver_flags = feature(
name = "archiver_flags",
enabled = True,
flag_sets = [
flag_set(
actions = [ACTION_NAMES.cpp_link_static_library],
flag_groups = [
flag_group(
flags = ["rcsD"],
),
flag_group(
expand_if_available = "output_execpath",
flags = ["%{output_execpath}"],
),
flag_group(
expand_if_available = "libraries_to_link",
iterate_over = "libraries_to_link",
flag_groups = [
flag_group(
expand_if_equal = variable_with_value(
"libraries_to_link.type",
"object_file",
),
flags = ["%{libraries_to_link.name}"],
),
flag_group(
expand_if_equal = variable_with_value(
"libraries_to_link.type",
"object_file_group",
),
iterate_over = "libraries_to_link.object_files",
flags = ["%{libraries_to_link.object_files}"],
),
],
),
],
),
],
)
features = [
linker_flags,
compiler_flags,
sysroot_flags,
archiver_flags,
feature(name = "dbg"),
feature(name = "opt"),
]
@ -199,6 +241,13 @@ def _impl(ctx):
),
)
if "apple" in ctx.attr.target_full_name or "osx" in ctx.attr.target_full_name or "darwin" in ctx.attr.target_full_name:
target_libc = "macosx"
elif "mingw" in ctx.attr.target_full_name:
target_libc = "mingw"
else:
target_libc = ctx.attr.target_cpu
return cc_common.create_cc_toolchain_config_info(
abi_libc_version = ctx.attr.abi_version,
abi_version = ctx.attr.abi_version,
@ -214,7 +263,7 @@ def _impl(ctx):
features = features,
host_system_name = "local",
target_cpu = ctx.attr.target_cpu,
target_libc = ctx.attr.target_cpu,
target_libc = target_libc,
target_system_name = ctx.attr.target_full_name,
toolchain_identifier = ctx.attr.target_full_name,
tool_paths = tool_paths,

View file

@ -1,5 +1,8 @@
"""Internal rule implementation for upb_*_proto_library() rules."""
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
def _filter_none(elems):
out = []
for elem in elems:
@ -32,6 +35,28 @@ def upb_proto_rule_impl(ctx, cc_info_provider, srcs_provider):
lib.pic_static_library,
lib.dynamic_library,
])
linker_inputs = []
dep_prefix = dep.label.name + "."
for input in cc_info.linking_context.linker_inputs.to_list():
if input.owner == dep.label or (input.owner.package == dep.label.package and input.owner.name.startswith(dep_prefix)):
linker_inputs.append(cc_common.create_linker_input(
owner = ctx.label,
libraries = depset(input.libraries),
user_link_flags = depset(input.user_link_flags),
additional_inputs = depset(input.additional_inputs),
))
else:
linker_inputs.append(input)
linking_context = cc_common.create_linking_context(
linker_inputs = depset(linker_inputs, order = "topological"),
)
cc_info = CcInfo(
compilation_context = cc_info.compilation_context,
linking_context = linking_context,
)
return [
DefaultInfo(files = depset(files + srcs.hdrs + srcs.srcs)),
srcs,

View file

@ -1,6 +1,5 @@
"""upb_minitable_proto_library() exposes upb's generated minitables (foo.upb_minitable.h)"""
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//bazel/common:proto_info.bzl", "ProtoInfo")
load("//upb/bazel:visibility.bzl", "upb_clients")
@ -28,22 +27,15 @@ def _upb_minitable_proto_library_aspect_impl(target, ctx):
cc_provider = UpbMinitableCcInfo,
dep_cc_provider = None,
file_provider = _UpbWrappedGeneratedSrcsInfo,
provide_cc_shared_library_hints = False,
)
def _get_upb_minitable_proto_library_aspect_provides():
provides = [
return [
UpbMinitableCcInfo,
_UpbWrappedGeneratedSrcsInfo,
]
if hasattr(cc_common, "CcSharedLibraryHintInfo"):
provides.append(cc_common.CcSharedLibraryHintInfo)
elif hasattr(cc_common, "CcSharedLibraryHintInfo_6_X_getter_do_not_use"):
# This branch can be deleted once 6.X is not supported by upb rules
provides.append(cc_common.CcSharedLibraryHintInfo_6_X_getter_do_not_use)
return provides
upb_minitable_proto_library_aspect = aspect(
attrs = {
"_copts": attr.label(