-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement autodetecting Python toolchain
This replaces the stub default Python toolchain with one that actually locates the target platform's Python interpreter at runtime. Try it out with bazel build //some_py_binary --experimental_use_python_toolchains and note that, unlike before (#4815), the correct Python interpreter gets invoked by default regardless of whether you specify `--python_version=PY2` or `--python_version=PY3`. This toolchain is only intended as a last resort, if the user doesn't define and register a better toolchain (that satisfies the target platform constraints). Work toward #7375 and #4815. Follow-up work needed to add a test (#7843) and windows support (#7844). RELNOTES: None PiperOrigin-RevId: 240417315
- Loading branch information
1 parent
fa161fa
commit 2299445
Showing
7 changed files
with
191 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/bin/bash | ||
|
||
# TODO(#7843): integration tests for failure to find python / wrong version | ||
# found / error while trying to print version | ||
|
||
set -euo pipefail | ||
|
||
GENERAL_FAILURE_MESSAGE="Error: The default python toolchain \ | ||
(@bazel_tools//tools/python:autodetecting_toolchain) was unable to locate a \ | ||
suitable Python interpreter on the target platform at execution time. Please \ | ||
register an appropriate Python toolchain. See the documentation for \ | ||
py_runtime_pair here: | ||
https://github.com/bazelbuild/bazel/blob/master/tools/python/toolchain.bzl." | ||
|
||
# Try the "python%VERSION%" command name first, then fall back on "python". | ||
PYTHON_BIN=$(which python%VERSION% || echo "") | ||
USED_FALLBACK=0 | ||
if [[ -z "${PYTHON_BIN:-}" ]]; then | ||
PYTHON_BIN=$(which python || echo "") | ||
USED_FALLBACK=1 | ||
fi | ||
if [[ -z "${PYTHON_BIN:-}" ]]; then | ||
echo "$GENERAL_FAILURE_MESSAGE" | ||
echo "Failure reason: Cannot locate 'python%VERSION%' or 'python' on the \ | ||
target platform's PATH, which is: | ||
$PATH" | ||
fi | ||
|
||
# Verify that we grabbed an interpreter with the right version. | ||
VERSION_STR=$("$PYTHON_BIN" -V 2>&1) | ||
if ! grep -q " %VERSION%\." <<< $VERSION_STR; then | ||
echo "$GENERAL_FAILURE_MESSAGE" | ||
echo "Failure reason: According to '$PYTHON_BIN -V', version is \ | ||
'$VERSION_STR', but we need version %VERSION%" | ||
fi | ||
|
||
exec "$PYTHON_BIN" "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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. | ||
|
||
"""Utilities for the @bazel_tools//tools/python package. | ||
This file does not access any Python-rules-specific logic, and is therefore | ||
less likely to be broken by Python-related changes. That in turn means this | ||
file is less likely to cause bootstrapping issues. | ||
""" | ||
|
||
def _expand_pyversion_template_impl(ctx): | ||
if ctx.outputs.out2: | ||
ctx.actions.expand_template( | ||
template = ctx.file.template, | ||
output = ctx.outputs.out2, | ||
substitutions = {"%VERSION%": "2"}, | ||
is_executable = True, | ||
) | ||
if ctx.outputs.out3: | ||
ctx.actions.expand_template( | ||
template = ctx.file.template, | ||
output = ctx.outputs.out3, | ||
substitutions = {"%VERSION%": "3"}, | ||
is_executable = True, | ||
) | ||
|
||
expand_pyversion_template = rule( | ||
implementation = _expand_pyversion_template_impl, | ||
attrs = { | ||
"template": attr.label( | ||
allow_single_file = True, | ||
doc = "The input template file.", | ||
), | ||
"out2": attr.output(doc = """\ | ||
The output file produced by substituting "%VERSION%" with "2"."""), | ||
"out3": attr.output(doc = """\ | ||
The output file produced by substituting "%VERSION%" with "3"."""), | ||
}, | ||
doc = """\ | ||
Given a template file, generates two expansions by replacing the substring | ||
"%VERSION%" with "2" and "3".""", | ||
) |