philwo | 00ec2e7 | 2019-06-24 05:05:50 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
brandjon | 861a7e1 | 2019-04-25 08:40:31 -0700 | [diff] [blame] | 3 | # Don't set -e because we don't have robust trapping and printing of errors. |
philwo | 00ec2e7 | 2019-06-24 05:05:50 -0700 | [diff] [blame] | 4 | set -u |
brandjon | 7166d4b | 2019-04-02 21:26:18 -0700 | [diff] [blame] | 5 | |
brandjon | 861a7e1 | 2019-04-25 08:40:31 -0700 | [diff] [blame] | 6 | # We use /bin/sh rather than /bin/bash for portability. See discussion here: |
brandjon | 7166d4b | 2019-04-02 21:26:18 -0700 | [diff] [blame] | 7 | # https://groups.google.com/forum/?nomobile=true#!topic/bazel-dev/4Ql_7eDcLC0 |
| 8 | # We do lose the ability to set -o pipefail. |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 9 | |
philwo | 00ec2e7 | 2019-06-24 05:05:50 -0700 | [diff] [blame] | 10 | STRICT="%STRICT%" |
brandjon | 50fa3ec | 2019-06-06 15:15:39 -0700 | [diff] [blame] | 11 | |
| 12 | if [ "$STRICT" = "1" ]; then |
| 13 | FAILURE_HEADER="\ |
brandjon | 861a7e1 | 2019-04-25 08:40:31 -0700 | [diff] [blame] | 14 | Error occurred while attempting to use the default Python toolchain \ |
| 15 | (@bazel_tools//tools/python:autodetecting_toolchain)." |
brandjon | 50fa3ec | 2019-06-06 15:15:39 -0700 | [diff] [blame] | 16 | else |
| 17 | FAILURE_HEADER="\ |
| 18 | Error occurred while attempting to use the non-strict autodetecting Python \ |
| 19 | toolchain (@bazel_tools//tools/python:autodetecting_toolchain_nonstrict)." |
| 20 | fi |
brandjon | 861a7e1 | 2019-04-25 08:40:31 -0700 | [diff] [blame] | 21 | |
| 22 | die() { |
brandjon | 50fa3ec | 2019-06-06 15:15:39 -0700 | [diff] [blame] | 23 | echo "$FAILURE_HEADER" 1>&2 |
| 24 | echo "$1" 1>&2 |
| 25 | exit 1 |
brandjon | 861a7e1 | 2019-04-25 08:40:31 -0700 | [diff] [blame] | 26 | } |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 27 | |
Keith Smiley | ddce723 | 2019-05-21 15:08:42 -0700 | [diff] [blame] | 28 | # 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 |
brandjon | 7f49531 | 2019-05-21 09:20:01 -0700 | [diff] [blame] | 46 | |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 47 | # Try the "python%VERSION%" command name first, then fall back on "python". |
philwo | 00ec2e7 | 2019-06-24 05:05:50 -0700 | [diff] [blame] | 48 | PYTHON_BIN="$(PATH="$PATH" which python%VERSION% 2> /dev/null)" |
| 49 | USED_FALLBACK="0" |
brandjon | 7166d4b | 2019-04-02 21:26:18 -0700 | [diff] [blame] | 50 | if [ -z "${PYTHON_BIN:-}" ]; then |
philwo | 00ec2e7 | 2019-06-24 05:05:50 -0700 | [diff] [blame] | 51 | PYTHON_BIN="$(PATH="$PATH" which python 2>/dev/null)" |
| 52 | USED_FALLBACK="1" |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 53 | fi |
brandjon | 7166d4b | 2019-04-02 21:26:18 -0700 | [diff] [blame] | 54 | if [ -z "${PYTHON_BIN:-}" ]; then |
brandjon | 50fa3ec | 2019-06-06 15:15:39 -0700 | [diff] [blame] | 55 | die "Neither 'python%VERSION%' nor 'python' were found on the target \ |
brandjon | 861a7e1 | 2019-04-25 08:40:31 -0700 | [diff] [blame] | 56 | platform's PATH, which is: |
| 57 | |
| 58 | $PATH |
| 59 | |
| 60 | Please ensure an interpreter is available on this platform (and marked \ |
| 61 | executable), or else register an appropriate Python toolchain as per the \ |
| 62 | documentation for py_runtime_pair \ |
| 63 | (https://github.com/bazelbuild/bazel/blob/master/tools/python/toolchain.bzl)." |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 64 | fi |
| 65 | |
brandjon | 052167e | 2019-06-04 16:04:06 -0700 | [diff] [blame] | 66 | if [ "$STRICT" = "1" ]; then |
| 67 | # Verify that we grabbed an interpreter with the right version. |
philwo | 00ec2e7 | 2019-06-24 05:05:50 -0700 | [diff] [blame] | 68 | VERSION_STR="$("$PYTHON_BIN" -V 2>&1)" \ |
brandjon | 052167e | 2019-06-04 16:04:06 -0700 | [diff] [blame] | 69 | || 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 \ |
brandjon | 861a7e1 | 2019-04-25 08:40:31 -0700 | [diff] [blame] | 72 | need version %VERSION%. PATH is: |
| 73 | |
| 74 | $PATH |
| 75 | |
| 76 | Please ensure an interpreter with version %VERSION% is available on this \ |
| 77 | platform as 'python%VERSION%' or 'python', or else register an appropriate \ |
| 78 | Python toolchain as per the documentation for py_runtime_pair \ |
brandjon | 052167e | 2019-06-04 16:04:06 -0700 | [diff] [blame] | 79 | (https://github.com/bazelbuild/bazel/blob/master/tools/python/toolchain.bzl). |
| 80 | |
| 81 | Note that prior to Bazel 0.27, there was no check to ensure that the \ |
| 82 | interpreter's version matched the version declared by the target (#4815). If \ |
| 83 | your build worked prior to Bazel 0.27, and you're sure your targets do not \ |
| 84 | require Python %VERSION%, you can opt out of this version check by using the \ |
| 85 | non-strict autodetecting toolchain instead of the standard autodetecting \ |
| 86 | toolchain. This can be done by passing the flag \ |
brandjon | 50fa3ec | 2019-06-06 15:15:39 -0700 | [diff] [blame] | 87 | \`--extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain_nonstrict\` \ |
brandjon | 052167e | 2019-06-04 16:04:06 -0700 | [diff] [blame] | 88 | on the command line or adding it to your bazelrc." |
| 89 | fi |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 90 | fi |
| 91 | |
| 92 | exec "$PYTHON_BIN" "$@" |