首页 > 解决方案 > 如何从 bazel 中的 cc_library 指定输出工件?

问题描述

我想将“foo.c”构建为一个库,然后在生成的 .so 上执行“readelf”,而不是“.a”,我该如何在 bazel 中编写它?

以下 BUILD.bazel 文件不起作用:

cc_library(
    name = "foo",
    srcs = ["foo.c"],
)

genrule(
    name = "readelf_foo",
    srcs = ["libfoo.so"],
    outs = ["readelf_foo.txt"],
    cmd = "readelf -a $(SRCS) > $@",
)

错误是“缺少输入文件'//:libfoo.so'”。

将 genrule 的 srcs 属性更改为“:foo”会将“.a”和“.so”文件都传递给 readelf,这不是我需要的。

有什么方法可以指定传递给 genrule 的 ":foo" 的哪个输出?

标签: bazel

解决方案


cc_library产生多个输出,这些输出由输出组分隔。如果您只想获得 .so 输出,您可以使用filegroup输出dynamic_library组。

所以,这应该工作:

cc_library(
    name = "foo",
    srcs = ["foo.c"],
)


filegroup(
    name='libfoo',
    srcs=[':foo'],
    output_group = 'dynamic_library'
)

genrule(
    name = "readelf_foo",
    srcs = [":libfoo"],
    outs = ["readelf_foo.txt"],
    cmd = "readelf -a $(SRCS) > $@",
)

推荐阅读