Skip to content

Commit 36db9b2

Browse files
committed
mark runtimeenv as build-time venv compatible if python 3.11+
1 parent 73217ed commit 36db9b2

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

python/private/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ bzl_library(
8686
name = "runtime_env_toolchain_bzl",
8787
srcs = ["runtime_env_toolchain.bzl"],
8888
deps = [
89+
":config_settings_bzl",
8990
":py_exec_tools_toolchain_bzl",
9091
":toolchain_types_bzl",
9192
"//python:py_runtime_bzl",

python/private/config_settings.bzl

+30
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,33 @@ _current_config = rule(
209209
"_template": attr.string(default = _DEBUG_ENV_MESSAGE_TEMPLATE),
210210
},
211211
)
212+
213+
def is_python_version_at_least(name, **kwargs):
214+
flag_name = "_{}_flag".format(name)
215+
native.config_setting(
216+
name = name,
217+
flag_values = {
218+
flag_name: "yes",
219+
},
220+
)
221+
_python_version_at_least(
222+
name = flag_name,
223+
visibility = ["//visibility:private"],
224+
**kwargs
225+
)
226+
227+
def _python_version_at_least_impl(ctx):
228+
at_least = tuple(ctx.attr.at_least.split("."))
229+
current = tuple(
230+
ctx.attr._major_minor[config_common.FeatureFlagInfo].value.split("."),
231+
)
232+
value = "yes" if current >= at_least else "no"
233+
return [config_common.FeatureFlagInfo(value = value)]
234+
235+
_python_version_at_least = rule(
236+
implementation = _python_version_at_least_impl,
237+
attrs = {
238+
"at_least": attr.string(mandatory = True),
239+
"_major_minor": attr.label(default = _PYTHON_VERSION_MAJOR_MINOR_FLAG),
240+
},
241+
)

python/private/runtime_env_toolchain.bzl

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@rules_cc//cc:cc_library.bzl", "cc_library")
1717
load("//python:py_runtime.bzl", "py_runtime")
1818
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
1919
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
20+
load("//python/private:config_settings.bzl", "is_python_version_at_least")
2021
load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
2122
load(":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "PY_CC_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE")
2223

@@ -38,14 +39,19 @@ def define_runtime_env_toolchain(name):
3839
"""
3940
base_name = name.replace("_toolchain", "")
4041

42+
supports_build_time_venv = select({
43+
":_is_at_least_py3.11": True,
44+
"//conditions:default": False,
45+
})
46+
4147
py_runtime(
4248
name = "_runtime_env_py3_runtime",
4349
interpreter = "//python/private:runtime_env_toolchain_interpreter.sh",
4450
python_version = "PY3",
4551
stub_shebang = "#!/usr/bin/env python3",
4652
visibility = ["//visibility:private"],
4753
tags = ["manual"],
48-
supports_build_time_venv = False,
54+
supports_build_time_venv = supports_build_time_venv,
4955
)
5056

5157
# This is a dummy runtime whose interpreter_path triggers the native rule
@@ -57,7 +63,7 @@ def define_runtime_env_toolchain(name):
5763
python_version = "PY3",
5864
visibility = ["//visibility:private"],
5965
tags = ["manual"],
60-
supports_build_time_venv = False,
66+
supports_build_time_venv = supports_build_time_venv,
6167
)
6268

6369
py_runtime_pair(
@@ -112,3 +118,7 @@ def define_runtime_env_toolchain(name):
112118
toolchain_type = PY_CC_TOOLCHAIN_TYPE,
113119
visibility = ["//visibility:public"],
114120
)
121+
is_python_version_at_least(
122+
name = "_is_at_least_py3.11",
123+
at_least = "3.11",
124+
)

0 commit comments

Comments
 (0)