5959C_ENDINGS = ('.c' , '.i' )
6060CXX_ENDINGS = ('.cpp' , '.cxx' , '.cc' , '.c++' , '.CPP' , '.CXX' , '.C' , '.CC' , '.C++' , '.ii' )
6161OBJC_ENDINGS = ('.m' , '.mi' )
62+ PREPROCESSED_ENDINGS = ('.i' , '.ii' )
6263OBJCXX_ENDINGS = ('.mm' , '.mii' )
6364SPECIAL_ENDINGLESS_FILENAMES = (os .devnull ,)
6465
@@ -822,10 +823,50 @@ def array_contains_any_of(hay, needles):
822823 return cflags + ['-Xclang' , '-iwithsysroot' + os .path .join ('/include' , 'compat' )]
823824
824825
825- def get_clang_flags ():
826+ def get_target_flags ():
826827 return ['-target' , shared .get_llvm_target ()]
827828
828829
830+ def get_clang_flags (user_args ):
831+ flags = get_target_flags ()
832+
833+ # if exception catching is disabled, we can prevent that code from being
834+ # generated in the frontend
835+ if settings .DISABLE_EXCEPTION_CATCHING and not settings .WASM_EXCEPTIONS :
836+ flags .append ('-fignore-exceptions' )
837+
838+ if settings .INLINING_LIMIT :
839+ flags .append ('-fno-inline-functions' )
840+
841+ if settings .RELOCATABLE and '-fPIC' not in user_args :
842+ flags .append ('-fPIC' )
843+
844+ # We use default visiibilty=default in emscripten even though the upstream
845+ # backend defaults visibility=hidden. This matched the expectations of C/C++
846+ # code in the wild which expects undecorated symbols to be exported to other
847+ # DSO's by default.
848+ if not any (a .startswith ('-fvisibility' ) for a in user_args ):
849+ flags .append ('-fvisibility=default' )
850+
851+ if settings .LTO :
852+ if not any (a .startswith ('-flto' ) for a in user_args ):
853+ flags .append ('-flto=' + settings .LTO )
854+ # setjmp/longjmp handling using Wasm EH
855+ # For non-LTO, '-mllvm -wasm-enable-eh' added in
856+ # building.llvm_backend_args() sets this feature in clang. But in LTO, the
857+ # argument is added to wasm-ld instead, so clang needs to know that EH is
858+ # enabled so that it can be added to the attributes in LLVM IR.
859+ if settings .SUPPORT_LONGJMP == 'wasm' :
860+ flags .append ('-mexception-handling' )
861+
862+ else :
863+ # In LTO mode these args get passed instead at link time when the backend runs.
864+ for a in building .llvm_backend_args ():
865+ flags += ['-mllvm' , a ]
866+
867+ return flags
868+
869+
829870cflags = None
830871
831872
@@ -836,7 +877,7 @@ def get_cflags(user_args):
836877
837878 # Flags we pass to the compiler when building C/C++ code
838879 # We add these to the user's flags (newargs), but not when building .s or .S assembly files
839- cflags = get_clang_flags ()
880+ cflags = get_clang_flags (user_args )
840881
841882 if settings .EMSCRIPTEN_TRACING :
842883 cflags .append ('-D__EMSCRIPTEN_TRACING__=1' )
@@ -858,40 +899,6 @@ def get_cflags(user_args):
858899 '-D__EMSCRIPTEN_minor__=' + str (shared .EMSCRIPTEN_VERSION_MINOR ),
859900 '-D__EMSCRIPTEN_tiny__=' + str (shared .EMSCRIPTEN_VERSION_TINY )]
860901
861- # if exception catching is disabled, we can prevent that code from being
862- # generated in the frontend
863- if settings .DISABLE_EXCEPTION_CATCHING and not settings .WASM_EXCEPTIONS :
864- cflags .append ('-fignore-exceptions' )
865-
866- if settings .INLINING_LIMIT :
867- cflags .append ('-fno-inline-functions' )
868-
869- if settings .RELOCATABLE and '-fPIC' not in user_args :
870- cflags .append ('-fPIC' )
871-
872- # We use default visiibilty=default in emscripten even though the upstream
873- # backend defaults visibility=hidden. This matched the expectations of C/C++
874- # code in the wild which expects undecorated symbols to be exported to other
875- # DSO's by default.
876- if not any (a .startswith ('-fvisibility' ) for a in user_args ):
877- cflags .append ('-fvisibility=default' )
878-
879- if settings .LTO :
880- if not any (a .startswith ('-flto' ) for a in user_args ):
881- cflags .append ('-flto=' + settings .LTO )
882- # setjmp/longjmp handling using Wasm EH
883- # For non-LTO, '-mllvm -wasm-enable-eh' added in
884- # building.llvm_backend_args() sets this feature in clang. But in LTO, the
885- # argument is added to wasm-ld instead, so clang needs to know that EH is
886- # enabled so that it can be added to the attributes in LLVM IR.
887- if settings .SUPPORT_LONGJMP == 'wasm' :
888- cflags .append ('-mexception-handling' )
889-
890- else :
891- # In LTO mode these args get passed instead at link time when the backend runs.
892- for a in building .llvm_backend_args ():
893- cflags += ['-mllvm' , a ]
894-
895902 # Changes to default clang behavior
896903
897904 # Implicit functions can cause horribly confusing function pointer type errors, see #2175
@@ -1045,7 +1052,7 @@ def run(args):
10451052 if len (args ) == 2 and args [1 ] == '-v' :
10461053 # autoconf likes to see 'GNU' in the output to enable shared object support
10471054 print (version_string (), file = sys .stderr )
1048- return shared .check_call ([clang , '-v' ] + get_clang_flags (), check = False ).returncode
1055+ return shared .check_call ([clang , '-v' ] + get_target_flags (), check = False ).returncode
10491056
10501057 # Additional compiler flags that we treat as if they were passed to us on the
10511058 # commandline
@@ -2709,8 +2716,11 @@ def get_compiler(src_file):
27092716 def get_clang_command (src_file ):
27102717 return get_compiler (src_file ) + get_cflags (state .orig_args ) + compile_args + [src_file ]
27112718
2719+ def get_clang_command_preprocessed (src_file ):
2720+ return get_compiler (src_file ) + get_clang_flags (state .orig_args ) + compile_args + [src_file ]
2721+
27122722 def get_clang_command_asm (src_file ):
2713- return get_compiler (src_file ) + get_clang_flags () + compile_args + [src_file ]
2723+ return get_compiler (src_file ) + get_target_flags () + compile_args + [src_file ]
27142724
27152725 # preprocessor-only (-E) support
27162726 if state .mode == Mode .PREPROCESS_ONLY :
@@ -2765,6 +2775,8 @@ def compile_source_file(i, input_file):
27652775 linker_inputs .append ((i , output_file ))
27662776 if get_file_suffix (input_file ) in ASSEMBLY_ENDINGS :
27672777 cmd = get_clang_command_asm (input_file )
2778+ elif get_file_suffix (input_file ) in PREPROCESSED_ENDINGS :
2779+ cmd = get_clang_command_preprocessed (input_file )
27682780 else :
27692781 cmd = get_clang_command (input_file )
27702782 if not state .has_dash_c :
0 commit comments