Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | r"""Generates chip_info.h for ROM build |
| 6 | """ |
| 7 | |
| 8 | import argparse |
| 9 | import logging as log |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 10 | import sys |
| 11 | from datetime import datetime |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 12 | from pathlib import Path |
| 13 | |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 14 | header_template = r""" |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 15 | // Copyright lowRISC contributors. |
| 16 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 17 | // SPDX-License-Identifier: Apache-2.0 |
| 18 | // |
Tobias Wölfel | ee06edc | 2019-09-24 12:06:34 +0200 | [diff] [blame] | 19 | // --------- W A R N I N G: A U T O - G E N E R A T E D C O D E !! ---------// |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 20 | |
| 21 | #ifndef _F_CHIPINFO_H__ |
| 22 | #define _F_CHIPINFO_H__ |
| 23 | |
| 24 | static const char chip_info[128] __attribute__((section(".chip_info"))) = |
Alphan Ulusoy | 022b181 | 2022-06-13 10:49:20 -0400 | [diff] [blame] | 25 | "Version: {%version%}, Build Date: {%build_date%}"; |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 26 | |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 27 | #endif // _F_CHIPINFO_H__ |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 28 | |
| 29 | """ |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 30 | |
| 31 | |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 32 | def main(): |
| 33 | parser = argparse.ArgumentParser(prog="rom_chip_info") |
| 34 | parser.add_argument('--outdir', |
| 35 | '-o', |
| 36 | required=True, |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 37 | help='Output Directory') |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 38 | parser.add_argument('--ot_version', |
Drew Macrae | 399af40 | 2021-11-01 18:15:09 +0000 | [diff] [blame] | 39 | required=False, |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 40 | help='OpenTitan Version') |
Drew Macrae | 399af40 | 2021-11-01 18:15:09 +0000 | [diff] [blame] | 41 | parser.add_argument('--ot_version_file', |
| 42 | required=False, |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 43 | help='Path to a file with the OpenTitan Version') |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 44 | |
| 45 | log.basicConfig(format="%(levelname)s: %(message)s") |
| 46 | args = parser.parse_args() |
| 47 | |
| 48 | if not args.outdir: |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 49 | log.error("Missing --outdir.") |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 50 | raise SystemExit(sys.exc_info()[1]) |
Drew Macrae | 399af40 | 2021-11-01 18:15:09 +0000 | [diff] [blame] | 51 | |
| 52 | if args.ot_version: |
| 53 | version = args.ot_version |
| 54 | elif args.ot_version_file: |
| 55 | version = open(args.ot_version_file, "rt").read().strip() |
| 56 | else: |
| 57 | log.error( |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 58 | "Missing ot_version, provide --ot_version or --ot_version_file.") |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 59 | raise SystemExit(sys.exc_info()[1]) |
| 60 | |
| 61 | outdir = Path(args.outdir) |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 62 | |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 63 | outdir.mkdir(parents=True, exist_ok=True) |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 64 | out_path = outdir / "chip_info.h" |
| 65 | |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 66 | now = datetime.now() |
Alphan Ulusoy | 022b181 | 2022-06-13 10:49:20 -0400 | [diff] [blame] | 67 | wall_time = now.strftime("%Y-%m-%d %H:%M:%S") |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 68 | |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 69 | log.info("Version: %s" % (version, )) |
| 70 | log.info("Build Date: %s" % (wall_time, )) |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 71 | |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 72 | output = header_template |
| 73 | output = output.replace('{%version%}', version, 1) |
Timothy Chen | c1b009e | 2019-09-19 18:07:11 -0700 | [diff] [blame] | 74 | output = output.replace('{%build_date%}', wall_time, 1) |
| 75 | |
| 76 | with out_path.open(mode='w', encoding='UTF-8') as fout: |
| 77 | fout.write(output + "\n") |
| 78 | |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | main() |