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?
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) > $@",
)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With