简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM