forked from facebook/buck2-prelude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifact_tset.bzl
92 lines (77 loc) · 2.63 KB
/
artifact_tset.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
# 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.
load("@prelude//utils:expect.bzl", "expect")
load(
"@prelude//utils:utils.bzl",
"flatten",
)
# Generic tag to provide more information about the artifact
ArtifactInfoTag = enum(
# Describes swiftmodule artifact generated by swift rules.
"swiftmodule",
# Describes clang pcm module artifact generated and consumed by swift rules.
"swift_pcm",
)
ArtifactInfo = record(
label = field(Label),
artifacts = field(list[Artifact]),
tags = field(list[ArtifactInfoTag]),
)
def _get_artifacts(entries: list[ArtifactInfo]) -> list[Artifact]:
return flatten([entry.artifacts for entry in entries])
_ArtifactTSet = transitive_set(
args_projections = {
"artifacts": _get_artifacts,
},
)
ArtifactTSet = record(
_tset = field([_ArtifactTSet, None], None),
)
def make_artifact_tset(
actions: AnalysisActions,
# Must be non-`None` if artifacts are passed in to `artifacts`.
label: [Label, None] = None,
artifacts: list[Artifact] = [],
infos: list[ArtifactInfo] = [],
children: list[ArtifactTSet] = [],
tags: list[ArtifactInfoTag] = []) -> ArtifactTSet:
expect(
label != None or not artifacts,
"must pass in `label` to associate with artifacts",
)
# As a convenience for our callers, filter our `None` children.
children = [c._tset for c in children if c._tset != None]
# Build list of all non-child values.
values = []
if artifacts:
values.append(ArtifactInfo(label = label, artifacts = artifacts, tags = tags))
values.extend(infos)
# If there's no children or artifacts, return `None`.
if not values and not children:
return ArtifactTSet()
# We only build a `_ArtifactTSet` if there's something to package.
kwargs = {}
if values:
kwargs["value"] = values
if children:
kwargs["children"] = children
return ArtifactTSet(
_tset = actions.tset(_ArtifactTSet, **kwargs),
)
def project_artifacts(
actions: AnalysisActions,
tsets: list[ArtifactTSet] = []) -> list[TransitiveSetArgsProjection]:
"""
Helper to project a list of optional tsets.
"""
tset = make_artifact_tset(
actions = actions,
children = tsets,
)
if tset._tset == None:
return []
return [tset._tset.project_as_args("artifacts")]