Skip to content
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
1 change: 1 addition & 0 deletions py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bzl_library(
srcs = ["repositories.bzl"],
visibility = ["//visibility:public"],
deps = [
"//py/private/toolchain:autodetecting",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
Expand Down
12 changes: 12 additions & 0 deletions py/private/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

exports_files(
["python.sh"],
visibility = ["//visibility:public"],
)

bzl_library(
name = "autodetecting",
srcs = ["autodetecting.bzl"],
visibility = ["//py:__subpackages__"],
)
62 changes: 62 additions & 0 deletions py/private/toolchain/autodetecting.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
def _autodetecting_py_wrapper_impl(rctx):
which_python = rctx.which("python3")
if which_python == None:
fail("Unable to locate 'python3' on the path")

rctx.template(
"python.sh",
rctx.attr._python_wrapper_tmpl,
substitutions = {
"{{PYTHON_BIN}}": str(which_python),
},
executable = True,
)

build_content = """\
load("@rules_python//python:defs.bzl", "py_runtime", "py_runtime_pair")

py_runtime(
name = "autodecting_python3_runtime",
interpreter = "@{name}//:python.sh",
python_version = "PY3",
)

py_runtime_pair(
name = "autodetecting_py_runtime_pair",
py2_runtime = None,
py3_runtime = ":autodecting_python3_runtime",
)

toolchain(
name = "py_toolchain",
toolchain = ":autodetecting_py_runtime_pair",
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)
""".format(
name = rctx.attr.name,
)
rctx.file("BUILD.bazel", content = build_content)

_autodetecting_py_toolchain = repository_rule(
implementation = _autodetecting_py_wrapper_impl,
attrs = {
"_python_wrapper_tmpl": attr.label(
default = "@rules_py//py/private/toolchain:python.sh",
),
},
)

def register_autodetecting_python_toolchain(name):
"""Registers a Python toolchain that will auto detect the location of Python that can be used with rules_py.

The autodetecting Python toolchain replaces the automatically registered one under bazel, and correctly handles the
Python virtual environment indirection created by rules_py. However it is recommended to instead use one of the prebuilt
Python interpreter toolchains from rules_python, rather than rely on the the correct Python binary being present on the host.
"""
_autodetecting_py_toolchain(
name = name,
)

native.register_toolchains(
"@%s//:py_toolchain" % name,
)
15 changes: 15 additions & 0 deletions py/private/toolchain/python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -o errexit -o nounset

PYTHON_BIN="{{PYTHON_BIN}}"

# We need to ensure that we exec Python from within the venv path if set so that the correct
# exec and base prefixes are set.
if [ -z "${VIRTUAL_ENV:-}" ]; then
exec "${PYTHON_BIN}" "$@"
else
PYTHON_REAL="${VIRTUAL_ENV}/bin/python_real"
ln -snf "${PYTHON_BIN}" "${PYTHON_REAL}"
exec "${PYTHON_REAL}" "$@"
fi
3 changes: 3 additions & 0 deletions py/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ See https://docs.bazel.build/versions/main/skylark/deploying.html#dependencies

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//py/private/toolchain:autodetecting.bzl", _register_autodetecting_python_toolchain = "register_autodetecting_python_toolchain")

register_autodetecting_python_toolchain = _register_autodetecting_python_toolchain

# WARNING: any changes in this function may be BREAKING CHANGES for users
# because we'll fetch a dependency which may be different from one that
Expand Down