4
4
import json
5
5
import os
6
6
import platform
7
- import re
8
7
import shutil
9
8
import subprocess
10
9
import sys
@@ -68,13 +67,16 @@ def swiftpm_bin_path(swift_exec: str, swiftpm_args: List[str], additional_env: D
68
67
return check_output (cmd , additional_env = additional_env , capture_stderr = False , verbose = verbose ).strip ()
69
68
70
69
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 :
72
71
"""Returns the target-triple of the current machine or for cross-compilation."""
73
72
try :
74
73
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" ]]
75
77
target_info_json = subprocess .check_output (command , stderr = subprocess .PIPE , universal_newlines = True ).strip ()
76
78
args .target_info = json .loads (target_info_json )
77
- if platform . system () == 'Darwin' :
79
+ if '-apple-macosx' in args . target_info [ "target" ][ "unversionedTriple" ] :
78
80
return args .target_info ["target" ]["unversionedTriple" ]
79
81
return args .target_info ["target" ]["triple" ]
80
82
except Exception as e :
@@ -105,7 +107,9 @@ def get_swiftpm_options(swift_exec: str, args: argparse.Namespace) -> List[str]:
105
107
for san in args .sanitize :
106
108
swiftpm_args += ['--sanitize=%s' % san ]
107
109
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' ):
109
113
swiftpm_args += [
110
114
'-Xlinker' , '-rpath' , '-Xlinker' , '/usr/lib/swift' ,
111
115
'-Xlinker' , '-rpath' , '-Xlinker' , '@executable_path/../lib/swift/macosx' ,
@@ -121,25 +125,23 @@ def get_swiftpm_options(swift_exec: str, args: argparse.Namespace) -> List[str]:
121
125
os .path .join (args .toolchain , 'lib' , 'swift' , 'Block' ),
122
126
]
123
127
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 :
126
129
swiftpm_args += [
127
130
'-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android' ,
128
131
# SwiftPM will otherwise try to compile against GNU strerror_r on
129
132
# Android and fail.
130
133
'-Xswiftc' , '-Xcc' , '-Xswiftc' , '-U_GNU_SOURCE' ,
131
134
]
132
- elif platform . system () == 'Linux' :
135
+ elif not build_os . startswith ( 'macosx' ) :
133
136
# Library rpath for swift, dispatch, Foundation, etc. when installing
134
137
swiftpm_args += [
135
- '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux' ,
138
+ '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/' + build_os ,
136
139
]
137
140
138
- build_target = get_build_target (swift_exec , args )
139
141
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-' ):
141
143
swiftpm_args += ["--arch" , "x86_64" , "--arch" , "arm64" ]
142
- elif re . match ('android-' , args . cross_compile_host ):
144
+ elif args . cross_compile_host . startswith ('android-' ):
143
145
print ('Cross-compiling for %s' % args .cross_compile_host )
144
146
swiftpm_args += ['--destination' , args .cross_compile_config ]
145
147
else :
0 commit comments