首页 > 解决方案 > 是否有 Bazel 命令行来确定一个或多个给定标签的输出的绝对路径?

问题描述

我知道有,bazel info <...> bazel-bin但这只是给出输出路径前缀。我希望有一些花哨cqueryaquery获得输出的完整路径。

标签: bazel

解决方案


据我所知,没有简单的方法来获得它(请参阅下面的相关讨论),但如果您的目标是可运行的,您可以使用这个好技巧:

bazel run --run_under "echo" //path/to/folder:target

这将在磁盘上打印二进制文件的完整路径(在 bazel 缓存文件夹中)。

获取目标输出路径的更通用方法是通过aquery,例如

bazel aquery "//path/to/folder:target" --noinclude_commandline --output=text

这将打印与给定目标相关的所有操作的输出(cc_binary这些操作将包括目标文件、二进制文件、剥离的二进制文件等) - 然后您可以使用 grep 查找Outputs并提取路径。

这是 a 的示例输出cc_binary

> bazel aquery "//path/to/folder:target" --noinclude_commandline --output=text

action 'Compiling path/to/folder/main.cc'
  Mnemonic: CppCompile
  Target: //path/to/folder:target
  Configuration: k8-fastbuild
  ActionKey: ...
  Inputs: [..., path/to/folder/main.cc]
  Outputs: [bazel-out/k8-fastbuild/bin/path/to/folder/_objs/target/main.pic.d,
            bazel-out/k8-fastbuild/bin/path/to/folder/_objs/target/main.pic.o]

...

action 'Linking path/to/folder/target'
  Mnemonic: CppLink
  Target: //path/to/folder:target
  Configuration: k8-fastbuild
  ActionKey: ...
  Inputs: [..., bazel-out/k8-fastbuild/bin/path/to/folder/_objs/target/main.pic.o, ...]
  Outputs: [bazel-out/k8-fastbuild/bin/path/to/folder/target]

action 'Stripping path/to/folder/target.stripped for //path/to/folder:target'
  Mnemonic: CcStrip
  Target: //path/to/folder:target
  Configuration: k8-fastbuild
  ActionKey: ...
  Inputs: [..., bazel-out/k8-fastbuild/bin/path/to/folder/target]
  Outputs: [bazel-out/k8-fastbuild/bin/path/to/folder/target.stripped]

...
...
...

路径是相对的,但您可以通过使用bazel info workspace.

如果您对特定操作的输出感兴趣,例如链接产生的最终二进制文件,您可以按助记符类型过滤:

> bazel aquery "mnemonic(CppLink, //path/to/folder:target)" --noinclude_commandline --output=text

action 'Linking path/to/folder/target'
  Mnemonic: CppLink
  Target: //path/to/folder:target
  Configuration: k8-fastbuild
  ActionKey: ...
  Inputs: [..., bazel-out/k8-fastbuild/bin/path/to/folder/_objs/target/target.pic.o, ...]
  Outputs: [bazel-out/k8-fastbuild/bin/path/to/folder/target]

不是一个完整的答案,但希望这会有所帮助。

其他相关讨论:


推荐阅读