Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c49bab3

Browse filesBrowse files
jheaff1f0rmiga
andauthored
Provide current_py_toolchain (bazel-contrib#731)
This commit introduces the current_py_toolchain rule, exposing the PYTHON2 and PYTHON3 "make" variables in bazel rules, analagous to @bazel_tools//tools/cpp:current_cc_toolchain. See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables Co-authored-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
1 parent e223da8 commit c49bab3
Copy full SHA for c49bab3

File tree

5 files changed

+85
-0
lines changed
Filter options

5 files changed

+85
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ After registration, your Python targets will use the toolchain's interpreter dur
8383
is still used to 'bootstrap' Python targets (see https://github.com/bazelbuild/rules_python/issues/691).
8484
You may also find some quirks while using this toolchain. Please refer to [python-build-standalone documentation's _Quirks_ section](https://python-build-standalone.readthedocs.io/en/latest/quirks.html) for details.
8585

86+
### Toolchain usage in other rules
87+
88+
Python toolchains can be utilised in other bazel rules, such as `genrule()`, by adding the `toolchains=["@rules_python//python:current_py_toolchain"]` attribute. The path to the python interpreter can be obtained by using the `$(PYTHON2)` and `$(PYTHON3)` ["Make" Variables](https://bazel.build/reference/be/make-variables). See the [`test_current_py_toolchain`](tests/load_from_macro/BUILD) target for an example.
89+
90+
8691
### "Hello World"
8792

8893
Once you've imported the rule set into your `WORKSPACE` using any of these

‎docs/python.md

Copy file name to clipboardExpand all lines: docs/python.md
+23Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎python/BUILD

Copy file name to clipboardExpand all lines: python/BUILD
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ In an ideal renaming, we'd move the packaging rules to a different package so
2424
that @rules_python//python is only concerned with the core rules.
2525
"""
2626

27+
load(":defs.bzl", "current_py_toolchain")
28+
2729
package(default_visibility = ["//visibility:public"])
2830

2931
licenses(["notice"]) # Apache 2.0
@@ -138,3 +140,7 @@ exports_files([
138140
"packaging.bzl",
139141
"pip.bzl",
140142
])
143+
144+
current_py_toolchain(
145+
name = "current_py_toolchain",
146+
)

‎python/defs.bzl

Copy file name to clipboardExpand all lines: python/defs.bzl
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,49 @@ def _add_tags(attrs):
4141
attrs["tags"] = [_MIGRATION_TAG]
4242
return attrs
4343

44+
def _current_py_toolchain_impl(ctx):
45+
toolchain = ctx.toolchains[ctx.attr._toolchain]
46+
47+
direct = []
48+
transitive = []
49+
vars = {}
50+
51+
if toolchain.py3_runtime and toolchain.py3_runtime.interpreter:
52+
direct.append(toolchain.py3_runtime.interpreter)
53+
transitive.append(toolchain.py3_runtime.files)
54+
vars["PYTHON3"] = toolchain.py3_runtime.interpreter.path
55+
56+
if toolchain.py2_runtime and toolchain.py2_runtime.interpreter:
57+
direct.append(toolchain.py2_runtime.interpreter)
58+
transitive.append(toolchain.py2_runtime.files)
59+
vars["PYTHON2"] = toolchain.py2_runtime.interpreter.path
60+
61+
files = depset(direct, transitive = transitive)
62+
return [
63+
toolchain,
64+
platform_common.TemplateVariableInfo(vars),
65+
DefaultInfo(
66+
runfiles = ctx.runfiles(transitive_files = files),
67+
files = files,
68+
),
69+
]
70+
71+
current_py_toolchain = rule(
72+
doc = """
73+
This rule exists so that the current python toolchain can be used in the `toolchains` attribute of
74+
other rules, such as genrule. It allows exposing a python toolchain after toolchain resolution has
75+
happened, to a rule which expects a concrete implementation of a toolchain, rather than a
76+
toolchain_type which could be resolved to that toolchain.
77+
""",
78+
implementation = _current_py_toolchain_impl,
79+
attrs = {
80+
"_toolchain": attr.string(default = str(Label("@bazel_tools//tools/python:toolchain_type"))),
81+
},
82+
toolchains = [
83+
str(Label("@bazel_tools//tools/python:toolchain_type")),
84+
],
85+
)
86+
4487
def py_library(**attrs):
4588
"""See the Bazel core [py_library](https://docs.bazel.build/versions/master/be/python.html#py_library) documentation.
4689

‎tests/load_from_macro/BUILD

Copy file name to clipboardExpand all lines: tests/load_from_macro/BUILD
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ py_library(
2424
# Allow a test to verify an "outside package" doesn't get included
2525
visibility = ["//examples/wheel:__pkg__"],
2626
)
27+
28+
genrule(
29+
name = "test_current_py_toolchain",
30+
srcs = [],
31+
outs = ["out.txt"],
32+
cmd = "$(PYTHON3) --version > $(location out.txt)",
33+
toolchains = ["//python:current_py_toolchain"],
34+
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.