From e9113a17f64068f3ce32bf802b31b6e309e600f8 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Wed, 9 Oct 2024 11:03:22 +0100 Subject: [PATCH 1/3] feat(config_toolchain): expose `env` and `data` --- labgrid/config/toolchain/macro.bzl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/labgrid/config/toolchain/macro.bzl b/labgrid/config/toolchain/macro.bzl index 99a7af81..14caad5b 100644 --- a/labgrid/config/toolchain/macro.bzl +++ b/labgrid/config/toolchain/macro.bzl @@ -2,16 +2,17 @@ load("@rules_labgrid//labgrid/config:defs.bzl", "labgrid_config") visibility("//...") -def labgrid_config_toolchain(*, name, src, state, target_compatible_with, manager = "@rules_labgrid//labgrid/manager:passthrough", deps = ()): +def labgrid_config_toolchain(*, name, src, state, target_compatible_with, manager = "@rules_labgrid//labgrid/manager:passthrough", deps = (), env = {}, data = []): src = native.package_relative_label(src) labgrid_config( name = "{}-config".format(name), src = src, deps = [native.package_relative_label(d) for d in deps], - env = { + env = env | { "LG_ENV": "$(rlocationpath {})".format(src), "LG_STATE": state, }, + data = data, manager = native.package_relative_label(manager), ) -- GitLab From c6d9ff0995720d68bbfa11903202abb98ad7f2a3 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Wed, 9 Oct 2024 11:17:00 +0100 Subject: [PATCH 2/3] feat(config): support `$(location)` for data lookup Due to executor implementation details, we need to use `$(rlocationpath)` to look up data files when setting env vars in `labgrid_config`. This is unintuive, and it's likely that users expect to be able to use `$(location)`, like they would on a `py_binary`, for example. We could potentially also support `$(rootpath)` and `$(execpath)`, but we probably need to understand the use cases a bit better, so I've left it for later. --- labgrid/config/rule.bzl | 7 ++++++- labgrid/config/toolchain/macro.bzl | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/labgrid/config/rule.bzl b/labgrid/config/rule.bzl index 596a00cf..4e5b8064 100644 --- a/labgrid/config/rule.bzl +++ b/labgrid/config/rule.bzl @@ -82,9 +82,14 @@ def _forward(target): def _flatten(iterable): return [e for x in iterable for e in x] +def _expand(ctx, data, value): + value = value.replace("$(location", "$(rlocationpath") + value = ctx.expand_location(value, targets = data) + return value + def implementation(ctx): data = ctx.attr.data + [ctx.attr.src] - env = {k: ctx.expand_location(v, targets = data) for k, v in ctx.attr.env.items()} + env = {k: _expand(ctx, data, v) for k, v in ctx.attr.env.items()} default = DefaultInfo(files = depset([ctx.file.src])) toolchain = platform_common.ToolchainInfo( src = ctx.file.src, diff --git a/labgrid/config/toolchain/macro.bzl b/labgrid/config/toolchain/macro.bzl index 14caad5b..e6d18f74 100644 --- a/labgrid/config/toolchain/macro.bzl +++ b/labgrid/config/toolchain/macro.bzl @@ -9,7 +9,7 @@ def labgrid_config_toolchain(*, name, src, state, target_compatible_with, manage src = src, deps = [native.package_relative_label(d) for d in deps], env = env | { - "LG_ENV": "$(rlocationpath {})".format(src), + "LG_ENV": "$(location {})".format(src), "LG_STATE": state, }, data = data, -- GitLab From e3bb7a33c9914485b478e43456195c8a06785c1d Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Wed, 9 Oct 2024 22:25:33 +0100 Subject: [PATCH 3/3] feat(config_toolchain): support `toolchains` The `env` attribute in `labgrid_config_toolchain` now supports "Make" variable substitution provided by the common attribute[1] `toolchains`. [1] https://bazel.build/reference/be/common-definitions#common-attributes --- labgrid/config/rule.bzl | 21 +++++++++++++++++++-- labgrid/config/toolchain/macro.bzl | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/labgrid/config/rule.bzl b/labgrid/config/rule.bzl index 4e5b8064..97733b1a 100644 --- a/labgrid/config/rule.bzl +++ b/labgrid/config/rule.bzl @@ -61,7 +61,7 @@ ATTRS = { cfg = "exec", ), "env": attr.string_dict( - doc = "The environment variables to set. Subject to `$(location)` substitution.", + doc = "The environment variables to set. Subject to `$(location)` and \"Make\" variable substitution.", ), "data": attr.label_list( allow_files = True, @@ -85,10 +85,27 @@ def _flatten(iterable): def _expand(ctx, data, value): value = value.replace("$(location", "$(rlocationpath") value = ctx.expand_location(value, targets = data) + for k, v in ctx.var.items(): + value = value.replace("$({})".format(k), _runfile(v)) + return value + +# FIXME: separator changes to `+` on Bazel 8+ +def _runfile(value, separator = "~"): + # Non-runfile value + if not value.startswith("bazel-out/"): + return value + + _, found, rest = value.partition("/external/") + if found: + workspace_name, workspaced, short_path = rest.partition("{}/".format(separator)) + if workspaced: + return "{}/{}".format(workspace_name, short_path) + fail("Failed to convert runfile `path` to `rlocationpath`: {}".format(value)) + return value def implementation(ctx): - data = ctx.attr.data + [ctx.attr.src] + data = ctx.attr.data + [ctx.attr.src] + ctx.attr.toolchains env = {k: _expand(ctx, data, v) for k, v in ctx.attr.env.items()} default = DefaultInfo(files = depset([ctx.file.src])) toolchain = platform_common.ToolchainInfo( diff --git a/labgrid/config/toolchain/macro.bzl b/labgrid/config/toolchain/macro.bzl index e6d18f74..3928a677 100644 --- a/labgrid/config/toolchain/macro.bzl +++ b/labgrid/config/toolchain/macro.bzl @@ -2,7 +2,7 @@ load("@rules_labgrid//labgrid/config:defs.bzl", "labgrid_config") visibility("//...") -def labgrid_config_toolchain(*, name, src, state, target_compatible_with, manager = "@rules_labgrid//labgrid/manager:passthrough", deps = (), env = {}, data = []): +def labgrid_config_toolchain(*, name, src, state, target_compatible_with, manager = "@rules_labgrid//labgrid/manager:passthrough", deps = (), env = {}, data = [], toolchains = []): src = native.package_relative_label(src) labgrid_config( name = "{}-config".format(name), @@ -13,6 +13,7 @@ def labgrid_config_toolchain(*, name, src, state, target_compatible_with, manage "LG_STATE": state, }, data = data, + toolchains = toolchains, manager = native.package_relative_label(manager), ) -- GitLab