| 1 |
# Rules for distributable C++ libraries |
| 2 |
|
| 3 |
load("@rules_cc//cc:action_names.bzl", cc_action_names = "ACTION_NAMES") |
| 4 |
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain") |
| 5 |
|
| 6 |
################################################################################ |
| 7 |
# Archive/linking support |
| 8 |
################################################################################ |
| 9 |
|
| 10 |
def _collect_linker_input_objects(dep_label, cc_info, objs, pic_objs): |
| 11 |
"""Accumulate .o and .pic.o files into `objs` and `pic_objs`.""" |
| 12 |
link_ctx = cc_info.linking_context |
| 13 |
if link_ctx == None: |
| 14 |
return |
| 15 |
|
| 16 |
linker_inputs = link_ctx.linker_inputs.to_list() |
| 17 |
for link_input in linker_inputs: |
| 18 |
if link_input.owner != dep_label: |
| 19 |
# This is a transitive dep: skip it. |
| 20 |
continue |
| 21 |
|
| 22 |
for lib in link_input.libraries: |
| 23 |
objs.extend(lib.objects or []) |
| 24 |
pic_objs.extend(lib.pic_objects or []) |
| 25 |
|
| 26 |
# Creates an action to build the `output_file` static library (archive) |
| 27 |
# using `object_files`. |
| 28 |
def _create_archive_action( |
| 29 |
ctx, |
| 30 |
feature_configuration, |
| 31 |
cc_toolchain_info, |
| 32 |
output_file, |
| 33 |
object_files): |
| 34 |
# Based on Bazel's src/main/starlark/builtins_bzl/common/cc/cc_import.bzl: |
| 35 |
|
| 36 |
# Build the command line and add args for all of the input files: |
| 37 |
archiver_variables = cc_common.create_link_variables( |
| 38 |
feature_configuration = feature_configuration, |
| 39 |
cc_toolchain = cc_toolchain_info, |
| 40 |
output_file = output_file.path, |
| 41 |
is_using_linker = False, |
| 42 |
) |
| 43 |
command_line = cc_common.get_memory_inefficient_command_line( |
| 44 |
feature_configuration = feature_configuration, |
| 45 |
action_name = cc_action_names.cpp_link_static_library, |
| 46 |
variables = archiver_variables, |
| 47 |
) |
| 48 |
args = ctx.actions.args() |
| 49 |
args.add_all(command_line) |
| 50 |
args.add_all(object_files) |
| 51 |
args.use_param_file("@%s", use_always = True) |
| 52 |
|
| 53 |
archiver_path = cc_common.get_tool_for_action( |
| 54 |
feature_configuration = feature_configuration, |
| 55 |
action_name = cc_action_names.cpp_link_static_library, |
| 56 |
) |
| 57 |
|
| 58 |
env = cc_common.get_environment_variables( |
| 59 |
feature_configuration = feature_configuration, |
| 60 |
action_name = cc_action_names.cpp_link_static_library, |
| 61 |
variables = archiver_variables, |
| 62 |
) |
| 63 |
|
| 64 |
ctx.actions.run( |
| 65 |
executable = archiver_path, |
| 66 |
arguments = [args], |
| 67 |
env = env, |
| 68 |
inputs = depset( |
| 69 |
direct = object_files, |
| 70 |
transitive = [ |
| 71 |
cc_toolchain_info.all_files, |
| 72 |
], |
| 73 |
), |
| 74 |
use_default_shell_env = False, |
| 75 |
outputs = [output_file], |
| 76 |
mnemonic = "CppArchiveDist", |
| 77 |
) |
| 78 |
|
| 79 |
def _create_dso_link_action( |
| 80 |
ctx, |
| 81 |
feature_configuration, |
| 82 |
cc_toolchain_info, |
| 83 |
object_files, |
| 84 |
pic_object_files): |
| 85 |
compilation_outputs = cc_common.create_compilation_outputs( |
| 86 |
objects = depset(object_files), |
| 87 |
pic_objects = depset(pic_object_files), |
| 88 |
) |
| 89 |
link_output = cc_common.link( |
| 90 |
actions = ctx.actions, |
| 91 |
feature_configuration = feature_configuration, |
| 92 |
cc_toolchain = cc_toolchain_info, |
| 93 |
compilation_outputs = compilation_outputs, |
| 94 |
name = ctx.label.name, |
| 95 |
output_type = "dynamic_library", |
| 96 |
user_link_flags = ctx.attr.linkopts, |
| 97 |
) |
| 98 |
library_to_link = link_output.library_to_link |
| 99 |
|
| 100 |
outputs = [] |
| 101 |
|
| 102 |
# Note: library_to_link.dynamic_library and interface_library are often |
| 103 |
# symlinks in the solib directory. For DefaultInfo, prefer reporting |
| 104 |
# the resolved artifact paths. |
| 105 |
if library_to_link.resolved_symlink_dynamic_library != None: |
| 106 |
outputs.append(library_to_link.resolved_symlink_dynamic_library) |
| 107 |
elif library_to_link.dynamic_library != None: |
| 108 |
outputs.append(library_to_link.dynamic_library) |
| 109 |
|
| 110 |
if library_to_link.resolved_symlink_interface_library != None: |
| 111 |
outputs.append(library_to_link.resolved_symlink_interface_library) |
| 112 |
elif library_to_link.interface_library != None: |
| 113 |
outputs.append(library_to_link.interface_library) |
| 114 |
|
| 115 |
return outputs |
| 116 |
|
| 117 |
################################################################################ |
| 118 |
# Source file/header support |
| 119 |
################################################################################ |
| 120 |
|
| 121 |
CcFileList = provider( |
| 122 |
doc = "List of files to be built into a library.", |
| 123 |
fields = { |
| 124 |
# As a rule of thumb, `hdrs` and `textual_hdrs` are the files that |
| 125 |
# would be installed along with a prebuilt library. |
| 126 |
"hdrs": "public header files, including those used by generated code", |
| 127 |
"textual_hdrs": "files which are included but are not self-contained", |
| 128 |
|
| 129 |
# The `internal_hdrs` are header files which appear in `srcs`. |
| 130 |
# These are only used when compiling the library. |
| 131 |
"internal_hdrs": "internal header files (only used to build .cc files)", |
| 132 |
"srcs": "source files", |
| 133 |
}, |
| 134 |
) |
| 135 |
|
| 136 |
def _flatten_target_files(targets): |
| 137 |
return depset(transitive = [ |
| 138 |
target.files |
| 139 |
for target in targets |
| 140 |
# Filter out targets from external workspaces |
| 141 |
if target.label.workspace_name == "" or |
| 142 |
target.label.workspace_name == "com_google_protobuf" |
| 143 |
]) |
| 144 |
|
| 145 |
def _get_transitive_sources(targets, attr, deps): |
| 146 |
return depset(targets, transitive = [getattr(dep[CcFileList], attr) for dep in deps if CcFileList in dep]) |
| 147 |
|
| 148 |
def _cc_file_list_aspect_impl(target, ctx): |
| 149 |
# Extract sources from a `cc_library` (or similar): |
| 150 |
if CcInfo not in target: |
| 151 |
return [] |
| 152 |
|
| 153 |
# We're going to reach directly into the attrs on the traversed rule. |
| 154 |
rule_attr = ctx.rule.attr |
| 155 |
|
| 156 |
# CcInfo is a proxy for what we expect this rule to look like. |
| 157 |
# However, some deps may expose `CcInfo` without having `srcs`, |
| 158 |
# `hdrs`, etc., so we use `getattr` to handle that gracefully. |
| 159 |
|
| 160 |
internal_hdrs = [] |
| 161 |
srcs = [] |
| 162 |
|
| 163 |
# Filter `srcs` so it only contains source files. Headers will go |
| 164 |
# into `internal_headers`. |
| 165 |
for src in _flatten_target_files(getattr(rule_attr, "srcs", [])).to_list(): |
| 166 |
if src.extension.lower() in ["c", "cc", "cpp", "cxx"]: |
| 167 |
srcs.append(src) |
| 168 |
else: |
| 169 |
internal_hdrs.append(src) |
| 170 |
|
| 171 |
return [CcFileList( |
| 172 |
hdrs = _get_transitive_sources( |
| 173 |
_flatten_target_files(rule_attr.hdrs).to_list(), |
| 174 |
"hdrs", |
| 175 |
rule_attr.deps, |
| 176 |
), |
| 177 |
textual_hdrs = _get_transitive_sources( |
| 178 |
_flatten_target_files(rule_attr.textual_hdrs).to_list(), |
| 179 |
"textual_hdrs", |
| 180 |
rule_attr.deps, |
| 181 |
), |
| 182 |
internal_hdrs = _get_transitive_sources( |
| 183 |
internal_hdrs, |
| 184 |
"internal_hdrs", |
| 185 |
rule_attr.deps, |
| 186 |
), |
| 187 |
srcs = _get_transitive_sources(srcs, "srcs", rule_attr.deps), |
| 188 |
)] |
| 189 |
|
| 190 |
cc_file_list_aspect = aspect( |
| 191 |
doc = """ |
| 192 |
Aspect to provide the list of sources and headers from a rule. |
| 193 |
|
| 194 |
Output is CcFileList. Example: |
| 195 |
|
| 196 |
cc_library( |
| 197 |
name = "foo", |
| 198 |
srcs = [ |
| 199 |
"foo.cc", |
| 200 |
"foo_internal.h", |
| 201 |
], |
| 202 |
hdrs = ["foo.h"], |
| 203 |
textual_hdrs = ["foo_inl.inc"], |
| 204 |
) |
| 205 |
# produces: |
| 206 |
# CcFileList( |
| 207 |
# hdrs = depset([File("foo.h")]), |
| 208 |
# textual_hdrs = depset([File("foo_inl.inc")]), |
| 209 |
# internal_hdrs = depset([File("foo_internal.h")]), |
| 210 |
# srcs = depset([File("foo.cc")]), |
| 211 |
# ) |
| 212 |
""", |
| 213 |
required_providers = [CcInfo], |
| 214 |
implementation = _cc_file_list_aspect_impl, |
| 215 |
attr_aspects = ["deps"], |
| 216 |
) |
| 217 |
|
| 218 |
################################################################################ |
| 219 |
# Rule impl |
| 220 |
################################################################################ |
| 221 |
|
| 222 |
def _collect_inputs(deps): |
| 223 |
"""Collects files from a list of deps. |
| 224 |
|
| 225 |
This rule collects source files and linker inputs transitively for C++ |
| 226 |
deps. |
| 227 |
|
| 228 |
The return value is a struct with object files (linker inputs), |
| 229 |
partitioned by PIC and non-pic, and the rules' source and header files: |
| 230 |
|
| 231 |
struct( |
| 232 |
objects = ..., # non-PIC object files |
| 233 |
pic_objects = ..., # PIC objects |
| 234 |
cc_file_list = ..., # a CcFileList |
| 235 |
) |
| 236 |
|
| 237 |
Args: |
| 238 |
deps: Iterable of immediate deps, which will be treated as roots to |
| 239 |
recurse transitively. |
| 240 |
Returns: |
| 241 |
A struct with linker inputs, source files, and header files. |
| 242 |
""" |
| 243 |
|
| 244 |
objs = [] |
| 245 |
pic_objs = [] |
| 246 |
|
| 247 |
# The returned CcFileList will contain depsets of the deps' file lists. |
| 248 |
# These lists hold `depset()`s from each of `deps`. |
| 249 |
srcs = [] |
| 250 |
hdrs = [] |
| 251 |
internal_hdrs = [] |
| 252 |
textual_hdrs = [] |
| 253 |
|
| 254 |
for dep in deps: |
| 255 |
if CcInfo in dep: |
| 256 |
_collect_linker_input_objects( |
| 257 |
dep.label, |
| 258 |
dep[CcInfo], |
| 259 |
objs, |
| 260 |
pic_objs, |
| 261 |
) |
| 262 |
|
| 263 |
if CcFileList in dep: |
| 264 |
cfl = dep[CcFileList] |
| 265 |
srcs.append(cfl.srcs) |
| 266 |
hdrs.append(cfl.hdrs) |
| 267 |
internal_hdrs.append(cfl.internal_hdrs) |
| 268 |
textual_hdrs.append(cfl.textual_hdrs) |
| 269 |
|
| 270 |
return struct( |
| 271 |
objects = objs, |
| 272 |
pic_objects = pic_objs, |
| 273 |
cc_file_list = CcFileList( |
| 274 |
srcs = depset(transitive = srcs), |
| 275 |
hdrs = depset(transitive = hdrs), |
| 276 |
internal_hdrs = depset(transitive = internal_hdrs), |
| 277 |
textual_hdrs = depset(transitive = textual_hdrs), |
| 278 |
), |
| 279 |
) |
| 280 |
|
| 281 |
# Given structs a and b returned from _collect_inputs(), returns a copy of a |
| 282 |
# but with all files from b subtracted out. |
| 283 |
def _subtract_files(a, b): |
| 284 |
result_args = {} |
| 285 |
|
| 286 |
top_level_fields = ["objects", "pic_objects"] |
| 287 |
for field in top_level_fields: |
| 288 |
to_remove = {e: None for e in getattr(b, field)} |
| 289 |
result_args[field] = [e for e in getattr(a, field) if not e in to_remove] |
| 290 |
|
| 291 |
cc_file_list_args = {} |
| 292 |
file_list_fields = ["srcs", "hdrs", "internal_hdrs", "textual_hdrs"] |
| 293 |
for field in file_list_fields: |
| 294 |
to_remove = {e: None for e in getattr(b.cc_file_list, field).to_list()} |
| 295 |
cc_file_list_args[field] = depset( |
| 296 |
[e for e in getattr(a.cc_file_list, field).to_list() if not e in to_remove], |
| 297 |
) |
| 298 |
result_args["cc_file_list"] = CcFileList(**cc_file_list_args) |
| 299 |
|
| 300 |
return struct(**result_args) |
| 301 |
|
| 302 |
# Implementation for cc_dist_library rule. |
| 303 |
def _cc_dist_library_impl(ctx): |
| 304 |
cc_toolchain_info = find_cc_toolchain(ctx) |
| 305 |
|
| 306 |
feature_configuration = cc_common.configure_features( |
| 307 |
ctx = ctx, |
| 308 |
cc_toolchain = cc_toolchain_info, |
| 309 |
) |
| 310 |
|
| 311 |
inputs = _subtract_files(_collect_inputs(ctx.attr.deps), _collect_inputs(ctx.attr.dist_deps)) |
| 312 |
|
| 313 |
# For static libraries, build separately with and without pic. |
| 314 |
|
| 315 |
stemname = "lib" + ctx.label.name |
| 316 |
outputs = [] |
| 317 |
|
| 318 |
if len(inputs.objects) > 0: |
| 319 |
archive_out = ctx.actions.declare_file(stemname + ".a") |
| 320 |
_create_archive_action( |
| 321 |
ctx, |
| 322 |
feature_configuration, |
| 323 |
cc_toolchain_info, |
| 324 |
archive_out, |
| 325 |
inputs.objects, |
| 326 |
) |
| 327 |
outputs.append(archive_out) |
| 328 |
|
| 329 |
if len(inputs.pic_objects) > 0: |
| 330 |
pic_archive_out = ctx.actions.declare_file(stemname + ".pic.a") |
| 331 |
_create_archive_action( |
| 332 |
ctx, |
| 333 |
feature_configuration, |
| 334 |
cc_toolchain_info, |
| 335 |
pic_archive_out, |
| 336 |
inputs.pic_objects, |
| 337 |
) |
| 338 |
outputs.append(pic_archive_out) |
| 339 |
|
| 340 |
# For dynamic libraries, use the `cc_common.link` command to ensure |
| 341 |
# everything gets built correctly according to toolchain definitions. |
| 342 |
outputs.extend(_create_dso_link_action( |
| 343 |
ctx, |
| 344 |
feature_configuration, |
| 345 |
cc_toolchain_info, |
| 346 |
inputs.objects, |
| 347 |
inputs.pic_objects, |
| 348 |
)) |
| 349 |
|
| 350 |
# We could expose the libraries for use from cc rules: |
| 351 |
# |
| 352 |
# linking_context = cc_common.create_linking_context( |
| 353 |
# linker_inputs = depset([ |
| 354 |
# cc_common.create_linker_input( |
| 355 |
# owner = ctx.label, |
| 356 |
# libraries = depset([library_to_link]), |
| 357 |
# ), |
| 358 |
# ]), |
| 359 |
# ) |
| 360 |
# cc_info = CcInfo(linking_context = linking_context) # and return this |
| 361 |
# |
| 362 |
# However, if the goal is to force a protobuf dependency to use the |
| 363 |
# DSO, then `cc_import` is a better-supported way to do so. |
| 364 |
# |
| 365 |
# If we wanted to expose CcInfo from this rule (and make it usable as a |
| 366 |
# C++ dependency), then we would probably want to include the static |
| 367 |
# archive and headers as well. exposing headers would probably require |
| 368 |
# an additional aspect to extract CcInfos with just the deps' headers. |
| 369 |
|
| 370 |
return [ |
| 371 |
DefaultInfo(files = depset(outputs)), |
| 372 |
inputs.cc_file_list, |
| 373 |
] |
| 374 |
|
| 375 |
cc_dist_library = rule( |
| 376 |
implementation = _cc_dist_library_impl, |
| 377 |
doc = """ |
| 378 |
Create libraries suitable for distribution. |
| 379 |
|
| 380 |
This rule creates static and dynamic libraries from the libraries listed in |
| 381 |
'deps'. The resulting libraries are suitable for distributing all of 'deps' |
| 382 |
in a single logical library, for example, in an installable binary package. |
| 383 |
The result includes all transitive dependencies, excluding those reachable |
| 384 |
from 'dist_deps' or defined in a separate repository (e.g. Abseil). |
| 385 |
|
| 386 |
The outputs of this rule are a dynamic library and a static library. (If |
| 387 |
the build produces both PIC and non-PIC object files, then there is also a |
| 388 |
second static library.) The example below illustrates additional details. |
| 389 |
|
| 390 |
This rule is different from Bazel's experimental `shared_cc_library` in two |
| 391 |
ways. First, this rule produces a static archive library in addition to the |
| 392 |
dynamic shared library. Second, this rule is not directly usable as a C++ |
| 393 |
dependency (although the outputs could be used, e.g., by `cc_import`). |
| 394 |
|
| 395 |
Example: |
| 396 |
|
| 397 |
cc_library(name = "a", srcs = ["a.cc"], hdrs = ["a.h"]) |
| 398 |
cc_library(name = "b", srcs = ["b.cc"], hdrs = ["b.h"], deps = [":a"]) |
| 399 |
cc_library(name = "c", srcs = ["c.cc"], hdrs = ["c.h"], deps = [":b"]) |
| 400 |
|
| 401 |
# Creates libdist.so and (typically) libdist.pic.a: |
| 402 |
# (This may also produce libdist.a if the build produces non-PIC objects.) |
| 403 |
cc_dist_library( |
| 404 |
name = "dist", |
| 405 |
linkopts = ["-la"], # libdist.so dynamically links against liba.so. |
| 406 |
deps = [":b", ":c"], # Output contains a.o, b.o, and c.o. |
| 407 |
) |
| 408 |
""", |
| 409 |
attrs = { |
| 410 |
"deps": attr.label_list( |
| 411 |
doc = ("The list of libraries to be included in the outputs, " + |
| 412 |
"along with their transitive dependencies."), |
| 413 |
aspects = [cc_file_list_aspect], |
| 414 |
), |
| 415 |
"dist_deps": attr.label_list( |
| 416 |
doc = ("The list of cc_dist_library dependencies that " + |
| 417 |
"should be excluded."), |
| 418 |
aspects = [cc_file_list_aspect], |
| 419 |
), |
| 420 |
"linkopts": attr.string_list( |
| 421 |
doc = ("Add these flags to the C++ linker command when creating " + |
| 422 |
"the dynamic library."), |
| 423 |
), |
| 424 |
# C++ toolchain before https://github.com/bazelbuild/bazel/issues/7260: |
| 425 |
"_cc_toolchain": attr.label( |
| 426 |
default = Label("@rules_cc//cc:current_cc_toolchain"), |
| 427 |
), |
| 428 |
}, |
| 429 |
toolchains = [ |
| 430 |
# C++ toolchain after https://github.com/bazelbuild/bazel/issues/7260: |
| 431 |
"@bazel_tools//tools/cpp:toolchain_type", |
| 432 |
], |
| 433 |
fragments = ["cpp"], |
| 434 |
) |