99import subprocess
1010import sys
1111import errno
12- import re
1312
1413if platform .system () == 'Darwin' :
1514 shared_lib_ext = '.dylib'
@@ -80,7 +79,9 @@ def get_swiftpm_options(args):
8079 if args .verbose :
8180 swiftpm_args += ['--verbose' ]
8281
83- if platform .system () == 'Darwin' :
82+ build_arch = args .build_target .split ('-' )[0 ]
83+ build_os = args .build_target .split ('-' )[2 ]
84+ if build_os .startswith ('macosx' ):
8485 swiftpm_args += [
8586 # Relative library rpath for swift; will only be used when /usr/lib/swift
8687 # is not available.
@@ -99,18 +100,17 @@ def get_swiftpm_options(args):
99100 if args .cross_compile_hosts :
100101 swiftpm_args += ['--destination' , args .cross_compile_config ]
101102
102- if 'ANDROID_DATA' in os .environ or (args .cross_compile_hosts and re .match (
103- 'android-' , args .cross_compile_hosts [0 ])):
103+ if '-android' in args .build_target :
104104 swiftpm_args += [
105- '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android' ,
105+ '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android/' + build_arch ,
106106 # SwiftPM will otherwise try to compile against GNU strerror_r on
107107 # Android and fail.
108108 '-Xswiftc' , '-Xcc' , '-Xswiftc' , '-U_GNU_SOURCE' ,
109109 ]
110110 else :
111111 # Library rpath for swift, dispatch, Foundation, etc. when installing
112112 swiftpm_args += [
113- '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux' ,
113+ '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/' + build_os + '/' + build_arch ,
114114 ]
115115
116116 if args .action == 'install' :
@@ -158,15 +158,14 @@ def handle_invocation(args):
158158 swiftpm_args = get_swiftpm_options (args )
159159 toolchain_bin = os .path .join (args .toolchain , 'bin' )
160160 swift_exec = os .path .join (toolchain_bin , 'swift' )
161- swiftc_exec = os .path .join (toolchain_bin , 'swiftc' )
162161
163162 # Platform-specific targets for which we must build swift-driver
164163 if args .cross_compile_hosts :
165164 targets = args .cross_compile_hosts
166- elif platform . system () == 'Darwin' :
167- targets = [get_build_target ( swiftc_exec , args ) + macos_deployment_target ]
165+ elif '-apple-macosx' in args . build_target :
166+ targets = [args . build_target + macos_deployment_target ]
168167 else :
169- targets = [get_build_target ( swiftc_exec , args ) ]
168+ targets = [args . build_target ]
170169
171170 env = os .environ
172171 # Use local dependencies (i.e. checked out next to swift-driver).
@@ -182,7 +181,7 @@ def handle_invocation(args):
182181 env ['SWIFT_EXEC' ] = '%sc' % (swift_exec )
183182
184183 if args .action == 'build' :
185- if args .cross_compile_hosts and not re . match ( '-macosx' , args .cross_compile_hosts [0 ]) :
184+ if args .cross_compile_hosts and not '-macosx' in args .cross_compile_hosts [0 ]:
186185 swiftpm ('build' , swift_exec , swiftpm_args , env )
187186 else :
188187 build_using_cmake (args , toolchain_bin , args .build_path , targets )
@@ -205,7 +204,7 @@ def handle_invocation(args):
205204 env ['SWIFT_DRIVER_LIT_DIR' ] = args .lit_test_dir
206205 swiftpm ('test' , swift_exec , test_args , env )
207206 elif args .action == 'install' :
208- if platform . system () == 'Darwin' :
207+ if '-apple-macosx' in args . build_target :
209208 build_using_cmake (args , toolchain_bin , args .build_path , targets )
210209 install (args , args .build_path , targets )
211210 else :
@@ -425,7 +424,7 @@ def build_using_cmake(args, toolchain_bin, build_dir, targets):
425424 base_cmake_flags = []
426425 swift_flags = base_swift_flags .copy ()
427426 swift_flags .append ('-target %s' % target )
428- if platform . system () == 'Darwin' :
427+ if '-apple-macosx' in args . build_target :
429428 base_cmake_flags .append ('-DCMAKE_OSX_DEPLOYMENT_TARGET=%s' % macos_deployment_target )
430429 base_cmake_flags .append ('-DCMAKE_OSX_ARCHITECTURES=%s' % target .split ('-' )[0 ])
431430
@@ -477,7 +476,7 @@ def build_llbuild_using_cmake(args, target, swiftc_exec, build_dir, base_cmake_f
477476 llbuild_cmake_flags .append ('-DSQLite3_INCLUDE_DIR=%s/usr/include' % args .sysroot )
478477 # FIXME: This may be particularly hacky but CMake finds a different version of libsqlite3
479478 # on some machines. This is also Darwin-specific...
480- if platform . system () == 'Darwin' :
479+ if '-apple-macosx' in args . build_target :
481480 llbuild_cmake_flags .append ('-DSQLite3_LIBRARY=%s/usr/lib/libsqlite3.tbd' % args .sysroot )
482481 llbuild_swift_flags = swift_flags [:]
483482
@@ -517,7 +516,7 @@ def build_yams_using_cmake(args, target, swiftc_exec, build_dir, base_cmake_flag
517516 '-DCMAKE_C_COMPILER:=clang' ,
518517 '-DBUILD_SHARED_LIBS=OFF' ]
519518
520- if platform . system () == 'Darwin' :
519+ if '-apple-macosx' in args . build_target :
521520 yams_cmake_flags .append ('-DCMAKE_OSX_DEPLOYMENT_TARGET=%s' % macos_deployment_target )
522521 yams_cmake_flags .append ('-DCMAKE_C_FLAGS=-target %s' % target )
523522 else :
@@ -595,16 +594,20 @@ def cmake_build(args, swiftc_exec, cmake_args, swift_flags, source_path,
595594 if args .verbose :
596595 print (stdout )
597596
598- def get_build_target (swiftc_path , args ):
597+ def get_build_target (swiftc_path , args , cross_compile = False ):
599598 """Returns the target-triple of the current machine."""
600599 try :
601- target_info_json = subprocess .check_output ([swiftc_path , '-print-target-info' ],
600+ command = [swiftc_path , '-print-target-info' ]
601+ if cross_compile :
602+ cross_compile_json = json .load (open (args .cross_compile_config ))
603+ command += ['-target' , cross_compile_json ["target" ]]
604+ target_info_json = subprocess .check_output (command ,
602605 stderr = subprocess .PIPE ,
603606 universal_newlines = True ).strip ()
604607 args .target_info = json .loads (target_info_json )
605608 triple = args .target_info ['target' ]['triple' ]
606609 # Windows also wants unversionedTriple, but does not use this.
607- if platform . system () == 'Darwin' :
610+ if '-apple-macosx' in args . target_info [ "target" ][ "unversionedTriple" ] :
608611 triple = args .target_info ['target' ]['unversionedTriple' ]
609612 return triple
610613 except Exception as e :
@@ -662,18 +665,18 @@ def add_common_args(parser):
662665 args .build_path = os .path .abspath (args .build_path )
663666 args .toolchain = os .path .abspath (args .toolchain )
664667
665- if platform .system () == 'Darwin' :
668+ swift_exec = os .path .join (os .path .join (args .toolchain , 'bin' ), 'swiftc' )
669+ args .build_target = get_build_target (swift_exec , args , cross_compile = (True if args .cross_compile_config else False ))
670+ if '-apple-macosx' in args .build_target :
666671 args .sysroot = call_output (["xcrun" , "--sdk" , "macosx" , "--show-sdk-path" ], verbose = args .verbose )
667672 else :
668673 args .sysroot = None
669674
670- swift_exec = os .path .join (os .path .join (args .toolchain , 'bin' ), 'swiftc' )
671- build_target = get_build_target (swift_exec , args )
672- if (build_target == 'x86_64-apple-macosx' and 'macosx-arm64' in args .cross_compile_hosts ):
673- args .cross_compile_hosts = [build_target + macos_deployment_target , 'arm64-apple-macosx%s' % macos_deployment_target ]
674- elif (build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args .cross_compile_hosts ):
675- args .cross_compile_hosts = [build_target + macos_deployment_target , 'x86_64-apple-macosx%s' % macos_deployment_target ]
676- elif args .cross_compile_hosts and re .match ('android-' , args .cross_compile_hosts [0 ]):
675+ if (args .build_target == 'x86_64-apple-macosx' and 'macosx-arm64' in args .cross_compile_hosts ):
676+ args .cross_compile_hosts = [args .build_target + macos_deployment_target , 'arm64-apple-macosx%s' % macos_deployment_target ]
677+ elif (args .build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args .cross_compile_hosts ):
678+ args .cross_compile_hosts = [args .build_target + macos_deployment_target , 'x86_64-apple-macosx%s' % macos_deployment_target ]
679+ elif args .cross_compile_hosts and 'android-' in args .cross_compile_hosts [0 ]:
677680 print ('Cross-compiling for %s' % args .cross_compile_hosts [0 ])
678681 elif args .cross_compile_hosts :
679682 error ("cannot cross-compile for %s" % cross_compile_hosts )
0 commit comments