• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 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"""This package contains two sets of rules:
16
17    1) the "core" Python rules, which were historically bundled with Bazel and
18       are now either re-exported or copied into this repository; and
19
20    2) the packaging rules, which were historically simply known as
21       rules_python.
22
23In an ideal renaming, we'd move the packaging rules to a different package so
24that @rules_python//python is only concerned with the core rules.
25"""
26
27load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
28load(":current_py_toolchain.bzl", "current_py_toolchain")
29
30package(default_visibility = ["//visibility:public"])
31
32licenses(["notice"])
33
34filegroup(
35    name = "distribution",
36    srcs = glob(["**"]) + [
37        "//python/api:distribution",
38        "//python/cc:distribution",
39        "//python/config_settings:distribution",
40        "//python/constraints:distribution",
41        "//python/entry_points:distribution",
42        "//python/extensions:distribution",
43        "//python/pip_install:distribution",
44        "//python/private:distribution",
45        "//python/runfiles:distribution",
46        "//python/uv:distribution",
47    ],
48    visibility = ["//:__pkg__"],
49)
50
51# ========= bzl_library targets end =========
52
53bzl_library(
54    name = "current_py_toolchain_bzl",
55    srcs = ["current_py_toolchain.bzl"],
56)
57
58bzl_library(
59    name = "defs_bzl",
60    srcs = [
61        "defs.bzl",
62    ],
63    visibility = ["//visibility:public"],
64    deps = [
65        ":current_py_toolchain_bzl",
66        ":py_binary_bzl",
67        ":py_import_bzl",
68        ":py_info_bzl",
69        ":py_library_bzl",
70        ":py_runtime_bzl",
71        ":py_runtime_info_bzl",
72        ":py_runtime_pair_bzl",
73        ":py_test_bzl",
74        "//python/private:bazel_tools_bzl",
75    ],
76)
77
78bzl_library(
79    name = "features_bzl",
80    srcs = ["features.bzl"],
81)
82
83bzl_library(
84    name = "packaging_bzl",
85    srcs = ["packaging.bzl"],
86    deps = [
87        ":py_binary_bzl",
88        "//python/private:bzlmod_enabled_bzl",
89        "//python/private:py_package.bzl",
90        "//python/private:py_wheel_bzl",
91        "//python/private:py_wheel_normalize_pep440.bzl",
92        "//python/private:stamp_bzl",
93        "//python/private:util_bzl",
94        "@bazel_skylib//rules:native_binary",
95    ],
96)
97
98bzl_library(
99    name = "pip_bzl",
100    srcs = ["pip.bzl"],
101    deps = [
102        "//python/private:normalize_name_bzl",
103        "//python/private/pypi:multi_pip_parse_bzl",
104        "//python/private/pypi:package_annotation_bzl",
105        "//python/private/pypi:pip_compile_bzl",
106        "//python/private/pypi:pip_repository_bzl",
107        "//python/private/pypi:whl_library_alias_bzl",
108        "//python/private/whl_filegroup:whl_filegroup_bzl",
109    ],
110)
111
112bzl_library(
113    name = "proto_bzl",
114    srcs = [
115        "proto.bzl",
116    ],
117    visibility = ["//visibility:public"],
118    deps = [
119        "//python/private/proto:py_proto_library_bzl",
120    ],
121)
122
123bzl_library(
124    name = "py_binary_bzl",
125    srcs = ["py_binary.bzl"],
126    deps = [
127        "//python/private:py_binary_macro_bzl",
128        "//python/private:register_extension_info_bzl",
129        "//python/private:util_bzl",
130        "@rules_python_internal//:rules_python_config_bzl",
131    ],
132)
133
134bzl_library(
135    name = "py_cc_link_params_info_bzl",
136    srcs = ["py_cc_link_params_info.bzl"],
137    deps = [
138        "//python/private:py_cc_link_params_info_bzl",
139        "@rules_python_internal//:rules_python_config_bzl",
140    ],
141)
142
143bzl_library(
144    name = "py_exec_tools_info_bzl",
145    srcs = ["py_exec_tools_info.bzl"],
146    deps = ["//python/private:py_exec_tools_info_bzl"],
147)
148
149bzl_library(
150    name = "py_exec_tools_toolchain_bzl",
151    srcs = ["py_exec_tools_toolchain.bzl"],
152    deps = ["//python/private:py_exec_tools_toolchain_bzl"],
153)
154
155bzl_library(
156    name = "py_executable_info_bzl",
157    srcs = ["py_executable_info.bzl"],
158    deps = ["//python/private:py_executable_info_bzl"],
159)
160
161bzl_library(
162    name = "py_import_bzl",
163    srcs = ["py_import.bzl"],
164    deps = [":py_info_bzl"],
165)
166
167bzl_library(
168    name = "py_info_bzl",
169    srcs = ["py_info.bzl"],
170    deps = [
171        "//python/private:py_info_bzl",
172        "//python/private:reexports_bzl",
173        "@rules_python_internal//:rules_python_config_bzl",
174    ],
175)
176
177bzl_library(
178    name = "py_library_bzl",
179    srcs = ["py_library.bzl"],
180    deps = [
181        "//python/private:py_library_macro_bzl",
182        "//python/private:register_extension_info_bzl",
183        "//python/private:util_bzl",
184        "@rules_python_internal//:rules_python_config_bzl",
185    ],
186)
187
188bzl_library(
189    name = "py_runtime_bzl",
190    srcs = ["py_runtime.bzl"],
191    deps = [
192        "//python/private:py_runtime_macro_bzl",
193        "//python/private:util_bzl",
194    ],
195)
196
197bzl_library(
198    name = "py_runtime_pair_bzl",
199    srcs = ["py_runtime_pair.bzl"],
200    deps = [
201        "//python/private:bazel_tools_bzl",
202        "//python/private:py_runtime_pair_macro_bzl",
203        "//python/private:util_bzl",
204    ],
205)
206
207bzl_library(
208    name = "py_runtime_info_bzl",
209    srcs = ["py_runtime_info.bzl"],
210    deps = [
211        "//python/private:py_runtime_info_bzl",
212        "//python/private:reexports_bzl",
213        "//python/private:util_bzl",
214        "@rules_python_internal//:rules_python_config_bzl",
215    ],
216)
217
218bzl_library(
219    name = "py_test_bzl",
220    srcs = ["py_test.bzl"],
221    deps = [
222        "//python/private:py_test_macro_bzl",
223        "//python/private:register_extension_info_bzl",
224        "//python/private:util_bzl",
225        "@rules_python_internal//:rules_python_config_bzl",
226    ],
227)
228
229bzl_library(
230    name = "repositories_bzl",
231    srcs = ["repositories.bzl"],
232    deps = [
233        "//python/private:is_standalone_interpreter_bzl",
234        "//python/private:py_repositories_bzl",
235        "//python/private:python_register_multi_toolchains_bzl",
236        "//python/private:python_register_toolchains_bzl",
237        "//python/private:python_repository_bzl",
238    ],
239)
240
241bzl_library(
242    name = "versions_bzl",
243    srcs = ["versions.bzl"],
244    visibility = ["//:__subpackages__"],
245)
246
247# NOTE: Remember to add bzl_library targets to //tests:bzl_libraries
248# ========= bzl_library targets end =========
249
250# Filegroup of bzl files that can be used by downstream rules for documentation generation
251filegroup(
252    name = "bzl",
253    srcs = [
254        "defs.bzl",
255        "packaging.bzl",
256        "pip.bzl",
257        "repositories.bzl",
258        "versions.bzl",
259        "//python/pip_install:bzl",
260        "//python/private:bzl",
261    ],
262    visibility = ["//visibility:public"],
263)
264
265# ========= Core rules =========
266
267exports_files([
268    "defs.bzl",
269    "python.bzl",  # Deprecated, please use defs.bzl
270])
271
272# This target can be used to inspect the current Python major version. To use,
273# put it in the `flag_values` attribute of a `config_setting` and test it
274# against the values "PY2" or "PY3". It will always match one or the other.
275#
276# If you do not need to test any other flags in combination with the Python
277# version, then as a convenience you may use the predefined `config_setting`s
278# `@rules_python//python:PY2` and `@rules_python//python:PY3`.
279#
280# Example usage:
281#
282#     config_setting(
283#         name = "py3_on_arm",
284#         values = {"cpu": "arm"},
285#         flag_values = {"@rules_python//python:python_version": "PY3"},
286#     )
287#
288#     my_target(
289#         ...
290#         some_attr = select({
291#             ":py3_on_arm": ...,
292#             ...
293#         }),
294#         ...
295#     )
296#
297# Caution: Do not `select()` on the built-in command-line flags `--force_python`
298# or `--python_version`, as they do not always reflect the true Python version
299# of the current target. `select()`-ing on them can lead to action conflicts and
300# will be disallowed.
301alias(
302    name = "python_version",
303    actual = "@bazel_tools//tools/python:python_version",
304)
305
306alias(
307    name = "PY2",
308    actual = "@bazel_tools//tools/python:PY2",
309)
310
311alias(
312    name = "PY3",
313    actual = "@bazel_tools//tools/python:PY3",
314)
315
316# The toolchain type for Python rules. Provides a Python 2 and/or Python 3
317# runtime.
318alias(
319    name = "toolchain_type",
320    actual = "@bazel_tools//tools/python:toolchain_type",
321)
322
323toolchain_type(
324    name = "exec_tools_toolchain_type",
325    visibility = ["//visibility:public"],
326)
327
328# Special target to indicate `None` for label attributes a default value.
329alias(
330    name = "none",
331    actual = "//python/private:sentinel",
332)
333
334# Definitions for a Python toolchain that, at execution time, attempts to detect
335# a platform runtime having the appropriate major Python version. Consider this
336# a toolchain of last resort.
337#
338# The non-strict version allows using a Python 2 interpreter for PY3 targets,
339# and vice versa. The only reason to use this is if you're working around
340# spurious failures due to PY2 vs PY3 validation. Even then, using this is only
341# safe if you know for a fact that your build is completely compatible with the
342# version of the `python` command installed on the target platform.
343
344alias(
345    name = "autodetecting_toolchain",
346    actual = "//python/runtime_env_toolchains:runtime_env_toolchain",
347    deprecation = "Use //python/runtime_env_toolchains:all instead",
348)
349
350alias(
351    name = "autodetecting_toolchain_nonstrict",
352    actual = "//python/runtime_env_toolchains:runtime_env_toolchain",
353    deprecation = "Use //python/runtime_env_toolchains:all instead",
354)
355
356# ========= Packaging rules =========
357
358exports_files([
359    "packaging.bzl",
360    "pip.bzl",
361])
362
363current_py_toolchain(
364    name = "current_py_toolchain",
365)
366