forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_env_toolchain.bzl
More file actions
125 lines (113 loc) · 4.68 KB
/
runtime_env_toolchain.bzl
File metadata and controls
125 lines (113 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Definitions related to the Python toolchain."""
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//python:py_runtime.bzl", "py_runtime")
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
load("//python/private:config_settings.bzl", "is_python_version_at_least")
load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
load(":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "PY_CC_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE")
_IS_EXEC_TOOLCHAIN_ENABLED = Label("//python/config_settings:is_exec_tools_toolchain_enabled")
def define_runtime_env_toolchain(name):
"""Defines the runtime_env Python toolchain.
This is a minimal suite of toolchains that provided limited functionality.
They're mostly only useful to aid migration off the builtin
`@bazel_tools//tools/python:autodetecting_toolchain` toolchain.
NOTE: This was previously called the "autodetecting" toolchain, but was
renamed to better reflect its behavior, since it doesn't autodetect
anything.
Args:
name: The name of the toolchain to introduce.
"""
base_name = name.replace("_toolchain", "")
supports_build_time_venv = select({
":_is_at_least_py3.11": True,
"//conditions:default": False,
})
py_runtime(
name = "_runtime_env_py3_runtime",
interpreter = "//python/private:runtime_env_toolchain_interpreter.sh",
python_version = "PY3",
stub_shebang = "#!/usr/bin/env python3",
visibility = ["//visibility:private"],
tags = ["manual"],
supports_build_time_venv = supports_build_time_venv,
)
# This is a dummy runtime whose interpreter_path triggers the native rule
# logic to use the legacy behavior on Windows.
# TODO(#7844): Remove this target.
py_runtime(
name = "_magic_sentinel_runtime",
interpreter_path = "/_magic_pyruntime_sentinel_do_not_use",
python_version = "PY3",
visibility = ["//visibility:private"],
tags = ["manual"],
supports_build_time_venv = supports_build_time_venv,
)
py_runtime_pair(
name = "_runtime_env_py_runtime_pair",
py3_runtime = select({
# If we're on windows, inject the sentinel to tell native rule logic
# that we attempted to use the runtime_env toolchain and need to
# switch back to legacy behavior.
# TODO(#7844): Remove this hack.
"@platforms//os:windows": ":_magic_sentinel_runtime",
"//conditions:default": ":_runtime_env_py3_runtime",
}),
visibility = ["//visibility:public"],
tags = ["manual"],
)
native.toolchain(
name = name,
toolchain = ":_runtime_env_py_runtime_pair",
toolchain_type = TARGET_TOOLCHAIN_TYPE,
visibility = ["//visibility:public"],
)
py_exec_tools_toolchain(
name = "_runtime_env_py_exec_tools_toolchain_impl",
precompiler = Label("//tools/precompiler:precompiler"),
visibility = ["//visibility:private"],
tags = ["manual"],
)
native.toolchain(
name = base_name + "_py_exec_tools_toolchain",
toolchain = "_runtime_env_py_exec_tools_toolchain_impl",
toolchain_type = EXEC_TOOLS_TOOLCHAIN_TYPE,
target_settings = [_IS_EXEC_TOOLCHAIN_ENABLED],
visibility = ["//visibility:public"],
)
cc_library(
name = "_empty_cc_lib",
visibility = ["//visibility:private"],
tags = ["manual"],
)
py_cc_toolchain(
name = "_runtime_env_py_cc_toolchain_impl",
headers = "//python/private/cc:empty",
headers_abi3 = "//python/private/cc:empty",
libs = "//python/private/cc:empty",
python_version = "0.0",
tags = ["manual"],
)
native.toolchain(
name = base_name + "_py_cc_toolchain",
toolchain = ":_runtime_env_py_cc_toolchain_impl",
toolchain_type = PY_CC_TOOLCHAIN_TYPE,
visibility = ["//visibility:public"],
)
is_python_version_at_least(
name = "_is_at_least_py3.11",
at_least = "3.11",
)