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