From 0f2df0f0599a21f3204d2da4f0a06db9bdaef996 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Tue, 5 Nov 2024 17:05:44 +0000 Subject: [PATCH 1/4] feat(genrule): add `$(RULEDIR)` Make variable To be compatible with built-in `genrule`[1] [1]: https://bazel.build/reference/be/make-variables#predefined_genrule_variables --- labgrid/genrule/rule.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/labgrid/genrule/rule.bzl b/labgrid/genrule/rule.bzl index 6edde1d6..c2af0ae0 100644 --- a/labgrid/genrule/rule.bzl +++ b/labgrid/genrule/rule.bzl @@ -56,6 +56,7 @@ def _substitutions(ctx): substitutions = { "$(OUTS)": " ".join(outs), "$(SRCS)": " ".join(srcs), + "$(RULEDIR)": "{}/{}".format(ctx.bin_dir.path, ctx.label.package), } if len(srcs) == 1: -- GitLab From 6bb9c4ae86864bf0cc65f08f7cd6cdaa2cdb8878 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 6 Nov 2024 10:09:02 +0000 Subject: [PATCH 2/4] feat(genrule): add `$D` Make variable To be compatible with built-in genrule[1] [1]: https://bazel.build/reference/be/make-variables#predefined_genrule_variables --- labgrid/genrule/rule.bzl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/labgrid/genrule/rule.bzl b/labgrid/genrule/rule.bzl index c2af0ae0..72e44625 100644 --- a/labgrid/genrule/rule.bzl +++ b/labgrid/genrule/rule.bzl @@ -48,6 +48,12 @@ ATTRS = { ), } +def _d(ctx): + if len(ctx.outputs.outs) == 1: + parent, _ = ctx.outputs.outs[0].path.rsplit("/", 1) + return parent + return "{}/{}".format(ctx.bin_dir.path, ctx.label.package) + def _substitutions(ctx): outs = [shell.quote(o.path) for o in ctx.outputs.outs] srcs = [shell.quote(o.path) for o in ctx.files.srcs] @@ -69,6 +75,8 @@ def _substitutions(ctx): elif "$@" in ctx.attr.cmd: fail("Cannot specify `$@` Make variable with zero or multiple `outs`") + substitutions["$D"] = shell.quote(_d(ctx)) + for key, value in ctx.var.items(): substitutions["$({})".format(key)] = value -- GitLab From 6729f52233db8066e81bc2694d373885ca7ee9ad Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 6 Nov 2024 10:10:18 +0000 Subject: [PATCH 3/4] feat(genrule): add `$D` Make variable to environment Allows LabGrid tools to easily understand the output directory. --- labgrid/genrule/rule.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/labgrid/genrule/rule.bzl b/labgrid/genrule/rule.bzl index 72e44625..1795e9f4 100644 --- a/labgrid/genrule/rule.bzl +++ b/labgrid/genrule/rule.bzl @@ -99,7 +99,7 @@ def implementation(ctx): outputs = ctx.outputs.outs, tools = [t.files_to_run for t in (ctx.attr.tools + ctx.attr.srcs)] + [ctx.executable._sh], use_default_shell_env = False, - env = ctx.configuration.default_shell_env | ctx.attr._executor[RunEnvironmentInfo].environment, + env = ctx.configuration.default_shell_env | ctx.attr._executor[RunEnvironmentInfo].environment | {"D": _d(ctx)}, mnemonic = "LabGridGenrule", ) -- GitLab From 9d5338e07c2cae59b801091a112cc44f6b6ba91e Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 6 Nov 2024 10:12:03 +0000 Subject: [PATCH 4/4] feat(run): resolve local paths to `$D` This allows, in the common case, to use a single argument `--get`. --- e2e/localhost/BUILD.bazel | 11 +++++++++++ labgrid/run/run.py | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/e2e/localhost/BUILD.bazel b/e2e/localhost/BUILD.bazel index 5a261932..5300154f 100644 --- a/e2e/localhost/BUILD.bazel +++ b/e2e/localhost/BUILD.bazel @@ -37,6 +37,16 @@ labgrid_genrule( tools = ["@rules_labgrid//labgrid/run"], ) +labgrid_genrule( + name = "hello-and-transfer-file-back-single-argument", + # We need Bash to redirect to a file on the target + srcs = ["@ape//ape:bash"], + outs = ["run-and-transfer-file-back-single-argument.actual"], + cmd = "$(location @rules_labgrid//labgrid/run) --get run-and-transfer-file-back-single-argument.actual $(location @ape//ape:bash) -c 'echo world > run-and-transfer-file-back-single-argument.actual'", + platform = "@rules_labgrid//platform:localhost", + tools = ["@rules_labgrid//labgrid/run"], +) + labgrid_genrule( name = "hello-with-env", srcs = ["@ape//ape:printenv"], @@ -72,6 +82,7 @@ labgrid_run_binary( "run", "run-with-runfiles", "run-and-transfer-file-back", + "run-and-transfer-file-back-single-argument", "run-with-env", "run-binary", ) diff --git a/labgrid/run/run.py b/labgrid/run/run.py index 78ccee71..c25e9e45 100644 --- a/labgrid/run/run.py +++ b/labgrid/run/run.py @@ -208,7 +208,7 @@ def arguments(prsr: ArgumentParser) -> None: ) prsr.add_argument( "--get", - help="Transfer files from target at end of execution. Relative paths are resolved to the execution root on both the local and remote. `REMOTE` can have a trailing `?` to indicate it's optional. `LOCAL` performs environment variable substitution. `LOCAL` can be omitted, which will use the same path for remote and local.", + help="Transfer files from target at end of execution. Relative paths are resolved to the execution root on the remote and `${D-${PWD}}` (as specified by Bazel `genrule` predefined variables) on the local machine. `LOCAL` can be omitted, which will use the same path for remote and local. `REMOTE` can have a trailing `?` that it is optional. `LOCAL` performs environment variable substitution.", type=get, metavar="REMOTE[?][:LOCAL]", action="append", @@ -245,9 +245,20 @@ class Get: optional: bool def resolve(self, remote: PurePath) -> Get: + try: + base = Path(environ["D"]) + except KeyError: + base = Path.cwd() + local = self.local + else: + try: + local = Path(self.local).relative_to(base) + except ValueError: + local = self.local + return Get( remote=self.join(remote, self.remote), - local=self.join(environ.get("BAZEL_GEN_DIR", Path.cwd()), self.local), + local=self.join(base, local), optional=self.optional, ) -- GitLab