[英]Bazel genrule: how to get absolute path to workspace directory?
I want to create a bazel gen-rule to create a header file that contains the current git commit hash:我想创建一个 bazel gen-rule 来创建一个包含当前 git 提交 hash 的 header 文件:
# Generate "version_info.h"
cc_library(
name = "version_info",
srcs = [],
hdrs = ["version_info.h"],
visibility = ["//visibility:private"],
)
VERSION_INFO_H_BASH = """
#ifndef VERSION_INFO_H_
#define VERSION_INFO_H_
#define COMMIT_HASH "%s"
#endif // VERSION_INFO_H_
"""
VERSION_INFO_H_PS = """
#ifndef VERSION_INFO_H_
#define VERSION_INFO_H_
#define COMMIT_HASH "{0}"
#endif // VERSION_INFO_H_
"""
genrule(
name = "generate_version_info",
outs = ["version_info.h"],
cmd_ps = ("cd what_comes_here; @'%s'@ -f \"$$(git.exe rev-parse HEAD)\" | Out-File $(OUTS)" % VERSION_INFO_H_PS),
cmd = ("cd what_comes_here"; printf '%s' \"$$(git rev-parse HEAD)\ > $(OUTS)" % VERSION_INFO_H_BASH),
visibility = ["//visibility:private"],
)
So far I haven't tested the linux version (cmd) but only on windows (cmd_ps) and it works, but the problem is that when powershell executes, it's not in the workspace directory (which is my git root). So far I haven't tested the linux version (cmd) but only on windows (cmd_ps) and it works, but the problem is that when powershell executes, it's not in the workspace directory (which is my git root). What do I need to replace
what_comes_here
with to first cd into the root of my workspace?我需要用什么替换
what_comes_here
才能首先 cd 进入我的工作区的根目录?
Make use of the workspace status feature of Bazel.利用 Bazel 的工作区状态功能。 An example can be found here .
一个例子可以在这里找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.