This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
forked from bazel-ios/rules_ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmap.bzl
131 lines (109 loc) · 3.88 KB
/
hmap.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
"""Header Map rules"""
HeaderMapInfo = provider(
doc = "Propagates header maps",
fields = {
"files": "depset with headermaps",
},
)
def _make_hmap(actions, headermap_builder, output, namespace, hdrs_lists):
"""Makes an hmap file.
Args:
actions: a ctx.actions struct
headermap_builder: an executable pointing to @bazel_build_rules_ios//rules/hmap:hmaptool
output: the output file that will contain the built hmap
namespace: the prefix to be used for header imports
hdrs_lists: an array of enumerables containing headers to be added to the hmap
"""
args = actions.args()
if namespace:
args.add("--namespace", namespace)
args.add("--output", output)
for hdrs in hdrs_lists:
args.add_all(hdrs)
args.set_param_file_format(format = "multiline")
args.use_param_file("@%s")
actions.run(
mnemonic = "HmapCreate",
arguments = [args],
executable = headermap_builder,
outputs = [output],
)
def _make_headermap_impl(ctx):
"""Implementation of the headermap() rule.
It creates a text file with
the mappings and creates an action that calls out to the hmapbuild
tool included here to create the actual .hmap file.
:param ctx: context for this rule. See
https://docs.bazel.build/versions/master/starlark/lib/ctx.html
:return: provider with the info for this rule
"""
hdrs_lists = [ctx.files.hdrs]
for provider in ctx.attr.direct_hdr_providers:
if apple_common.Objc in provider:
hdrs_lists.append(provider[apple_common.Objc].direct_headers)
if CcInfo in provider:
hdrs_lists.append(provider[CcInfo].compilation_context.direct_headers)
if len(hdrs_lists) == 1:
# means neither apple_common.Objc nor CcInfo in hdr provider target
fail("direct_hdr_provider %s must contain either 'CcInfo' or 'objc' provider" % provider)
hmap.make_hmap(
actions = ctx.actions,
headermap_builder = ctx.executable._headermap_builder,
output = ctx.outputs.headermap,
namespace = ctx.attr.namespace,
hdrs_lists = hdrs_lists,
)
cc_info_provider = CcInfo(
compilation_context = cc_common.create_compilation_context(
headers = depset([ctx.outputs.headermap]),
),
)
providers = [
apple_common.new_objc_provider(),
cc_info_provider,
]
hdrs_lists = [l for l in hdrs_lists if l]
if len(hdrs_lists) > 0:
providers.append(HeaderMapInfo(
files = depset([ctx.outputs.headermap]),
))
return providers
# Derive a headermap from transitive headermaps
# hdrs: a file group containing headers for this rule
# namespace: the Apple style namespace these header should be under
headermap = rule(
implementation = _make_headermap_impl,
output_to_genfiles = True,
attrs = {
"namespace": attr.string(
mandatory = False,
doc = "The prefix to be used for header imports",
),
"hdrs": attr.label_list(
mandatory = True,
allow_files = True,
doc = "The list of headers included in the headermap",
),
"direct_hdr_providers": attr.label_list(
mandatory = False,
doc = "Targets whose direct headers should be added to the list of hdrs",
),
"_headermap_builder": attr.label(
executable = True,
cfg = "host",
default = Label(
"//rules/hmap:hmaptool",
),
),
},
outputs = {"headermap": "%{name}.hmap"},
doc = """\
Creates a binary headermap file from the given headers,
suitable for passing to clang.
This can be used to allow headers to be imported at a consistent path,
regardless of the package structure being used.
""",
)
hmap = struct(
make_hmap = _make_hmap,
)