如何在bazel规则中获取WORKSPACE目录

Mik*_*yke 8 bazel

我为了使用像这样的 clang 工具clang-formatclang-tidy或者生成这样编译数据库,我需要知道 .bzl 文件中的 WORKSPACE 目录。我怎样才能获得它?考虑以下示例,我只想打印工作区中所有 src 文件的完整路径:

# 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),
    }
)
Run Code Online (Sandbox Code Playgroud)

# BUILD

echo_full_path(
    name = "echo",
    srcs = glob(["src/**/*.cc"])
)
Run Code Online (Sandbox Code Playgroud)

有没有更干净/更好的方法来做到这一点?

ahu*_*sky 5

您也许可以通过使用来解决这个问题realpath。就像是:

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),
    }
)
Run Code Online (Sandbox Code Playgroud)

请注意execution_requirements解决我上面评论中的潜在问题。