1# Rules for distributable C++ libraries 2 3load("@rules_cc//cc:action_names.bzl", cc_action_names = "ACTION_NAMES") 4load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain") 5 6# Creates an action to build the `output_file` static library (archive) 7# using `object_files`. 8def _create_archive_action( 9 ctx, 10 feature_configuration, 11 cc_toolchain, 12 output_file, 13 object_files): 14 # Based on Bazel's src/main/starlark/builtins_bzl/common/cc/cc_import.bzl: 15 16 # Build the command line and add args for all of the input files: 17 archiver_variables = cc_common.create_link_variables( 18 feature_configuration = feature_configuration, 19 cc_toolchain = cc_toolchain, 20 output_file = output_file.path, 21 is_using_linker = False, 22 ) 23 command_line = cc_common.get_memory_inefficient_command_line( 24 feature_configuration = feature_configuration, 25 action_name = cc_action_names.cpp_link_static_library, 26 variables = archiver_variables, 27 ) 28 args = ctx.actions.args() 29 args.add_all(command_line) 30 args.add_all(object_files) 31 args.use_param_file("@%s", use_always = True) 32 33 archiver_path = cc_common.get_tool_for_action( 34 feature_configuration = feature_configuration, 35 action_name = cc_action_names.cpp_link_static_library, 36 ) 37 38 env = cc_common.get_environment_variables( 39 feature_configuration = feature_configuration, 40 action_name = cc_action_names.cpp_link_static_library, 41 variables = archiver_variables, 42 ) 43 44 ctx.actions.run( 45 executable = archiver_path, 46 arguments = [args], 47 env = env, 48 inputs = depset( 49 direct = object_files, 50 transitive = [ 51 cc_toolchain.all_files, 52 ], 53 ), 54 use_default_shell_env = True, 55 outputs = [output_file], 56 mnemonic = "CppArchiveDist", 57 ) 58 59# Implementation for cc_dist_library rule. 60def _cc_dist_library_impl(ctx): 61 cc_toolchain_info = find_cc_toolchain(ctx) 62 if cc_toolchain_info.ar_executable == None: 63 return [] 64 65 feature_configuration = cc_common.configure_features( 66 ctx = ctx, 67 cc_toolchain = cc_toolchain_info, 68 ) 69 70 # Collect the set of object files from the immediate deps. 71 72 objs = [] 73 pic_objs = [] 74 for dep in ctx.attr.deps: 75 if CcInfo not in dep: 76 continue 77 78 link_ctx = dep[CcInfo].linking_context 79 if link_ctx == None: 80 continue 81 82 linker_inputs = link_ctx.linker_inputs.to_list() 83 for link_input in linker_inputs: 84 if link_input.owner != dep.label: 85 # This is a transitive dep: skip it. 86 continue 87 88 for lib in link_input.libraries: 89 objs.extend(lib.objects or []) 90 pic_objs.extend(lib.pic_objects or []) 91 92 # For static libraries, build separately with and without pic. 93 94 stemname = "lib" + ctx.label.name 95 outputs = [] 96 97 if len(objs) > 0: 98 archive_out = ctx.actions.declare_file(stemname + ".a") 99 _create_archive_action( 100 ctx, 101 feature_configuration, 102 cc_toolchain_info, 103 archive_out, 104 objs, 105 ) 106 outputs.append(archive_out) 107 108 if len(pic_objs) > 0: 109 pic_archive_out = ctx.actions.declare_file(stemname + ".pic.a") 110 _create_archive_action( 111 ctx, 112 feature_configuration, 113 cc_toolchain_info, 114 pic_archive_out, 115 pic_objs, 116 ) 117 outputs.append(pic_archive_out) 118 119 # For dynamic libraries, use the `cc_common.link` command to ensure 120 # everything gets built correctly according to toolchain definitions. 121 122 compilation_outputs = cc_common.create_compilation_outputs( 123 objects = depset(objs), 124 pic_objects = depset(pic_objs), 125 ) 126 link_output = cc_common.link( 127 actions = ctx.actions, 128 feature_configuration = feature_configuration, 129 cc_toolchain = cc_toolchain_info, 130 compilation_outputs = compilation_outputs, 131 name = ctx.label.name, 132 output_type = "dynamic_library", 133 user_link_flags = ctx.attr.linkopts, 134 ) 135 library_to_link = link_output.library_to_link 136 137 # Note: library_to_link.dynamic_library and interface_library are often 138 # symlinks in the solib directory. For DefaultInfo, prefer reporting 139 # the resolved artifact paths. 140 if library_to_link.resolved_symlink_dynamic_library != None: 141 outputs.append(library_to_link.resolved_symlink_dynamic_library) 142 elif library_to_link.dynamic_library != None: 143 outputs.append(library_to_link.dynamic_library) 144 145 if library_to_link.resolved_symlink_interface_library != None: 146 outputs.append(library_to_link.resolved_symlink_interface_library) 147 elif library_to_link.interface_library != None: 148 outputs.append(library_to_link.interface_library) 149 150 # We could expose the libraries for use from cc rules: 151 # 152 # linking_context = cc_common.create_linking_context( 153 # linker_inputs = depset([ 154 # cc_common.create_linker_input( 155 # owner = ctx.label, 156 # libraries = depset([library_to_link]), 157 # ), 158 # ]), 159 # ) 160 # cc_info = CcInfo(linking_context = linking_context) # and return this 161 # 162 # However, if the goal is to force a protobuf dependency to use the 163 # DSO, then `cc_import` is a better-supported way to do so. 164 # 165 # If we wanted to expose CcInfo from this rule (and make it usable as a 166 # C++ dependency), then we would probably want to include the static 167 # archive and headers as well. exposing headers would probably require 168 # an additional aspect to extract CcInfos with just the deps' headers. 169 170 return [ 171 DefaultInfo(files = depset(outputs)), 172 ] 173 174cc_dist_library = rule( 175 implementation = _cc_dist_library_impl, 176 doc = """ 177Create libraries suitable for distribution. 178 179This rule creates static and dynamic libraries from the libraries listed in 180'deps'. The resulting libraries are suitable for distributing all of 'deps' 181in a single logical library, for example, in an installable binary package. 182Only the targets listed in 'deps' are included in the result (i.e., the 183output does not include transitive dependencies), allowing precise control 184over the library boundary. 185 186The outputs of this rule are a dynamic library and a static library. (If 187the build produces both PIC and non-PIC object files, then there is also a 188second static library.) The example below illustrates additional details. 189 190This rule is different from Bazel's experimental `shared_cc_library` in 191several ways. First, this rule ignores transitive dependencies, which means 192that dynamic library dependencies generally need to be specified via 193'linkopts'. Second, this rule produces a static archive library in addition 194to the dynamic shared library. Third, this rule is not directly usable as a 195C++ dependency (although the outputs could be used, e.g., by `cc_import`). 196 197Example: 198 199 cc_library(name = "a", srcs = ["a.cc"], hdrs = ["a.h"]) 200 cc_library(name = "b", srcs = ["b.cc"], hdrs = ["b.h"], deps = [":a"]) 201 cc_library(name = "c", srcs = ["c.cc"], hdrs = ["c.h"], deps = [":b"]) 202 203 # Creates libdist.so and (typically) libdist.pic.a: 204 # (This may also produce libdist.a if the build produces non-PIC objects.) 205 cc_dist_library( 206 name = "dist", 207 linkopts = ["-la"], # libdist.so dynamically links against liba.so. 208 deps = [":b", ":c"], # Output contains b.o and c.o, but not a.o. 209 ) 210""", 211 attrs = { 212 "deps": attr.label_list( 213 doc = ("The list of libraries to be included in the outputs. " + 214 "Only these targets' compilation outputs will be " + 215 "included (i.e., the transitive dependencies are not " + 216 "included in the output)."), 217 ), 218 "linkopts": attr.string_list( 219 doc = ("Add these flags to the C++ linker command when creating " + 220 "the dynamic library."), 221 ), 222 # C++ toolchain before https://github.com/bazelbuild/bazel/issues/7260: 223 "_cc_toolchain": attr.label( 224 default = Label("@rules_cc//cc:current_cc_toolchain"), 225 ), 226 }, 227 toolchains = [ 228 # C++ toolchain after https://github.com/bazelbuild/bazel/issues/7260: 229 "@bazel_tools//tools/cpp:toolchain_type", 230 ], 231 fragments = ["cpp"], 232) 233