Skip to content

Commit 5fbb8fb

Browse files
Merge pull request #63 from tiagocoutinho/codegen
Code generation fixes
2 parents ec5a5b3 + e105678 commit 5fbb8fb

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

linuxpy/codegen/base.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def decode_macro_value(value, context, name_map):
140140
value = value.replace("*/", "")
141141
value = value.replace("/*", "#")
142142
value = value.replace("struct ", "")
143+
value = value.replace("union ", "")
143144
value = value.replace("(1U", "(1")
144145
value = value.strip()
145146
for dtype in "ui":
@@ -218,12 +219,12 @@ def find_xml_base_type(etree, context, type_id):
218219
def get_structs(header_filename, xml_filename, decode_name):
219220
etree = xml.etree.ElementTree.parse(xml_filename)
220221
header_tag = etree.find(f"File[@name='{header_filename}']")
221-
structs = {}
222+
struct_map = {}
223+
structs = []
222224
if header_tag is None:
223225
return structs
224226
header_id = header_tag.get("id")
225-
nodes = etree.findall(f"Struct[@file='{header_id}']")
226-
nodes += etree.findall(f"Union[@file='{header_id}']")
227+
nodes = [node for node in etree.findall(f"*[@file='{header_id}']") if node.tag in {"Union", "Struct"}]
227228
for node in nodes:
228229
members = node.get("members")
229230
if members is not None:
@@ -233,23 +234,28 @@ def get_structs(header_filename, xml_filename, decode_name):
233234
member_ids = []
234235
fields = (etree.find(f"*[@id='{member_id}']") for member_id in member_ids)
235236
fields = [field for field in fields if field.tag not in {"Union", "Struct", "Unimplemented"}]
236-
pack = int(node.get("align")) == 8
237+
align = node.get("align")
238+
if align is None:
239+
pack = False
240+
else:
241+
pack = int(align) == 8
237242
name = node.get("name")
238243
if name:
239244
name = decode_name(name)
240245
struct = CStruct(node, name, fields, pack)
241-
structs[struct.id] = struct
242-
for struct in structs.values():
246+
struct_map[struct.id] = struct
247+
structs.append(struct)
248+
for struct in structs:
243249
if struct.context_id != "_1":
244-
parent = structs.get(struct.context_id)
250+
parent = struct_map.get(struct.context_id)
245251
if parent:
246252
parent.children[struct.id] = struct
247253
struct.parent = parent
248254
else:
249255
logging.error("Could not find parent")
250256
if not struct.name and struct.parent:
251257
struct.name = f"M{len(struct.parent.children)}"
252-
for struct in structs.values():
258+
for struct in structs:
253259
for node in struct.node_members:
254260
name = node.get("name")
255261
base = find_xml_base_type(etree, struct, node.get("type"))
@@ -295,8 +301,11 @@ def get_enums(header_filename, xml_filename, enums, decode_name):
295301
continue
296302
py_name = cname_to_pyname(decode_name(cname))
297303
prefix = cname.upper() + "_"
298-
raw_names = [child.get("name") for child in node]
299-
common_prefix = os.path.commonprefix(raw_names)
304+
if len(node) > 1:
305+
raw_names = [child.get("name") for child in node]
306+
common_prefix = os.path.commonprefix(raw_names)
307+
else:
308+
common_prefix = ""
300309
values = []
301310
for child in node:
302311
name = child.get("name")
@@ -347,8 +356,7 @@ def run(name, headers, template, macro_enums, output=None, decode_struct_name=nu
347356
xml_filename = os.path.join(temp_dir, base_header + ".xml")
348357
cmd = f"castxml --castxml-output=1.0.0 -o {xml_filename} {header}"
349358
assert os.system(cmd) == 0
350-
new_structs = get_structs(header, xml_filename, decode_name=decode_struct_name)
351-
structs += list(new_structs.values())
359+
structs += get_structs(header, xml_filename, decode_name=decode_struct_name)
352360

353361
get_enums(header, xml_filename, macro_enums, decode_name=decode_enum_name)
354362

linuxpy/codegen/cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from .gpio import main as gpio_main
44
from .input import main as input_run
55
from .magic import main as magic_run
6+
from .midi import main as midi_run
7+
from .network import main as network_run
68
from .usbfs import main as usbfs_run
79
from .usbids import main as usbids_main
810
from .video import main as video_main
@@ -12,11 +14,13 @@ def main():
1214
logging.basicConfig(level="INFO")
1315

1416
gpio_main()
15-
magic_run()
1617
input_run()
17-
video_main()
18+
magic_run()
19+
midi_run()
20+
network_run()
1821
usbfs_run()
1922
usbids_main()
23+
video_main()
2024

2125

2226
if __name__ == "__main__":

0 commit comments

Comments
 (0)