-
Notifications
You must be signed in to change notification settings - Fork 269
/
transition_support.bzl
290 lines (265 loc) · 12.7 KB
/
transition_support.bzl
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Copyright 2019 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.
"""Starlark transition support for Apple rules."""
load("@bazel_skylib//lib:dicts.bzl", "dicts")
def _cpu_string(*, cpu, platform_type, settings):
"""Generates a <platform>_<arch> string for the current target based on the given parameters.
Args:
cpu: A valid Apple cpu command line option as a string, or None to infer a value from
command line options passed through settings.
platform_type: The Apple platform for which the rule should build its targets (`"ios"`,
`"macos"`, `"tvos"`, or `"watchos"`).
settings: A dictionary whose set of keys is defined by the inputs parameter, typically from
the settings argument found on the implementation function of the current Starlark
transition.
Returns:
A <platform>_<arch> string defined for the current target.
"""
if platform_type == "ios":
if cpu:
return "ios_{}".format(cpu)
ios_cpus = settings["//command_line_option:ios_multi_cpus"]
if ios_cpus:
return "ios_{}".format(ios_cpus[0])
cpu_value = settings["//command_line_option:cpu"]
if cpu_value.startswith("ios_"):
return cpu_value
return "ios_x86_64"
if platform_type == "macos":
if cpu:
return "darwin_{}".format(cpu)
macos_cpus = settings["//command_line_option:macos_cpus"]
if macos_cpus:
return "darwin_{}".format(macos_cpus[0])
return "darwin_x86_64"
if platform_type == "tvos":
if cpu:
return "tvos_{}".format(cpu)
tvos_cpus = settings["//command_line_option:tvos_cpus"]
if tvos_cpus:
return "tvos_{}".format(tvos_cpus[0])
return "tvos_x86_64"
if platform_type == "watchos":
if cpu:
return "watchos_{}".format(cpu)
watchos_cpus = settings["//command_line_option:watchos_cpus"]
if watchos_cpus:
return "watchos_{}".format(watchos_cpus[0])
return "watchos_i386"
fail("ERROR: Unknown platform type: {}".format(platform_type))
def _min_os_version_or_none(*, minimum_os_version, platform, platform_type):
if platform_type == platform:
return minimum_os_version
return None
def _command_line_options(*, cpu = None, minimum_os_version, platform_type, settings):
"""Generates a dictionary of command line options suitable for the current target.
Args:
cpu: A valid Apple cpu command line option as a string, or None to infer a value from
command line options passed through settings.
minimum_os_version: A string representing the minimum OS version specified for this
platform, represented as a dotted version number (for example, `"9.0"`).
platform_type: The Apple platform for which the rule should build its targets (`"ios"`,
`"macos"`, `"tvos"`, or `"watchos"`).
settings: A dictionary whose set of keys is defined by the inputs parameter, typically from
the settings argument found on the implementation function of the current Starlark
transition.
Returns:
A dictionary of `"//command_line_option"`s defined for the current target.
"""
return {
"//command_line_option:apple configuration distinguisher": "applebin_" + platform_type,
"//command_line_option:apple_platform_type": platform_type,
"//command_line_option:apple_split_cpu": cpu if cpu else "",
"//command_line_option:compiler": settings["//command_line_option:apple_compiler"],
"//command_line_option:cpu": _cpu_string(
cpu = cpu,
platform_type = platform_type,
settings = settings,
),
"//command_line_option:crosstool_top": (
settings["//command_line_option:apple_crosstool_top"]
),
"//command_line_option:fission": [],
"//command_line_option:grte_top": settings["//command_line_option:apple_grte_top"],
"//command_line_option:ios_minimum_os": _min_os_version_or_none(
minimum_os_version = minimum_os_version,
platform = "ios",
platform_type = platform_type,
),
"//command_line_option:macos_minimum_os": _min_os_version_or_none(
minimum_os_version = minimum_os_version,
platform = "macos",
platform_type = platform_type,
),
"//command_line_option:tvos_minimum_os": _min_os_version_or_none(
minimum_os_version = minimum_os_version,
platform = "tvos",
platform_type = platform_type,
),
"//command_line_option:watchos_minimum_os": _min_os_version_or_none(
minimum_os_version = minimum_os_version,
platform = "watchos",
platform_type = platform_type,
),
}
def _command_line_options_for_platform(
*,
minimum_os_version,
platform_attr,
platform_type,
settings,
target_environments):
"""Generates a dictionary of command line options keyed by 1:2+ transition for this platform.
Args:
minimum_os_version: A string representing the minimum OS version specified for this
platform, represented as a dotted version number (for example, `"9.0"`).
platform_attr: The attribute for the apple platform specifying in dictionary form which
architectures to build for given a target environment as the key for this platform.
platform_type: The Apple platform for which the rule should build its targets (`"ios"`,
`"macos"`, `"tvos"`, or `"watchos"`).
settings: A dictionary whose set of keys is defined by the inputs parameter, typically from
the settings argument found on the implementation function of the current Starlark
transition.
target_environments: A list of strings representing target environments supported by the
platform. Possible strings include "device" and "simulator".
Returns:
A dictionary of keys for each <platform>_<arch>_<target_environment> found with a
corresponding dictionary of `"//command_line_option"`s as each key's value.
"""
output_dictionary = {}
for target_environment in target_environments:
if platform_attr.get(target_environment):
cpus = platform_attr[target_environment]
for cpu in cpus:
found_cpu = {
_cpu_string(
cpu = cpu,
platform_type = platform_type,
settings = settings,
) + "_" + target_environment: _command_line_options(
cpu = cpu,
minimum_os_version = minimum_os_version,
platform_type = platform_type,
settings = settings,
),
}
output_dictionary = dicts.add(found_cpu, output_dictionary)
return output_dictionary
def _apple_rule_base_transition_impl(settings, attr):
"""Rule transition for Apple rules."""
return _command_line_options(
minimum_os_version = attr.minimum_os_version,
platform_type = attr.platform_type,
settings = settings,
)
# These flags are a mix of options defined in native Bazel from the following fragments:
# - https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java
# - https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/rules/apple/AppleCommandLineOptions.java
# - https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java
_apple_rule_common_transition_inputs = [
"//command_line_option:apple_compiler",
"//command_line_option:apple_crosstool_top",
"//command_line_option:apple_grte_top",
]
_apple_rule_base_transition_inputs = _apple_rule_common_transition_inputs + [
"//command_line_option:cpu",
"//command_line_option:ios_multi_cpus",
"//command_line_option:macos_cpus",
"//command_line_option:tvos_cpus",
"//command_line_option:watchos_cpus",
]
_apple_rule_base_transition_outputs = [
"//command_line_option:apple configuration distinguisher",
"//command_line_option:apple_platform_type",
"//command_line_option:apple_split_cpu",
"//command_line_option:compiler",
"//command_line_option:cpu",
"//command_line_option:crosstool_top",
"//command_line_option:fission",
"//command_line_option:grte_top",
"//command_line_option:ios_minimum_os",
"//command_line_option:macos_minimum_os",
"//command_line_option:tvos_minimum_os",
"//command_line_option:watchos_minimum_os",
]
_apple_rule_base_transition = transition(
implementation = _apple_rule_base_transition_impl,
inputs = _apple_rule_base_transition_inputs,
outputs = _apple_rule_base_transition_outputs,
)
def _apple_rule_arm64_as_arm64e_transition_impl(settings, attr):
"""Rule transition for Apple rules that map arm64 to arm64e."""
key = "//command_line_option:macos_cpus"
# These additional settings are sent to both the base implementation and the final transition.
additional_settings = {key: [cpu if cpu != "arm64" else "arm64e" for cpu in settings[key]]}
return dicts.add(
_apple_rule_base_transition_impl(dicts.add(settings, additional_settings), attr),
additional_settings,
)
_apple_rule_arm64_as_arm64e_transition = transition(
implementation = _apple_rule_arm64_as_arm64e_transition_impl,
inputs = _apple_rule_base_transition_inputs,
outputs = _apple_rule_base_transition_outputs + ["//command_line_option:macos_cpus"],
)
def _static_framework_transition_impl(settings, attr):
"""Attribute transition for static frameworks to enable swiftinterface generation."""
return {
"@build_bazel_rules_swift//swift:emit_swiftinterface": True,
}
# This transition is used, for now, to enable swiftinterface generation on swift_library targets.
# Once apple_common.split_transition is migrated to Starlark, this transition should be merged into
# that one, being enabled by reading either a private attribute on the static framework rules, or
# some other mechanism, so that it is only enabled on static framework rules and not all Apple
# rules.
_static_framework_transition = transition(
implementation = _static_framework_transition_impl,
inputs = [],
outputs = [
"@build_bazel_rules_swift//swift:emit_swiftinterface",
],
)
def _xcframework_transition_impl(settings, attr):
"""Starlark 1:2+ transition for generation of multiple frameworks for the current target."""
output_dictionary = {}
if hasattr(attr, "macos"):
command_line_options_for_platform = _command_line_options_for_platform(
minimum_os_version = attr.minimum_os_versions.get("macos"),
platform_attr = attr.macos,
platform_type = "macos",
settings = settings,
target_environments = ["device"],
)
output_dictionary = dicts.add(command_line_options_for_platform, output_dictionary)
for platform_type in ["ios", "tvos", "watchos"]:
if hasattr(attr, platform_type):
command_line_options_for_platform = _command_line_options_for_platform(
minimum_os_version = attr.minimum_os_versions.get(platform_type),
platform_attr = getattr(attr, platform_type),
platform_type = platform_type,
settings = settings,
target_environments = ["device", "simulator"],
)
output_dictionary = dicts.add(command_line_options_for_platform, output_dictionary)
return output_dictionary
_xcframework_transition = transition(
implementation = _xcframework_transition_impl,
inputs = _apple_rule_common_transition_inputs,
outputs = _apple_rule_base_transition_outputs,
)
transition_support = struct(
apple_rule_transition = _apple_rule_base_transition,
apple_rule_arm64_as_arm64e_transition = _apple_rule_arm64_as_arm64e_transition,
static_framework_transition = _static_framework_transition,
xcframework_transition = _xcframework_transition,
)