forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_exec_tools_toolchain.bzl
More file actions
135 lines (118 loc) · 5.05 KB
/
Copy pathpy_exec_tools_toolchain.bzl
File metadata and controls
135 lines (118 loc) · 5.05 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
126
127
128
129
130
131
132
133
134
135
# Copyright 2024 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.
"""Rule that defines a toolchain for build tools."""
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load(":common_labels.bzl", "labels")
load(":py_exec_tools_info.bzl", "PyExecToolsInfo")
load(":sentinel.bzl", "SentinelInfo")
load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
def _py_exec_tools_toolchain_impl(ctx):
extra_kwargs = {}
if ctx.attr._visible_for_testing[BuildSettingInfo].value:
extra_kwargs["toolchain_label"] = ctx.label
exec_interpreter = ctx.attr.exec_interpreter
if SentinelInfo in ctx.attr.exec_interpreter:
exec_interpreter = None
exec_runtime = None
if exec_interpreter != None and platform_common.ToolchainInfo in exec_interpreter:
tc = exec_interpreter[platform_common.ToolchainInfo]
exec_runtime = getattr(tc, "py3_runtime", None)
return [
platform_common.ToolchainInfo(
exec_tools = PyExecToolsInfo(
exec_interpreter = exec_interpreter,
precompiler = ctx.attr.precompiler,
exec_runtime = exec_runtime,
),
**extra_kwargs
),
]
py_exec_tools_toolchain = rule(
implementation = _py_exec_tools_toolchain_impl,
doc = """
Provides a toolchain for build time tools.
This provides `ToolchainInfo` with the following attributes:
* `exec_tools`: {type}`PyExecToolsInfo`
* `toolchain_label`: {type}`Label` _only present when `--visibile_for_testing=True`
for internal testing_. The rule's label; this allows identifying what toolchain
implmentation was selected for testing purposes.
""",
attrs = {
"exec_interpreter": attr.label(
default = "//python/private:current_interpreter_executable",
providers = [
DefaultInfo,
# Add the toolchain provider so that we can forward provider fields.
platform_common.ToolchainInfo,
],
cfg = "exec",
doc = """
An interpreter that is directly usable in the exec configuration
If not specified, the interpreter from {obj}`//python:toolchain_type` will
be used.
To disable, specify the special target {obj}`//python:none`; the raw value `None`
will use the default.
:::{note}
This is only useful for `ctx.actions.run` calls that _directly_ invoke the
interpreter, which is fairly uncommon and low level. It is better to use a
`cfg="exec"` attribute that points to a `py_binary` rule instead, which will
handle all the necessary transitions and runtime setup to invoke a program.
:::
See {obj}`PyExecToolsInfo.exec_interpreter` for further docs.
:::{versionchanged} 1.4.0
From now on the provided target also needs to provide `platform_common.ToolchainInfo`
so that the toolchain `py_runtime` field can be correctly forwarded.
:::
""",
),
"precompiler": attr.label(
allow_files = True,
cfg = "exec",
doc = "See {obj}`PyExecToolsInfo.precompiler`",
),
"_visible_for_testing": attr.label(
default = labels.VISIBLE_FOR_TESTING,
),
},
)
def _current_interpreter_executable_impl(ctx):
toolchain = ctx.toolchains[TARGET_TOOLCHAIN_TYPE]
runtime = toolchain.py3_runtime
# NOTE: We name the output filename after the underlying file name
# because of things like pyenv: they use $0 to determine what to
# re-exec. If it's not a recognized name, then they fail.
if runtime.interpreter:
executable = ctx.actions.declare_file(runtime.interpreter.basename)
# NOTE: Using ctx.actions.symlink() here doesn't always work with RBE
# because it's not guaranteed that it will materialize as a symlink, but
# we rely on it being a symlink so that Python can find its actual
# PYTHONHOME.
# See https://github.com/bazelbuild/bazel/issues/23620
ctx.actions.symlink(output = executable, target_file = runtime.interpreter, is_executable = True)
else:
executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path))
ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path)
return [
toolchain,
DefaultInfo(
executable = executable,
runfiles = ctx.runfiles([executable], transitive_files = runtime.files),
),
]
current_interpreter_executable = rule(
implementation = _current_interpreter_executable_impl,
toolchains = [TARGET_TOOLCHAIN_TYPE],
executable = True,
)