From cc40e67f394f5c6d1bfbc3cf49e80921c4f979d1 Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Thu, 18 Jan 2024 15:52:40 +0000 Subject: [PATCH] [bazel] Convert `autogen_chip_info_src` to stamping With this change, `autogen_chip_info_src` will only pass version information if stamping is enabled on the command-line. This commit changes the meaning of the `--ot_version_file` flag of `//util:rom_chip_info`. Previously, it was a path to a file that contains the git sha. It is now a path to the bazel version info file which is parsed using the `version_info` library. Note: with this change, unless stamping is enabled, the ROM will not contained the git sha in the chip_info anymore. Signed-off-by: Amaury Pouly --- rules/autogen.bzl | 20 ++++++++++---------- util/rom_chip_info.py | 16 ++++++++++------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/rules/autogen.bzl b/rules/autogen.bzl index 66a670f6b55d56..123d44157c116b 100644 --- a/rules/autogen.bzl +++ b/rules/autogen.bzl @@ -76,21 +76,25 @@ autogen_hjson_header = rule( ) def _chip_info_src(ctx): + stamp_args = [] + stamp_files = [] + if stamping_enabled(ctx): + stamp_files = [ctx.version_file] + stamp_args.append("--ot_version_file") + stamp_args.append(ctx.version_file.path) + out_source = ctx.actions.declare_file("chip_info.c") ctx.actions.run( outputs = [ out_source, ], inputs = [ - ctx.file.version, ctx.executable._tool, - ], + ] + stamp_files, arguments = [ "-o", out_source.dirname, - "--ot_version_file", - ctx.file.version.path, - ], + ] + stamp_args, executable = ctx.executable._tool, ) @@ -101,16 +105,12 @@ def _chip_info_src(ctx): autogen_chip_info_src = rule( implementation = _chip_info_src, attrs = { - "version": attr.label( - default = "//util:scm_revision_file", - allow_single_file = True, - ), "_tool": attr.label( default = "//util:rom_chip_info", executable = True, cfg = "exec", ), - }, + } | stamp_attr(-1, "//rules:stamp_flag"), ) def autogen_chip_info(name): diff --git a/util/rom_chip_info.py b/util/rom_chip_info.py index c73fec8d339a23..9cbf6b502a9651 100755 --- a/util/rom_chip_info.py +++ b/util/rom_chip_info.py @@ -8,6 +8,7 @@ import logging as log from pathlib import Path +import version_file def generate_chip_info_c_source(scm_revision: int) -> str: """Return the contents of a C source file that defines `kChipInfo`. @@ -43,11 +44,14 @@ def generate_chip_info_c_source(scm_revision: int) -> str: """ -def read_version_file(path: Path) -> int: - """Interprets the contents of `path` as a big-endian hex literal.""" +def read_version_file(version_info) -> int: + """ + Search for the BUILD_SCM_REVISION variable and interprets + the contents as a big-endian hex literal. Return a constant + if the variable is not found. + """ - with open(path, "rt") as f: - version = f.read().strip() + version = version_info.get('BUILD_SCM_REVISION', "8badF00d") return int(version, base=16) @@ -73,13 +77,13 @@ def main(): type=Path, help='Output Directory') parser.add_argument('--ot_version_file', - required=True, type=Path, help='Path to a file with the OpenTitan Version') args = parser.parse_args() - version = read_version_file(args.ot_version_file) + # Extract version stamp from file + version = read_version_file(version_file.parse_version_file(args.ot_version_file)) log.info("Version: %x" % (version, )) generated_source = generate_chip_info_c_source(version)