44import  json 
55import  os 
66import  platform 
7- import  re 
87import  shutil 
98import  subprocess 
109import  sys 
@@ -68,13 +67,16 @@ def swiftpm_bin_path(swift_exec: str, swiftpm_args: List[str], additional_env: D
6867    return  check_output (cmd , additional_env = additional_env , capture_stderr = False , verbose = verbose ).strip ()
6968
7069
71- def  get_build_target (swift_exec : str , args : argparse .Namespace ) ->  str :
70+ def  get_build_target (swift_exec : str , args : argparse .Namespace ,  cross_compile :  bool   =   False ) ->  str :
7271    """Returns the target-triple of the current machine or for cross-compilation.""" 
7372    try :
7473        command  =  [swift_exec , '-print-target-info' ]
74+         if  cross_compile :
75+             cross_compile_json  =  json .load (open (args .cross_compile_config ))
76+             command  +=  ['-target' , cross_compile_json ["target" ]]
7577        target_info_json  =  subprocess .check_output (command , stderr = subprocess .PIPE , universal_newlines = True ).strip ()
7678        args .target_info  =  json .loads (target_info_json )
77-         if  platform . system ()  ==   'Darwin' :
79+         if  '-apple-macosx'   in   args . target_info [ "target" ][ "unversionedTriple" ] :
7880            return  args .target_info ["target" ]["unversionedTriple" ]
7981        return  args .target_info ["target" ]["triple" ]
8082    except  Exception  as  e :
@@ -105,7 +107,10 @@ def get_swiftpm_options(swift_exec: str, args: argparse.Namespace) -> List[str]:
105107        for  san  in  args .sanitize :
106108            swiftpm_args  +=  ['--sanitize=%s'  %  san ]
107109
108-     if  platform .system () ==  'Darwin' :
110+     build_target  =  get_build_target (swift_exec , args , cross_compile = (True  if  args .cross_compile_config  else  False ))
111+     build_arch  =  build_target .split ('-' )[0 ]
112+     build_os  =  build_target .split ('-' )[2 ]
113+     if  build_os .startswith ('macosx' ):
109114        swiftpm_args  +=  [
110115            '-Xlinker' , '-rpath' , '-Xlinker' , '/usr/lib/swift' ,
111116            '-Xlinker' , '-rpath' , '-Xlinker' , '@executable_path/../lib/swift/macosx' ,
@@ -121,25 +126,23 @@ def get_swiftpm_options(swift_exec: str, args: argparse.Namespace) -> List[str]:
121126            os .path .join (args .toolchain , 'lib' , 'swift' , 'Block' ),
122127        ]
123128
124-     if  'ANDROID_DATA'  in  os .environ  or  (args .cross_compile_host  and  re .match (
125-             'android-' , args .cross_compile_host )):
129+     if  '-android'  in  build_target :
126130        swiftpm_args  +=  [
127-             '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android'  ,
131+             '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android/'    +   build_arch ,
128132            # SwiftPM will otherwise try to compile against GNU strerror_r on 
129133            # Android and fail. 
130134            '-Xswiftc' , '-Xcc' , '-Xswiftc' , '-U_GNU_SOURCE' ,
131135        ]
132-     elif  platform . system ()  ==   'Linux' :
136+     elif  not   build_os . startswith ( 'macosx' ) :
133137        # Library rpath for swift, dispatch, Foundation, etc. when installing 
134138        swiftpm_args  +=  [
135-             '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux'  ,
139+             '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/'    +   build_os   +   '/'   +   build_arch ,
136140        ]
137141
138-     build_target  =  get_build_target (swift_exec , args )
139142    if  args .cross_compile_host :
140-         if  re . search ( '-apple- macosx',  build_target ) and  re . match ('macosx-' ,  args . cross_compile_host ):
143+         if  build_os . startswith ( ' macosx'and  args . cross_compile_host . startswith ('macosx-' ):
141144            swiftpm_args  +=  ["--arch" , "x86_64" , "--arch" , "arm64" ]
142-         elif  re . match ('android-' ,  args . cross_compile_host ):
145+         elif  args . cross_compile_host . startswith ('android-' ):
143146            print ('Cross-compiling for %s'  %  args .cross_compile_host )
144147            swiftpm_args  +=  ['--destination' , args .cross_compile_config ]
145148        else :
0 commit comments