2020-04-27 05:47:35 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2021-02-11 19:08:24 +00:00
|
|
|
import argparse
|
2020-06-19 01:12:58 +00:00
|
|
|
from pathlib import Path
|
2020-04-27 05:47:35 +00:00
|
|
|
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
root_dir = script_dir + "/../"
|
|
|
|
src_dir = root_dir + "src/"
|
|
|
|
|
|
|
|
|
|
|
|
def get_c_dir(dirname):
|
|
|
|
for root, dirs, files in os.walk(src_dir):
|
2020-05-04 16:03:12 +00:00
|
|
|
for directory in dirs:
|
|
|
|
if directory == dirname:
|
|
|
|
return os.path.join(root, directory)
|
2020-04-27 05:47:35 +00:00
|
|
|
|
|
|
|
|
2020-05-04 16:03:12 +00:00
|
|
|
def get_c_file(directory):
|
|
|
|
for root, dirs, files in os.walk(directory):
|
2020-04-27 05:47:35 +00:00
|
|
|
for file in files:
|
|
|
|
if file.endswith(".c") and "data" not in file:
|
|
|
|
return file
|
|
|
|
|
2021-02-11 19:08:24 +00:00
|
|
|
|
2020-06-19 01:12:58 +00:00
|
|
|
def import_c_file(in_file):
|
|
|
|
in_file = os.path.relpath(in_file, root_dir)
|
2021-02-11 19:08:24 +00:00
|
|
|
cpp_command = ["gcc", "-E", "-P", "-Iinclude", "-Iassets", "-Isrc", "-undef", "-D__sgi", "-D_LANGUAGE_C",
|
2020-06-19 01:12:58 +00:00
|
|
|
"-DNON_MATCHING", "-D_Static_assert(x, y)=", "-D__attribute__(x)=", in_file]
|
|
|
|
try:
|
|
|
|
return subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8")
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print(
|
|
|
|
"Failed to preprocess input file, when running command:\n"
|
|
|
|
+ cpp_command,
|
|
|
|
file=sys.stderr,
|
2021-02-11 19:08:24 +00:00
|
|
|
)
|
2020-06-19 01:12:58 +00:00
|
|
|
sys.exit(1)
|
2020-04-27 05:47:35 +00:00
|
|
|
|
2021-02-11 19:08:24 +00:00
|
|
|
|
2020-04-27 05:47:35 +00:00
|
|
|
def main():
|
2021-02-11 19:08:24 +00:00
|
|
|
parser = argparse.ArgumentParser(usage="./m2ctx.py path/to/file.c or ./m2ctx.py (from an actor or gamestate's asm dir)",
|
|
|
|
description="Creates a ctx.c file for mips2c. "
|
|
|
|
"Output will be saved as oot/ctx.c")
|
|
|
|
parser.add_argument('filepath', help="path of c file to be processed")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.filepath:
|
|
|
|
c_file_path = args.filepath
|
|
|
|
print("Using file: {}".format(c_file_path))
|
2020-06-19 01:12:58 +00:00
|
|
|
else:
|
|
|
|
this_dir = Path.cwd()
|
|
|
|
c_dir_path = get_c_dir(this_dir.name)
|
|
|
|
if c_dir_path is None:
|
2021-02-11 19:08:24 +00:00
|
|
|
sys.exit(
|
|
|
|
"Cannot find appropriate c file dir. In argumentless mode, run this script from the c file's corresponding asm dir.")
|
2020-06-19 01:12:58 +00:00
|
|
|
c_file = get_c_file(c_dir_path)
|
|
|
|
c_file_path = os.path.join(c_dir_path, c_file)
|
2021-02-11 19:08:24 +00:00
|
|
|
print("Using file: {}".format(c_file_path))
|
|
|
|
|
2020-05-04 16:03:12 +00:00
|
|
|
output = import_c_file(c_file_path)
|
|
|
|
|
2020-07-04 12:34:51 +00:00
|
|
|
with open(os.path.join(root_dir, "ctx.c"), "w", encoding="UTF-8") as f:
|
2020-04-27 05:47:35 +00:00
|
|
|
f.write(output)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|