|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import sys |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +# assuming that this util is in tools/ |
| 11 | +script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 12 | +root_dir = os.path.abspath(os.path.join(script_dir, "..")) |
| 13 | +print(root_dir) |
| 14 | + |
| 15 | +# Project-specific |
| 16 | +CPP_FLAGS = [ |
| 17 | + "-undef", |
| 18 | + "-D__sgi", |
| 19 | + "-DVERSION_US", |
| 20 | + "-DTARGET_N64", |
| 21 | + "-D_LANGUAGE_C", |
| 22 | + "-DF3DEX_GBI", |
| 23 | + "-D_MIPS_SZLONG=32", |
| 24 | + "-ffreestanding", |
| 25 | + "-nostdinc", |
| 26 | + "-Iinclude/ido", |
| 27 | + "-Iinclude", |
| 28 | + "-Ibuild", |
| 29 | + "-Iinclude/PR", |
| 30 | + "-Isrc", |
| 31 | + "-I.", |
| 32 | +] |
| 33 | + |
| 34 | +import os |
| 35 | +import subprocess |
| 36 | +import shutil |
| 37 | + |
| 38 | +def import_c_file(in_file) -> str: |
| 39 | + |
| 40 | + Message = '/**** Context file for Pokemon Stadium *****/\n' |
| 41 | + |
| 42 | + in_file = os.path.relpath(in_file, root_dir) |
| 43 | + # Prefer clang as C preprocessor if installed on the system |
| 44 | + if shutil.which('clang') is not None: |
| 45 | + cpp = ['clang', '-E', '-P', '-x', 'c', '-Wno-trigraphs'] |
| 46 | + else: |
| 47 | + cpp = ['gcc', '-E', '-P'] |
| 48 | + |
| 49 | + cpp_getdefines = [*cpp, "-dM", *CPP_FLAGS, in_file] |
| 50 | + cpp_procfile = [*cpp, *CPP_FLAGS, in_file] |
| 51 | + |
| 52 | + out_text = "" |
| 53 | + try: |
| 54 | + out_text += subprocess.check_output(cpp_getdefines, cwd=root_dir, encoding="utf-8") |
| 55 | + out_text += subprocess.check_output(cpp_procfile, cwd=root_dir, encoding="utf-8") |
| 56 | + except subprocess.CalledProcessError: |
| 57 | + print( |
| 58 | + "Failed to preprocess input file, when running command:\n", |
| 59 | + cpp_getdefines, |
| 60 | + file=sys.stderr, |
| 61 | + ) |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + if not out_text: |
| 65 | + print("Output is empty - aborting") |
| 66 | + sys.exit(1) |
| 67 | + return Message + out_text |
| 68 | + |
| 69 | +def main(): |
| 70 | + parser = argparse.ArgumentParser( |
| 71 | + description="""Create a context file which can be used for mips_to_c""" |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + "c_file", |
| 75 | + help="""File from which to create context""", |
| 76 | + ) |
| 77 | + args = parser.parse_args() |
| 78 | + |
| 79 | + output = import_c_file(args.c_file) |
| 80 | + |
| 81 | + with open(os.path.join(root_dir, "ctx.c"), "w", encoding="UTF-8") as f: |
| 82 | + f.write(output) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments