5
5
import argparse
6
6
import os
7
7
import platform
8
+ import re
8
9
import shutil
9
10
import subprocess
10
11
import sys
@@ -50,25 +51,28 @@ def get_swiftpm_options(args):
50
51
os .path .join (args .toolchain , 'lib' , 'swift' , 'Block' ),
51
52
]
52
53
53
- if 'ANDROID_DATA' in os .environ :
54
- swiftpm_args += [
55
- '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android' ,
56
- # SwiftPM will otherwise try to compile against GNU strerror_r on
57
- # Android and fail.
58
- '-Xswiftc' , '-Xcc' , '-Xswiftc' , '-U_GNU_SOURCE' ,
59
- ]
60
- else :
61
- # Library rpath for swift, dispatch, Foundation, etc. when installing
62
- swiftpm_args += [
63
- '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux' ,
64
- ]
54
+ if 'ANDROID_DATA' in os .environ or (args .cross_compile_host and re .match (
55
+ 'android-' , args .cross_compile_host )):
56
+ swiftpm_args += [
57
+ '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/android' ,
58
+ # SwiftPM will otherwise try to compile against GNU strerror_r on
59
+ # Android and fail.
60
+ '-Xswiftc' , '-Xcc' , '-Xswiftc' , '-U_GNU_SOURCE' ,
61
+ ]
62
+ elif platform .system () == 'Linux' :
63
+ # Library rpath for swift, dispatch, Foundation, etc. when installing
64
+ swiftpm_args += [
65
+ '-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux' ,
66
+ ]
67
+
68
+ if args .cross_compile_host :
69
+ swiftpm_args += ['--destination' , args .cross_compile_config ]
65
70
66
71
return swiftpm_args
67
72
68
- def install (swiftpm_bin_path , toolchain ):
69
- toolchain_bin = os .path .join (toolchain , 'bin' )
70
- for exe in ['sourcekit-lsp' ]:
71
- install_binary (exe , swiftpm_bin_path , toolchain_bin , toolchain )
73
+ def install (swiftpm_bin_path , prefixes , toolchain ):
74
+ for prefix in prefixes :
75
+ install_binary ('sourcekit-lsp' , swiftpm_bin_path , os .path .join (prefix , 'bin' ), toolchain )
72
76
73
77
def install_binary (exe , source_dir , install_dir , toolchain ):
74
78
cmd = ['rsync' , '-a' , os .path .join (source_dir , exe ), install_dir ]
@@ -112,6 +116,8 @@ def handle_invocation(swift_exec, args):
112
116
print ('Cleaning ' + args .build_path )
113
117
shutil .rmtree (args .build_path , ignore_errors = True )
114
118
119
+ env ['SWIFT_EXEC' ] = '%sc' % (swift_exec )
120
+
115
121
if args .action == 'build' :
116
122
swiftpm ('build' , swift_exec , swiftpm_args , env )
117
123
elif args .action == 'test' :
@@ -131,7 +137,7 @@ def handle_invocation(swift_exec, args):
131
137
bin_path = swiftpm_bin_path (swift_exec , swiftpm_args , env )
132
138
swiftpm_args += ['-Xswiftc' , '-no-toolchain-stdlib-rpath' ]
133
139
swiftpm ('build' , swift_exec , swiftpm_args , env )
134
- install (bin_path , args .toolchain )
140
+ install (bin_path , args .install_prefixes if not None else [ args . toolchain ], args . toolchain )
135
141
else :
136
142
assert False , 'unknown action \' {}\' ' .format (args .action )
137
143
@@ -148,6 +154,8 @@ def add_common_args(parser):
148
154
parser .add_argument ('--sanitize' , action = 'append' , help = 'build using the given sanitizer(s) (address|thread|undefined)' )
149
155
parser .add_argument ('--sanitize-all' , action = 'store_true' , help = 'build using every available sanitizer in sub-directories of build path' )
150
156
parser .add_argument ('--verbose' , '-v' , action = 'store_true' , help = 'enable verbose output' )
157
+ parser .add_argument ('--cross-compile-host' , help = 'cross-compile for another host instead' )
158
+ parser .add_argument ('--cross-compile-config' , help = 'an SPM JSON destination file containing Swift cross-compilation flags' )
151
159
152
160
subparsers = parser .add_subparsers (title = 'subcommands' , dest = 'action' , metavar = 'action' )
153
161
build_parser = subparsers .add_parser ('build' , help = 'build the package' )
@@ -159,6 +167,7 @@ def add_common_args(parser):
159
167
160
168
install_parser = subparsers .add_parser ('install' , help = 'build the package' )
161
169
add_common_args (install_parser )
170
+ install_parser .add_argument ('--prefix' , dest = 'install_prefixes' , nargs = '*' , metavar = 'PATHS' , help = "paths to install sourcekit-lsp, default: 'toolchain/bin'" )
162
171
163
172
args = parser .parse_args (sys .argv [1 :])
164
173
@@ -175,6 +184,11 @@ def add_common_args(parser):
175
184
else :
176
185
swift_exec = 'swift'
177
186
187
+ if args .cross_compile_host and re .match ('android-' , args .cross_compile_host ):
188
+ print ('Cross-compiling for %s' % args .cross_compile_host )
189
+ elif args .cross_compile_host :
190
+ error ("cannot cross-compile for %s" % args .cross_compile_host )
191
+
178
192
handle_invocation (swift_exec , args )
179
193
if args .sanitize_all :
180
194
base = args .build_path
0 commit comments