Mike van Dyke
Mike van Dyke

Reputation: 2868

Runfiles location substitution

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?

Upvotes: 2

Views: 1411

Answers (1)

Mike van Dyke
Mike van Dyke

Reputation: 2868

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

Upvotes: 2

Related Questions