Skip to content

Commit

Permalink
[bazel] Convert autogen_chip_info_src to stamping
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pamaury committed Jan 18, 2024
1 parent a8cb50e commit cc40e67
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
20 changes: 10 additions & 10 deletions rules/autogen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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):
Expand Down
16 changes: 10 additions & 6 deletions util/rom_chip_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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)


Expand All @@ -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)
Expand Down

0 comments on commit cc40e67

Please sign in to comment.