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) 28""" 29 30# The py_internal symbol is only accessible from within @rules_python, so we have to 31# load it from there and re-export it so that rules_python can later load it. 32_PY_INTERNAL_SHIM = """\ 33load("@rules_python//tools/build_defs/python/private:py_internal_renamed.bzl", "py_internal_renamed") 34py_internal_impl = py_internal_renamed 35""" 36 37ROOT_BUILD_TEMPLATE = """\ 38load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 39 40package( 41 default_visibility = [ 42 "{visibility}", 43 ] 44) 45 46bzl_library( 47 name = "rules_python_config_bzl", 48 srcs = ["rules_python_config.bzl"] 49) 50 51bzl_library( 52 name = "py_internal_bzl", 53 srcs = ["py_internal.bzl"], 54 deps = [{py_internal_dep}], 55) 56""" 57 58def _internal_config_repo_impl(rctx): 59 pystar_requested = _bool_from_environ(rctx, _ENABLE_PYSTAR_ENVVAR_NAME, _ENABLE_PYSTAR_DEFAULT) 60 61 # Bazel 7+ (dev and later) has native.starlark_doc_extract, and thus the 62 # py_internal global, which are necessary for the pystar implementation. 63 if pystar_requested and hasattr(native, "starlark_doc_extract"): 64 enable_pystar = pystar_requested 65 else: 66 enable_pystar = False 67 68 rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format( 69 enable_pystar = enable_pystar, 70 )) 71 72 if enable_pystar: 73 shim_content = _PY_INTERNAL_SHIM 74 py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"' 75 else: 76 shim_content = "py_internal_impl = None\n" 77 py_internal_dep = "" 78 79 # Bazel 5 doesn't support repository visibility, so just use public 80 # as a stand-in 81 if native.bazel_version.startswith("5."): 82 visibility = "//visibility:public" 83 else: 84 visibility = "@rules_python//:__subpackages__" 85 86 rctx.file("BUILD", ROOT_BUILD_TEMPLATE.format( 87 py_internal_dep = py_internal_dep, 88 visibility = visibility, 89 )) 90 rctx.file("py_internal.bzl", shim_content) 91 return None 92 93internal_config_repo = repository_rule( 94 implementation = _internal_config_repo_impl, 95 configure = True, 96 environ = [_ENABLE_PYSTAR_ENVVAR_NAME], 97) 98 99def _bool_from_environ(rctx, key, default): 100 return bool(int(rctx.os.environ.get(key, default))) 101