Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(toolchain): Add new make vars for Python interpreter path compliant with --no_legacy_external_runfiles #2772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Unreleased changes template.
* (toolchains) Local Python installs can be used to create a toolchain
equivalent to the standard toolchains. See [Local toolchains] docs for how to
configure them.
* (toolchains) Expose `$(PYTHON2_ROOTPATH)` and `$(PYTHON3_ROOTPATH)` which are runfiles
locations equivalents of `$(PYTHON2)` and `$(PYTHON3) respectively.


{#v0-0-0-removed}
Expand Down
6 changes: 5 additions & 1 deletion docs/toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ attribute. You can obtain the path to the Python interpreter using the
`$(PYTHON2)` and `$(PYTHON3)` ["Make"
Variables](https://bazel.build/reference/be/make-variables). See the
{gh-path}`test_current_py_toolchain <tests/load_from_macro/BUILD.bazel>` target
for an example.
for an example. We also make available `$(PYTHON2_ROOTPATH)` and `$(PYTHON3_ROOTPATH)`
which are Make Variable equivalents of `$(PYTHON2)` and `$(PYTHON3)` but for runfiles
locations. These will be helpful if you need to set env vars of binary/test rules
while using [`--nolegacy_external_runfiles`](https://bazel.build/reference/command-line-reference#flag--legacy_external_runfiles).
The original make variables still work in exec contexts such as genrules.

### Overriding toolchain defaults and adding more versions

Expand Down
7 changes: 7 additions & 0 deletions python/current_py_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ def _current_py_toolchain_impl(ctx):
direct.append(toolchain.py3_runtime.interpreter)
transitive.append(toolchain.py3_runtime.files)
vars["PYTHON3"] = toolchain.py3_runtime.interpreter.path
vars["PYTHON3_ROOTPATH"] = toolchain.py3_runtime.interpreter.short_path

if toolchain.py2_runtime and toolchain.py2_runtime.interpreter:
direct.append(toolchain.py2_runtime.interpreter)
transitive.append(toolchain.py2_runtime.files)
vars["PYTHON2"] = toolchain.py2_runtime.interpreter.path
vars["PYTHON2_ROOTPATH"] = toolchain.py2_runtime.interpreter.short_path

files = depset(direct, transitive = transitive)
return [
Expand All @@ -49,6 +51,11 @@ current_py_toolchain = rule(
other rules, such as genrule. It allows exposing a python toolchain after toolchain resolution has
happened, to a rule which expects a concrete implementation of a toolchain, rather than a
toolchain_type which could be resolved to that toolchain.
:::{versionchanged} VERSION_NEXT_FEATURE
From now on, we also expose `$(PYTHON2_ROOTPATH)` and `$(PYTHON3_ROOTPATH)` which are runfiles
locations equivalents of `$(PYTHON2)` and `$(PYTHON3) respectively.
:::
""",
implementation = _current_py_toolchain_impl,
attrs = {
Expand Down