Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify output artifact from a cc_library in bazel?

Tags:

bazel

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?

like image 526
yannay livneh Avatar asked Oct 27 '25 05:10

yannay livneh


1 Answers

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) > $@",
)
like image 51
Ray Avatar answered Oct 30 '25 14:10

Ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!