自定义规则不会构建相关目标

时间:2017-12-13 19:15:07

标签: bazel

我想在qemu上运行单元测试。我创建了一个自定义规则,它使用规则中指定的参数调用qemu。其中一个参数是elf文件(规则属性" target"),它被qemu用作内核。
当我使用以下命令调用我的自定义规则时,elf文件(" kernel.elf")不会被编译:

 bazel build //test:custom_rule

即使bazel query 'deps(//test:custom_rule)'列出了目标":kernel.elf"作为依赖。

此外,我还有自定义规则的另一个问题。当我手动构建":kernel.elf"然后调用自定义规则qemu告诉我,它无法加载内核文件。在shell中手动调用qemu命令确实有效,所以我猜问题不在于内核。#34; kernel .elf"文件。

有人对我的问题有答案吗?

提前致谢!

run_tests.bzl

def _impl(ctx):
  qemu = ctx.attr.qemu
  machine = ctx.attr.machine
  cpu = ctx.attr.cpu
  target = ctx.file.target.path
  output = ctx.outputs.out
  # The command may only access files declared in inputs.
  ctx.actions.run_shell(
      arguments = [qemu, machine, cpu, target],
      outputs=[output],
      command="$1 -M $2 -cpu $3 -nographic -monitor null 
               -serial null -semihosting -kernel $4 > %s" % (output.path))


run_tests = rule(
    implementation=_impl,
    attrs = {"qemu" : attr.string(),
             "machine" : attr.string(),
             "cpu" : attr.string(),
             "target" : attr.label(allow_files=True, single_file=True,
                        mandatory=True)},
    outputs={"out": "run_tests.log"}
)

BUILD

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

run_tests(
    name = "custom_rule",
    qemu = "qemu-system-arm",
    machine = "xilinx-zynq-a9",
    cpu = "cortex-a9",
    target = ":kernel.elf"
)

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

1 个答案:

答案 0 :(得分:6)

问题可能是需要为操作指定输入,请参阅 https://docs.bazel.build/versions/master/skylark/lib/actions.html#run_shell.inputs

你也可能需要让qemu成为一个标签并将其作为动作的输入(如果这是一个qemu需要的文件,也可以加机)

E.g。类似的东西:

def _impl(ctx):
  qemu = ctx.attr.qemu
  machine = ctx.attr.machine
  cpu = ctx.attr.cpu
  target = ctx.file.target.path
  output = ctx.outputs.out
  # The command may only access files declared in inputs.
  ctx.actions.run_shell(
      inputs = [qemu, target],
      outputs=[output],
      arguments = [qemu, machine, cpu, target],
      command="$1 -M $2 -cpu $3 -nographic -monitor null 
               -serial null -semihosting -kernel $4 > %s" % (output.path))


run_tests = rule(
    implementation=_impl,
    attrs = {
        "qemu" : attr.label(allow_files=True, single_file=True,
                            mandatory=True),
        "machine" : attr.string(),
        "cpu" : attr.string(),
        "target" : attr.label(allow_files=True, single_file=True,
                              mandatory=True)
    },
    outputs={"out": "run_tests.log"}
)