How to specify output artifact from a cc_library in bazel?

安稳与你 提交于 2020-05-15 05:21:05

问题


I want to build "foo.c" as a library and then execute "readelf" on the generated .so but not the ".a", how can I write it in bazel?

The following BUILD.bazel file doesn't work:

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

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

The error is "missing input file '//:libfoo.so'".

Changing the genrule's srcs attribute to ":foo" passes both the ".a" and ".so" file to readelf, which is not what I need.

Is there any way to specify which output of ":foo" to pass to the genrule?


回答1:


cc_library produces several outputs, which are separated by output groups. If you want to get only .so outputs, you can use filegroup with dynamic_library output group.

So, this should work:

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) > $@",
)


来源:https://stackoverflow.com/questions/59454576/how-to-specify-output-artifact-from-a-cc-library-in-bazel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!