9
9
import subprocess
10
10
import sys
11
11
import errno
12
- import re
13
12
14
13
if platform .system () == 'Darwin' :
15
14
shared_lib_ext = '.dylib'
@@ -80,7 +79,8 @@ def get_swiftpm_options(args):
80
79
if args .verbose :
81
80
swiftpm_args += ['--verbose' ]
82
81
83
- if platform .system () == 'Darwin' :
82
+ build_os = args .build_target .split ('-' )[2 ]
83
+ if build_os .startswith ('macosx' ):
84
84
swiftpm_args += [
85
85
# Relative library rpath for swift; will only be used when /usr/lib/swift
86
86
# is not available.
@@ -99,8 +99,7 @@ def get_swiftpm_options(args):
99
99
if args .cross_compile_hosts :
100
100
swiftpm_args += ['--destination' , args .cross_compile_config ]
101
101
102
- if 'ANDROID_DATA' in os .environ or (args .cross_compile_hosts and re .match (
103
- 'android-' , args .cross_compile_hosts [0 ])):
102
+ if '-android' in args .build_target :
104
103
swiftpm_args += [
105
104
'-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android' ,
106
105
# SwiftPM will otherwise try to compile against GNU strerror_r on
@@ -110,7 +109,7 @@ def get_swiftpm_options(args):
110
109
else :
111
110
# Library rpath for swift, dispatch, Foundation, etc. when installing
112
111
swiftpm_args += [
113
- '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux' ,
112
+ '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/' + build_os ,
114
113
]
115
114
116
115
if args .action == 'install' :
@@ -158,15 +157,14 @@ def handle_invocation(args):
158
157
swiftpm_args = get_swiftpm_options (args )
159
158
toolchain_bin = os .path .join (args .toolchain , 'bin' )
160
159
swift_exec = os .path .join (toolchain_bin , 'swift' )
161
- swiftc_exec = os .path .join (toolchain_bin , 'swiftc' )
162
160
163
161
# Platform-specific targets for which we must build swift-driver
164
162
if args .cross_compile_hosts :
165
163
targets = args .cross_compile_hosts
166
- elif platform . system () == 'Darwin' :
167
- targets = [get_build_target ( swiftc_exec , args ) + macos_deployment_target ]
164
+ elif '-apple-macosx' in args . build_target :
165
+ targets = [args . build_target + macos_deployment_target ]
168
166
else :
169
- targets = [get_build_target ( swiftc_exec , args ) ]
167
+ targets = [args . build_target ]
170
168
171
169
env = os .environ
172
170
# Use local dependencies (i.e. checked out next to swift-driver).
@@ -182,7 +180,7 @@ def handle_invocation(args):
182
180
env ['SWIFT_EXEC' ] = '%sc' % (swift_exec )
183
181
184
182
if args .action == 'build' :
185
- if args .cross_compile_hosts and not re . match ( '-macosx' , args .cross_compile_hosts [0 ]) :
183
+ if args .cross_compile_hosts and not '-macosx' in args .cross_compile_hosts [0 ]:
186
184
swiftpm ('build' , swift_exec , swiftpm_args , env )
187
185
else :
188
186
build_using_cmake (args , toolchain_bin , args .build_path , targets )
@@ -205,7 +203,7 @@ def handle_invocation(args):
205
203
env ['SWIFT_DRIVER_LIT_DIR' ] = args .lit_test_dir
206
204
swiftpm ('test' , swift_exec , test_args , env )
207
205
elif args .action == 'install' :
208
- if platform . system () == 'Darwin' :
206
+ if '-apple-macosx' in args . build_target :
209
207
build_using_cmake (args , toolchain_bin , args .build_path , targets )
210
208
install (args , args .build_path , targets )
211
209
else :
@@ -428,7 +426,7 @@ def build_using_cmake(args, toolchain_bin, build_dir, targets):
428
426
base_cmake_flags = []
429
427
swift_flags = base_swift_flags .copy ()
430
428
swift_flags .append ('-target %s' % target )
431
- if platform . system () == 'Darwin' :
429
+ if '-apple-macosx' in args . build_target :
432
430
base_cmake_flags .append ('-DCMAKE_OSX_DEPLOYMENT_TARGET=%s' % macos_deployment_target )
433
431
base_cmake_flags .append ('-DCMAKE_OSX_ARCHITECTURES=%s' % target .split ('-' )[0 ])
434
432
@@ -480,7 +478,7 @@ def build_llbuild_using_cmake(args, target, swiftc_exec, build_dir, base_cmake_f
480
478
llbuild_cmake_flags .append ('-DSQLite3_INCLUDE_DIR=%s/usr/include' % args .sysroot )
481
479
# FIXME: This may be particularly hacky but CMake finds a different version of libsqlite3
482
480
# on some machines. This is also Darwin-specific...
483
- if platform . system () == 'Darwin' :
481
+ if '-apple-macosx' in args . build_target :
484
482
llbuild_cmake_flags .append ('-DSQLite3_LIBRARY=%s/usr/lib/libsqlite3.tbd' % args .sysroot )
485
483
llbuild_swift_flags = swift_flags [:]
486
484
@@ -520,7 +518,7 @@ def build_yams_using_cmake(args, target, swiftc_exec, build_dir, base_cmake_flag
520
518
'-DCMAKE_C_COMPILER:=clang' ,
521
519
'-DBUILD_SHARED_LIBS=OFF' ]
522
520
523
- if platform . system () == 'Darwin' :
521
+ if '-apple-macosx' in args . build_target :
524
522
yams_cmake_flags .append ('-DCMAKE_OSX_DEPLOYMENT_TARGET=%s' % macos_deployment_target )
525
523
yams_cmake_flags .append ('-DCMAKE_C_FLAGS=-target %s' % target )
526
524
else :
@@ -598,16 +596,20 @@ def cmake_build(args, swiftc_exec, cmake_args, swift_flags, source_path,
598
596
if args .verbose :
599
597
print (stdout )
600
598
601
- def get_build_target (swiftc_path , args ):
599
+ def get_build_target (swiftc_path , args , cross_compile = False ):
602
600
"""Returns the target-triple of the current machine."""
603
601
try :
604
- target_info_json = subprocess .check_output ([swiftc_path , '-print-target-info' ],
602
+ command = [swiftc_path , '-print-target-info' ]
603
+ if cross_compile :
604
+ cross_compile_json = json .load (open (args .cross_compile_config ))
605
+ command += ['-target' , cross_compile_json ["target" ]]
606
+ target_info_json = subprocess .check_output (command ,
605
607
stderr = subprocess .PIPE ,
606
608
universal_newlines = True ).strip ()
607
609
args .target_info = json .loads (target_info_json )
608
610
triple = args .target_info ['target' ]['triple' ]
609
611
# Windows also wants unversionedTriple, but does not use this.
610
- if platform . system () == 'Darwin' :
612
+ if '-apple-macosx' in args . target_info [ "target" ][ "unversionedTriple" ] :
611
613
triple = args .target_info ['target' ]['unversionedTriple' ]
612
614
return triple
613
615
except Exception as e :
@@ -666,18 +668,18 @@ def add_common_args(parser):
666
668
args .build_path = os .path .abspath (args .build_path )
667
669
args .toolchain = os .path .abspath (args .toolchain )
668
670
669
- if platform .system () == 'Darwin' :
671
+ swift_exec = os .path .join (os .path .join (args .toolchain , 'bin' ), 'swiftc' )
672
+ args .build_target = get_build_target (swift_exec , args , cross_compile = (True if args .cross_compile_config else False ))
673
+ if '-apple-macosx' in args .build_target :
670
674
args .sysroot = call_output (["xcrun" , "--sdk" , "macosx" , "--show-sdk-path" ], verbose = args .verbose )
671
675
else :
672
676
args .sysroot = None
673
677
674
- swift_exec = os .path .join (os .path .join (args .toolchain , 'bin' ), 'swiftc' )
675
- build_target = get_build_target (swift_exec , args )
676
- if (build_target == 'x86_64-apple-macosx' and 'macosx-arm64' in args .cross_compile_hosts ):
677
- args .cross_compile_hosts = [build_target + macos_deployment_target , 'arm64-apple-macosx%s' % macos_deployment_target ]
678
- elif (build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args .cross_compile_hosts ):
679
- args .cross_compile_hosts = [build_target + macos_deployment_target , 'x86_64-apple-macosx%s' % macos_deployment_target ]
680
- elif args .cross_compile_hosts and re .match ('android-' , args .cross_compile_hosts [0 ]):
678
+ if (args .build_target == 'x86_64-apple-macosx' and 'macosx-arm64' in args .cross_compile_hosts ):
679
+ args .cross_compile_hosts = [args .build_target + macos_deployment_target , 'arm64-apple-macosx%s' % macos_deployment_target ]
680
+ elif (args .build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args .cross_compile_hosts ):
681
+ args .cross_compile_hosts = [args .build_target + macos_deployment_target , 'x86_64-apple-macosx%s' % macos_deployment_target ]
682
+ elif args .cross_compile_hosts and 'android-' in args .cross_compile_hosts [0 ]:
681
683
print ('Cross-compiling for %s' % args .cross_compile_hosts [0 ])
682
684
elif args .cross_compile_hosts :
683
685
error ("cannot cross-compile for %s" % cross_compile_hosts )
0 commit comments