1111###
1212
1313import argparse
14+ import fnmatch
1415import os
1516import re
1617from typing import List
1920root_dir = os .path .abspath (os .path .join (script_dir , ".." ))
2021src_dir = os .path .join (root_dir , "src" )
2122include_dirs : List [str ] = [] # Set with -I flag
23+ exclude_globs : List [str ] = [] # Set with -x flag
2224
2325include_pattern = re .compile (r'^#\s*include\s*[<"](.+?)[>"]' )
2426guard_pattern = re .compile (r"^#\s*ifndef\s+(.*)$" )
2830deps = []
2931
3032
33+ def generate_prelude (defines ) -> str :
34+ out_text = ""
35+ if len (defines ) > 0 :
36+ out_text += "/* decompctx prelude */\n "
37+ for define in defines :
38+ if define .count ("=" ) > 0 :
39+ macro_name , macro_val = define .split ("=" , 1 )
40+ out_text += f"#define { macro_name } { macro_val } \n "
41+ else :
42+ out_text += f"#define { define } \n "
43+ if len (defines ) > 0 :
44+ out_text += "/* end decompctx prelude */\n \n "
45+
46+ return out_text
47+
48+
3149def import_h_file (in_file : str , r_path : str ) -> str :
3250 rel_path = os .path .join (root_dir , r_path , in_file )
3351 if os .path .exists (rel_path ):
@@ -56,6 +74,7 @@ def import_c_file(in_file: str) -> str:
5674
5775
5876def process_file (in_file : str , lines : List [str ]) -> str :
77+ print (f"process_file: { in_file } " )
5978 out_text = ""
6079 for idx , line in enumerate (lines ):
6180 if idx == 0 :
@@ -73,8 +92,17 @@ def process_file(in_file: str, lines: List[str]) -> str:
7392 print ("Processing file" , in_file )
7493 include_match = include_pattern .match (line .strip ())
7594 if include_match and not include_match [1 ].endswith (".s" ):
95+ excluded = False
96+ for glob in exclude_globs :
97+ if fnmatch .fnmatch (include_match [1 ], glob ):
98+ excluded = True
99+ break
100+
76101 out_text += f'/* "{ in_file } " line { idx } "{ include_match [1 ]} " */\n '
77- out_text += import_h_file (include_match [1 ], os .path .dirname (in_file ))
102+ if excluded :
103+ out_text += f'/* Skipped excluded file */\n '
104+ else :
105+ out_text += import_h_file (include_match [1 ], os .path .dirname (in_file ))
78106 out_text += f'/* end "{ include_match [1 ]} " */\n '
79107 else :
80108 out_text += line
@@ -111,13 +139,29 @@ def main():
111139 help = """Include directory""" ,
112140 action = "append" ,
113141 )
142+ parser .add_argument (
143+ "-x" ,
144+ "--exclude" ,
145+ help = """Excluded file name glob""" ,
146+ action = "append" ,
147+ )
148+ parser .add_argument (
149+ "-D" ,
150+ "--define" ,
151+ help = """Macro definition""" ,
152+ action = "append" ,
153+ )
114154 args = parser .parse_args ()
115155
116156 if args .include is None :
117157 exit ("No include directories specified" )
118158 global include_dirs
119159 include_dirs = args .include
120- output = import_c_file (args .c_file )
160+ global exclude_globs
161+ exclude_globs = args .exclude or []
162+ prelude_defines = args .define or []
163+ output = generate_prelude (prelude_defines )
164+ output += import_c_file (args .c_file )
121165
122166 with open (os .path .join (root_dir , args .output ), "w" , encoding = "utf-8" ) as f :
123167 f .write (output )
0 commit comments