我想创建一个 bazel gen-rule 来创建一个包含当前 git 提交哈希的头文件:
# 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"],
)
到目前为止,我还没有测试过 linux 版本(cmd),但只在 windows(cmd_ps)上测试过,它可以工作,但问题是当 powershell 执行时,它不在工作区目录(这是我的 git root)中。我需要用什么替换what_comes_here
第一个 cd 到我的工作区的根目录?