From 62266c93f41b9d019de164e89d3f3aa571a8e3cd Mon Sep 17 00:00:00 2001 From: Tim Newsome Date: Fri, 16 Feb 2024 10:47:53 -0800 Subject: [PATCH] Propagate XML file licenses into generated C code. --- registers.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/registers.py b/registers.py index cbeb9821..47524a73 100755 --- a/registers.py +++ b/registers.py @@ -14,7 +14,7 @@ class Registers( object ): def __init__( self, name, label, prefix, description, skip_index, - skip_access, skip_reset, depth ): + skip_access, skip_reset, depth, licenses ): self.name = name self.label = label self.prefix = prefix or "" @@ -23,6 +23,7 @@ def __init__( self, name, label, prefix, description, skip_index, self.skip_access = skip_access self.skip_reset = skip_reset self.depth = depth + self.licenses = licenses self.registers = [] def add_register( self, register ): @@ -279,7 +280,13 @@ def parse_bits( field ): else: assert False, text +def parse_spdx( path ): + with open( path ) as f: + data = f.read(65536) + return set(re.findall(r"SPDX-License-Identifier:\s*(.+)$", data, re.MULTILINE)) + def parse_xml( path ): + licenses = parse_spdx(path) e = xml.etree.ElementTree.parse( path ).getroot() if e.text: description = e.text.strip() @@ -290,7 +297,8 @@ def parse_xml( path ): int( e.get( 'skip_index', 0 ) ), int( e.get( 'skip_access', 0 ) ), int( e.get( 'skip_reset', 0 ) ), - int( e.get( 'depth', 1 ))) + int( e.get( 'depth', 1 )), + licenses) for r in e.findall( 'register' ): name = r.get( 'name' ) short = r.get( 'short' ) @@ -423,6 +431,10 @@ def c_max(args): return c_max(args) raise Exception("Unsupported sympy object %r of type %r" % (expression, type(expression))) +def write_c_licenses( fd, licenses ): + for license in licenses: + fd.write(f"/* SPDX-License-Identifier: {license} */\n") + def write_cheader( fd, registers ): definitions = [] for r in registers.registers: @@ -815,11 +827,17 @@ def main(): parsed = parser.parse_args() if (parsed.xml_paths): + registers_list = [parse_xml( xml_path ) for xml_path in parsed.xml_paths] + license_lists = [registers.licenses for registers in registers_list] + # Assert every license list is the same + assert all(license_lists[0] == license_list for license_list in license_lists), \ + "All XML files must have the same SPDX-License-Identifier" fd_h = open( parsed.path + ".h", "a" ) + write_c_licenses( fd_h, license_lists[0] ) fd_h.write("#ifndef DEBUG_DEFINES_H\n#define DEBUG_DEFINES_H\n") fd_c = open( parsed.path + ".c", "a" ) + write_c_licenses( fd_c, license_lists[0] ) fd_c.write(f'#include "{parsed.path}.h"\n#include \n#include \n') - registers_list = [parse_xml( xml_path ) for xml_path in parsed.xml_paths] for registers in registers_list: write_cheader( fd_h, registers ) print_cgetters(registers_list, fd_h, fd_c) @@ -830,7 +848,9 @@ def main(): if parsed.definitions: write_definitions( open( parsed.definitions, "w" ), registers ) if parsed.cheader: - write_cheader( open( parsed.cheader, "w" ), registers ) + with open( parsed.cheader, "w" ) as fd: + write_c_licenses( fd, registers.licenses ) + write_cheader( fd, registers ) if parsed.chisel: write_chisel( open( parsed.chisel, "w" ), registers ) if not registers.skip_index: