5

I have generated some output files using bazel build, but its is a bit tedious to specify the path of the bazel-bin directory everytime I need to access the output.

In deeply nested bazel projects, not only do I need to get the specific repository, /Users/username/repos/organisation/folder/folder/repo, I also need to add the bazel-bin/folder1/folder2/folder3/folder4/binary_i_want. I would prefer to say $output/binary_i_want. Bazel should be able to get the project directory (as it looks up the workspace file), and find the bazel-bin, and then look for the equivalent directory I am in. This is because I might not be running it directly, but instead copying this file to an android device, with adb push.

Is this possible? Thank you

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167

1 Answers1

6

You can use $(bazel info bazel-bin)/binary_i_want for this.

Edit: Getting the complete path to an artifact generate by a rule is a bit more involved. One option using jq could be:

$(bazel info workspace)/$(bazel aquery //:some_path --output jsonproto 2>/dev/null | jq -r ".artifacts[0].execPath")

(Inspired by this answer: Bazel: How do you get the path to a generated file?)

Sjoerd Visscher
  • 11,840
  • 2
  • 47
  • 59
  • Thanks @SjoerdVisscher, however is there a way to also include the current relative-to-project-root path as well? The tensorflow repo is very nested :D – Ben Butterworth Nov 23 '20 at 10:38
  • @BenButterworth sorry, I missed that part of you question. I updated my answer. – Sjoerd Visscher Nov 23 '20 at 11:07
  • I have to say, I was looking for a simple answer, but now I have learnt about [jq](https://stedolan.github.io/jq/) and action queries! And more about bazel in general. Unfortunately the query returns `bazel-out` directories, instead of `bazel-bin`. I'm not clear on the differences, so I'll read more about it. It looks like bazel-bin 2 levels inside bazel-out, inside a architecture directory. `e.g. tensorflow/bazel-out/android-arm64-v8a-opt/bin`. – Ben Butterworth Nov 24 '20 at 22:04
  • @BenButterworth this documentation covers the output directory relations https://bazel.build/remote/output-directories#layout – jxramos Jul 29 '23 at 01:34