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
133 changes: 63 additions & 70 deletions docs/python.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion python/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ Core rules for building Python projects.
"""

load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements")
load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
load(
"//python/private:reexports.bzl",
"internal_PyInfo",
"internal_PyRuntimeInfo",
_py_binary = "py_binary",
_py_library = "py_library",
_py_runtime = "py_runtime",
_py_runtime_pair = "py_runtime_pair",
_py_test = "py_test",
)

Expand Down
84 changes: 84 additions & 0 deletions python/private/reexports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ different name. Then we can load it from defs.bzl and export it there under
the original name.
"""

load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")

# The implementation of the macros and tagging mechanism follows the example
# set by rules_cc and rules_java.

Expand Down Expand Up @@ -64,6 +66,8 @@ def py_library(**attrs):
Args:
**attrs: Rule attributes
"""
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")

# buildifier: disable=native-python
native.py_library(**_add_tags(attrs))
Expand All @@ -74,6 +78,10 @@ def py_binary(**attrs):
Args:
**attrs: Rule attributes
"""
if attrs.get("python_version") == "PY2":
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")

# buildifier: disable=native-python
native.py_binary(**_add_tags(attrs))
Expand All @@ -84,6 +92,10 @@ def py_test(**attrs):
Args:
**attrs: Rule attributes
"""
if attrs.get("python_version") == "PY2":
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")

# buildifier: disable=native-python
native.py_test(**_add_tags(attrs))
Expand All @@ -94,6 +106,78 @@ def py_runtime(**attrs):
Args:
**attrs: Rule attributes
"""
if attrs.get("python_version") == "PY2":
fail("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")

# buildifier: disable=native-python
native.py_runtime(**_add_tags(attrs))

# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
# doc generator gives useful API docs.
def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
"""A toolchain rule for Python.

This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
However, Python 2 is no longer supported, so it now only wraps a single Python 3
runtime.

Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
rule returning a `PyRuntimeInfo` provider may be used.

This rule returns a `platform_common.ToolchainInfo` provider with the following
schema:

```python
platform_common.ToolchainInfo(
py2_runtime = None,
py3_runtime = <PyRuntimeInfo or None>,
)
```

Example usage:

```python
# In your BUILD file...

load("@rules_python//python:defs.bzl", "py_runtime_pair")

py_runtime(
name = "my_py3_runtime",
interpreter_path = "/system/python3",
python_version = "PY3",
)

py_runtime_pair(
name = "my_py_runtime_pair",
py3_runtime = ":my_py3_runtime",
)

toolchain(
name = "my_toolchain",
target_compatible_with = <...>,
toolchain = ":my_py_runtime_pair",
toolchain_type = "@rules_python//python:toolchain_type",
)
```

```python
# In your WORKSPACE...

register_toolchains("//my_pkg:my_toolchain")
```

Args:
name: str, the name of the target
py2_runtime: optional Label; must be unset or None; an error is raised
otherwise.
py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
**attrs: Extra attrs passed onto the native rule
"""
if attrs.get("py2_runtime"):
fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
_py_runtime_pair(
name = name,
py2_runtime = py2_runtime,
py3_runtime = py3_runtime,
**attrs
)