Skip to content

Commit 5a856e5

Browse files
authored
feat: add //python:none as public target to disable exec_interpreter (bazel-contrib#2226)
When writing the toolchain docs, I realized there wasn't a public target to use for disabling the exec_interpreter. Fixed by adding an alias to the internal target. Along the way: * Add the exec tools and cc toolchains to the doc gen * A few improvements to the cc/exec tools docs * Add public bzl file for py_exec_tools_toolchain and PyExecToolsInfo * Fix a bug in sphinx_bzl where local names were hiding global names even if the requested type didn't match (e.g. a macro foo referring to rule foo) * Fix xrefs in the python/cc/index.md; it wasn't setting the default domain to bzl * Fix object type definition for attributes: the object type name was "attribute", but everything else was using "attr"; switched to "attr"
1 parent 63114a3 commit 5a856e5

16 files changed

+175
-61
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ A brief description of the categories of changes:
7373
`TOOL_VERSIONS` for registering patched toolchains please consider setting
7474
the `patch_strip` explicitly to `1` if you depend on this value - in the
7575
future the value may change to default to `0`.
76-
76+
* (toolchains) Added `//python:none`, a special target for use with
77+
{obj}`py_exec_tools_toolchain.exec_interpreter` to treat the value as `None`.
7778

7879
### Removed
7980
* (toolchains): Removed accidentally exposed `http_archive` symbol from

docs/BUILD.bazel

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ sphinx_docs(
7272
deps = [
7373
":bzl_api_docs",
7474
":py_api_srcs",
75-
":py_cc_toolchain",
7675
":py_runtime_pair",
7776
"//sphinxdocs/docs:docs_lib",
7877
],
@@ -86,16 +85,18 @@ sphinx_stardocs(
8685
"//python:pip_bzl",
8786
"//python:py_binary_bzl",
8887
"//python:py_cc_link_params_info_bzl",
88+
"//python:py_exec_tools_info_bzl",
89+
"//python:py_exec_tools_toolchain_bzl",
8990
"//python:py_executable_info_bzl",
9091
"//python:py_library_bzl",
9192
"//python:py_runtime_bzl",
9293
"//python:py_runtime_info_bzl",
9394
"//python:py_test_bzl",
9495
"//python:repositories_bzl",
96+
"//python/cc:py_cc_toolchain_bzl",
9597
"//python/cc:py_cc_toolchain_info_bzl",
9698
"//python/entry_points:py_console_script_binary_bzl",
97-
"//python/private:py_exec_tools_info_bzl",
98-
"//python/private:py_exec_tools_toolchain_bzl",
99+
"//python/private:py_cc_toolchain_rule_bzl",
99100
"//python/private/common:py_binary_rule_bazel_bzl",
100101
"//python/private/common:py_library_rule_bazel_bzl",
101102
"//python/private/common:py_runtime_rule_bzl",
@@ -112,16 +113,6 @@ sphinx_stardocs(
112113
target_compatible_with = _TARGET_COMPATIBLE_WITH,
113114
)
114115

115-
sphinx_stardoc(
116-
name = "py_cc_toolchain",
117-
src = "//python/private:py_cc_toolchain_rule.bzl",
118-
prefix = "api/rules_python/",
119-
public_load_path = "//python/cc:py_cc_toolchain.bzl",
120-
tags = ["docs"],
121-
target_compatible_with = _TARGET_COMPATIBLE_WITH,
122-
deps = ["//python/cc:py_cc_toolchain_bzl"],
123-
)
124-
125116
sphinx_stardoc(
126117
name = "py_runtime_pair",
127118
src = "//python/private:py_runtime_pair_rule_bzl",

docs/api/rules_python/python/cc/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:::{default-domain} bzl
2+
:::
13
:::{bzl:currentfile} //python/cc:BUILD.bazel
24
:::
35
# //python/cc
@@ -31,4 +33,9 @@ This target provides:
3133
Toolchain type identifier for the Python C toolchain.
3234

3335
This toolchain type is typically implemented by {obj}`py_cc_toolchain`.
36+
37+
::::{seealso}
38+
{any}`Custom Toolchains` for how to define custom toolchains
39+
::::
40+
3441
:::

docs/api/rules_python/python/index.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Identifier for the toolchain type for the target platform.
1111

1212
This toolchain type gives information about the runtime for the target platform.
13-
It is typically implemented by the {obj}`py_runtime` rule
13+
It is typically implemented by the {obj}`py_runtime` rule.
1414

1515
::::{seealso}
1616
{any}`Custom Toolchains` for how to define custom toolchains
@@ -21,14 +21,22 @@ It is typically implemented by the {obj}`py_runtime` rule
2121
:::{bzl:target} exec_tools_toolchain_type
2222

2323
Identifier for the toolchain type for exec tools used to build Python targets.
24+
25+
This toolchain type gives information about tools needed to build Python targets
26+
at build time. It is typically implemented by the {obj}`py_exec_tools_toolchain`
27+
rule.
28+
29+
::::{seealso}
30+
{any}`Custom Toolchains` for how to define custom toolchains
31+
::::
2432
:::
2533

2634
:::{bzl:target} current_py_toolchain
2735

2836
Helper target to resolve to the consumer's current Python toolchain. This target
2937
provides:
3038

31-
* `PyRuntimeInfo`: The consuming target's target toolchain information
39+
* {obj}`PyRuntimeInfo`: The consuming target's target toolchain information
3240

3341
:::
3442

@@ -42,3 +50,16 @@ Use {obj}`@rules_python//python/runtime_env_toolchains:all` instead.
4250
:::
4351
::::
4452

53+
:::{target} none
54+
A special target so that label attributes with default values can be set to
55+
`None`.
56+
57+
Bazel interprets `None` to mean "use the default value", which
58+
makes it impossible to have a label attribute with a default value that is
59+
optional. To work around this, a target with a special provider is used;
60+
internally rules check for this, then treat the value as `None`.
61+
62+
::::{versionadded} 0.36.0
63+
::::
64+
65+
:::

docs/toolchains.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ py_cc_toolchain(
395395
396396
py_exec_tools_toolchain(
397397
name = "exec_tools_toolchain_impl",
398-
exec_interpreter = "@rules_python/python:null_target",
398+
exec_interpreter = "@rules_python/python:none",
399399
precompiler = "precompiler-cpython-3.12"
400400
)
401401

python/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ bzl_library(
139139
],
140140
)
141141

142+
bzl_library(
143+
name = "py_exec_tools_info_bzl",
144+
srcs = ["py_exec_tools_info.bzl"],
145+
deps = ["//python/private:py_exec_tools_info_bzl"],
146+
)
147+
148+
bzl_library(
149+
name = "py_exec_tools_toolchain_bzl",
150+
srcs = ["py_exec_tools_toolchain.bzl"],
151+
deps = ["//python/private:py_exec_tools_toolchain_bzl"],
152+
)
153+
142154
bzl_library(
143155
name = "py_executable_info_bzl",
144156
srcs = ["py_executable_info.bzl"],
@@ -308,6 +320,12 @@ toolchain_type(
308320
visibility = ["//visibility:public"],
309321
)
310322

323+
# Special target to indicate `None` for label attributes a default value.
324+
alias(
325+
name = "none",
326+
actual = "//python/private:sentinel",
327+
)
328+
311329
# Definitions for a Python toolchain that, at execution time, attempts to detect
312330
# a platform runtime having the appropriate major Python version. Consider this
313331
# a toolchain of last resort.

python/cc/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bzl_library(
4040
name = "py_cc_toolchain_bzl",
4141
srcs = ["py_cc_toolchain.bzl"],
4242
visibility = ["//visibility:public"],
43-
deps = ["//python/private:py_cc_toolchain_bzl"],
43+
deps = ["//python/private:py_cc_toolchain_macro_bzl"],
4444
)
4545

4646
bzl_library(

python/cc/py_cc_toolchain_info.bzl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023 The Bazel Authors. All rights reserved.
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -11,11 +11,12 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
"""Provider for C/C++ information from the toolchain.
1415
15-
"""Provider for C/C++ information about the Python runtime.
16-
17-
NOTE: This is a beta-quality feature. APIs subject to change until
18-
https://github.com/bazelbuild/rules_python/issues/824 is considered done.
16+
:::{seealso}
17+
* {any}`Custom toolchains` for how to define custom toolchains.
18+
* {obj}`py_cc_toolchain` rule for defining the toolchain.
19+
:::
1920
"""
2021

2122
load("//python/private:py_cc_toolchain_info.bzl", _PyCcToolchainInfo = "PyCcToolchainInfo")

python/private/BUILD.bazel

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,16 @@ bzl_library(
168168
)
169169

170170
bzl_library(
171-
name = "py_cc_toolchain_bzl",
172-
srcs = [
173-
"py_cc_toolchain_macro.bzl",
174-
"py_cc_toolchain_rule.bzl",
175-
],
176-
visibility = [
177-
"//docs:__subpackages__",
178-
"//python/cc:__pkg__",
171+
name = "py_cc_toolchain_macro_bzl",
172+
srcs = ["py_cc_toolchain_macro.bzl"],
173+
deps = [
174+
":py_cc_toolchain_rule_bzl",
179175
],
176+
)
177+
178+
bzl_library(
179+
name = "py_cc_toolchain_rule_bzl",
180+
srcs = ["py_cc_toolchain_rule.bzl"],
180181
deps = [
181182
":py_cc_toolchain_info_bzl",
182183
":rules_cc_srcs_bzl",
@@ -188,7 +189,6 @@ bzl_library(
188189
bzl_library(
189190
name = "py_cc_toolchain_info_bzl",
190191
srcs = ["py_cc_toolchain_info.bzl"],
191-
visibility = ["//python/cc:__pkg__"],
192192
)
193193

194194
bzl_library(
@@ -370,7 +370,6 @@ exports_files(
370370
[
371371
"coverage.patch",
372372
"repack_whl.py",
373-
"py_cc_toolchain_rule.bzl",
374373
"py_package.bzl",
375374
"py_wheel.bzl",
376375
"py_wheel_normalize_pep440.bzl",

python/private/py_cc_toolchain_macro.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ load(":util.bzl", "add_tag")
2222
def py_cc_toolchain(**kwargs):
2323
"""Creates a py_cc_toolchain target.
2424
25+
This is a macro around the {rule}`py_cc_toolchain` rule.
26+
2527
Args:
26-
**kwargs: Keyword args to pass onto underlying rule.
28+
**kwargs: Keyword args to pass onto underlying {rule}`py_cc_toolchain` rule.
2729
"""
2830

2931
# This tag is added to easily identify usages through other macros.

0 commit comments

Comments
 (0)