Files
af/tools/m2ctx.py
T
Anghelo Carvajal c7729c05c0 game.c OK (#37)
* C file

* game_dt

* game_ct

* fix offsets and building

* game_init_hyral and SetGameFrame

* more funcs

* game_main

* game_get_controller and func_800D2E00_jp

* Input

* game_draw_last

* a few attempts

* game_draw_first

* improve func_800D2E58_jp a bit

* format

* migrate data and bss

* what

* whoops

* name bss

* minor cleanup

* match game.c

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* fix malloc function wrongly named

* Update src/code/game.c

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>

* review

---------

Co-authored-by: Derek Hensley <hensley.derek58@gmail.com>
2023-08-09 20:51:25 -04:00

85 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import os
import sys
import subprocess
import tempfile
script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = os.path.abspath(os.path.join(script_dir, ".."))
# Project-specific
CPP_FLAGS = [
"-I.",
"-Iinclude",
"-Isrc",
"-Ibin",
"-Ilib/ultralib/include",
"-Ilib/ultralib/include/PR",
"-D_LANGUAGE_C",
"-DF3DEX_GBI_2",
"-D__USE_ISOC99",
"-DNDEBUG",
"-D_FINALROM",
"-D_MIPS_SZLONG=32",
"-D__USE_ISOC99",
"-DM2CTX",
"-DCC_CHECK=1"
"-DNON_MATCHING",
"-ffreestanding",
"-std=gnu89",
]
def import_c_file(in_file, version: str) -> 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]
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")
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")
except subprocess.CalledProcessError:
print(
"Failed to preprocess input file, when running command:\n"
+ " ".join(cpp_command),
file=sys.stderr,
)
sys.exit(1)
if not out_text:
print("Output is empty - aborting")
sys.exit(1)
for line in stock_macros.strip().splitlines():
out_text = out_text.replace(line + "\n", "")
return out_text
def main():
parser = argparse.ArgumentParser(
description="""Create a context file which can be used for m2c"""
)
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)
with open(os.path.join(root_dir, "ctx.c"), "w", encoding="UTF-8") as f:
f.write(output)
if __name__ == "__main__":
main()