From 720e5468cebbb124e23c72080b28f2a41c4134a2 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 17 Aug 2026 21:01:02 -0700 Subject: [PATCH] Migrate Protobuf Python C extensions to rules_python toolchain targets. Update _message to use rules_python's py_extension macro (which automatically injects required toolchain headers and manages shared library extensions across platforms), and update _api_implementation.so to depend directly on `@rules_python//python/cc:current_py_cc_headers` instead of `@system_python`. Using `py_extension` and `current_py_cc_headers` decouples Python C extensions from the custom system_python.bzl script while maintaining current host-interpreter behavior during the migration. #test-continuous PiperOrigin-RevId: 966345187 --- MODULE.bazel | 2 +- examples/MODULE.bazel | 2 +- protobuf_deps.bzl | 6 +++--- python/build_targets.bzl | 16 ++++++++-------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index e8e6af5bfa..bf32036840 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -33,7 +33,7 @@ bazel_dep(name = "rules_jvm_external", version = "6.7") 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 = "1.6.0") +bazel_dep(name = "rules_python", version = "2.3.0") bazel_dep(name = "rules_rust", version = "0.69.0") bazel_dep(name = "rules_ruby", version = "0.20.1", dev_dependency = True) diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index 464407cbe1..f13e8f723e 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -22,4 +22,4 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1") 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 = "1.6.0") +bazel_dep(name = "rules_python", version = "2.3.0") diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl index 72785fb58d..31b2fa34eb 100644 --- a/protobuf_deps.bzl +++ b/protobuf_deps.bzl @@ -131,9 +131,9 @@ def protobuf_deps(): if not native.existing_rule("rules_python"): http_archive( name = "rules_python", - sha256 = "fa7dd2c6b7d63b3585028dd8a90a6cf9db83c33b250959c2ee7b583a6c130e12", - strip_prefix = "rules_python-1.6.0", - url = "https://github.com/bazel-contrib/rules_python/releases/download/1.6.0/rules_python-1.6.0.tar.gz", + sha256 = "70c519e8f0a5d9244c0d3496a6993bd42edf111c4499b68ce2f155c26fc8d4ee", + strip_prefix = "rules_python-2.3.0", + url = "https://github.com/bazel-contrib/rules_python/releases/download/2.3.0/rules_python-2.3.0.tar.gz", ) if not native.existing_rule("system_python"): diff --git a/python/build_targets.bzl b/python/build_targets.bzl index f7417b148e..df412dd2cf 100644 --- a/python/build_targets.bzl +++ b/python/build_targets.bzl @@ -12,6 +12,7 @@ load("@rules_cc//cc:cc_binary.bzl", "cc_binary") load("@rules_cc//cc:defs.bzl", "cc_library") load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_python//python:defs.bzl", "py_library") +load("@rules_python//python/cc:py_extension.bzl", "py_extension") load("//:protobuf.bzl", "internal_py_proto_library") load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain") load("//build_defs:arch_tests.bzl", "aarch64_test", "x86_64_test") @@ -33,7 +34,7 @@ def build_targets(name): "//conditions:default": [], ":use_fast_cpp_protos": [ ":google/protobuf/internal/_api_implementation.so", - ":google/protobuf/pyext/_message.so", + ":google/protobuf/pyext/_message", ], }), visibility = ["//:__pkg__"], @@ -99,7 +100,7 @@ def build_targets(name): # https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes "manual", ], - deps = ["@system_python//:python_headers"], + deps = ["@rules_python//python/cc:current_py_cc_headers"], ) native.config_setting( @@ -114,8 +115,8 @@ def build_targets(name): visibility = ["//python:__subpackages__"], ) - cc_binary( - name = "google/protobuf/pyext/_message.so", + py_extension( + name = "google/protobuf/pyext/_message", srcs = native.glob([ "google/protobuf/pyext/*.cc", "google/protobuf/pyext/*.h", @@ -162,7 +163,6 @@ def build_targets(name): "@abseil-cpp//absl/strings", "@abseil-cpp//absl/synchronization", "@abseil-cpp//absl/types:span", - "@system_python//:python_headers", ], ) @@ -170,7 +170,7 @@ def build_targets(name): name = "aarch64_test", bazel_binaries = [ "google/protobuf/internal/_api_implementation.so", - "google/protobuf/pyext/_message.so", + "google/protobuf/pyext/_message", ], ) @@ -178,7 +178,7 @@ def build_targets(name): name = "x86_64_test", bazel_binaries = [ "google/protobuf/internal/_api_implementation.so", - "google/protobuf/pyext/_message.so", + "google/protobuf/pyext/_message", ], ) @@ -485,7 +485,7 @@ def build_targets(name): "@abseil-cpp//absl/log:absl_check", "@abseil-cpp//absl/status", "@abseil-cpp//absl/status:statusor", - "@system_python//:python_headers", + "@rules_python//python/cc:current_py_cc_headers", ], )