blob: c217a3ac9d561e250a3e205f8675c341626b4341 [file] [log] [blame]
philwo00ec2e72019-06-24 05:05:50 -07001#!/bin/sh
2
brandjon861a7e12019-04-25 08:40:31 -07003# Don't set -e because we don't have robust trapping and printing of errors.
philwo00ec2e72019-06-24 05:05:50 -07004set -u
brandjon7166d4b2019-04-02 21:26:18 -07005
brandjon861a7e12019-04-25 08:40:31 -07006# We use /bin/sh rather than /bin/bash for portability. See discussion here:
brandjon7166d4b2019-04-02 21:26:18 -07007# https://groups.google.com/forum/?nomobile=true#!topic/bazel-dev/4Ql_7eDcLC0
8# We do lose the ability to set -o pipefail.
brandjon22994452019-03-26 13:37:31 -07009
philwo00ec2e72019-06-24 05:05:50 -070010STRICT="%STRICT%"
brandjon50fa3ec2019-06-06 15:15:39 -070011
12if [ "$STRICT" = "1" ]; then
13 FAILURE_HEADER="\
brandjon861a7e12019-04-25 08:40:31 -070014Error occurred while attempting to use the default Python toolchain \
15(@bazel_tools//tools/python:autodetecting_toolchain)."
brandjon50fa3ec2019-06-06 15:15:39 -070016else
17 FAILURE_HEADER="\
18Error occurred while attempting to use the non-strict autodetecting Python \
19toolchain (@bazel_tools//tools/python:autodetecting_toolchain_nonstrict)."
20fi
brandjon861a7e12019-04-25 08:40:31 -070021
22die() {
brandjon50fa3ec2019-06-06 15:15:39 -070023 echo "$FAILURE_HEADER" 1>&2
24 echo "$1" 1>&2
25 exit 1
brandjon861a7e12019-04-25 08:40:31 -070026}
brandjon22994452019-03-26 13:37:31 -070027
Keith Smileyddce7232019-05-21 15:08:42 -070028# We use `which` to locate the Python interpreter command on PATH. `command -v`
29# is another option, but it doesn't check whether the file it finds has the
30# executable bit.
31#
32# A tricky situation happens when this wrapper is invoked as part of running a
33# tool, e.g. passing a py_binary target to `ctx.actions.run()`. Bazel will unset
34# the PATH variable. Then the shell will see there's no PATH and initialize its
35# own, sometimes without exporting it. This causes `which` to fail and this
36# script to think there's no Python interpreter installed. To avoid this we
37# explicitly pass PATH to each `which` invocation. We can't just export PATH
38# because that would modify the environment seen by the final user Python
39# program.
40#
41# See also:
42#
43# https://github.com/bazelbuild/continuous-integration/issues/578
44# https://github.com/bazelbuild/bazel/issues/8414
45# https://github.com/bazelbuild/bazel/issues/8415
brandjon7f495312019-05-21 09:20:01 -070046
brandjon22994452019-03-26 13:37:31 -070047# Try the "python%VERSION%" command name first, then fall back on "python".
philwo00ec2e72019-06-24 05:05:50 -070048PYTHON_BIN="$(PATH="$PATH" which python%VERSION% 2> /dev/null)"
49USED_FALLBACK="0"
brandjon7166d4b2019-04-02 21:26:18 -070050if [ -z "${PYTHON_BIN:-}" ]; then
philwo00ec2e72019-06-24 05:05:50 -070051 PYTHON_BIN="$(PATH="$PATH" which python 2>/dev/null)"
52 USED_FALLBACK="1"
brandjon22994452019-03-26 13:37:31 -070053fi
brandjon7166d4b2019-04-02 21:26:18 -070054if [ -z "${PYTHON_BIN:-}" ]; then
brandjon50fa3ec2019-06-06 15:15:39 -070055 die "Neither 'python%VERSION%' nor 'python' were found on the target \
brandjon861a7e12019-04-25 08:40:31 -070056platform's PATH, which is:
57
58$PATH
59
60Please ensure an interpreter is available on this platform (and marked \
61executable), or else register an appropriate Python toolchain as per the \
62documentation for py_runtime_pair \
63(https://github.com/bazelbuild/bazel/blob/master/tools/python/toolchain.bzl)."
brandjon22994452019-03-26 13:37:31 -070064fi
65
brandjon052167e2019-06-04 16:04:06 -070066if [ "$STRICT" = "1" ]; then
67 # Verify that we grabbed an interpreter with the right version.
philwo00ec2e72019-06-24 05:05:50 -070068 VERSION_STR="$("$PYTHON_BIN" -V 2>&1)" \
brandjon052167e2019-06-04 16:04:06 -070069 || die "Could not get interpreter version via '$PYTHON_BIN -V'"
70 if ! echo "$VERSION_STR" | grep -q " %VERSION%\." ; then
71 die "According to '$PYTHON_BIN -V', version is '$VERSION_STR', but we \
brandjon861a7e12019-04-25 08:40:31 -070072need version %VERSION%. PATH is:
73
74$PATH
75
76Please ensure an interpreter with version %VERSION% is available on this \
77platform as 'python%VERSION%' or 'python', or else register an appropriate \
78Python toolchain as per the documentation for py_runtime_pair \
brandjon052167e2019-06-04 16:04:06 -070079(https://github.com/bazelbuild/bazel/blob/master/tools/python/toolchain.bzl).
80
81Note that prior to Bazel 0.27, there was no check to ensure that the \
82interpreter's version matched the version declared by the target (#4815). If \
83your build worked prior to Bazel 0.27, and you're sure your targets do not \
84require Python %VERSION%, you can opt out of this version check by using the \
85non-strict autodetecting toolchain instead of the standard autodetecting \
86toolchain. This can be done by passing the flag \
brandjon50fa3ec2019-06-06 15:15:39 -070087\`--extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain_nonstrict\` \
brandjon052167e2019-06-04 16:04:06 -070088on the command line or adding it to your bazelrc."
89 fi
brandjon22994452019-03-26 13:37:31 -070090fi
91
92exec "$PYTHON_BIN" "$@"