• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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"""Repository to generate configuration settings info from the environment.
15
16This handles settings that can't be encoded as regular build configuration flags,
17such as globals available to Bazel versions, or propagating user environment
18settings for rules to later use.
19"""
20
21_ENABLE_PYSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PYSTAR"
22_ENABLE_PYSTAR_DEFAULT = "1"
23
24_CONFIG_TEMPLATE = """\
25config = struct(
26  enable_pystar = {enable_pystar},
27  BuiltinPyInfo = getattr(getattr(native, "legacy_globals", None), "PyInfo", {builtin_py_info_symbol}),
28  BuiltinPyRuntimeInfo = getattr(getattr(native, "legacy_globals", None), "PyRuntimeInfo", {builtin_py_runtime_info_symbol}),
29  BuiltinPyCcLinkParamsProvider = getattr(getattr(native, "legacy_globals", None), "PyCcLinkParamsProvider", {builtin_py_cc_link_params_provider}),
30)
31"""
32
33# The py_internal symbol is only accessible from within @rules_python, so we have to
34# load it from there and re-export it so that rules_python can later load it.
35_PY_INTERNAL_SHIM = """\
36load("@rules_python//tools/build_defs/python/private:py_internal_renamed.bzl", "py_internal_renamed")
37py_internal_impl = py_internal_renamed
38"""
39
40ROOT_BUILD_TEMPLATE = """\
41load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
42
43package(
44    default_visibility = [
45        "{visibility}",
46    ]
47)
48
49bzl_library(
50    name = "rules_python_config_bzl",
51    srcs = ["rules_python_config.bzl"]
52)
53
54bzl_library(
55    name = "py_internal_bzl",
56    srcs = ["py_internal.bzl"],
57    deps = [{py_internal_dep}],
58)
59"""
60
61def _internal_config_repo_impl(rctx):
62    pystar_requested = _bool_from_environ(rctx, _ENABLE_PYSTAR_ENVVAR_NAME, _ENABLE_PYSTAR_DEFAULT)
63
64    # Bazel 7+ (dev and later) has native.starlark_doc_extract, and thus the
65    # py_internal global, which are necessary for the pystar implementation.
66    if pystar_requested and hasattr(native, "starlark_doc_extract"):
67        enable_pystar = pystar_requested
68    else:
69        enable_pystar = False
70
71    if not native.bazel_version or int(native.bazel_version.split(".")[0]) >= 8:
72        builtin_py_info_symbol = "None"
73        builtin_py_runtime_info_symbol = "None"
74        builtin_py_cc_link_params_provider = "None"
75    else:
76        builtin_py_info_symbol = "PyInfo"
77        builtin_py_runtime_info_symbol = "PyRuntimeInfo"
78        builtin_py_cc_link_params_provider = "PyCcLinkParamsProvider"
79
80    rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format(
81        enable_pystar = enable_pystar,
82        builtin_py_info_symbol = builtin_py_info_symbol,
83        builtin_py_runtime_info_symbol = builtin_py_runtime_info_symbol,
84        builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider,
85    ))
86
87    if enable_pystar:
88        shim_content = _PY_INTERNAL_SHIM
89        py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"'
90    else:
91        shim_content = "py_internal_impl = None\n"
92        py_internal_dep = ""
93
94    # Bazel 5 doesn't support repository visibility, so just use public
95    # as a stand-in
96    if native.bazel_version.startswith("5."):
97        visibility = "//visibility:public"
98    else:
99        visibility = "@rules_python//:__subpackages__"
100
101    rctx.file("BUILD", ROOT_BUILD_TEMPLATE.format(
102        py_internal_dep = py_internal_dep,
103        visibility = visibility,
104    ))
105    rctx.file("py_internal.bzl", shim_content)
106    return None
107
108internal_config_repo = repository_rule(
109    implementation = _internal_config_repo_impl,
110    configure = True,
111    environ = [_ENABLE_PYSTAR_ENVVAR_NAME],
112)
113
114def _bool_from_environ(rctx, key, default):
115    return bool(int(rctx.os.environ.get(key, default)))
116