【问题标题】:Bazel: Run a command without an outputBazel:运行没有输出的命令
【发布时间】:2017-12-03 10:57:51
【问题描述】:

我正在使用 bazel 来构建裸机程序。我想在 qemu 上运行 bazel 生成的单元测试。

qemu-system-* -some_args -kernel bazel-bin/whatever/generated.elf

我尝试通过在“.bzl”文件中创建自己的规则来运行这些,但似乎所有规则操作都必须输出。请注意,我需要根据目标架构调用具有不同参数的不同 qemu 命令。我想将这些传递给规则。

有没有办法在没有任何输出的情况下调用 shell 命令?

如果需要,这是我目前所拥有的(但我不确定哪些部分是正确的,因为 bazel 在分析阶段停止了):

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

run_tests = rule(
    implementation=_impl,
    attrs = {"qemu" : attr.string(),
             "machine" : attr.string(),
             "cpu" : attr.string(),
             "target" : attr.string(),},
    executable = True
)

还有我的构建文件:

# BUILD
=======
load("//make:run_tests.bzl", "run_tests")

run_tests(
    name = "portos",
    qemu = "qemu-system-arm",
    machine = "realview-pbx-a9",
    cpu = "cortex-a9",
    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"] 
)

【问题讨论】:

    标签: qemu bazel


    【解决方案1】:

    你快到了:是的,你需要输出,否则 bazel 无事可做。对于规则输出,您可能需要测试日志或测试结果。

    【讨论】:

      【解决方案2】:

      云雀支持编写测试规则。基本上不是设置executable = True,而是设置test = True,然后你的规则将创建一个可执行文件作为测试,然后将ctx.outputs.executable设置为该可执行文件。然后您可以将bazel test 命令与您的规则一起使用。

      见:

      文档:https://docs.bazel.build/versions/master/skylark/rules.html#test-rules

      示例:https://github.com/bazelbuild/examples/tree/master/rules/test_rule

      rule.test:https://docs.bazel.build/versions/master/skylark/lib/globals.html#rule.test

      【讨论】:

      • 谢谢,这正是我想要的。
      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 2021-03-19
      • 2021-07-26
      • 1970-01-01
      • 2016-06-05
      • 2016-10-06
      相关资源
      最近更新 更多