Skip to content

Commit

Permalink
[bazel] Add an exec_env aware filegroup rule
Browse files Browse the repository at this point in the history
In order to construct bundles needed for provisioning tests, we need to
associated offline-signed binary files with their corresponding exec_env
providers.  The `exec_env_filegroup` rule allows matching pre-built
binaries with their corresponding exec_env.

Signed-off-by: Chris Frantz <[email protected]>
  • Loading branch information
cfrantz committed Nov 22, 2024
1 parent 0e10a11 commit 96eb371
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
45 changes: 41 additions & 4 deletions rules/opentitan/cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ opentitan_test = rv_rule(

def _opentitan_binary_assemble_impl(ctx):
assembled_bins = []
result = []
tc = ctx.toolchains[LOCALTOOLS_TOOLCHAIN]
for env in ctx.attr.exec_env:
exec_env_name = env[ExecEnvInfo].exec_env
Expand All @@ -477,10 +478,10 @@ def _opentitan_binary_assemble_impl(ctx):
fail("Only flash binaries can be assembled.")
input_bins.append(binary[exec_env_provider].default)
spec.append("{}@{}".format(binary[exec_env_provider].default.path, offset))
assembled_bins.append(
assemble_for_test(ctx, name, spec, input_bins, tc.tools.opentitantool),
)
return [DefaultInfo(files = depset(assembled_bins))]
img = assemble_for_test(ctx, name, spec, input_bins, tc.tools.opentitantool)
result.append(exec_env_provider(default = img, kind = "flash"))
assembled_bins.append(img)
return result + [DefaultInfo(files = depset(assembled_bins))]

opentitan_binary_assemble = rule(
implementation = _opentitan_binary_assemble_impl,
Expand All @@ -497,3 +498,39 @@ opentitan_binary_assemble = rule(
},
toolchains = [LOCALTOOLS_TOOLCHAIN],
)

def _exec_env_filegroup(ctx):
files = {v: k for k, v in ctx.attr.files.items()}
exec_env = {v: k for k, v in ctx.attr.exec_env.items()}

fset = {k: 1 for k in files.keys()}
eset = {k: 1 for k in exec_env.keys()}

if fset != eset:
fail("The set of files and exec_envs must be matched: files =", fset.keys(), ", exec_env =", eset.keys())

result = []
for k in files.keys():
provider = exec_env[k][ExecEnvInfo].provider
f = files[k].files.to_list()
if len(f) != 1:
fail("files[{}] must supply exactly one file".format(k))
result.append(provider(default = f[0], kind = ctx.attr.kind))
return result

exec_env_filegroup = rule(
implementation = _exec_env_filegroup,
attrs = {
"files": attr.label_keyed_string_dict(
allow_files = True,
mandatory = True,
doc = "Dictionary of files to exec_envs.",
),
"exec_env": attr.label_keyed_string_dict(
providers = [ExecEnvInfo],
mandatory = True,
doc = "Dictionary of execution environments for this target.",
),
"kind": attr.string(default = "flash", doc = "The kind of binary"),
},
)
2 changes: 1 addition & 1 deletion rules/opentitan/exec_env.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def update_file_attr(name, attr, provider, data_files, param, action_param = Non
elif DefaultInfo in attr:
file = attr[DefaultInfo].files.to_list()
if len(file) > 1:
fail("Expected to find exactly one file in", attr)
fail("Expected to find exactly one file in", attr, ", but got", file)
_update(name, file[0], data_files, param, action_param)
else:
fail("No file providers in", attr)
Expand Down

0 comments on commit 96eb371

Please sign in to comment.