diff --git a/e2e/localhost/BUILD.bazel b/e2e/localhost/BUILD.bazel index 5a261932413cadd310f5a4034b384e3b617f9637..5300154f099e67f7dc963348242491aee1dc066d 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/genrule/rule.bzl b/labgrid/genrule/rule.bzl index 6edde1d6d519d205bca2e3c91444b279cc968932..1795e9f4f27f172b06b68af1a15d233bc3f12280 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] @@ -56,6 +62,7 @@ def _substitutions(ctx): substitutions = { "$(OUTS)": " ".join(outs), "$(SRCS)": " ".join(srcs), + "$(RULEDIR)": "{}/{}".format(ctx.bin_dir.path, ctx.label.package), } if len(srcs) == 1: @@ -68,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 @@ -90,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", ) diff --git a/labgrid/run/run.py b/labgrid/run/run.py index 78ccee71c1019e2bc4326229cd3147e342db5951..c25e9e453268fa917b6d63cb46f66e7a8f8702ba 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, )