forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal_config_repo.bzl
More file actions
152 lines (130 loc) · 5.5 KB
/
internal_config_repo.bzl
File metadata and controls
152 lines (130 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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 = "0"
_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},
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.
bazel_major_version = int(native.bazel_version.split(".")[0]) if native.bazel_version else 99999
if bazel_major_version >= 8:
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,
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)))