How can I query the attribute of a rule in Bazel?

1.9k Views Asked by At

If I have

cc_binary(
    name = "stooges",
    srcs = [ "larry.cc", "curly.cc", "moe.cc" ],
)

Is there a Bazel query which will return "larry.cc", "curly.cc", "moe.cc" ?

At the moment the only thing I can think of is

$ bazel query --output=build //:stooges | perl -nwle 'print $1 if /srcs\s*=\s*\[([^]]*)\]/'
1

There are 1 best solutions below

0
Vertexwahn On BEST ANSWER

Get all labels listed in the srcs attribute:

bazel query 'labels(srcs,//your_package:your_target)'

In your case bazel query 'labels(srcs,//:stooges)'.

Should return:

//:larry.cc 
//:curly.cc
//:moe.cc

If you want to have all hdrs and srcs labels:

bazel query 'labels(srcs,//your_package:your_target) union labels(hdrs,//your_package:your_target)'

You can also make use of Bazel Aspects to query for source files. More details here.