Skip to content

Commit

Permalink
Replace system python with hermetic python from rules_python
Browse files Browse the repository at this point in the history
Closes #15850

PiperOrigin-RevId: 633147621
  • Loading branch information
protobuf-github-bot authored and copybara-github committed May 13, 2024
1 parent f7a866a commit 823c098
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 370 deletions.
4 changes: 3 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_java", version = "5.3.5")
bazel_dep(name = "rules_jvm_external", version = "5.1")
bazel_dep(name = "rules_pkg", version = "0.7.0")
bazel_dep(name = "rules_python", version = "0.10.2")
bazel_dep(name = "rules_python", version = "0.28.0")
python = use_extension("@rules_python//extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "zlib", version = "1.2.11")

Expand Down
22 changes: 15 additions & 7 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,21 @@ http_archive(
patch_cmds = ["find google -type f -name BUILD.bazel -delete"],
)

load("@system_python//:pip.bzl", "pip_parse")
load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python_3_12",
python_version = "3.12",
)

load("@python_3_12//:defs.bzl", "interpreter")

load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip_deps",
requirements = "//python:requirements.txt",
requirements_lock = "//python:requirements.txt",
python_interpreter_target = interpreter,
)

load("@pip_deps//:requirements.bzl", "install_deps")
Expand All @@ -177,11 +187,9 @@ install_deps()

http_archive(
name = "rules_fuzzing",
sha256 = "ff52ef4845ab00e95d29c02a9e32e9eff4e0a4c9c8a6bcf8407a2f19eb3f9190",
strip_prefix = "rules_fuzzing-0.4.1",
urls = ["https://github.com/bazelbuild/rules_fuzzing/releases/download/v0.4.1/rules_fuzzing-0.4.1.zip"],
patches = ["//third_party:rules_fuzzing.patch"],
patch_args = ["-p1"],
sha256 = "77206c54b71f4dd5335123a6ff2a8ea688eca5378d34b4838114dff71652cf26",
strip_prefix = "rules_fuzzing-0.5.1",
urls = ["https://github.com/bazelbuild/rules_fuzzing/releases/download/v0.5.1/rules_fuzzing-0.5.1.zip"],
)

load("@rules_fuzzing//fuzzing:repositories.bzl", "rules_fuzzing_dependencies")
Expand Down
13 changes: 3 additions & 10 deletions protobuf_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//python/dist:python_downloads.bzl", "python_nuget_package", "python_source_archive")
load("//python/dist:system_python.bzl", "system_python")

PROTOBUF_MAVEN_ARTIFACTS = [
"com.google.caliper:caliper:1.0-beta-3",
Expand Down Expand Up @@ -100,15 +99,9 @@ def protobuf_deps():
if not native.existing_rule("rules_python"):
http_archive(
name = "rules_python",
sha256 = "9d04041ac92a0985e344235f5d946f71ac543f1b1565f2cdbc9a2aaee8adf55b",
strip_prefix = "rules_python-0.26.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.26.0/rules_python-0.26.0.tar.gz",
)

if not native.existing_rule("system_python"):
system_python(
name = "system_python",
minimum_python_version = "3.7",
sha256 = "d70cd72a7a4880f0000a6346253414825c19cdd40a28289bdf67b8e6480edff8",
strip_prefix = "rules_python-0.28.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.28.0/rules_python-0.28.0.tar.gz",
)

if not native.existing_rule("rules_jvm_external"):
Expand Down
9 changes: 3 additions & 6 deletions python/build_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def build_targets(name):
],
deps = select({
"//conditions:default": [],
":use_fast_cpp_protos": ["@system_python//:python_headers"],
":use_fast_cpp_protos": ["@rules_python//python/cc:current_py_cc_headers"],
}),
)

Expand Down Expand Up @@ -136,7 +136,7 @@ def build_targets(name):
"@com_google_absl//absl/strings",
] + select({
"//conditions:default": [],
":use_fast_cpp_protos": ["@system_python//:python_headers"],
":use_fast_cpp_protos": ["@rules_python//python/cc:current_py_cc_headers"],
}),
)

Expand Down Expand Up @@ -426,7 +426,7 @@ def build_targets(name):
hdrs = ["google/protobuf/proto_api.h"],
visibility = ["//visibility:public"],
deps = [
"@system_python//:python_headers",
"@rules_python//python/cc:current_py_cc_headers",
],
)

Expand All @@ -440,7 +440,6 @@ 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"],
":use_fast_cpp_protos": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
Expand All @@ -455,7 +454,6 @@ def build_targets(name):
env = {"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION": "cpp"},
failure_list = "//conformance:failure_list_python.txt",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
":use_fast_cpp_protos": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
Expand All @@ -469,7 +467,6 @@ 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"],
":use_fast_cpp_protos": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
Expand Down
17 changes: 0 additions & 17 deletions python/dist/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ load("@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 @@ -272,7 +271,6 @@ pkg_tar(
package_file_name = "protobuf.tar.gz",
strip_prefix = ".",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
Expand All @@ -291,7 +289,6 @@ genrule(
mv protobuf/dist/*.tar.gz $@
""",
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
tools = [requirement("setuptools")],
Expand Down Expand Up @@ -333,24 +330,12 @@ py_wheel(
"//conditions:default": "any",
}),
python_requires = ">=3.8",
# LINT.IfChange(python_tag)
python_tag = selects.with_or({
("//python:limited_api_3.8", "//python:full_api_3.8"): "cp38",
"//python:full_api_3.9": "cp39",
"//python:limited_api_3.10": "cp310",
"//conditions:default": "cp" + SYSTEM_PYTHON_VERSION,
}),
# LINT.ThenChange(
# :full_api_version,
# :limited_api_wheels,
# )
strip_path_prefixes = [
"python/dist/",
"python/",
"src/",
],
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
version = PROTOBUF_PYTHON_VERSION,
Expand Down Expand Up @@ -390,7 +375,6 @@ py_wheel(
"src/",
],
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
version = PROTOBUF_PYTHON_VERSION,
Expand All @@ -416,7 +400,6 @@ py_wheel(
"src/",
],
target_compatible_with = select({
"@system_python//:none": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
version = PROTOBUF_PYTHON_VERSION,
Expand Down
22 changes: 0 additions & 22 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 @@ -15,27 +14,6 @@ def _get_suffix(limited_api, python_version, cpu):
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",
"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"

Expand Down
Loading

0 comments on commit 823c098

Please sign in to comment.