Runfiles location substitution

徘徊边缘 提交于 2020-01-15 05:13:10

问题


I'm trying to run qemu on the output of a cc_binary rule. For that I have created a custom rule, which is pretty similiar to this example, but instead of the cat command on the txt-file, I want to invoke qemu on the output elf-file (":test_portos.elf") of the cc_binary rule. My files are the following:

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)
    },
)

BUILD

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"] 
)

The problem is, that in the command (of the custom rule) the location of the ":test_portos.elf" is used and not the location of the runfile. I have also tried, like shown in the example, to use $(location :test_portos.elf) together with ctx.expand_location but the result was the same.

How can I get the location of the "test_portos.elf" runfile and insert it into the command of my custom rule?


回答1:


Seems that the runfiles are safed according to the short_path of the File, so this was all I needed to change in my run_tests.bzl file:

target = ctx.file.target.short_path



来源:https://stackoverflow.com/questions/47856239/runfiles-location-substitution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!