From 21194a2a349b445b4746cfe88ba201062494d8ca Mon Sep 17 00:00:00 2001 From: Fredrik Paulsson Date: Wed, 18 Sep 2024 16:42:36 +0200 Subject: [PATCH] feat: add a labgrid_run_binary rule --- labgrid/run_binary/BUILD.bazel | 0 labgrid/run_binary/defs.bzl | 5 ++ labgrid/run_binary/rule.bzl | 101 +++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 labgrid/run_binary/BUILD.bazel create mode 100644 labgrid/run_binary/defs.bzl create mode 100644 labgrid/run_binary/rule.bzl diff --git a/labgrid/run_binary/BUILD.bazel b/labgrid/run_binary/BUILD.bazel new file mode 100644 index 00000000..e69de29b diff --git a/labgrid/run_binary/defs.bzl b/labgrid/run_binary/defs.bzl new file mode 100644 index 00000000..216c708b --- /dev/null +++ b/labgrid/run_binary/defs.bzl @@ -0,0 +1,5 @@ +load(":rule.bzl", _binary = "binary") + +visibility("public") + +labgrid_run_binary = _binary diff --git a/labgrid/run_binary/rule.bzl b/labgrid/run_binary/rule.bzl new file mode 100644 index 00000000..4f682b97 --- /dev/null +++ b/labgrid/run_binary/rule.bzl @@ -0,0 +1,101 @@ +load("//labgrid/cfg:store.bzl", _cfg = "store") + +visibility("//...") + +DOC = """Executes a binary on the host within a LabGrid environment.""" + +ATTRS = { + "tool": attr.label( + doc = "The tool to run in the action.\n\nMust be the label of a *_binary rule," + + " of a rule that generates an executable file, or of a file that can be" + + " executed as a subprocess (e.g. an .exe or .bat file on Windows or a binary" + + " with executable permission on Linux). This label is available for" + + " `$(execpath)` and `$(location)` expansion in `args` and `env`.", + executable = True, + allow_files = True, + mandatory = True, + cfg = "exec", + ), + "env": attr.string_dict( + doc = "Environment variables of the action.\n\nSubject to " + + " [`$(execpath)` and `$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" + + " expansion.", + ), + "srcs": attr.label_list( + allow_files = True, + doc = "Additional inputs of the action.\n\nThese labels are available for" + + " `$(execpath)` and `$(location)` expansion in `args` and `env`.", + ), + "outs": attr.output_list( + mandatory = True, + doc = "Output files generated by the action.\n\nThese labels are available for" + + " `$(execpath)` and `$(location)` expansion in `args` and `env`.", + ), + "args": attr.string_list( + doc = "Command line arguments of the binary.\n\nSubject to" + + " [`$(execpath)` and `$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" + + " expansion.", + ), + "platform": attr.label( + doc = "Platform to transition to.", + providers = [platform_common.PlatformInfo], + ), + "_executor": attr.label( + doc = "The LabGrid environment executor", + default = "//labgrid/executor", + executable = True, + cfg = "exec", + ) +} + +def implementation(ctx): + tool_as_list = [ctx.attr.tool] + expanded_args = [ + # Expand $(execpath ...) / $(execpaths ...) / $(location ...) / $(locations ...) in args. + # + # To keep the rule simple, do not expand Make Variables (like *_binary.args usually would). + # (We can add this feature later if users ask for it.) + # + # Also for simple implementation and usage, do not Bash-tokenize the arguments. Without + # tokenization the user can write args=["a b"] to pass (a b) as one argument, but with + # tokenization they would have to write args=["'a b'"] or args=["a\\ b"]. There's no + # documented tokenization function anyway (as of 2019-05-21 ctx.tokenize exists but is + # undocumented, see https://github.com/bazelbuild/bazel/issues/8389). + ctx.expand_location(a, tool_as_list) + for a in ctx.attr.args + ] + envs = { + # Expand $(execpath ...) / $(execpaths ...) / $(location ...) / $(locations ...) in the values. + k: ctx.expand_location(v, tool_as_list) + for k, v in ctx.attr.env.items() + } + + args = ctx.actions.args() + args.add("--") + args.add(ctx.executable.tool) + args.add_all(expanded_args) + + ctx.actions.run( + outputs = ctx.outputs.outs, + inputs = ctx.files.srcs, + tools = [ctx.executable.tool], + executable = ctx.executable._executor, + arguments = [args], + mnemonic = "LabGridRunBinary", + use_default_shell_env = False, + env = ctx.configuration.default_shell_env | ctx.attr._executor[RunEnvironmentInfo].environment | envs, + ) + + return DefaultInfo( + files = depset(ctx.outputs.outs), + runfiles = ctx.runfiles(files = ctx.outputs.outs), + ) + +labgrid_run_binary = rule( + doc = DOC, + attrs = ATTRS, + implementation = implementation, + cfg = _cfg, +) + +binary = labgrid_run_binary -- GitLab