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,9 @@ 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_os = build_target .split ('-' )[2 ]
112+ if build_os .startswith ('macosx' ):
109113 swiftpm_args += [
110114 '-Xlinker' , '-rpath' , '-Xlinker' , '/usr/lib/swift' ,
111115 '-Xlinker' , '-rpath' , '-Xlinker' , '@executable_path/../lib/swift/macosx' ,
@@ -121,25 +125,23 @@ def get_swiftpm_options(swift_exec: str, args: argparse.Namespace) -> List[str]:
121125 os .path .join (args .toolchain , 'lib' , 'swift' , 'Block' ),
122126 ]
123127
124- if 'ANDROID_DATA' in os .environ or (args .cross_compile_host and re .match (
125- 'android-' , args .cross_compile_host )):
128+ if '-android' in build_target :
126129 swiftpm_args += [
127130 '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android' ,
128131 # SwiftPM will otherwise try to compile against GNU strerror_r on
129132 # Android and fail.
130133 '-Xswiftc' , '-Xcc' , '-Xswiftc' , '-U_GNU_SOURCE' ,
131134 ]
132- elif platform . system () == 'Linux' :
135+ elif not build_os . startswith ( 'macosx' ) :
133136 # Library rpath for swift, dispatch, Foundation, etc. when installing
134137 swiftpm_args += [
135- '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux' ,
138+ '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/' + build_os ,
136139 ]
137140
138- build_target = get_build_target (swift_exec , args )
139141 if args .cross_compile_host :
140- if re . search ( '-apple- macosx', build_target ) and re . match ('macosx-' , args . cross_compile_host ):
142+ if build_os . startswith ( ' macosx' ) and args . cross_compile_host . startswith ('macosx-' ):
141143 swiftpm_args += ["--arch" , "x86_64" , "--arch" , "arm64" ]
142- elif re . match ('android-' , args . cross_compile_host ):
144+ elif args . cross_compile_host . startswith ('android-' ):
143145 print ('Cross-compiling for %s' % args .cross_compile_host )
144146 swiftpm_args += ['--destination' , args .cross_compile_config ]
145147 else :
0 commit comments