Skip to content

Commit

Permalink
[bazel] Capture output of updatemem to make it less verbose
Browse files Browse the repository at this point in the history
updatemem likes to print a lot of things which makes the CI output
less readable for no particular benefit. This commit introduces a
bash script that runs updatmem and captures the output. If an error
occurs, it will print the path to the output and error logs. Those
files are also added to the output groups of the bitstream_splice
rules so that they can be queried if needed.

Signed-off-by: Amaury Pouly <[email protected]>
  • Loading branch information
pamaury committed Dec 20, 2024
1 parent c3911ca commit 1b6bb98
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 8 deletions.
91 changes: 91 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions rules/opentitan/splice.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def gen_vivado_mem_file(ctx, name, src, tool, swap_nibbles = True):

def vivado_updatemem(ctx, name, src, instance, mmi, update, debug = False):
spliced = ctx.actions.declare_file("{}.bit".format(name))
stdout = ctx.actions.declare_file("{}.out.log".format(name))
stderr = ctx.actions.declare_file("{}.err.log".format(name))

# Vivado's `updatemem` only accepts bitstream filenames that end with `.bit`.
# If the filename doesn't have that extension, symlink to a name that does.
Expand All @@ -57,17 +59,20 @@ def vivado_updatemem(ctx, name, src, instance, mmi, update, debug = False):

ctx.actions.run(
mnemonic = "SpliceBitstream",
outputs = [spliced],
outputs = [spliced, stdout, stderr],
inputs = [src, mmi, update],
arguments = [args],
executable = "updatemem",
arguments = ["updatemem", args],
executable = ctx.executable._run_script,
use_default_shell_env = False,
execution_requirements = {
"no-sandbox": "",
},
env = ENV,
env = ENV | {
"RUN_CAPTURE_STDOUT": stdout.path,
"RUN_CAPTURE_STDERR": stderr.path,
},
)
return spliced
return spliced, stdout, stderr

def update_usr_access(ctx, name, src, tool):
"""Updates the USR_ACCESS value in the bistream.
Expand Down Expand Up @@ -125,7 +130,7 @@ def _bitstream_splice_impl(ctx):
tool = tc.tools.gen_mem_image,
swap_nibbles = ctx.attr.swap_nibbles,
)
src = vivado_updatemem(
src, rom_updatemem_stdout, rom_updatemem_stderr = vivado_updatemem(
ctx = ctx,
name = "{}-rom".format(ctx.label.name),
src = src,
Expand All @@ -148,7 +153,7 @@ def _bitstream_splice_impl(ctx):
tool = tc.tools.gen_mem_image,
swap_nibbles = ctx.attr.swap_nibbles,
)
src = vivado_updatemem(
src, otp_updatemem_stdout, otp_updatemem_stderr = vivado_updatemem(
ctx = ctx,
name = "{}-otp".format(ctx.label.name),
src = src,
Expand All @@ -164,7 +169,15 @@ def _bitstream_splice_impl(ctx):
src = src,
tool = tc.tools.opentitantool,
)
return DefaultInfo(files = depset([output]))
return [
DefaultInfo(files = depset([output])),
OutputGroupInfo(
rom_updatemem_stdout = depset([rom_updatemem_stdout]),
rom_updatemem_stderr = depset([rom_updatemem_stderr]),
otp_updatemem_stdout = depset([otp_updatemem_stdout]),
otp_updatemem_stderr = depset([otp_updatemem_stderr]),
),
]

bitstream_splice_ = rule(
implementation = _bitstream_splice_impl,
Expand All @@ -177,6 +190,7 @@ bitstream_splice_ = rule(
"swap_nibbles": attr.bool(default = True, doc = "Swap nybbles while preparing the memory image"),
"debug": attr.bool(default = False, doc = "Emit debug info while updating"),
"skip": attr.bool(default = False, doc = "Skip splice and do not modify the bitstream"),
"_run_script": attr.label(default = "//rules/scripts:run_and_capture", executable = True, cfg = "exec", doc = "Path to runner script that captures the output"),
},
toolchains = [LOCALTOOLS_TOOLCHAIN],
)
Expand Down
5 changes: 5 additions & 0 deletions rules/scripts/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ sh_binary(
name = "modid_check",
srcs = ["modid_check.sh"],
)

sh_binary(
name = "run_and_capture",
srcs = ["run_and_capture.sh"],
)
14 changes: 14 additions & 0 deletions rules/scripts/run_and_capture.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
#
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
#
set -e

# Run the program and redirect the stdout and stderr to given files
if ! "$@" >$RUN_CAPTURE_STDOUT 2>$RUN_CAPTURE_STDERR; then
echo "$1 failed" >&2
echo "see stdout in $RUN_CAPTURE_STDOUT" >&2
echo "see stderr in $RUN_CAPTURE_STDERR" >&2
fi

0 comments on commit 1b6bb98

Please sign in to comment.