Bulletmagnet
Bulletmagnet

Reputation: 6022

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

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*\[([^]]*)\]/'

Upvotes: 1

Views: 2729

Answers (1)

Vertexwahn
Vertexwahn

Reputation: 8142

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.

Upvotes: 3

Related Questions