Skip to content

Commit

Permalink
Propagate XML file licenses into generated C code.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtwfroody committed Feb 16, 2024
1 parent 9b18901 commit 62266c9
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions registers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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 ):
Expand Down Expand Up @@ -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()
Expand All @@ -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' )
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 <stddef.h>\n#include <assert.h>\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)
Expand All @@ -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:
Expand Down

0 comments on commit 62266c9

Please sign in to comment.