-
Notifications
You must be signed in to change notification settings - Fork 34
/
native.bzl
506 lines (437 loc) · 18.1 KB
/
native.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
# `native` is fine to use in the prelude for v2
# @lint-ignore-every BUCKLINT
# This is buck2's shim import. Any public symbols here will be available within
# **all** interpreted files.
load("@prelude//android:cpu_filters.bzl", "ALL_CPU_FILTERS", "CPU_FILTER_FOR_DEFAULT_PLATFORM")
load("@prelude//apple:apple_macro_layer.bzl", "apple_binary_macro_impl", "apple_bundle_macro_impl", "apple_library_macro_impl", "apple_package_macro_impl", "apple_test_macro_impl", "apple_universal_executable_macro_impl", "apple_xcuitest_macro_impl", "prebuilt_apple_framework_macro_impl")
load("@prelude//apple/swift:swift_toolchain_macro_layer.bzl", "swift_toolchain_macro_impl")
load("@prelude//cxx:cxx_toolchain.bzl", "cxx_toolchain_inheriting_target_platform")
load("@prelude//cxx:cxx_toolchain_macro_layer.bzl", "cxx_toolchain_macro_impl")
load("@prelude//cxx:cxx_toolchain_types.bzl", _cxx = "cxx")
load("@prelude//erlang:erlang.bzl", _erlang_application = "erlang_application", _erlang_tests = "erlang_tests")
load("@prelude//python:toolchain.bzl", _python = "python")
load("@prelude//rust:link_info.bzl", "RustLinkInfo")
load("@prelude//rust:rust_common.bzl", "rust_common_macro_wrapper")
load("@prelude//rust:rust_library.bzl", "rust_library_macro_wrapper")
load("@prelude//rust:with_workspace.bzl", "with_rust_workspace")
load("@prelude//user:all.bzl", _user_rules = "rules")
load("@prelude//utils:buckconfig.bzl", _read_config = "read_config_with_logging", _read_root_config = "read_root_config_with_logging", log_buckconfigs = "LOG_BUCKCONFIGS")
load("@prelude//utils:expect.bzl", "expect")
load("@prelude//utils:selects.bzl", "selects")
load(":is_full_meta_repo.bzl", "is_full_meta_repo")
load(":paths.bzl", "paths")
load(":rules.bzl", __rules__ = "rules")
def __struct_to_dict(s):
vals = {}
for name in dir(s):
vals[name] = getattr(s, name)
return vals
def _tp2_constraint(project, version):
"""
Return the target configuration constraint to use for the given project
version
"""
return "ovr_config//third-party/{}/constraints:{}".format(project, version)
def _tp2_constraint_multi(versions):
"""
Return the `select` key rule name which corresponds to the given tp2 project
versions.
"""
expect(len(versions) >= 1, str(versions))
# If there's only a single project/version pair, then just return the its
# pre-defined constraint value rule name.
if len(versions) == 1:
(project, version) = versions.items()[0]
return _tp2_constraint(project, version)
# Otherwise, generate a `config_setting` to combine the constraint value
# rules for all the project/version pairs.
name = "-".join(["_tp2_constraints_"] + ["{}-{}".format(p, v) for p, v in sorted(versions.items())])
if not rule_exists(name):
__rules__["config_setting"](
name = name,
constraint_values = [_tp2_constraint(p, v) for p, v in versions.items()],
)
return ":" + name
def _extract_versions(constraints):
"""
Convert v1-style version constraints to a v2-compatible config settings.
The constraints are normally of the form:
`{"//third-party-buck/platform*/build/python:__project__": "3.8"}`.
"""
versions = {}
# Since the constraints will be duplicated for each fbcode "platform", do
# some initial work to de-duplicate them here, by extracting just the
# project and version and verify we get just a single reduced result.
for target, version in constraints.items():
expect(target.startswith("fbcode//") or target.startswith("//"), target)
base, name = target.split(":")
expect(name == "__project__", name)
project = paths.basename(base)
expect(project not in versions or version == versions[project])
versions[project] = version
return versions
def _versioned_param_to_select(items, default = None):
"""
Convert a v1-style `versioned_*` param to a `select` map.
Parameters:
- items: A list of 2-tuples of a list of version constraints to match and
the values to use in case of match.
"""
if items == None:
return None
# TODO(agallagher): Remove once we move to a `uquery` based TD.
if read_root_config("fbcode", "cquery_td") == "true":
return None
# Special case a form of "empty" constraints that `buckify_tp2` may
# generate in tp2 TARGETS.
if len(items) == 1 and not items[0][0]:
return items[0][1]
select_map = {}
# If a default is provided add that.
if default != None:
select_map["DEFAULT"] = default
# Convert v1 tp2-style versioned_* params to their analogous v2 select
# constraint maps.
for constraints, item in items:
versions = _extract_versions(constraints)
select_map[_tp2_constraint_multi(versions)] = item
if not select_map:
return None
return select(select_map)
def _concat(*items):
"""
Concatenate non-`None` items and return result.
"""
res = None
for item in items:
if item == None:
continue
if res == None:
res = item
elif type(res) == type({}) and type(item) == type({}):
new_res = {}
new_res.update(res)
new_res.update(item)
res = new_res
else:
res += item # buildifier: disable=dict-concatenation
return res
def _at_most_one(*items):
"""
Return a non-`None` value if it exists. Fail if more that one non-`None`
exists.
"""
res = None
for item in items:
if item == None:
continue
expect(res == None)
res = item
return res
def _get_valid_cpu_filters(cpu_filters: [list[str], None]) -> list[str]:
if read_root_config("buck2", "android_force_single_default_cpu") in ("True", "true"):
return [CPU_FILTER_FOR_DEFAULT_PLATFORM]
cpu_abis_config_string = read_root_config("ndk", "cpu_abis")
if cpu_abis_config_string:
cpu_abis = [v.strip() for v in cpu_abis_config_string.split(",")]
for cpu_abi in cpu_abis:
if cpu_abi not in ALL_CPU_FILTERS:
fail("Entries in ndk.cpu_abis must be one of {}, but {} is not".format(ALL_CPU_FILTERS, cpu_abi))
else:
cpu_abis = ALL_CPU_FILTERS
cpu_filters = cpu_filters or ALL_CPU_FILTERS
return [cpu_filter for cpu_filter in cpu_filters if cpu_filter in cpu_abis]
def _android_aar_macro_stub(
cpu_filters = None,
**kwargs):
__rules__["android_aar"](
cpu_filters = _get_valid_cpu_filters(cpu_filters),
**kwargs
)
def _android_binary_macro_stub(
allow_r_dot_java_in_secondary_dex = False,
cpu_filters = None,
primary_dex_patterns = [],
**kwargs):
if not allow_r_dot_java_in_secondary_dex:
primary_dex_patterns = primary_dex_patterns + [
"/R^",
"/R$",
# Pin this to the primary for apps with no primary dex classes.
"^com/facebook/buck_generated/AppWithoutResourcesStub^",
]
__rules__["android_binary"](
allow_r_dot_java_in_secondary_dex = allow_r_dot_java_in_secondary_dex,
cpu_filters = _get_valid_cpu_filters(cpu_filters),
primary_dex_patterns = primary_dex_patterns,
**kwargs
)
def _android_bundle_macro_stub(
cpu_filters = None,
**kwargs):
__rules__["android_bundle"](
cpu_filters = _get_valid_cpu_filters(cpu_filters),
**kwargs
)
def _android_instrumentation_apk_macro_stub(
cpu_filters = None,
primary_dex_patterns = [],
**kwargs):
primary_dex_patterns = primary_dex_patterns + [
"/R^",
"/R$",
# Pin this to the primary for apps with no primary dex classes.
"^com/facebook/buck_generated/AppWithoutResourcesStub^",
]
__rules__["android_instrumentation_apk"](
cpu_filters = _get_valid_cpu_filters(cpu_filters),
primary_dex_patterns = primary_dex_patterns,
**kwargs
)
# export_file src defaults to name, despite being string vs source, so adjust it in the macros
def _export_file_macro_stub(name, src = None, **kwargs):
__rules__["export_file"](name = name, src = name if src == None else src, **kwargs)
def _prebuilt_cxx_library_macro_stub(
exported_preprocessor_flags = None,
versioned_exported_preprocessor_flags = None,
exported_lang_preprocessor_flags = None,
versioned_exported_lang_preprocessor_flags = None,
exported_platform_preprocessor_flags = None,
versioned_exported_platform_preprocessor_flags = None,
exported_lang_platform_preprocessor_flags = None,
versioned_exported_lang_platform_preprocessor_flags = None,
static_lib = None,
versioned_static_lib = None,
static_pic_lib = None,
versioned_static_pic_lib = None,
shared_lib = None,
versioned_shared_lib = None,
header_dirs = None,
versioned_header_dirs = None,
**kwargs):
__rules__["prebuilt_cxx_library"](
exported_preprocessor_flags = _concat(
exported_preprocessor_flags,
_versioned_param_to_select(versioned_exported_preprocessor_flags),
),
exported_lang_preprocessor_flags = _concat(
exported_lang_preprocessor_flags,
_versioned_param_to_select(versioned_exported_lang_preprocessor_flags),
),
exported_platform_preprocessor_flags = _concat(
exported_platform_preprocessor_flags,
_versioned_param_to_select(versioned_exported_platform_preprocessor_flags),
),
exported_lang_platform_preprocessor_flags = _concat(
exported_lang_platform_preprocessor_flags,
_versioned_param_to_select(versioned_exported_lang_platform_preprocessor_flags),
),
static_lib = _at_most_one(static_lib, _versioned_param_to_select(versioned_static_lib)),
static_pic_lib = _at_most_one(static_pic_lib, _versioned_param_to_select(versioned_static_pic_lib)),
shared_lib = _at_most_one(shared_lib, _versioned_param_to_select(versioned_shared_lib)),
header_dirs = _at_most_one(header_dirs, _versioned_param_to_select(versioned_header_dirs)),
**kwargs
)
def _python_library_macro_stub(
srcs = None,
versioned_srcs = None,
resources = None,
versioned_resources = None,
**kwargs):
__rules__["python_library"](
srcs = _concat(srcs, _versioned_param_to_select(versioned_srcs, default = None)),
resources = _concat(resources, _versioned_param_to_select(versioned_resources, default = None)),
**kwargs
)
def _versioned_alias_macro_stub(versions = {}, **kwargs):
project = paths.basename(package_name())
__rules__["alias"](
actual = select({
_tp2_constraint(project, version): actual
for version, actual in versions.items()
}),
**kwargs
)
def _configured_alias_macro_stub(
name,
actual,
platform,
# Whether to fallback to a unconfigured `alias` if `platform` is `None`.
fallback_to_unconfigured_alias = False,
**kwargs):
pred = lambda platform: platform != None or not fallback_to_unconfigured_alias
__rules__["configured_alias"](
name = name,
# `actual` needs to be a pair of target + platform, as that's the format
# expected by the `configured_dep()` field
# Use a select map to make this thing `None` if `platform` is `None`.
configured_actual = selects.apply(
platform,
lambda platform: (actual, platform) if pred(platform) else None,
),
# Make sure that exactly one of `configured_actual` or `fallback_actual` is set
fallback_actual = selects.apply(
platform,
lambda platform: None if pred(platform) else actual,
),
# Unused.
actual = actual,
platform = platform,
**kwargs
)
def _apple_bundle_macro_stub(**kwargs):
apple_bundle_macro_impl(
apple_bundle_rule = __rules__["apple_bundle"],
apple_resource_bundle_rule = _user_rules["apple_resource_bundle"],
**kwargs
)
def _apple_watchos_bundle_macro_stub(**kwargs):
apple_bundle_macro_impl(
apple_bundle_rule = _user_rules["apple_watchos_bundle"],
apple_resource_bundle_rule = _user_rules["apple_resource_bundle"],
**kwargs
)
def _apple_macos_bundle_macro_stub(**kwargs):
apple_bundle_macro_impl(
apple_bundle_rule = _user_rules["apple_macos_bundle"],
apple_resource_bundle_rule = _user_rules["apple_resource_bundle"],
**kwargs
)
def _apple_test_macro_stub(**kwargs):
apple_test_macro_impl(
apple_test_rule = __rules__["apple_test"],
apple_resource_bundle_rule = _user_rules["apple_resource_bundle"],
**kwargs
)
def _apple_xcuitest_macro_stub(**kwargs):
apple_xcuitest_macro_impl(
apple_xcuitest_rule = __rules__["apple_xcuitest"],
**kwargs
)
def _apple_binary_macro_stub(**kwargs):
apple_binary_macro_impl(
apple_binary_rule = __rules__["apple_binary"],
apple_universal_executable = __rules__["apple_universal_executable"],
**kwargs
)
def _apple_library_macro_stub(**kwargs):
apple_library_macro_impl(
apple_library_rule = __rules__["apple_library"],
**kwargs
)
def _apple_package_macro_stub(**kwargs):
apple_package_macro_impl(
apple_package_rule = __rules__["apple_package"],
apple_ipa_package_rule = _user_rules["apple_ipa_package"],
**kwargs
)
def _apple_universal_executable_macro_stub(**kwargs):
apple_universal_executable_macro_impl(
apple_universal_executable_rule = __rules__["apple_universal_executable"],
**kwargs
)
def _swift_toolchain_macro_stub(**kwargs):
rule = __rules__["swift_toolchain"]
swift_toolchain_macro_impl(
swift_toolchain_rule = rule,
**kwargs
)
def _cxx_toolchain_macro_stub(**kwargs):
if is_full_meta_repo():
cache_links = kwargs.get("cache_links")
kwargs["cache_links"] = select({
"DEFAULT": cache_links,
"ovr_config//platform/execution/constraints:execution-platform-transitioned": True,
})
cxx_toolchain_macro_impl(
cxx_toolchain_rule = cxx_toolchain_inheriting_target_platform,
**kwargs
)
def _cxx_toolchain_override_macro_stub(**kwargs):
cxx_toolchain_macro_impl(
cxx_toolchain_rule = _user_rules["cxx_toolchain_override"],
**kwargs
)
def _erlang_application_macro_stub(**kwargs):
_erlang_application(
erlang_app_rule = __rules__["erlang_app"],
erlang_app_includes_rule = __rules__["erlang_app_includes"],
**kwargs
)
def _erlang_tests_macro_stub(**kwargs):
_erlang_tests(
erlang_app_rule = __rules__["erlang_app"],
erlang_test_rule = __rules__["erlang_test"],
**kwargs
)
def _rust_library_macro_stub(**kwargs):
rust_library = rust_common_macro_wrapper(__rules__["rust_library"])
rust_library = rust_library_macro_wrapper(rust_library)
rust_library(**kwargs)
def _rust_binary_macro_stub(**kwargs):
rust_binary = rust_common_macro_wrapper(__rules__["rust_binary"])
rust_binary(**kwargs)
def _rust_test_macro_stub(**kwargs):
rust_test = rust_common_macro_wrapper(__rules__["rust_test"])
rust_test(**kwargs)
def _prebuilt_apple_framework_macro_stub(**kwargs):
prebuilt_apple_framework_macro_impl(
prebuilt_apple_framework_rule = __rules__["prebuilt_apple_framework"],
**kwargs
)
# TODO(cjhopman): These macro wrappers should be handled in prelude/rules.bzl+rule_impl.bzl.
# Probably good if they were defined to take in the base rule that
# they are wrapping and return the wrapped one.
__extra_rules__ = {
"android_aar": _android_aar_macro_stub,
"android_binary": _android_binary_macro_stub,
"android_bundle": _android_bundle_macro_stub,
"android_instrumentation_apk": _android_instrumentation_apk_macro_stub,
"apple_binary": _apple_binary_macro_stub,
"apple_bundle": _apple_bundle_macro_stub,
"apple_library": _apple_library_macro_stub,
"apple_macos_bundle": _apple_macos_bundle_macro_stub,
"apple_package": _apple_package_macro_stub,
"apple_test": _apple_test_macro_stub,
"apple_universal_executable": _apple_universal_executable_macro_stub,
"apple_watchos_bundle": _apple_watchos_bundle_macro_stub,
"apple_xcuitest": _apple_xcuitest_macro_stub,
"configured_alias": _configured_alias_macro_stub,
"cxx_toolchain": _cxx_toolchain_macro_stub,
"cxx_toolchain_override": _cxx_toolchain_override_macro_stub,
"erlang_application": _erlang_application_macro_stub,
"erlang_tests": _erlang_tests_macro_stub,
"export_file": _export_file_macro_stub,
"prebuilt_apple_framework": _prebuilt_apple_framework_macro_stub,
"prebuilt_cxx_library": _prebuilt_cxx_library_macro_stub,
"python_library": _python_library_macro_stub,
"rust_binary": _rust_binary_macro_stub,
"rust_library": _rust_library_macro_stub,
"rust_test": _rust_test_macro_stub,
"rust_with_workspace": with_rust_workspace,
"swift_toolchain": _swift_toolchain_macro_stub,
"versioned_alias": _versioned_alias_macro_stub,
}
__overridden_builtins__ = {
"read_config": _read_config,
"read_root_config": _read_root_config,
} if log_buckconfigs else {}
__shimmed_native__ = __struct_to_dict(__buck2_builtins__)
__shimmed_native__.update(__overridden_builtins__)
__shimmed_native__.update(__rules__)
__shimmed_native__.update(_user_rules)
# Should come after the rules which are macro overridden
__shimmed_native__.update(__extra_rules__)
__shimmed_native__.update({"cxx": _cxx, "python": _python})
__shimmed_native__.update({
"__internal_autodeps_hacks__": struct(
rust_link_info = RustLinkInfo,
),
})
native = struct(**__shimmed_native__)