Skip to content

try hermetic python for 3.12 #22296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
43 changes: 11 additions & 32 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -118,42 +118,21 @@ register_toolchains(
# Proto toolchains
register_toolchains("//bazel/private/toolchains:all")

SUPPORTED_PYTHON_VERSIONS = [
"3.9",
"3.10",
"3.11",
"3.12",
]

# TODO: Support hermetic / system python in bzlmod.
python = use_extension("@rules_python//python/extensions:python.bzl", "python")

[
python.toolchain(
# Disable root warning for .pyc cache misses since CI runs as root.
# See https://github.com/bazelbuild/rules_python/pull/713
ignore_root_user_error = True,
is_default = python_version == SUPPORTED_PYTHON_VERSIONS[-1],
python_version = python_version,
)
for python_version in SUPPORTED_PYTHON_VERSIONS
]

use_repo(
python,
system_python = "python_{}".format(SUPPORTED_PYTHON_VERSIONS[-1].replace(".", "_")),
python.toolchain(
ignore_root_user_error = True,
is_default = True,
python_version = "3.12",
)

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip", dev_dependency = True)
use_repo(python, "python_3_12")

[
pip.parse(
hub_name = "protobuf_pip_deps",
python_version = python_version,
requirements_lock = "//python:requirements.txt",
)
for python_version in SUPPORTED_PYTHON_VERSIONS
]
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "protobuf_pip_deps",
python_version = "3.12",
requirements_lock = "//python:requirements.txt",
)

use_repo(pip, "protobuf_pip_deps")

Expand Down
12 changes: 6 additions & 6 deletions protobuf_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ py_repositories()
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility
load("//python/dist:python_downloads.bzl", "python_nuget_package", "python_source_archive")
load("//python/dist:system_python.bzl", "system_python")
#load("//python/dist:system_python.bzl", "system_python")

PROTOBUF_MAVEN_ARTIFACTS = [
"com.google.code.findbugs:jsr305:3.0.2",
Expand Down Expand Up @@ -146,11 +146,11 @@ def protobuf_deps():
url = "https://github.com/bazelbuild/rules_python/releases/download/1.1.0/rules_python-1.1.0.tar.gz",
)

if not native.existing_rule("system_python"):
system_python(
name = "system_python",
minimum_python_version = "3.9",
)
#if not native.existing_rule("system_python"):
# system_python(
# name = "system_python",
# minimum_python_version = "3.9",
# )

if not native.existing_rule("rules_jvm_external"):
http_archive(
Expand Down
2 changes: 0 additions & 2 deletions python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ selects.config_setting_group(

_message_target_compatible_with = {
"@platforms//os:windows": ["@platforms//:incompatible"],
"@system_python//:none": ["@platforms//:incompatible"],
"@system_python//:unsupported": ["@platforms//:incompatible"],
"//conditions:default": [],
}

Expand Down
12 changes: 6 additions & 6 deletions python/build_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def build_targets(name):
],
deps = select({
"//conditions:default": [],
":use_fast_cpp_protos": ["@system_python//:python_headers"],
":use_fast_cpp_protos": ["@python_3_12//cc:current_py_cc_headers"],
}),
)

Expand Down Expand Up @@ -154,7 +154,7 @@ def build_targets(name):
"@abseil-cpp//absl/strings",
] + select({
"//conditions:default": [],
":use_fast_cpp_protos": ["@system_python//:python_headers"],
":use_fast_cpp_protos": ["@python_3_12//cc:current_py_cc_headers"],
}),
)

Expand Down Expand Up @@ -460,7 +460,7 @@ def build_targets(name):
"//src/google/protobuf/io",
"@abseil-cpp//absl/log:absl_check",
"@abseil-cpp//absl/status",
"@system_python//:python_headers",
"@python_3_12//cc:current_py_cc_headers",
],
)

Expand All @@ -474,7 +474,7 @@ def build_targets(name):
env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "python"},
failure_list = "//conformance:failure_list_python.txt",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
":use_fast_cpp_protos": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
Expand All @@ -489,7 +489,7 @@ def build_targets(name):
env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "cpp"},
failure_list = "//conformance:failure_list_python_cpp.txt",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
":use_fast_cpp_protos": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
Expand All @@ -503,7 +503,7 @@ def build_targets(name):
env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "upb"},
failure_list = "//conformance:failure_list_python_upb.txt",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
":use_fast_cpp_protos": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
Expand Down
13 changes: 6 additions & 7 deletions python/dist/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ load("@protobuf_pip_deps//:requirements.bzl", "requirement")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("@rules_python//python:packaging.bzl", "py_wheel")
load("@system_python//:version.bzl", "SYSTEM_PYTHON_VERSION")
load("//:protobuf_version.bzl", "PROTOBUF_PYTHON_VERSION")
load(":dist.bzl", "py_dist", "py_dist_module")
load(":py_proto_library.bzl", "py_proto_library")
Expand Down Expand Up @@ -294,7 +293,7 @@ pkg_tar(
package_file_name = "protobuf.tar.gz",
strip_prefix = ".",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
Expand All @@ -313,7 +312,7 @@ genrule(
mv protobuf/dist/*.tar.gz $@
""",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
tools = [requirement("setuptools")],
Expand Down Expand Up @@ -360,7 +359,7 @@ py_wheel(
python_tag = selects.with_or({
("//python:limited_api_3.9", "//python:full_api_3.9"): "cp39",
"//python:limited_api_3.10": "cp310",
"//conditions:default": "cp" + SYSTEM_PYTHON_VERSION,
"//conditions:default": "cp312",
}),
# LINT.ThenChange(
# :full_api_version,
Expand All @@ -372,7 +371,7 @@ py_wheel(
"src/",
],
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
version = PROTOBUF_PYTHON_VERSION,
Expand Down Expand Up @@ -412,7 +411,7 @@ py_wheel(
"src/",
],
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
version = PROTOBUF_PYTHON_VERSION,
Expand All @@ -438,7 +437,7 @@ py_wheel(
"src/",
],
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
#"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
version = PROTOBUF_PYTHON_VERSION,
Expand Down
3 changes: 1 addition & 2 deletions python/dist/dist.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""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."""
Expand All @@ -17,7 +16,7 @@ def _get_suffix(limited_api, python_version, cpu):
return ".cp{}-{}.{}".format(python_version, abi, "pyd")

if python_version == "system":
python_version = SYSTEM_PYTHON_VERSION
python_version = "312"
if int(python_version) < 38:
python_version += "m"
abis = {
Expand Down
4 changes: 0 additions & 4 deletions python/internal.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,5 @@ def internal_py_test(deps = [], **kwargs):
"//python:python_test_lib",
"@com_google_absl_py//absl/testing:parameterized",
],
target_compatible_with = select({
"@system_python//:supported": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
**kwargs
)
2 changes: 1 addition & 1 deletion python/py_extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def py_extension(name, srcs, copts, deps = [], **kwargs):
"//python:full_api_3.9_win64": ["@nuget_python_x86-64_3.9.0//:python_full_api"],
"//python:limited_api_3.10_win32": ["@nuget_python_i686_3.10.0//:python_limited_api"],
"//python:limited_api_3.10_win64": ["@nuget_python_x86-64_3.10.0//:python_limited_api"],
"//conditions:default": ["@system_python//:python_headers"],
"//conditions:default": ["@python_3_12//cc:current_py_cc_headers"],
}),
**kwargs
)
Expand Down
Loading