Skip to content

Commit 3b9c85e

Browse files
authored
cleanup: factor reexports.bzl into the respective implementation files (#1137)
This helps avoid loading one rule requiring loading everything. Within Google, this makes some Starlark testing frameworks avoid having to mock far away dependencies of dependencies.
1 parent c394c46 commit 3b9c85e

File tree

10 files changed

+159
-162
lines changed

10 files changed

+159
-162
lines changed

python/BUILD.bazel

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ bzl_library(
5656
visibility = ["//visibility:public"],
5757
deps = [
5858
":current_py_toolchain_bzl",
59+
":py_binary_bzl",
5960
":py_import_bzl",
61+
":py_info_bzl",
62+
":py_library_bzl",
63+
":py_runtime_bzl",
64+
":py_runtime_info_bzl",
65+
":py_runtime_pair_bzl",
66+
":py_test_bzl",
6067
"//python/private:bazel_tools_bzl",
61-
"//python/private:reexports_bzl",
6268
],
6369
)
6470

@@ -76,7 +82,7 @@ bzl_library(
7682
bzl_library(
7783
name = "py_binary_bzl",
7884
srcs = ["py_binary.bzl"],
79-
deps = ["//python/private:reexports_bzl"],
85+
deps = ["//python/private:util_bzl"],
8086
)
8187

8288
bzl_library(
@@ -99,19 +105,19 @@ bzl_library(
99105
bzl_library(
100106
name = "py_library_bzl",
101107
srcs = ["py_library.bzl"],
102-
deps = ["//python/private:reexports_bzl"],
108+
deps = ["//python/private:util_bzl"],
103109
)
104110

105111
bzl_library(
106112
name = "py_runtime_bzl",
107113
srcs = ["py_runtime.bzl"],
108-
deps = ["//python/private:reexports_bzl"],
114+
deps = ["//python/private:util_bzl"],
109115
)
110116

111117
bzl_library(
112118
name = "py_runtime_pair_bzl",
113119
srcs = ["py_runtime_pair.bzl"],
114-
deps = ["//python/private:reexports_bzl"],
120+
deps = ["//python/private:bazel_tools_bzl"],
115121
)
116122

117123
bzl_library(
@@ -123,7 +129,7 @@ bzl_library(
123129
bzl_library(
124130
name = "py_test_bzl",
125131
srcs = ["py_test.bzl"],
126-
deps = ["//python/private:reexports_bzl"],
132+
deps = ["//python/private:util_bzl"],
127133
)
128134

129135
# NOTE: Remember to add bzl_library targets to //tests:bzl_libraries

python/defs.bzl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
"""Core rules for building Python projects."""
1515

1616
load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements")
17-
load(
18-
"//python/private:reexports.bzl",
19-
"internal_PyInfo",
20-
"internal_PyRuntimeInfo",
21-
_py_binary = "py_binary",
22-
_py_library = "py_library",
23-
_py_runtime = "py_runtime",
24-
_py_runtime_pair = "py_runtime_pair",
25-
_py_test = "py_test",
26-
)
17+
load("//python:py_binary.bzl", _py_binary = "py_binary")
18+
load("//python:py_info.bzl", internal_PyInfo = "PyInfo")
19+
load("//python:py_library.bzl", _py_library = "py_library")
20+
load("//python:py_runtime.bzl", _py_runtime = "py_runtime")
21+
load("//python:py_runtime_info.bzl", internal_PyRuntimeInfo = "PyRuntimeInfo")
22+
load("//python:py_runtime_pair.bzl", _py_runtime_pair = "py_runtime_pair")
23+
load("//python:py_test.bzl", _py_test = "py_test")
2724
load(":current_py_toolchain.bzl", _current_py_toolchain = "current_py_toolchain")
2825
load(":py_import.bzl", _py_import = "py_import")
2926

python/private/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ bzl_library(
4141
deps = [":bazel_tools_bzl"],
4242
)
4343

44+
bzl_library(
45+
name = "util_bzl",
46+
srcs = ["util.bzl"],
47+
visibility = ["//python:__subpackages__"],
48+
)
49+
4450
# @bazel_tools can't define bzl_library itself, so we just put a wrapper around it.
4551
bzl_library(
4652
name = "bazel_tools_bzl",

python/private/reexports.bzl

Lines changed: 0 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ different name. Then we can load it from defs.bzl and export it there under
3737
the original name.
3838
"""
3939

40-
load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
41-
42-
# The implementation of the macros and tagging mechanism follows the example
43-
# set by rules_cc and rules_java.
44-
45-
_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
46-
47-
def _add_tags(attrs):
48-
if "tags" in attrs and attrs["tags"] != None:
49-
attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
50-
else:
51-
attrs["tags"] = [_MIGRATION_TAG]
52-
return attrs
53-
5440
# Don't use underscore prefix, since that would make the symbol local to this
5541
# file only. Use a non-conventional name to emphasize that this is not a public
5642
# symbol.
@@ -59,125 +45,3 @@ internal_PyInfo = PyInfo
5945

6046
# buildifier: disable=name-conventions
6147
internal_PyRuntimeInfo = PyRuntimeInfo
62-
63-
def py_library(**attrs):
64-
"""See the Bazel core [py_library](https://docs.bazel.build/versions/master/be/python.html#py_library) documentation.
65-
66-
Args:
67-
**attrs: Rule attributes
68-
"""
69-
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
70-
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
71-
72-
# buildifier: disable=native-python
73-
native.py_library(**_add_tags(attrs))
74-
75-
def py_binary(**attrs):
76-
"""See the Bazel core [py_binary](https://docs.bazel.build/versions/master/be/python.html#py_binary) documentation.
77-
78-
Args:
79-
**attrs: Rule attributes
80-
"""
81-
if attrs.get("python_version") == "PY2":
82-
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
83-
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
84-
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
85-
86-
# buildifier: disable=native-python
87-
native.py_binary(**_add_tags(attrs))
88-
89-
def py_test(**attrs):
90-
"""See the Bazel core [py_test](https://docs.bazel.build/versions/master/be/python.html#py_test) documentation.
91-
92-
Args:
93-
**attrs: Rule attributes
94-
"""
95-
if attrs.get("python_version") == "PY2":
96-
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
97-
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
98-
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
99-
100-
# buildifier: disable=native-python
101-
native.py_test(**_add_tags(attrs))
102-
103-
def py_runtime(**attrs):
104-
"""See the Bazel core [py_runtime](https://docs.bazel.build/versions/master/be/python.html#py_runtime) documentation.
105-
106-
Args:
107-
**attrs: Rule attributes
108-
"""
109-
if attrs.get("python_version") == "PY2":
110-
fail("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
111-
112-
# buildifier: disable=native-python
113-
native.py_runtime(**_add_tags(attrs))
114-
115-
# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
116-
# doc generator gives useful API docs.
117-
def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
118-
"""A toolchain rule for Python.
119-
120-
This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
121-
However, Python 2 is no longer supported, so it now only wraps a single Python 3
122-
runtime.
123-
124-
Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
125-
rule returning a `PyRuntimeInfo` provider may be used.
126-
127-
This rule returns a `platform_common.ToolchainInfo` provider with the following
128-
schema:
129-
130-
```python
131-
platform_common.ToolchainInfo(
132-
py2_runtime = None,
133-
py3_runtime = <PyRuntimeInfo or None>,
134-
)
135-
```
136-
137-
Example usage:
138-
139-
```python
140-
# In your BUILD file...
141-
142-
load("@rules_python//python:defs.bzl", "py_runtime_pair")
143-
144-
py_runtime(
145-
name = "my_py3_runtime",
146-
interpreter_path = "/system/python3",
147-
python_version = "PY3",
148-
)
149-
150-
py_runtime_pair(
151-
name = "my_py_runtime_pair",
152-
py3_runtime = ":my_py3_runtime",
153-
)
154-
155-
toolchain(
156-
name = "my_toolchain",
157-
target_compatible_with = <...>,
158-
toolchain = ":my_py_runtime_pair",
159-
toolchain_type = "@rules_python//python:toolchain_type",
160-
)
161-
```
162-
163-
```python
164-
# In your WORKSPACE...
165-
166-
register_toolchains("//my_pkg:my_toolchain")
167-
```
168-
169-
Args:
170-
name: str, the name of the target
171-
py2_runtime: optional Label; must be unset or None; an error is raised
172-
otherwise.
173-
py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
174-
**attrs: Extra attrs passed onto the native rule
175-
"""
176-
if attrs.get("py2_runtime"):
177-
fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
178-
_py_runtime_pair(
179-
name = name,
180-
py2_runtime = py2_runtime,
181-
py3_runtime = py3_runtime,
182-
**attrs
183-
)

python/private/util.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ def copy_propagating_kwargs(from_kwargs, into_kwargs = None):
2929
if attr in from_kwargs and attr not in into_kwargs:
3030
into_kwargs[attr] = from_kwargs[attr]
3131
return into_kwargs
32+
33+
# The implementation of the macros and tagging mechanism follows the example
34+
# set by rules_cc and rules_java.
35+
36+
_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
37+
38+
def add_migration_tag(attrs):
39+
if "tags" in attrs and attrs["tags"] != None:
40+
attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
41+
else:
42+
attrs["tags"] = [_MIGRATION_TAG]
43+
return attrs

python/py_binary.bzl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414

1515
"""Public entry point for py_binary."""
1616

17-
load("//python/private:reexports.bzl", _py_binary = "py_binary")
17+
load("//python/private:util.bzl", "add_migration_tag")
1818

19-
py_binary = _py_binary
19+
def py_binary(**attrs):
20+
"""See the Bazel core [py_binary](https://docs.bazel.build/versions/master/be/python.html#py_binary) documentation.
21+
22+
Args:
23+
**attrs: Rule attributes
24+
"""
25+
if attrs.get("python_version") == "PY2":
26+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
27+
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
28+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
29+
30+
# buildifier: disable=native-python
31+
native.py_binary(**add_migration_tag(attrs))

python/py_library.bzl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414

1515
"""Public entry point for py_library."""
1616

17-
load("//python/private:reexports.bzl", _py_library = "py_library")
17+
load("//python/private:util.bzl", "add_migration_tag")
1818

19-
py_library = _py_library
19+
def py_library(**attrs):
20+
"""See the Bazel core [py_library](https://docs.bazel.build/versions/master/be/python.html#py_library) documentation.
21+
22+
Args:
23+
**attrs: Rule attributes
24+
"""
25+
if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
26+
fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
27+
28+
# buildifier: disable=native-python
29+
native.py_library(**add_migration_tag(attrs))

python/py_runtime.bzl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414

1515
"""Public entry point for py_runtime."""
1616

17-
load("//python/private:reexports.bzl", _py_runtime = "py_runtime")
17+
load("//python/private:util.bzl", "add_migration_tag")
1818

19-
py_runtime = _py_runtime
19+
def py_runtime(**attrs):
20+
"""See the Bazel core [py_runtime](https://docs.bazel.build/versions/master/be/python.html#py_runtime) documentation.
21+
22+
Args:
23+
**attrs: Rule attributes
24+
"""
25+
if attrs.get("python_version") == "PY2":
26+
fail("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
27+
28+
# buildifier: disable=native-python
29+
native.py_runtime(**add_migration_tag(attrs))

python/py_runtime_pair.bzl

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,74 @@
1414

1515
"""Public entry point for py_runtime_pair."""
1616

17-
load("//python/private:reexports.bzl", _py_runtime_pair = "py_runtime_pair")
17+
load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
1818

19-
py_runtime_pair = _py_runtime_pair
19+
# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
20+
# doc generator gives useful API docs.
21+
def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
22+
"""A toolchain rule for Python.
23+
24+
This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
25+
However, Python 2 is no longer supported, so it now only wraps a single Python 3
26+
runtime.
27+
28+
Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
29+
rule returning a `PyRuntimeInfo` provider may be used.
30+
31+
This rule returns a `platform_common.ToolchainInfo` provider with the following
32+
schema:
33+
34+
```python
35+
platform_common.ToolchainInfo(
36+
py2_runtime = None,
37+
py3_runtime = <PyRuntimeInfo or None>,
38+
)
39+
```
40+
41+
Example usage:
42+
43+
```python
44+
# In your BUILD file...
45+
46+
load("@rules_python//python:defs.bzl", "py_runtime_pair")
47+
48+
py_runtime(
49+
name = "my_py3_runtime",
50+
interpreter_path = "/system/python3",
51+
python_version = "PY3",
52+
)
53+
54+
py_runtime_pair(
55+
name = "my_py_runtime_pair",
56+
py3_runtime = ":my_py3_runtime",
57+
)
58+
59+
toolchain(
60+
name = "my_toolchain",
61+
target_compatible_with = <...>,
62+
toolchain = ":my_py_runtime_pair",
63+
toolchain_type = "@rules_python//python:toolchain_type",
64+
)
65+
```
66+
67+
```python
68+
# In your WORKSPACE...
69+
70+
register_toolchains("//my_pkg:my_toolchain")
71+
```
72+
73+
Args:
74+
name: str, the name of the target
75+
py2_runtime: optional Label; must be unset or None; an error is raised
76+
otherwise.
77+
py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
78+
**attrs: Extra attrs passed onto the native rule
79+
"""
80+
if attrs.get("py2_runtime"):
81+
fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
82+
_py_runtime_pair(
83+
name = name,
84+
py2_runtime = py2_runtime,
85+
py3_runtime = py3_runtime,
86+
**attrs
87+
)

0 commit comments

Comments
 (0)