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"""This package contains two sets of rules: 15 16 1) the "core" Python rules, which were historically bundled with Bazel and 17 are now either re-exported or copied into this repository; and 18 19 2) the packaging rules, which were historically simply known as 20 rules_python. 21 22In an ideal renaming, we'd move the packaging rules to a different package so 23that @rules_python//python is only concerned with the core rules. 24""" 25 26load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 27load(":current_py_toolchain.bzl", "current_py_toolchain") 28 29package(default_visibility = ["//visibility:public"]) 30 31licenses(["notice"]) 32 33filegroup( 34 name = "distribution", 35 srcs = glob(["**"]) + [ 36 "//python/cc:distribution", 37 "//python/config_settings:distribution", 38 "//python/constraints:distribution", 39 "//python/private:distribution", 40 "//python/runfiles:distribution", 41 ], 42 visibility = ["//:__pkg__"], 43) 44 45# ========= bzl_library targets end ========= 46 47bzl_library( 48 name = "current_py_toolchain_bzl", 49 srcs = ["current_py_toolchain.bzl"], 50) 51 52bzl_library( 53 name = "defs_bzl", 54 srcs = [ 55 "defs.bzl", 56 ], 57 visibility = ["//visibility:public"], 58 deps = [ 59 ":current_py_toolchain_bzl", 60 ":py_binary_bzl", 61 ":py_import_bzl", 62 ":py_info_bzl", 63 ":py_library_bzl", 64 ":py_runtime_bzl", 65 ":py_runtime_info_bzl", 66 ":py_runtime_pair_bzl", 67 ":py_test_bzl", 68 "//python/private:bazel_tools_bzl", 69 ], 70) 71 72bzl_library( 73 name = "proto_bzl", 74 srcs = [ 75 "proto.bzl", 76 ], 77 visibility = ["//visibility:public"], 78 deps = [ 79 "//python/private/proto:py_proto_library_bzl", 80 ], 81) 82 83bzl_library( 84 name = "py_binary_bzl", 85 srcs = ["py_binary.bzl"], 86 deps = ["//python/private:util_bzl"], 87) 88 89bzl_library( 90 name = "py_cc_link_params_info_bzl", 91 srcs = ["py_cc_link_params_info.bzl"], 92) 93 94bzl_library( 95 name = "py_import_bzl", 96 srcs = ["py_import.bzl"], 97 deps = [":py_info_bzl"], 98) 99 100bzl_library( 101 name = "py_info_bzl", 102 srcs = ["py_info.bzl"], 103 deps = ["//python/private:reexports_bzl"], 104) 105 106bzl_library( 107 name = "py_library_bzl", 108 srcs = ["py_library.bzl"], 109 deps = ["//python/private:util_bzl"], 110) 111 112bzl_library( 113 name = "py_runtime_bzl", 114 srcs = ["py_runtime.bzl"], 115 deps = ["//python/private:util_bzl"], 116) 117 118bzl_library( 119 name = "py_runtime_pair_bzl", 120 srcs = ["py_runtime_pair.bzl"], 121 deps = ["//python/private:bazel_tools_bzl"], 122) 123 124bzl_library( 125 name = "py_runtime_info_bzl", 126 srcs = ["py_runtime_info.bzl"], 127 deps = ["//python/private:reexports_bzl"], 128) 129 130bzl_library( 131 name = "py_test_bzl", 132 srcs = ["py_test.bzl"], 133 deps = ["//python/private:util_bzl"], 134) 135 136# NOTE: Remember to add bzl_library targets to //tests:bzl_libraries 137# ========= bzl_library targets end ========= 138 139# Filegroup of bzl files that can be used by downstream rules for documentation generation 140filegroup( 141 name = "bzl", 142 srcs = [ 143 "defs.bzl", 144 "packaging.bzl", 145 "pip.bzl", 146 "repositories.bzl", 147 "versions.bzl", 148 "//python/pip_install:bzl", 149 "//python/private:bzl", 150 ], 151 visibility = ["//visibility:public"], 152) 153 154# ========= Core rules ========= 155 156exports_files([ 157 "defs.bzl", 158 "python.bzl", # Deprecated, please use defs.bzl 159]) 160 161# This target can be used to inspect the current Python major version. To use, 162# put it in the `flag_values` attribute of a `config_setting` and test it 163# against the values "PY2" or "PY3". It will always match one or the other. 164# 165# If you do not need to test any other flags in combination with the Python 166# version, then as a convenience you may use the predefined `config_setting`s 167# `@rules_python//python:PY2` and `@rules_python//python:PY3`. 168# 169# Example usage: 170# 171# config_setting( 172# name = "py3_on_arm", 173# values = {"cpu": "arm"}, 174# flag_values = {"@rules_python//python:python_version": "PY3"}, 175# ) 176# 177# my_target( 178# ... 179# some_attr = select({ 180# ":py3_on_arm": ..., 181# ... 182# }), 183# ... 184# ) 185# 186# Caution: Do not `select()` on the built-in command-line flags `--force_python` 187# or `--python_version`, as they do not always reflect the true Python version 188# of the current target. `select()`-ing on them can lead to action conflicts and 189# will be disallowed. 190alias( 191 name = "python_version", 192 actual = "@bazel_tools//tools/python:python_version", 193) 194 195alias( 196 name = "PY2", 197 actual = "@bazel_tools//tools/python:PY2", 198) 199 200alias( 201 name = "PY3", 202 actual = "@bazel_tools//tools/python:PY3", 203) 204 205# The toolchain type for Python rules. Provides a Python 2 and/or Python 3 206# runtime. 207alias( 208 name = "toolchain_type", 209 actual = "@bazel_tools//tools/python:toolchain_type", 210) 211 212# Definitions for a Python toolchain that, at execution time, attempts to detect 213# a platform runtime having the appropriate major Python version. Consider this 214# a toolchain of last resort. 215# 216# The non-strict version allows using a Python 2 interpreter for PY3 targets, 217# and vice versa. The only reason to use this is if you're working around 218# spurious failures due to PY2 vs PY3 validation. Even then, using this is only 219# safe if you know for a fact that your build is completely compatible with the 220# version of the `python` command installed on the target platform. 221 222alias( 223 name = "autodetecting_toolchain", 224 actual = "@bazel_tools//tools/python:autodetecting_toolchain", 225) 226 227alias( 228 name = "autodetecting_toolchain_nonstrict", 229 actual = "@bazel_tools//tools/python:autodetecting_toolchain_nonstrict", 230) 231 232# ========= Packaging rules ========= 233 234exports_files([ 235 "packaging.bzl", 236 "pip.bzl", 237]) 238 239current_py_toolchain( 240 name = "current_py_toolchain", 241) 242