我使用下面的 bazel 规则来构建带有 bazel 的静态库:
def _cc_static_library_impl(ctx):
cc_deps = [dep[CcInfo] for dep in ctx.attr.deps]
libraries = []
for cc_dep in cc_deps:
for link_input in cc_dep.linking_context.linker_inputs.to_list():
for library in link_input.libraries:
libraries += library.pic_objects
args = ["r", ctx.outputs.out.path] + [f.path for f in libraries]
ctx.actions.run(
inputs = libraries,
outputs = [ctx.outputs.out],
executable = "/usr/bin/ar",
arguments = args,
)
return [DefaultInfo()]
cc_static_library = rule(
implementation = _cc_static_library_impl,
attrs = {
"deps": attr.label_list(providers = [CcInfo]),
},
outputs = {"out": "lib%{name}.a"},
)
如何从当前工具链中提取要使用的命令而不是使用硬编码/usr/bin/ar
?我的规则是基于我在互联网上找到的,我对此知之甚少。这个例子似乎做了一些相关的事情:
https ://github.com/bazelbuild/rules_cc/blob/main/examples/my_c_archive/my_c_archive.bzl