| # 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_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_deprecation_warnings = {enable_deprecation_warnings}, | |
| extract_needs_chmod = {extract_needs_chmod}, | |
| bazel_8_or_later = {bazel_8_or_later}, | |
| bazel_9_or_later = {bazel_9_or_later}, | |
| bazel_10_or_later = {bazel_10_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}), | |
| modules_using_explicit_initpy = {modules_using_explicit_initpy}, | |
| tool_deps_dev_only = {tool_deps_dev_only}, | |
| ) | |
| """ | |
| ROOT_BUILD_TEMPLATE = """ | |
| load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | |
| package( | |
| default_visibility = [ | |
| "{visibility}", | |
| ] | |
| ) | |
| bzl_library( | |
| name = "extra_transition_settings", | |
| srcs = ["extra_transition_settings.bzl"], | |
| ) | |
| bzl_library( | |
| name = "rules_python_config", | |
| srcs = ["rules_python_config.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 | |
| extract_needs_chmod = bazel_major_version < 8 or (bazel_major_version == 8 and bazel_minor_version < 6) | |
| 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" | |
| explicit_init_py_modules = {k: str(v) == "True" for k, v in rctx.attr.explicit_init_py_modules.items()} | |
| rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format( | |
| build_python_zip_default = repo_utils.get_platforms_os_name(rctx) == "windows", | |
| 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), | |
| extract_needs_chmod = str(extract_needs_chmod), | |
| builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider, | |
| modules_using_explicit_initpy = str(explicit_init_py_modules), | |
| tool_deps_dev_only = str(rctx.attr.tool_deps_dev_only), | |
| bazel_8_or_later = str(bazel_major_version >= 8), | |
| bazel_9_or_later = str(bazel_major_version >= 9), | |
| bazel_10_or_later = str(bazel_major_version > 9), | |
| )) | |
| rctx.file("BUILD", ROOT_BUILD_TEMPLATE.format( | |
| visibility = "@rules_python//:__subpackages__", | |
| )) | |
| 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 = [], | |
| attrs = { | |
| "explicit_init_py_modules": attr.string_dict(), | |
| "tool_deps_dev_only": attr.bool(default = True), | |
| "transition_setting_generators": attr.string_list_dict(), | |
| "transition_settings": attr.string_list(), | |
| }, | |
| ) | |
| def _bool_from_environ(rctx, key, default): | |
| return bool(int(rctx.getenv(key, default))) |