From cb38d0cc8100a7615728173352a6f82b03d7fe34 Mon Sep 17 00:00:00 2001 From: David Benepe Date: Fri, 28 Oct 2022 18:35:16 -0400 Subject: [PATCH] Fixed mips2c issues hopefully. --- tools/python/m2ctx.py | 46 ++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/tools/python/m2ctx.py b/tools/python/m2ctx.py index a5f5d7b3..c6634267 100644 --- a/tools/python/m2ctx.py +++ b/tools/python/m2ctx.py @@ -235,7 +235,9 @@ definesRegex = r"(^#\s*define.*\\(?:\n.*\\)*\n.*$)|(^#(?!include)(?!undef).*$)" def collect_directives(filename, filetext, data): defs = regex_get_matches(filetext, definesRegex) for direct in defs: - data.append(direct.group(0)) + if direct.group(1) is None: + data['singleline'].append(direct.group(0)) + data['all'].append(direct.group(0)) # Return the end position of a string def find_end(text, findStr): @@ -313,7 +315,10 @@ def collect(filenames): 'functions': {}, 'variables': {}, 'types': {}, # typedefs, structs, unions, enums - 'directives': [] # Should ignore `#include` + 'directives': { + 'singleline': [], + 'all': [] + } } mapText = load_map() @@ -382,12 +387,9 @@ def write_output_functions(funcs): return out + '\n' # Writes all the preprocessor directives to the output file -def write_output_directives(directivesArray): - out = '' - out += '#define NULL (void*)0\n' # Hack, since cleanup() will remove the original NULL definition. - for direct in directivesArray: - out += direct + '\n' - return out + '\n' +def write_output_directives(directives): + directives['singleline'].append('#define NULL (void*)0\n') # Hack, since cleanup() will remove the original NULL definition. + return [ '\n'.join(directives['singleline']) + '\n', '\n'.join(directives['all']) + '\n' ] # Writes a variable to the output file def write_output_variable(key, variables, varKeys, doneKeys): @@ -402,7 +404,7 @@ def write_output_variable(key, variables, varKeys, doneKeys): continue if key2 in text: out += write_output_variable(key2, variables, varKeys, doneKeys) - out += text + '\n' + out += text.replace('UNUSED', '') + '\n' doneKeys[key] = 1 return out @@ -417,16 +419,36 @@ def write_output_variables(variables): out += write_output_variable(key, variables, varKeys, doneKeys) return out + '\n' +# Preprocess everything, since mips2c doesn't like the preprocessor. +def preprocess_all(directivesText): + open("__temp.c", "w").write(directivesText) + cpp_command = ["gcc", "-E", "-P", "-undef", "-D_LANGUAGE_C", "-D__sgi", "-DNON_MATCHING", "-D_Static_assert(x, y)=", "-D__attribute__(x)=", "__temp.c"] + out = subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8") + os.remove("__temp.c") + return out + +# Preprocess everything but the #defines. +def preprocess_directives(directivesText): + open("__temp.c", "w").write(directivesText) + cpp_command = ["gcc", "-E", "-P", "-fdirectives-only", "-undef", "-D_LANGUAGE_C", "-D__sgi", "-DNON_MATCHING", "-D_Static_assert(x, y)=", "-D__attribute__(x)=", "__temp.c"] + out = subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8") + os.remove("__temp.c") + return out + # Creates the output file text. def write_output(data): - out = '/*** DKR decomp context file (Automatically generated by m2ctx.py [' + date.today().strftime("%Y/%m/%d") + ']) ***/\n\n' - out += write_output_directives(data['directives']) + header = '/*** DKR decomp context file (Automatically generated by m2ctx.py [' + date.today().strftime("%Y/%m/%d") + ']) ***/\n\n' + out = '' + directives = write_output_directives(data['directives']) + out += directives[0] # Only add single-line directives at first for preprocessing. out += write_output_types(data['types']) out += write_output_variables(data['variables']) out += write_output_functions(data['functions']) out = fix_enums(out) # Fixes trailing commas in enums, which gets rid of a warning. out = cleanup(out) # Removes useless stuff - return out + out = preprocess_all(out) # Preprocesses everything, removing all the directives. + out = preprocess_directives(directives[1]) + out # Add the defines back in. + return header + out # Uses the `find` program to return the filenames in the specified folders. def find_files():