forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc_dist_library.bzl
400 lines (342 loc) · 13.6 KB
/
cc_dist_library.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
# Rules for distributable C++ libraries
load("@rules_cc//cc:action_names.bzl", cc_action_names = "ACTION_NAMES")
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
################################################################################
# Archive/linking support
################################################################################
def _collect_linker_input_objects(dep_label, cc_info, objs, pic_objs):
"""Accumulate .o and .pic.o files into `objs` and `pic_objs`."""
link_ctx = cc_info.linking_context
if link_ctx == None:
return
linker_inputs = link_ctx.linker_inputs.to_list()
for link_input in linker_inputs:
if link_input.owner != dep_label:
# This is a transitive dep: skip it.
continue
for lib in link_input.libraries:
objs.extend(lib.objects or [])
pic_objs.extend(lib.pic_objects or [])
# Creates an action to build the `output_file` static library (archive)
# using `object_files`.
def _create_archive_action(
ctx,
feature_configuration,
cc_toolchain_info,
output_file,
object_files):
# Based on Bazel's src/main/starlark/builtins_bzl/common/cc/cc_import.bzl:
# Build the command line and add args for all of the input files:
archiver_variables = cc_common.create_link_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain_info,
output_file = output_file.path,
is_using_linker = False,
)
command_line = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = cc_action_names.cpp_link_static_library,
variables = archiver_variables,
)
args = ctx.actions.args()
args.add_all(command_line)
args.add_all(object_files)
args.use_param_file("@%s", use_always = True)
archiver_path = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = cc_action_names.cpp_link_static_library,
)
env = cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = cc_action_names.cpp_link_static_library,
variables = archiver_variables,
)
ctx.actions.run(
executable = archiver_path,
arguments = [args],
env = env,
inputs = depset(
direct = object_files,
transitive = [
cc_toolchain_info.all_files,
],
),
use_default_shell_env = False,
outputs = [output_file],
mnemonic = "CppArchiveDist",
)
def _create_dso_link_action(
ctx,
feature_configuration,
cc_toolchain_info,
object_files,
pic_object_files):
compilation_outputs = cc_common.create_compilation_outputs(
objects = depset(object_files),
pic_objects = depset(pic_object_files),
)
link_output = cc_common.link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain_info,
compilation_outputs = compilation_outputs,
name = ctx.label.name,
output_type = "dynamic_library",
user_link_flags = ctx.attr.linkopts,
)
library_to_link = link_output.library_to_link
outputs = []
# Note: library_to_link.dynamic_library and interface_library are often
# symlinks in the solib directory. For DefaultInfo, prefer reporting
# the resolved artifact paths.
if library_to_link.resolved_symlink_dynamic_library != None:
outputs.append(library_to_link.resolved_symlink_dynamic_library)
elif library_to_link.dynamic_library != None:
outputs.append(library_to_link.dynamic_library)
if library_to_link.resolved_symlink_interface_library != None:
outputs.append(library_to_link.resolved_symlink_interface_library)
elif library_to_link.interface_library != None:
outputs.append(library_to_link.interface_library)
return outputs
################################################################################
# Source file/header support
################################################################################
CcFileList = provider(
doc = "List of files to be built into a library.",
fields = {
# As a rule of thumb, `hdrs` and `textual_hdrs` are the files that
# would be installed along with a prebuilt library.
"hdrs": "public header files, including those used by generated code",
"textual_hdrs": "files which are included but are not self-contained",
# The `internal_hdrs` are header files which appear in `srcs`.
# These are only used when compiling the library.
"internal_hdrs": "internal header files (only used to build .cc files)",
"srcs": "source files",
},
)
def _flatten_target_files(targets):
return depset(transitive = [target.files for target in targets])
files = []
for target in targets:
files.extend(target.files.to_list())
return files
def _cc_file_list_aspect_impl(target, ctx):
# Extract sources from a `cc_library` (or similar):
if CcInfo not in target:
return []
# We're going to reach directly into the attrs on the traversed rule.
rule_attr = ctx.rule.attr
# CcInfo is a proxy for what we expect this rule to look like.
# However, some deps may expose `CcInfo` without having `srcs`,
# `hdrs`, etc., so we use `getattr` to handle that gracefully.
internal_hdrs = []
srcs = []
# Filter `srcs` so it only contains source files. Headers will go
# into `internal_headers`.
for src in _flatten_target_files(getattr(rule_attr, "srcs", [])).to_list():
if src.extension.lower() in ["c", "cc", "cpp", "cxx"]:
srcs.append(src)
else:
internal_hdrs.append(src)
return [CcFileList(
hdrs = _flatten_target_files(getattr(rule_attr, "hdrs", depset())),
textual_hdrs = _flatten_target_files(getattr(
rule_attr,
"textual_hdrs",
depset(),
)),
internal_hdrs = depset(internal_hdrs),
srcs = depset(srcs),
)]
cc_file_list_aspect = aspect(
doc = """
Aspect to provide the list of sources and headers from a rule.
Output is CcFileList. Example:
cc_library(
name = "foo",
srcs = [
"foo.cc",
"foo_internal.h",
],
hdrs = ["foo.h"],
textual_hdrs = ["foo_inl.inc"],
)
# produces:
# CcFileList(
# hdrs = depset([File("foo.h")]),
# textual_hdrs = depset([File("foo_inl.inc")]),
# internal_hdrs = depset([File("foo_internal.h")]),
# srcs = depset([File("foo.cc")]),
# )
""",
implementation = _cc_file_list_aspect_impl,
)
################################################################################
# Rule impl
################################################################################
def _collect_inputs(deps):
"""Collects files from a list of immediate deps.
This rule collects source files and linker inputs for C++ deps. Only
these immediate deps are considered, not transitive deps.
The return value is a struct with object files (linker inputs),
partitioned by PIC and non-pic, and the rules' source and header files:
struct(
objects = ..., # non-PIC object files
pic_objects = ..., # PIC objects
cc_file_list = ..., # a CcFileList
)
Args:
deps: Iterable of immediate deps. These will be treated as the "inputs,"
but not the transitive deps.
Returns:
A struct with linker inputs, source files, and header files.
"""
objs = []
pic_objs = []
# The returned CcFileList will contain depsets of the deps' file lists.
# These lists hold `depset()`s from each of `deps`.
srcs = []
hdrs = []
internal_hdrs = []
textual_hdrs = []
for dep in deps:
if CcInfo in dep:
_collect_linker_input_objects(
dep.label,
dep[CcInfo],
objs,
pic_objs,
)
if CcFileList in dep:
cfl = dep[CcFileList]
srcs.append(cfl.srcs)
hdrs.append(cfl.hdrs)
internal_hdrs.append(cfl.internal_hdrs)
textual_hdrs.append(cfl.textual_hdrs)
return struct(
objects = objs,
pic_objects = pic_objs,
cc_file_list = CcFileList(
srcs = depset(transitive = srcs),
hdrs = depset(transitive = hdrs),
internal_hdrs = depset(transitive = internal_hdrs),
textual_hdrs = depset(transitive = textual_hdrs),
),
)
# Implementation for cc_dist_library rule.
def _cc_dist_library_impl(ctx):
cc_toolchain_info = find_cc_toolchain(ctx)
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain_info,
)
inputs = _collect_inputs(ctx.attr.deps)
# For static libraries, build separately with and without pic.
stemname = "lib" + ctx.label.name
outputs = []
if len(inputs.objects) > 0:
archive_out = ctx.actions.declare_file(stemname + ".a")
_create_archive_action(
ctx,
feature_configuration,
cc_toolchain_info,
archive_out,
inputs.objects,
)
outputs.append(archive_out)
if len(inputs.pic_objects) > 0:
pic_archive_out = ctx.actions.declare_file(stemname + ".pic.a")
_create_archive_action(
ctx,
feature_configuration,
cc_toolchain_info,
pic_archive_out,
inputs.pic_objects,
)
outputs.append(pic_archive_out)
# For dynamic libraries, use the `cc_common.link` command to ensure
# everything gets built correctly according to toolchain definitions.
outputs.extend(_create_dso_link_action(
ctx,
feature_configuration,
cc_toolchain_info,
inputs.objects,
inputs.pic_objects,
))
# We could expose the libraries for use from cc rules:
#
# linking_context = cc_common.create_linking_context(
# linker_inputs = depset([
# cc_common.create_linker_input(
# owner = ctx.label,
# libraries = depset([library_to_link]),
# ),
# ]),
# )
# cc_info = CcInfo(linking_context = linking_context) # and return this
#
# However, if the goal is to force a protobuf dependency to use the
# DSO, then `cc_import` is a better-supported way to do so.
#
# If we wanted to expose CcInfo from this rule (and make it usable as a
# C++ dependency), then we would probably want to include the static
# archive and headers as well. exposing headers would probably require
# an additional aspect to extract CcInfos with just the deps' headers.
return [
DefaultInfo(files = depset(outputs)),
inputs.cc_file_list,
]
cc_dist_library = rule(
implementation = _cc_dist_library_impl,
doc = """
Create libraries suitable for distribution.
This rule creates static and dynamic libraries from the libraries listed in
'deps'. The resulting libraries are suitable for distributing all of 'deps'
in a single logical library, for example, in an installable binary package.
Only the targets listed in 'deps' are included in the result (i.e., the
output does not include transitive dependencies), allowing precise control
over the library boundary.
The outputs of this rule are a dynamic library and a static library. (If
the build produces both PIC and non-PIC object files, then there is also a
second static library.) The example below illustrates additional details.
This rule is different from Bazel's experimental `shared_cc_library` in
several ways. First, this rule ignores transitive dependencies, which means
that dynamic library dependencies generally need to be specified via
'linkopts'. Second, this rule produces a static archive library in addition
to the dynamic shared library. Third, this rule is not directly usable as a
C++ dependency (although the outputs could be used, e.g., by `cc_import`).
Example:
cc_library(name = "a", srcs = ["a.cc"], hdrs = ["a.h"])
cc_library(name = "b", srcs = ["b.cc"], hdrs = ["b.h"], deps = [":a"])
cc_library(name = "c", srcs = ["c.cc"], hdrs = ["c.h"], deps = [":b"])
# Creates libdist.so and (typically) libdist.pic.a:
# (This may also produce libdist.a if the build produces non-PIC objects.)
cc_dist_library(
name = "dist",
linkopts = ["-la"], # libdist.so dynamically links against liba.so.
deps = [":b", ":c"], # Output contains b.o and c.o, but not a.o.
)
""",
attrs = {
"deps": attr.label_list(
doc = ("The list of libraries to be included in the outputs. " +
"Only these targets' compilation outputs will be " +
"included (i.e., the transitive dependencies are not " +
"included in the output)."),
aspects = [cc_file_list_aspect],
),
"linkopts": attr.string_list(
doc = ("Add these flags to the C++ linker command when creating " +
"the dynamic library."),
),
# C++ toolchain before https://github.com/bazelbuild/bazel/issues/7260:
"_cc_toolchain": attr.label(
default = Label("@rules_cc//cc:current_cc_toolchain"),
),
},
toolchains = [
# C++ toolchain after https://github.com/bazelbuild/bazel/issues/7260:
"@bazel_tools//tools/cpp:toolchain_type",
],
fragments = ["cpp"],
)