summaryrefslogtreecommitdiff
path: root/tools/decompctx.py
diff options
context:
space:
mode:
authorLagoLunatic <LagoLunatic@users.noreply.github.com>2025-01-28 14:01:04 -0500
committerLagoLunatic <LagoLunatic@users.noreply.github.com>2025-01-28 14:01:04 -0500
commitd6c3081559f39dfd65fcd221b60f2a1aeea5fc8b (patch)
tree5acc21a7f514d57efbe0a6369ce4ba7797715869 /tools/decompctx.py
parent901bc51f40b875338e5c7552ffd2b58e0cdaae83 (diff)
Update dtk-template, dtk, and objdiff
Diffstat (limited to 'tools/decompctx.py')
-rwxr-xr-xtools/decompctx.py55
1 files changed, 32 insertions, 23 deletions
diff --git a/tools/decompctx.py b/tools/decompctx.py
index f31ade12..f2f31dfa 100755
--- a/tools/decompctx.py
+++ b/tools/decompctx.py
@@ -18,63 +18,63 @@ from typing import List
script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = os.path.abspath(os.path.join(script_dir, ".."))
src_dir = os.path.join(root_dir, "src")
-include_dirs = [
- os.path.join(root_dir, "include"),
- os.path.join(root_dir, "src"),
- os.path.join(root_dir, "src/PowerPC_EABI_Support/MSL/MSL_C/MSL_Common/Include"),
- os.path.join(root_dir, "src/PowerPC_EABI_Support/MSL/MSL_C/MSL_Common_Embedded/Math/Include"),
- os.path.join(root_dir, "src/PowerPC_EABI_Support/MSL/MSL_C/PPC_EABI/Include"),
- os.path.join(root_dir, "src/PowerPC_EABI_Support/MSL/MSL_C++/MSL_Common/Include"),
- os.path.join(root_dir, "src/PowerPC_EABI_Support/Runtime/Inc"),
-]
-
-include_pattern = re.compile(r'^#include\s*[<"](.+?)[>"]')
-guard_pattern = re.compile(r"^#ifndef\s+(.*)$")
+include_dirs: List[str] = [] # Set with -I flag
+
+include_pattern = re.compile(r'^#\s*include\s*[<"](.+?)[>"]')
+guard_pattern = re.compile(r"^#\s*ifndef\s+(.*)$")
+once_pattern = re.compile(r"^#\s*pragma\s+once$")
defines = set()
+deps = []
-def import_h_file(in_file: str, r_path: str, deps: List[str]) -> str:
+def import_h_file(in_file: str, r_path: str) -> str:
rel_path = os.path.join(root_dir, r_path, in_file)
if os.path.exists(rel_path):
- return import_c_file(rel_path, deps)
+ return import_c_file(rel_path)
for include_dir in include_dirs:
inc_path = os.path.join(include_dir, in_file)
if os.path.exists(inc_path):
- return import_c_file(inc_path, deps)
+ return import_c_file(inc_path)
else:
print("Failed to locate", in_file)
return ""
-def import_c_file(in_file: str, deps: List[str]) -> str:
+def import_c_file(in_file: str) -> str:
in_file = os.path.relpath(in_file, root_dir)
deps.append(in_file)
out_text = ""
try:
with open(in_file, encoding="utf-8") as file:
- out_text += process_file(in_file, list(file), deps)
+ out_text += process_file(in_file, list(file))
except Exception:
with open(in_file) as file:
- out_text += process_file(in_file, list(file), deps)
+ out_text += process_file(in_file, list(file))
return out_text
-def process_file(in_file: str, lines: List[str], deps: List[str]) -> str:
+def process_file(in_file: str, lines: List[str]) -> str:
out_text = ""
for idx, line in enumerate(lines):
- guard_match = guard_pattern.match(line.strip())
if idx == 0:
+ guard_match = guard_pattern.match(line.strip())
if guard_match:
if guard_match[1] in defines:
break
defines.add(guard_match[1])
+ else:
+ once_match = once_pattern.match(line.strip())
+ if once_match:
+ if in_file in defines:
+ break
+ defines.add(in_file)
print("Processing file", in_file)
include_match = include_pattern.match(line.strip())
if include_match and not include_match[1].endswith(".s"):
out_text += f'/* "{in_file}" line {idx} "{include_match[1]}" */\n'
- out_text += import_h_file(include_match[1], os.path.dirname(in_file), deps)
+ out_text += import_h_file(include_match[1], os.path.dirname(in_file))
out_text += f'/* end "{include_match[1]}" */\n'
else:
out_text += line
@@ -105,10 +105,19 @@ def main():
"--depfile",
help="""Dependency file""",
)
+ parser.add_argument(
+ "-I",
+ "--include",
+ help="""Include directory""",
+ action="append",
+ )
args = parser.parse_args()
- deps = []
- output = import_c_file(args.c_file, deps)
+ if args.include is None:
+ exit("No include directories specified")
+ global include_dirs
+ include_dirs = args.include
+ output = import_c_file(args.c_file)
with open(os.path.join(root_dir, args.output), "w", encoding="utf-8") as f:
f.write(output)