@@ -1292,8 +1292,8 @@ def compile_program_async(
12921292 def preprocess_program (
12931293 self ,
12941294 program : ProgramType ,
1295- make_compiler_agnostic : bool = False ,
12961295 additional_flags : tuple [str , ...] = tuple (),
1296+ do_not_expand_includes : tuple [str , ...] = tuple (),
12971297 timeout : int | None = None ,
12981298 ) -> ProgramType :
12991299 """Preprocesses the program
@@ -1303,9 +1303,9 @@ def preprocess_program(
13031303 input program
13041304 additional_flags (tuple[str, ...]):
13051305 additional flags used for the compilation
1306- make_compiler_agnostic (bool ):
1307- if true will try to remove certain constructs (e.g., attributes)
1308- such that the resulting program can be compiled with both gcc and clang
1306+ do_not_expand_includes (tuple[str, ...] ):
1307+ include directives that should not be expanded,
1308+ this only works with <...> includes
13091309 timeout (int | None):
13101310 timeout in seconds for the compilation command
13111311
@@ -1314,6 +1314,18 @@ def preprocess_program(
13141314 the prepocessed program
13151315 """
13161316
1317+ if do_not_expand_includes :
1318+ tmpdir = tempfile .TemporaryDirectory ()
1319+ for include in do_not_expand_includes :
1320+ p = Path (tmpdir .name ) / Path (include )
1321+ if not p .parent .exists ():
1322+ p .parent .mkdir (parents = True )
1323+ with open (p , "w" ) as f :
1324+ print (
1325+ f"//unpreprocessed_include_#include <{ include } >" , file = f , end = ""
1326+ )
1327+ additional_flags += ("-I" , tmpdir .name , "-C" )
1328+
13171329 result = self .compile_program (
13181330 program ,
13191331 ASMCompilationOutput (None ),
@@ -1322,26 +1334,10 @@ def preprocess_program(
13221334 )
13231335 preprocessed_source = result .output .read ()
13241336
1325- if make_compiler_agnostic :
1326- # remove malloc attributes with args, clang doesn't understand these
1327- preprocessed_source = re .sub (
1328- r"__attribute__ \(\(__malloc__ \(.*, .*\)\)\)" , r"" , preprocessed_source
1329- )
1330- # remove f128 builtins builtins, clang doesn't understand these
1331- preprocessed_source = re .sub (
1332- r"extern int [^;]*f128[^;]*;" , r"" , preprocessed_source
1333- )
1334- # remove Float*** typedefs, gcc doesn't like these
1335- preprocessed_source = re .sub (
1336- r"typedef [^;]*_Float\d+x?;" , r"" , preprocessed_source
1337- )
1338- # replace remaining FloatX types with the standard ones
1339- preprocessed_source = re .sub (r"_Float32x" , r"double" , preprocessed_source )
1340- preprocessed_source = re .sub (
1341- r"_Float64x" , r"long double" , preprocessed_source
1337+ if do_not_expand_includes :
1338+ preprocessed_source = preprocessed_source .replace (
1339+ "//unpreprocessed_include_" , ""
13421340 )
1343- preprocessed_source = re .sub (r"_Float32" , r"float" , preprocessed_source )
1344- preprocessed_source = re .sub (r"_Float64" , r"double" , preprocessed_source )
13451341
13461342 return program .with_preprocessed_code (preprocessed_source )
13471343
0 commit comments