运行文件位置替换

Runfiles location substitution

我正在尝试 运行 qemu 在 cc_binary 规则的输出上。为此,我创建了一个自定义规则,它与 this example 非常相似,但我想在输出 elf 文件(“: test_portos.elf") cc_binary 规则。我的文件如下:

run_tests.bzl

def _impl(ctx):
  # The path of ctx.file.target.path is:
    'bazel-out/cortex-a9-fastbuild/bin/test/test_portos.elf'
  target = ctx.file.target.path
  command = "qemu-system-arm -M xilinx-zynq-a9 -cpu cortex-a9 -nographic
              -monitor null -serial null -semihosting 
              -kernel %s" % (target)

  ctx.actions.write(
      output=ctx.outputs.executable,
      content=command,
      is_executable=True)

  return [DefaultInfo(
      runfiles=ctx.runfiles(files=[ctx.file.target])
  )]

execute = rule(
    implementation=_impl,
    executable=True,
    attrs={
        "command": attr.string(),
        "target" : attr.label(cfg="data", allow_files=True, 
                              single_file=True, mandatory=True)
    },
)

建造

load("//make:run_tests.bzl", "execute")

execute(
    name = "portos",
    target = ":test_portos.elf"
)

cc_binary(
    name = "test_portos.elf",
    srcs = glob(["*.cc"]),
    deps = ["//src:portos", 
            "@unity//:unity"],
    copts = ["-Isrc", 
             "-Iexternal/unity/src",
             "-Iexternal/unity/extras/fixture/src"] 
)

问题是,在(自定义规则的)命令中使用了“:test_portos.elf”的位置,而不是 运行 文件的位置。我也尝试过,如 example 所示,将 $(location :test_portos.elf)ctx.expand_location 一起使用,但结果是一样的。

如何获取 "test_portos.elf" 运行 文件的位置并将其插入自定义规则的命令中?

根据 File 的 short_path,运行文件似乎是安全的,所以这就是我需要在我的 run_tests.bzl 文件中更改的全部内容:

target = ctx.file.target.short_path