blob: 9fc301cd2e140ad241a9b06c671a7f2a2af94929 [file] [log] [blame]
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Repository to generate configuration settings info from the environment.
This handles settings that can't be encoded as regular build configuration flags,
such as globals available to Bazel versions, or propagating user environment
settings for rules to later use.
"""
load("//python/private:text_util.bzl", "render")
load(":repo_utils.bzl", "repo_utils")
_ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR"
_ENABLE_PIPSTAR_DEFAULT = "1"
_ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS"
_ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0"
_CONFIG_TEMPLATE = """
config = struct(
build_python_zip_default = {build_python_zip_default},
supports_whl_extraction = {supports_whl_extraction},
enable_pystar = True,
enable_pipstar = {enable_pipstar},
enable_deprecation_warnings = {enable_deprecation_warnings},
bazel_8_or_later = {bazel_8_or_later},
bazel_9_or_later = {bazel_9_or_later},
BuiltinPyInfo = getattr(getattr(native, "legacy_globals", None), "PyInfo", {builtin_py_info_symbol}),
BuiltinPyRuntimeInfo = getattr(getattr(native, "legacy_globals", None), "PyRuntimeInfo", {builtin_py_runtime_info_symbol}),
BuiltinPyCcLinkParamsProvider = getattr(getattr(native, "legacy_globals", None), "PyCcLinkParamsProvider", {builtin_py_cc_link_params_provider}),
)
"""
# The py_internal symbol is only accessible from within @rules_python, so we have to
# load it from there and re-export it so that rules_python can later load it.
_PY_INTERNAL_SHIM = """
load("@rules_python//tools/build_defs/python/private:py_internal_renamed.bzl", "py_internal_renamed")
py_internal_impl = py_internal_renamed
"""
ROOT_BUILD_TEMPLATE = """
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
package(
default_visibility = [
"{visibility}",
]
)
bzl_library(
name = "rules_python_config_bzl",
srcs = ["rules_python_config.bzl"]
)
bzl_library(
name = "py_internal_bzl",
srcs = ["py_internal.bzl"],
deps = [{py_internal_dep}],
)
bzl_library(
name = "extra_transition_settings_bzl",
srcs = ["extra_transition_settings.bzl"],
)
"""
_EXTRA_TRANSITIONS_TEMPLATE = """
# Generated by @rules_python//python/private:internal_config_repo.bzl
#
# For a list of what modules added what labels, see
# transition_settings_debug.txt
EXTRA_TRANSITION_SETTINGS = {labels}
"""
_TRANSITION_SETTINGS_DEBUG_TEMPLATE = """
# Generated by @rules_python//python/private:internal_config_repo.bzl
{lines}
"""
def _internal_config_repo_impl(rctx):
# An empty version signifies a development build, which is treated as
# the latest version.
if native.bazel_version:
version_parts = native.bazel_version.split(".")
bazel_major_version = int(version_parts[0])
bazel_minor_version = int(version_parts[1])
else:
bazel_major_version = 99999
bazel_minor_version = 99999
supports_whl_extraction = False
if bazel_major_version >= 8:
# Extracting .whl files requires Bazel 8.3.0 or later.
if bazel_major_version > 8 or bazel_minor_version >= 3:
supports_whl_extraction = True
builtin_py_info_symbol = "None"
builtin_py_runtime_info_symbol = "None"
builtin_py_cc_link_params_provider = "None"
else:
builtin_py_info_symbol = "PyInfo"
builtin_py_runtime_info_symbol = "PyRuntimeInfo"
builtin_py_cc_link_params_provider = "PyCcLinkParamsProvider"
rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format(
build_python_zip_default = repo_utils.get_platforms_os_name(rctx) == "windows",
enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT),
enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT),
builtin_py_info_symbol = builtin_py_info_symbol,
builtin_py_runtime_info_symbol = builtin_py_runtime_info_symbol,
supports_whl_extraction = str(supports_whl_extraction),
builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider,
bazel_8_or_later = str(bazel_major_version >= 8),
bazel_9_or_later = str(bazel_major_version >= 9),
))
shim_content = _PY_INTERNAL_SHIM
py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"'
rctx.file("BUILD", ROOT_BUILD_TEMPLATE.format(
py_internal_dep = py_internal_dep,
visibility = "@rules_python//:__subpackages__",
))
rctx.file("py_internal.bzl", shim_content)
rctx.file(
"extra_transition_settings.bzl",
_EXTRA_TRANSITIONS_TEMPLATE.format(
labels = render.list(rctx.attr.transition_settings),
),
)
debug_lines = [
"{} added by modules: {}".format(setting, ", ".join(sorted(requesters)))
for setting, requesters in rctx.attr.transition_setting_generators.items()
]
rctx.file(
"transition_settings_debug.txt",
_TRANSITION_SETTINGS_DEBUG_TEMPLATE.format(lines = "\n".join(debug_lines)),
)
return None
internal_config_repo = repository_rule(
implementation = _internal_config_repo_impl,
configure = True,
environ = [_ENABLE_PIPSTAR_ENVVAR_NAME],
attrs = {
"transition_setting_generators": attr.string_list_dict(),
"transition_settings": attr.string_list(),
},
)
def _bool_from_environ(rctx, key, default):
return bool(int(repo_utils.getenv(rctx, key, default)))