Skip to content

Commit 64ac503

Browse files
committed
[dv] Small refactor based on feedback from backport PR.
1 parent c8c4366 commit 64ac503

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

util/py/scripts/build_sw_collateral_for_sim.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
directly out of ROM instead of flash. There are more possible flags documented below.
2626
2727
A string for each sw image (root filename + index + flags) is also passed through
28-
to the tesbench, which re-parses it (in chip_env_cfg.sv) to extract the index and
28+
to the testbench, which re-parses it (in chip_env_cfg.sv) to extract the index and
2929
flags which determines which piece of collateral should be loaded (by filename)
3030
to which simulated memory model.
3131
@@ -74,6 +74,7 @@
7474
import os
7575
import sys
7676
import shutil
77+
import shlex
7778
from dataclasses import dataclass, field
7879
from enum import Enum
7980
import textwrap
@@ -187,7 +188,7 @@ def query(self, label: str, opts: list[str]) -> list[str]:
187188
logger.info(f"query_cmd = {' '.join(query_cmd)}")
188189
return self._run_cmd(query_cmd)
189190

190-
def cquery(self, label: str, opts: list[str]) -> list[str]:
191+
def cquery(self, label: str, opts: tuple[str]) -> tuple[str]:
191192
cquery_cmd = (
192193
self.cmd,
193194
"cquery",
@@ -212,12 +213,14 @@ def _run_cmd(self, cmd: list[str]) -> list[str]:
212213
res = subprocess.run(cmd, capture_output=True, encoding="utf-8", text=True)
213214

214215
if res.returncode != 0:
216+
print("---- STDOUT ----")
215217
print(res.stdout, flush=True)
218+
print("---- STDERR ----")
216219
print(res.stderr, flush=True)
217220
sys.exit(f"_run_cmd -> had a non-zero return code of {res.returncode}.")
218221

219-
logger.debug(f"_run_cmd -> stdout:\n{res.stdout}")
220-
logger.debug(f"_run_cmd -> stderr:\n{res.stderr}")
222+
logger.debug("_run_cmd -> stdout:%s\n", res.stdout)
223+
logger.debug("_run_cmd -> stderr:%s\n", res.stderr)
221224

222225
stdout_lines = res.stdout.split("\n")
223226
return [s for s in stdout_lines if s]
@@ -240,11 +243,15 @@ def __post_init__(self):
240243

241244
self.package = parts[0]
242245
self.target = parts[1]
243-
self.index = int(parts[2])
244-
self.flags = parts[3:] if len(parts) > 3 else ()
246+
try:
247+
self.index = sw_type_e(int(parts[2]))
248+
except ValueError as e:
249+
sys.exit(f"Invalid index value: {e}")
250+
self.flags = set(parts[3:])
245251
self.label = f"{self.package}:{self.target}"
246252

247-
assert all((f in KNOWN_FLAGS) for f in self.flags), "Unknown FLAG used in sw_image"
253+
for flag in self.flags:
254+
assert flag in KNOWN_FLAGS, f"Unknown flag '{flag}' used in sw_image '{self.raw}'"
248255
logger.debug(f"flags={self}")
249256

250257

@@ -298,7 +305,7 @@ def _deploy_software_collateral(args) -> None:
298305
f"--repository_cache={ENV.get('BAZEL_CACHE')}",
299306
]
300307

301-
# Export this environment variable to build with a non-default OTP permuation
308+
# Export this environment variable to build with a non-default OTP permutation
302309
if ENV.get("BAZEL_OTP_DATA_PERM_FLAG"):
303310
bazel_runner.build_opts += [
304311
f"--//util/design/data:data_perm={ENV.get('BAZEL_OTP_DATA_PERM_FLAG')}",
@@ -310,7 +317,7 @@ def _deploy_software_collateral(args) -> None:
310317

311318
# Determine the final label and cquery expression to build and get the
312319
# artifacts for each image.
313-
image_query_set = {}
320+
image_query_map = {}
314321
for image in args.sw_images:
315322
image_string = ImageString(image)
316323

@@ -353,12 +360,12 @@ def _deploy_software_collateral(args) -> None:
353360
kind = kind_query[0].split(" ")[0]
354361

355362
# Add a query object to the set for this image
356-
image_query_set[image] = ImageQuery(image_string, label, cquery, kind)
363+
image_query_map[image] = ImageQuery(image_string, label, cquery, kind)
357364

358365
logger.info("Image query parameters determined.\n")
359366

360367
# Build all the software artifacts
361-
bazel_labels = (v.label for v in image_query_set.values())
368+
bazel_labels = [v.label for v in image_query_map.values()]
362369
logger.info("Building all labels...")
363370
bazel_runner.build(bazel_labels)
364371
logger.info("All labels built.\n")
@@ -367,7 +374,7 @@ def _deploy_software_collateral(args) -> None:
367374
for image in args.sw_images:
368375
logger.info(f"Querying runfiles for image : {image}")
369376

370-
iq = image_query_set[image]
377+
iq = image_query_map[image]
371378

372379
# First, run the query to get the maximal set of runfiles for the image
373380
runfiles = _get_image_runfiles(iq, bazel_runner)
@@ -418,7 +425,7 @@ def _get_image_runfiles(iq: ImageQuery, bazel_runner: BazelRunner) -> list[str]:
418425
runfiles: list[str] = []
419426

420427
logger.info(f"kind = {iq.kind}")
421-
if iq.kind in ("opentitan_test", "opentitan_binary", "alias"):
428+
if iq.kind in BAZEL_STARLARK_QUERY_RULE_KIND:
422429
# For targets of this kind, we can query directly for the set of runfiles.
423430
# The query may have a slightly different starlark expression to extract the
424431
# files from the target depending on the kind.
@@ -495,11 +502,10 @@ def _int_or_str(value):
495502
except Exception:
496503
return value
497504

498-
mod_doc = sys.modules[__name__].__doc__
499505
parser = argparse.ArgumentParser(
500506
# Use the module description to generate CLI --help docs
501-
description=(mod_doc.split("\n")[0]),
502-
epilog=(80 * "-" + f"\n\n{mod_doc}"),
507+
description=(__doc__.splitlines()[0]),
508+
epilog=(80 * "-" + f"\n\n{__doc__}"),
503509
formatter_class=argparse.RawDescriptionHelpFormatter,
504510
)
505511
parser.add_argument(
@@ -514,7 +520,7 @@ def _int_or_str(value):
514520
default=[],
515521
help="Additional opts to be passed while building software",
516522
)
517-
parser.add_argument("--sw-build-device", type=str, help="")
523+
parser.add_argument("--sw-build-device")
518524
parser.add_argument("--seed", type=int, help="Seed")
519525
parser.add_argument("--build-seed", type=_int_or_str, help="Build Seed")
520526
parser.add_argument(
@@ -542,11 +548,12 @@ def _mk_argstr(args) -> str:
542548
argstr += " \\\n// --" + argname + " " + str(a) + ""
543549
return argstr
544550

545-
# Log the script name and invocation arguments
546-
logger.info(f"\n// {sys.argv[0]} {_mk_argstr(args)}\n")
551+
logger.info("Command: %s", shlex.join(sys.argv))
552+
logger.info("Parsed arguments:")
553+
for name, val in sorted(vars(args).items()):
554+
logger.info(" %s = %r", name, val)
547555

548556
_deploy_software_collateral(args)
549557

550-
551558
if __name__ == "__main__":
552559
sys.exit(_main())

0 commit comments

Comments
 (0)