2021-10-22 16:01:27 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-08-11 04:04:25 +02:00
|
|
|
|
2021-10-22 16:01:27 +02:00
|
|
|
import argparse
|
2020-08-11 04:04:25 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2021-12-28 12:43:24 +01:00
|
|
|
import tempfile
|
2020-08-11 04:04:25 +02:00
|
|
|
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
2020-10-02 08:07:54 +02:00
|
|
|
root_dir = os.path.abspath(os.path.join(script_dir, ".."))
|
2020-08-11 04:04:25 +02:00
|
|
|
src_dir = root_dir + "src/"
|
|
|
|
|
2021-10-22 16:01:27 +02:00
|
|
|
# Project-specific
|
|
|
|
CPP_FLAGS = [
|
|
|
|
"-Iinclude",
|
|
|
|
"-Isrc",
|
2022-10-04 16:09:23 +02:00
|
|
|
"-Iassets/us",
|
2021-10-22 16:01:27 +02:00
|
|
|
"-Iver/current/build/include",
|
|
|
|
"-D_LANGUAGE_C",
|
|
|
|
"-DF3DEX_GBI_2",
|
|
|
|
"-D_MIPS_SZLONG=32",
|
2022-02-17 18:11:27 +01:00
|
|
|
"-DSCRIPT(test...)={}"
|
|
|
|
"-D__attribute__(test...)=",
|
|
|
|
"-D__asm__(test...)=",
|
2021-10-22 16:01:27 +02:00
|
|
|
"-ffreestanding",
|
2021-11-19 15:31:28 +01:00
|
|
|
"-DM2CTX",
|
2021-10-22 16:01:27 +02:00
|
|
|
]
|
2020-08-11 04:04:25 +02:00
|
|
|
|
2021-10-22 16:01:27 +02:00
|
|
|
def import_c_file(in_file) -> str:
|
2020-08-11 04:04:25 +02:00
|
|
|
in_file = os.path.relpath(in_file, root_dir)
|
2021-10-22 16:01:27 +02:00
|
|
|
cpp_command = ["gcc", "-E", "-P", "-dM", *CPP_FLAGS, in_file]
|
|
|
|
cpp_command2 = ["gcc", "-E", "-P", *CPP_FLAGS, in_file]
|
|
|
|
|
2021-12-28 12:43:24 +01:00
|
|
|
with tempfile.NamedTemporaryFile(suffix=".c") as tmp:
|
|
|
|
stock_macros = subprocess.check_output(["gcc", "-E", "-P", "-dM", tmp.name], cwd=root_dir, encoding="utf-8")
|
|
|
|
|
2021-10-22 16:01:27 +02:00
|
|
|
out_text = ""
|
2020-08-11 04:04:25 +02:00
|
|
|
try:
|
2021-10-22 16:01:27 +02:00
|
|
|
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")
|
2020-08-11 04:04:25 +02:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print(
|
|
|
|
"Failed to preprocess input file, when running command:\n"
|
|
|
|
+ cpp_command,
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-10-22 16:01:27 +02:00
|
|
|
if not out_text:
|
|
|
|
print("Output is empty - aborting")
|
|
|
|
sys.exit(1)
|
2021-12-28 12:43:24 +01:00
|
|
|
|
|
|
|
for line in stock_macros.strip().splitlines():
|
|
|
|
out_text = out_text.replace(line + "\n", "")
|
2021-10-22 16:01:27 +02:00
|
|
|
return out_text
|
2020-10-02 08:07:54 +02:00
|
|
|
|
2020-08-11 04:04:25 +02:00
|
|
|
def main():
|
2021-10-22 16:01:27 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="""Create a context file which can be used for mips_to_c"""
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"c_file",
|
|
|
|
help="""File from which to create context""",
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
2020-10-02 08:07:54 +02:00
|
|
|
|
2021-10-22 16:01:27 +02:00
|
|
|
output = import_c_file(args.c_file)
|
2020-08-11 04:04:25 +02:00
|
|
|
|
|
|
|
with open(os.path.join(root_dir, "ctx.c"), "w", encoding="UTF-8") as f:
|
2021-10-22 16:01:27 +02:00
|
|
|
f.write(output)
|
2020-08-11 04:04:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|