yannay livneh
yannay livneh

Reputation: 57

How to specify output artifact from a cc_library in 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?

Upvotes: 4

Views: 2526

Answers (1)

Ray
Ray

Reputation: 986

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

Upvotes: 9

Related Questions