@@ -738,23 +738,26 @@ def phase_parse_arguments(state):
738738
739739
740740def separate_linker_flags (state , newargs ):
741- newargs = list (newargs )
741+ """Process argument list separating out intput files, compiler flags
742+ and linker flags.
743+
744+ - Linker flags are stored in state.link_flags
745+ - Input files and compiler-only flags are return as two separate lists.
746+
747+ Both linker flags and input files are stored as pairs of (i, entry) where
748+ `i` is the orginal index in the command line arguments. This allow the two
749+ lists to be recombined in the correct order by the linker code (link.py).
750+
751+ Note that this index can have a fractional part for input arguments that
752+ expand into multiple processed arguments, as in -Wl,-f1,-f2.
753+ """
742754
743755 if settings .RUNTIME_LINKED_LIBS :
744756 newargs += settings .RUNTIME_LINKED_LIBS
745757
746- # Find input files
747-
748- # These three arrays are used to store arguments of different types for
749- # type-specific processing. In order to shuffle the arguments back together
750- # after processing, all of these arrays hold tuples (original_index, value).
751- # Note that the index part of the tuple can have a fractional part for input
752- # arguments that expand into multiple processed arguments, as in -Wl,-f1,-f2.
753758 input_files = []
759+ compiler_args = []
754760
755- # find input files with a simple heuristic. we should really analyze
756- # based on a full understanding of gcc params, right now we just assume that
757- # what is left contains no more |-x OPT| things
758761 skip = False
759762 for i in range (len (newargs )):
760763 if skip :
@@ -770,28 +773,16 @@ def get_next_arg():
770773 exit_with_error (f"option '{ arg } ' requires an argument" )
771774 return newargs [i + 1 ]
772775
773- if not arg .startswith ('-' ):
774- # we already removed -o <target>, so all these should be inputs
775- newargs [i ] = ''
776+ if not arg .startswith ('-' ) or arg == '-' :
776777 # os.devnul should always be reported as existing but there is bug in windows
777778 # python before 3.8:
778779 # https://bugs.python.org/issue1311
779- if not os .path .exists (arg ) and arg != os .devnull :
780+ if not os .path .exists (arg ) and arg not in ( os .devnull , '-' ) :
780781 exit_with_error ('%s: No such file or directory ("%s" was expected to be an input file, based on the commandline arguments provided)' , arg , arg )
781782 input_files .append ((i , arg ))
782- elif arg .startswith ('-o' ):
783- newargs [i ] = ''
784- elif arg .startswith ('-L' ):
785- state .add_link_flag (i , arg )
786- elif arg .startswith ('-l' ):
787- state .add_link_flag (i , arg )
788783 elif arg == '-z' :
789784 state .add_link_flag (i , newargs [i ])
790785 state .add_link_flag (i + 1 , get_next_arg ())
791- elif arg .startswith ('-z' ):
792- state .add_link_flag (i , newargs [i ])
793- elif arg .startswith ('--js-library=' ):
794- state .add_link_flag (i , newargs [i ])
795786 elif arg .startswith ('-Wl,' ):
796787 # Multiple comma separated link flags can be specified. Create fake
797788 # fractional indices for these: -Wl,a,b,c,d at index 4 becomes:
@@ -803,13 +794,15 @@ def get_next_arg():
803794 state .add_link_flag (i + 1 , get_next_arg ())
804795 elif arg == '-s' :
805796 state .add_link_flag (i , newargs [i ])
806- elif arg == '-' :
807- input_files .append ((i , arg ))
808- newargs [i ] = ''
809-
810- newargs = [a for a in newargs if a ]
797+ elif arg == '-s' or arg .startswith (('-l' , '-L' , '--js-library=' , '-z' )):
798+ state .add_link_flag (i , arg )
799+ elif not arg .startswith ('-o' ) and arg not in ('-nostdlib' , '-nostartfiles' , '-nolibc' , '-nodefaultlibs' , '-s' ):
800+ # All other flags are for the compiler
801+ compiler_args .append (arg )
802+ if skip :
803+ compiler_args .append (get_next_arg ())
811804
812- return newargs , input_files
805+ return compiler_args , input_files
813806
814807
815808@ToolchainProfiler .profile_block ('setup' )
@@ -916,28 +909,6 @@ def phase_setup(options, state):
916909 default_setting ('GL_ENABLE_GET_PROC_ADDRESS' , 1 )
917910
918911
919- def filter_out_link_flags (args ):
920- rtn = []
921-
922- def is_link_flag (flag ):
923- if flag in ('-nostdlib' , '-nostartfiles' , '-nolibc' , '-nodefaultlibs' , '-s' ):
924- return True
925- return flag .startswith (('-l' , '-L' , '-Wl,' , '-z' , '--js-library' ))
926-
927- skip = False
928- for arg in args :
929- if skip :
930- skip = False
931- continue
932- if is_link_flag (arg ):
933- continue
934- if arg == '-Xlinker' :
935- skip = True
936- continue
937- rtn .append (arg )
938- return rtn
939-
940-
941912@ToolchainProfiler .profile_block ('compile inputs' )
942913def phase_compile_inputs (options , state , newargs ):
943914 if shared .run_via_emxx :
@@ -975,7 +946,6 @@ def get_clang_command_asm():
975946 assert state .mode == Mode .COMPILE_AND_LINK
976947 assert not options .dash_c
977948 compile_args , input_files = separate_linker_flags (state , newargs )
978- compile_args = filter_out_link_flags (compile_args )
979949 linker_inputs = []
980950 seen_names = {}
981951
0 commit comments