summaryrefslogtreecommitdiff
path: root/tools/sort_actor_funcs.py
blob: 1a75d568552ef790fc1b525739f09cd77df85a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3

from pathlib import Path
import argparse

parser = argparse.ArgumentParser(description="Script to sort forward-declared functions and extern'd symbols in actors")
parser.add_argument("file", type=str, help="Path to c file to sort")
args = parser.parse_args()

file_path = Path(args.file).resolve()

if not file_path.suffix == ".c":
    print(f"Should only be run on a .c file!")
    exit()
if "/overlays/actors/" not in str(file_path):
    print(f"This should only be run on actors, are you sure you gave it the right file?")
    exit()

map_path = file_path.parent.parent.parent.parent.parent
map_lines = (map_path / "build" / "mm.map").read_text().splitlines()[10000:]

i = 0
while not f"{file_path.stem}.o(.text)" in map_lines[i]:
    i += 1
i += 2
assert map_lines[i-1].startswith(" .text")

orig_funcs = set()
while map_lines[i].startswith("  "):
    func = map_lines[i].split(" ")[-1]
    if not (func.endswith("_Init") or func.endswith("_Destroy") or func.endswith("_Update") or func.endswith("_Draw")):
        orig_funcs.add(func)
    i += 1

lines = file_path.read_text().splitlines()
start_func_line = 0
end_func_line = 0
func_list = set()
externs = []
extern_set = set()

i = 0
while (not (lines[i].startswith("const") or lines[i].startswith("static") or lines[i].startswith("typedef"))):
    line = lines[i]
    if line.startswith("extern"):
        extern_type = line.rsplit(" ",1)[0]
        extern_name = line.split(" ",2)[2][:-1]
        if extern_name not in extern_set:
            externs.append({"type":extern_type, "name":extern_name})
            extern_set.add(extern_name)
    i += 1

declares_needed = set()
funcs_seen = set()
funcs_types = []
for count, line in enumerate(lines):
    if not line.startswith(" ") and "(" in line and "{" in line and "static" not in line:
        func_ret = line.split(" ",1)[0]
        func_name = line.split(" ",1)[1].split("(",1)[0]
        func_args = line.split("(",1)[1].split(")",1)[0]
        funcs_seen.add(func_name)
        funcs_types.append({"ret":func_ret, "name":func_name, "args":func_args})
    else:
        for func in orig_funcs:
            if func in line and func not in funcs_seen:
                declares_needed.add(func)

externs.sort(key=lambda x: x["name"])

declares = []
for func in funcs_types:
    if func["name"] in declares_needed:
        declares.append(f"{func['ret']} {func['name']}({func['args']});")

print("\n".join(declares))
print()
print("\n".join([f"{x['type']} {x['name']};" for x in externs]))