简体   繁体   English

Bazel genrule:如何获取工作区目录的绝对路径?

[英]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.

相关问题 如何在Katalon Studio中使用TestDataFactory获取绝对路径 - How to get absolute path with TestDataFactory in Katalon Studio Git“无法确定git目录的绝对路径” - Git “unable to determine absolute path of git directory” 如何获取带有子模块的Git存储库中所有文件的绝对路径? - How to get the absolute path of all files in a Git repository with submodules? git忽略特定目录,绝对路径或相对路径 - git ignore specific directory, absolute path or relative path 项目根目录的第N个子目录中的bash脚本如何可靠地知道项目根目录的绝对路径? - How can a bash script in an Nth subdirectory of a project's root directory reliably know the absolute path of the project's root directory? 无法获得已安装模块的绝对路径 - Failed to get absolute path to installed module 使用绝对路径获取Git repo url - Get Git repo url using absolute path 如何获取在 git commit 中已更改但具有绝对路径的文件列表? - How do I get a list of files that have changed in a git commit, but with an absolute path? 如何获取当前目录相对于git仓库根目录的路径? - How to get the path of the current directory relative to root of the git repository? Windows XP上的Git Pull错误:无法确定git目录的绝对路径 - Git pull error on Windows XP: Unable to determine absolute path of git directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM