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