8000 Add re-exports for Starlark-defined symbols · bazel-contrib/rules_python@7ac6910 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ac6910

Browse files
committed
Add re-exports for Starlark-defined symbols
This adds export definitions for built-in symbols like `PyInfo` and `@bazel_tools`-defined symbols like py_runtime_pair.
1 parent 7fd12fd commit 7ac6910

File tree

4 files changed

+162
-2
lines changed

4 files changed

+162
-2
lines changed

python/BUILD

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,86 @@ package(default_visibility = ["//visibility:public"])
2828

2929
licenses(["notice"]) # Apache 2.0
3030

31-
# Core rules.
31+
# ========= Core rules =========
32+
3233
exports_files([
3334
"defs.bzl",
3435
"python.bzl", # Deprecated, please use defs.bzl
3536
])
3637

37-
# Packaging rules, unrelated to the core rules.
38+
# This target can be used to inspect the current Python major version. To use,
39+
# put it in the `flag_values` attribute of a `config_setting` and test it
40+
# against the values "PY2" or "PY3". It will always match one or the other.
41+
#
42+
# If you do not need to test any other flags in combination with the Python
43+
# version, then as a convenience you may use the predefined `config_setting`s
44+
# `@rules_python//python:PY2` and `@rules_python//python:PY3`.
45+
#
46+
# Example usage:
47+
#
48+
# config_setting(
49+
# name = "py3_on_arm",
50+
# values = {"cpu": "arm"},
51+
# flag_values = {"@rules_python//python:python_version": "PY3"},
52+
# )
53+
#
54+
# my_target(
55+
# ...
56+
# some_attr = select({
57+
# ":py3_on_arm": ...,
58+
# ...
59+
# }),
60+
# ...
61+
# )
62+
#
63+
# Caution: Do not `select()` on the built-in command-line flags `--force_python`
64+
# or `--python_version`, as they do not always reflect the true Python version
65+
# of the current target. `select()`-ing on them can lead to action conflicts and
66+
# will be disallowed.
67+
alias(
68+
name = "python_version",
69+
actual = "@bazel_tools//tools/python:python_version",
70+
)
71+
72+
alias(
73+
name = "PY2",
74+
actual = "@bazel_tools//tools/python:PY2",
75+
)
76+
77+
alias(
78+
name = "PY3",
79+
actual = "@bazel_tools//tools/python:PY3",
80+
)
81+
82+
# The toolchain type for Python rules. Provides a Python 2 and/or Python 3
83+
# runtime.
84+
alias(
85+
name = "toolchain_type",
86+
actual = "@bazel_tools//tools/python:toolchain_type",
87+
)
88+
89+
# Definitions for a Python toolchain that, at execution time, attempts to detect
90+
# a platform runtime having the appropriate major Python version. Consider this
91+
# a toolchain of last resort.
92+
#
93+
# The non-strict version allows using a Python 2 interpreter for PY3 targets,
94+
# and vice versa. The only reason to use this is if you're working around
95+
# spurious failures due to PY2 vs PY3 validation. Even then, using this is only
96+
# safe if you know for a fact that your build is completely compatible with the
97+
# version of the `python` command installed on the target platform.
98+
99+
alias(
100+
name = "autodetecting_toolchain",
101+
actual = "@bazel_tools//tools/python:autodetecting_toolchain",
102+
)
103+
104+
alias(
105+
name = "autodetecting_toolchain_nonstrict",
106+
actual = "@bazel_tools//tools/python:autodetecting_toolchain_nonstrict",
107+
)
108+
109+
# ========= Packaging rules =========
110+
38111
exports_files([
39112
"pip.bzl",
40113
"whl.bzl",

python/constraints/BUILD

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
package(default_visibility = ["//visibility:public"])
16+
17+
licenses(["notice"])
18+
19+
# A constraint_setting to use for constraints related to the location of the
20+
# system Python 2 interpreter on a platform.
21+
alias(
22+
name = "py2_interpreter_path",
23+
actual = "@bazel_tools//tools/python:py2_interpreter_path",
24+
)
25+
26+
# A constraint_setting to use for constraints related to the location of the
27+
# system Python 3 interpreter on a platform.
28+
alias(
29+
name = "py3_interpreter_path",
30+
actual = "@bazel_tools//tools/python:py3_interpreter_path",
31+
)

python/defs.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ work under `--incompatible_load_python_rules_from_bzl`. As the native rules get
1919
migrated to Starlark, their implementations will be moved here.
2020
"""
2121

22+
load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
23+
load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements")
24+
load(":private/reexports.bzl", "internal_PyInfo", "internal_PyRuntimeInfo")
25+
26+
# Exports of native-defined providers.
27+
28+
PyInfo = internal_PyInfo
29+
30+
PyRuntimeInfo = internal_PyRuntimeInfo
31+
2232
# The implementation of the macros and tagging mechanism follows the example
2333
# set by rules_cc and rules_java.
2434

@@ -70,3 +80,9 @@ def py_runtime(**attrs):
7080
**attrs: Rule attributes
7181
"""
7282
native.py_runtime(**_add_tags(attrs))
83+
84+
# Re-exports of Starlark-defined symbols in @bazel_tools//tools/python.
85+
86+
py_runtime_pair = _py_runtime_pair
87+
88+
find_requirements = _find_requirements

python/private/reexports.bzl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Internal re-exports of built-in symbols.
16+
17+
We want to re-export a built-in symbol as if it were defined in a Starlark
18+
file, so that users can for instance do:
19+
20+
```
21+
load("@rules_python//python:defs.bzl", "PyInfo")
22+
```
23+
24+
Unfortunately, we can't just write in defs.bzl
25+
26+
```
27+
PyInfo = PyInfo
28+
```
29+
30+
because the declaration of module-level symbol `PyInfo` makes the builtin
31+
inaccessible. So instead we access the builtin here and export it under a
32+
different name. Then we can load it from defs.bzl and export it there under
33+
the original name.
34+
"""
35+
36+
# Don't use underscore prefix, since that would make the symbol local to this
37+
# file only.
38+
39+
internal_PyInfo = PyInfo
40+
internal_PyRuntimeInfo = PyRuntimeInfo

0 commit comments

Comments
 (0)
0