如何在bazel规则中获取WORKSPACE目录 [英] How to get WORKSPACE directory in bazel rule
问题描述
我要使用像clang-format
,clang-tidy
这样的clang工具或生成"> 这样的编译数据库,我需要知道WORKSPACE目录在.bzl文件中.我如何获得它?考虑下面的示例,我只想在我的工作区中打印所有src文件的完整路径:
I order to use clang tools like clang-format
, clang-tidy
or generate a compilation database like this, I need to know the WORKSPACE directory within the .bzl file. How can I obtain it? Consider the following example where I just want to print the full path of all the src files in my workspace:
# simple_example.bzl
def _impl(ctx):
workspace_dir = // ---> what comes here? <---
command = "\n".join([echo %s/%s" % (workspace_dir, f.short_path)
for f in ctx.files.srcs])
ctx.actions.write(
output=ctx.outputs.executable,
content=command,
is_executable=True)
echo_full_path = rule(
implementation=_impl,
executable=True,
attrs={
"srcs": attr.label_list(allow_files=True),
}
)
# BUILD
echo_full_path(
name = "echo",
srcs = glob(["src/**/*.cc"])
)
是否有一种更清洁/更精细的方法?
Is there a cleaner/nicer way of doing this?
推荐答案
您可能可以通过使用realpath
来解决此问题.像这样:
You can probably get around this by using realpath
. Something like:
def _impl(ctx):
ctx.actions.run_shell(
inputs = ctx.files.srcs,
outputs = [ctx.outputs.executable],
command = "\n".join(["echo echo $(realpath \"%s\") >> %s" % (f.path,
ctx.outputs.executable.path) for f in ctx.files.srcs]),
execution_requirements = {
"no-sandbox": "1",
"no-cache": "1",
"no-remote": "1",
"local": "1",
},
)
echo_full_path = rule(
implementation=_impl,
executable=True,
attrs={
"srcs": attr.label_list(allow_files=True),
}
)
请注意execution_requirements
,以避开我上面的评论中的潜在问题.
Note the execution_requirements
to get around the potential issues in my comment above.
这篇关于如何在bazel规则中获取WORKSPACE目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!