summaryrefslogtreecommitdiff
path: root/tools/m2ctx.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/m2ctx.py')
-rwxr-xr-xtools/m2ctx.py38
1 files changed, 26 insertions, 12 deletions
diff --git a/tools/m2ctx.py b/tools/m2ctx.py
index e21bf5a..b2d6dd4 100755
--- a/tools/m2ctx.py
+++ b/tools/m2ctx.py
@@ -17,6 +17,7 @@ CPP_FLAGS = [
"-Ibin",
"-Ilib/ultralib/include",
"-Ilib/ultralib/include/PR",
+ "-Ilib/ultralib/include/ido",
"-D_LANGUAGE_C",
"-DF3DEX_GBI_2",
@@ -34,19 +35,16 @@ CPP_FLAGS = [
"-std=gnu89",
]
-def import_c_file(in_file, version: str) -> str:
+def import_c_file(in_file) -> str:
in_file = os.path.relpath(in_file, root_dir)
- cpp_command = ["gcc", "-E", "-P", "-undef", "-dM", *CPP_FLAGS, in_file]
- cpp_command2 = ["gcc", "-E", "-P", "-undef", *CPP_FLAGS, in_file]
+ cpp_command = ["gcc", "-E", "-P", "-dD", *CPP_FLAGS, in_file]
with tempfile.NamedTemporaryFile(suffix=".c") as tmp:
- stock_macros = subprocess.check_output(["gcc", "-E", "-P", "-undef", "-dM", tmp.name], cwd=root_dir, encoding="utf-8")
+ stock_macros = subprocess.check_output(["gcc", "-E", "-P", "-dM", tmp.name], cwd=root_dir, encoding="utf-8")
- out_text = ""
try:
- out_text += subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8")
- out_text += subprocess.check_output(cpp_command2, cwd=root_dir, encoding="utf-8")
+ out_text = subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8")
except subprocess.CalledProcessError:
print(
"Failed to preprocess input file, when running command:\n"
@@ -59,22 +57,37 @@ def import_c_file(in_file, version: str) -> str:
print("Output is empty - aborting")
sys.exit(1)
+ defines = {}
+ source_lines = []
+ for line in out_text.splitlines(keepends=True):
+ if line.startswith("#define"):
+ sym = line.split()[1].split("(")[0]
+ defines[sym] = line
+ elif line.startswith("#undef"):
+ sym = line.split()[1]
+ if sym in defines:
+ del defines[sym]
+ else:
+ source_lines.append(line)
+
for line in stock_macros.strip().splitlines():
- out_text = out_text.replace(line + "\n", "")
- return out_text
+ sym = line.split()[1].split("(")[0]
+ if sym in defines:
+ del defines[sym]
+
+ return "".join(defines.values()) + "".join(source_lines)
def main():
parser = argparse.ArgumentParser(
- description="""Create a context file which can be used for m2c"""
+ description="""Create a context file which can be used for m2c / decomp.me"""
)
parser.add_argument(
"c_file",
help="""File from which to create context""",
)
- parser.add_argument("-v", "--version", help="Which version should be processed", default="us", choices=["us", "cn"])
args = parser.parse_args()
- output = import_c_file(args.c_file, args.version)
+ output = import_c_file(args.c_file)
with open(os.path.join(root_dir, "ctx.c"), "w", encoding="UTF-8") as f:
f.write(output)
@@ -82,3 +95,4 @@ def main():
if __name__ == "__main__":
main()
+ \ No newline at end of file