Igor Gatis
Igor Gatis

Reputation: 4898

Bazel: how to get path to output binary?

Consider somepath/BUILD file:

load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")

proto_library(
    name = "bar_proto",
    srcs = ["bar.proto"],
)

java_proto_library(
    name = "bar_java_proto",
    deps = [":bar_proto"],
)

By inspecting bazel-bin folder, I find bazel-bin/somepath/libbar_proto-speed.jar.

How do I get bazel-bin/somepath/libbar_proto-speed.jar from //somepath:bar_java_proto using bazel query?

Upvotes: 2

Views: 5066

Answers (3)

darkdragon
darkdragon

Reputation: 460

You can use cquery's --output=starlark option:

bazel cquery //somepath:bar_java_proto --output=starlark --starlark:expr="target.files.to_list()[0].path"

Upvotes: 1

silvergasp
silvergasp

Reputation: 1677

You can do this using action query; e.g.

bazel aquery 'outputs("lib.*proto.*\.jar", //somepath:bar_java_proto)'

By default, you'll get an output looking something like;

action 'Some action name'
  Mnemonic: ...
  Target: ...
  Configuration: ...
  ActionKey: ...
  Inputs: [...]
  Outputs: [...]

But you can change this using the --output flag (overloaded terminology here). From the bazel docs;

--output=(text|summary|proto|jsonproto|textproto), default=text The text (default) and summary output formats are human-readable, select proto, textproto, or jsonproto for a machine-readable format. The proto message for the machine-readable formats is analysis.ActionGraphContainer.

You can of course broaden or narrow the output glob to suite your needs e.g. List all .jar file outputs from //somepath:bar_java_proto.

bazel aquery 'outputs(".*\.jar", //somepath:bar_java_proto)'

Upvotes: 0

László
László

Reputation: 4271

You don't.

Knowing output paths requires executing Bazel's loading and analysis phases, i.e. (1) loading the BUILD files and (2) analyzing dependencies to come up with the execution plan and concrete build actions (called the "action graph").

Bazel query only runs the loading phase, not the analysis phase, therefore it doesn't know about output paths.

Bazel cquery ("configured query") runs after the analysis phase [1], but as far as I understand it also cannot return output paths.

[1] https://docs.bazel.build/versions/master/cquery.html

Upvotes: 5

Related Questions