2020-08-16 07:11:40 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-02-18 12:54:07 +01:00
|
|
|
import os
|
2020-08-16 07:11:40 +02:00
|
|
|
import re
|
2021-02-18 12:54:07 +01:00
|
|
|
import sys
|
2020-09-18 03:28:34 +02:00
|
|
|
from pathlib import Path
|
2020-08-16 07:11:40 +02:00
|
|
|
|
|
|
|
def strip_c_comments(text):
|
|
|
|
def replacer(match):
|
|
|
|
s = match.group(0)
|
|
|
|
if s.startswith('/'):
|
|
|
|
return " "
|
|
|
|
else:
|
|
|
|
return s
|
|
|
|
pattern = re.compile(
|
|
|
|
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
|
|
|
|
re.DOTALL | re.MULTILINE
|
|
|
|
)
|
|
|
|
return re.sub(pattern, replacer, text)
|
|
|
|
|
|
|
|
c_func_pattern = re.compile(
|
2020-10-19 06:42:17 +02:00
|
|
|
r"^(static\s+)?[^\s]+\s+([^\s(]+)\(([^;)]*)\)[^;]+{",
|
2020-08-16 07:11:40 +02:00
|
|
|
re.MULTILINE
|
|
|
|
)
|
|
|
|
def funcs_in_c(text):
|
2020-10-14 22:48:16 +02:00
|
|
|
return (match.group(2) for match in c_func_pattern.finditer(text))
|
2020-08-19 03:45:26 +02:00
|
|
|
|
|
|
|
asm_func_pattern = re.compile(
|
2020-09-26 03:51:54 +02:00
|
|
|
r"INCLUDE_ASM\([^,]+, [^,]+, ([^,)]+)",
|
2020-08-19 03:45:26 +02:00
|
|
|
re.MULTILINE
|
|
|
|
)
|
|
|
|
def include_asms_in_c(text):
|
|
|
|
return (match.group(1) for match in asm_func_pattern.finditer(text))
|
2020-08-16 07:11:40 +02:00
|
|
|
|
2021-02-22 10:21:23 +01:00
|
|
|
def stuff(version):
|
|
|
|
DIR = os.path.dirname(__file__)
|
|
|
|
NONMATCHINGS_DIR = Path(os.path.join(DIR, "ver", version, "asm", "nonmatchings"))
|
|
|
|
|
|
|
|
C_FILES = Path(os.path.join(DIR, "src")).rglob("*.c")
|
|
|
|
ASM_FILES = NONMATCHINGS_DIR.rglob("*.s")
|
|
|
|
|
|
|
|
matched = []
|
|
|
|
asm = []
|
|
|
|
for filename in C_FILES:
|
|
|
|
with open(filename, "r") as file:
|
|
|
|
text = strip_c_comments(file.read())
|
|
|
|
matched.extend((m for m in funcs_in_c(text) if not m in matched))
|
|
|
|
asm.extend((m for m in include_asms_in_c(text) if not m in asm))
|
|
|
|
|
|
|
|
non_matched = [os.path.splitext(os.path.basename(filename))[0] for filename in ASM_FILES]
|
2020-08-16 07:11:40 +02:00
|
|
|
|
2021-02-22 10:21:23 +01:00
|
|
|
partial_matched = [f for f in matched if f in asm]
|
|
|
|
matched = [f for f in matched if not f in partial_matched]
|
|
|
|
matched_but_undeleted_asm = set([f for f in matched if f in non_matched and not f in partial_matched])
|
|
|
|
orphan_asm = set(non_matched) - set(asm) - matched_but_undeleted_asm
|
2020-08-19 03:45:26 +02:00
|
|
|
|
2021-02-22 10:21:23 +01:00
|
|
|
to_delete = matched_but_undeleted_asm | orphan_asm
|
2021-02-18 12:54:07 +01:00
|
|
|
|
2021-02-22 10:21:23 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if "--help" in sys.argv:
|
|
|
|
print("--fail-undeleted exit with error code 1 if obsolete .s functions exist")
|
|
|
|
print("--delete delete obsolete .s functions without asking")
|
|
|
|
exit()
|
2020-08-16 07:11:40 +02:00
|
|
|
|
2021-02-22 10:21:23 +01:00
|
|
|
if len(to_delete) > 0:
|
2021-03-04 10:00:56 +01:00
|
|
|
print(f"The following functions can be deleted: {to_delete}")
|
2021-02-22 10:21:23 +01:00
|
|
|
if "--fail-undeleted" in sys.argv:
|
|
|
|
exit(1)
|
|
|
|
elif "--delete" in sys.argv or input("Delete them [y/N]? ").upper() == "Y":
|
|
|
|
for func in to_delete:
|
|
|
|
f = next(NONMATCHINGS_DIR.rglob(func + ".s"))
|
|
|
|
os.remove(f)
|
2021-02-18 12:54:07 +01:00
|
|
|
|
2021-02-22 10:21:23 +01:00
|
|
|
# Remove empty directories
|
|
|
|
for folder in list(os.walk(NONMATCHINGS_DIR)):
|
|
|
|
if not os.listdir(folder[0]):
|
|
|
|
os.removedirs(folder[0])
|
2020-10-14 22:48:16 +02:00
|
|
|
|
2021-02-22 10:21:23 +01:00
|
|
|
stuff("us")
|
|
|
|
stuff("jp")
|
2023-03-02 02:16:55 +01:00
|
|
|
stuff("cn")
|