17
17
else :
18
18
shared_lib_ext = '.so'
19
19
macos_deployment_target = '10.15'
20
- macos_target_architectures = ['x86_64' ,'arm64' ]
20
+
21
+ def error (message ):
22
+ print ("--- %s: error: %s" % (os .path .basename (sys .argv [0 ]), message ))
23
+ sys .stdout .flush ()
24
+ raise SystemExit (1 )
21
25
22
26
# Tools constructed as a part of the a development build toolchain
23
27
driver_toolchain_tools = ['swift' , 'swift-frontend' , 'clang' , 'swift-help' ,
@@ -146,10 +150,19 @@ def should_test_parallel():
146
150
return False
147
151
return True
148
152
149
- def handle_invocation (toolchain_bin , args ):
153
+ def handle_invocation (args ):
150
154
swiftpm_args = get_swiftpm_options (args )
151
-
155
+ toolchain_bin = os . path . join ( args . toolchain , 'bin' )
152
156
swift_exec = os .path .join (toolchain_bin , 'swift' )
157
+ swiftc_exec = os .path .join (toolchain_bin , 'swiftc' )
158
+
159
+ # Platform-specific targets for which we must build swift-driver
160
+ if args .cross_compile_hosts :
161
+ targets = args .cross_compile_hosts
162
+ elif platform .system () == 'Darwin' :
163
+ targets = [get_build_target (swiftc_exec , args ) + macos_deployment_target ]
164
+ else :
165
+ targets = [get_build_target (swiftc_exec , args )]
153
166
154
167
env = os .environ
155
168
# Use local dependencies (i.e. checked out next to swift-driver).
@@ -163,7 +176,7 @@ def handle_invocation(toolchain_bin, args):
163
176
env ['SDKROOT' ] = args .sysroot
164
177
165
178
if args .action == 'build' :
166
- build_using_cmake (args , toolchain_bin , args .build_path )
179
+ build_using_cmake (args , toolchain_bin , args .build_path , targets )
167
180
168
181
elif args .action == 'clean' :
169
182
print ('Cleaning ' + args .build_path )
@@ -181,8 +194,8 @@ def handle_invocation(toolchain_bin, args):
181
194
swiftpm ('test' , swift_exec , test_args , env )
182
195
elif args .action == 'install' :
183
196
if platform .system () == 'Darwin' :
184
- build_using_cmake (args , toolchain_bin , args .build_path )
185
- install (args , args .build_path )
197
+ build_using_cmake (args , toolchain_bin , args .build_path , targets )
198
+ install (args , args .build_path , targets )
186
199
else :
187
200
bin_path = swiftpm_bin_path (swift_exec , swiftpm_args , env )
188
201
swiftpm ('build' , swift_exec , swiftpm_args , env )
@@ -197,12 +210,16 @@ def non_darwin_install(swiftpm_bin_path, toolchain, verbose):
197
210
for exe in ['swift-driver' , 'swift-help' ]:
198
211
install_binary (exe , swiftpm_bin_path , toolchain_bin , verbose )
199
212
200
- def install (args , build_dir ):
213
+ def install (args , build_dir , targets ):
201
214
# Construct and install universal swift-driver, swift-help executables
202
215
# and libSwiftDriver, libSwiftOptions libraries, along with their dependencies.
203
- toolchain_bin = os .path .join (args .toolchain , 'bin' )
204
- toolchain_lib = os .path .join (args .toolchain , 'lib' , 'swift' , 'macosx' )
205
- toolchain_include = os .path .join (args .toolchain , 'include' , 'swift' )
216
+ for prefix in args .install_prefixes :
217
+ install_swiftdriver (args , build_dir , prefix , targets )
218
+
219
+ def install_swiftdriver (args , build_dir , prefix , targets ) :
220
+ install_bin = os .path .join (prefix , 'bin' )
221
+ install_lib = os .path .join (prefix , 'lib' , 'swift' , 'macosx' )
222
+ install_include = os .path .join (prefix , 'include' , 'swift' )
206
223
universal_dir = os .path .join (build_dir , 'universal-apple-macos%s' % macos_deployment_target )
207
224
bin_dir = os .path .join (universal_dir , 'bin' )
208
225
lib_dir = os .path .join (universal_dir , 'lib' )
@@ -211,35 +228,35 @@ def install(args, build_dir):
211
228
mkdir_p (lib_dir )
212
229
213
230
# swift-driver and swift-help
214
- install_executables (args , build_dir , bin_dir , toolchain_bin )
231
+ install_executables (args , build_dir , bin_dir , install_bin , targets )
215
232
216
233
# libSwiftDriver and libSwiftDriverExecution and libSwiftOptions
217
- install_libraries (args , build_dir , lib_dir , toolchain_lib )
234
+ install_libraries (args , build_dir , lib_dir , install_lib , targets )
218
235
219
236
# Binary Swift Modules:
220
237
# swift-driver: SwiftDriver.swiftmodule, SwiftOptions.swiftmodule
221
238
# TODO: swift-argument-parser: ArgumentParser.swiftmodule (disabled until needed)
222
239
# swift-tools-support-core: TSCUtility.swiftmodule, TSCLibc.swiftmodule, TSCBasic.swiftmodule
223
- install_binary_swift_modules (args , build_dir , toolchain_lib )
240
+ install_binary_swift_modules (args , build_dir , install_lib , targets )
224
241
225
242
# Modulemaps for C Modules:
226
243
# TSCclibc
227
- install_c_module_includes (args , build_dir , toolchain_include )
244
+ install_c_module_includes (args , build_dir , install_include )
228
245
229
246
# Install universal binaries for swift-driver and swift-help into the toolchain bin
230
247
# directory
231
- def install_executables (args , build_dir , universal_bin_dir , toolchain_bin_dir ):
248
+ def install_executables (args , build_dir , universal_bin_dir , toolchain_bin_dir , targets ):
232
249
for exe in ['swift-driver' , 'swift-help' ]:
233
250
# Fixup rpaths
234
- for arch in macos_target_architectures :
235
- exe_bin_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
251
+ for target in targets :
252
+ exe_bin_path = os .path .join (build_dir , target ,
236
253
args .configuration , 'bin' , exe )
237
- driver_lib_dir_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
254
+ driver_lib_dir_path = os .path .join (build_dir , target ,
238
255
args .configuration , 'lib' )
239
256
delete_rpath (driver_lib_dir_path , exe_bin_path , args .verbose )
240
257
241
258
for lib in ['swift-tools-support-core' , 'swift-argument-parser' ]:
242
- lib_dir_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
259
+ lib_dir_path = os .path .join (build_dir , target ,
243
260
args .configuration , 'dependencies' ,
244
261
lib , 'lib' )
245
262
delete_rpath (lib_dir_path , exe_bin_path , args .verbose )
@@ -251,22 +268,22 @@ def install_executables(args, build_dir, universal_bin_dir, toolchain_bin_dir):
251
268
output_bin_path = os .path .join (universal_bin_dir , exe )
252
269
lipo_cmd = ['lipo' ]
253
270
# Inputs
254
- for arch in macos_target_architectures :
255
- input_bin_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
271
+ for target in targets :
272
+ input_bin_path = os .path .join (build_dir , target ,
256
273
args .configuration , 'bin' , exe )
257
274
lipo_cmd .append (input_bin_path )
258
275
lipo_cmd .extend (['-create' , '-output' , output_bin_path ])
259
276
subprocess .check_call (lipo_cmd )
260
277
install_binary (exe , universal_bin_dir , toolchain_bin_dir , args .verbose )
261
278
262
279
# Install shared libraries for the driver and its dependencies into the toolchain
263
- def install_libraries (args , build_dir , universal_lib_dir , toolchain_lib_dir ):
280
+ def install_libraries (args , build_dir , universal_lib_dir , toolchain_lib_dir , targets ):
264
281
# Fixup the SwiftDriver rpath for libSwiftDriver and libSwiftDriverExecution
265
282
for lib in ['libSwiftDriver' , 'libSwiftDriverExecution' ]:
266
- for arch in macos_target_architectures :
267
- lib_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
283
+ for target in targets :
284
+ lib_path = os .path .join (build_dir , target ,
268
285
args .configuration , 'lib' , lib + shared_lib_ext )
269
- driver_lib_dir_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
286
+ driver_lib_dir_path = os .path .join (build_dir , target ,
270
287
args .configuration , 'lib' )
271
288
delete_rpath (driver_lib_dir_path , lib_path , args .verbose )
272
289
@@ -275,11 +292,11 @@ def install_libraries(args, build_dir, universal_lib_dir, toolchain_lib_dir):
275
292
tsc_libs = map (lambda d : os .path .join ('dependencies' , 'swift-tools-support-core' , 'lib' , d ),
276
293
['libTSCBasic' , 'libTSCLibc' , 'libTSCUtility' ])
277
294
for lib in driver_libs + tsc_libs :
278
- for arch in macos_target_architectures :
279
- lib_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
295
+ for target in targets :
296
+ lib_path = os .path .join (build_dir , target ,
280
297
args .configuration , lib + shared_lib_ext )
281
298
for dep in ['swift-tools-support-core' , 'llbuild' ]:
282
- lib_dir_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
299
+ lib_dir_path = os .path .join (build_dir , target ,
283
300
args .configuration , 'dependencies' ,
284
301
dep , 'lib' )
285
302
delete_rpath (lib_dir_path , lib_path , args .verbose )
@@ -289,57 +306,57 @@ def install_libraries(args, build_dir, universal_lib_dir, toolchain_lib_dir):
289
306
package_subpath = args .configuration
290
307
for lib in ['libSwiftDriver' , 'libSwiftOptions' , 'libSwiftDriverExecution' ]:
291
308
install_library (args , build_dir , package_subpath , lib ,
292
- universal_lib_dir , toolchain_lib_dir , 'swift-driver' )
309
+ universal_lib_dir , toolchain_lib_dir , 'swift-driver' , targets )
293
310
294
311
# Instal the swift-tools-support core shared libraries into the toolchain lib
295
312
package_subpath = os .path .join (args .configuration , 'dependencies' , 'swift-tools-support-core' )
296
313
for lib in ['libTSCBasic' , 'libTSCLibc' , 'libTSCUtility' ]:
297
314
install_library (args , build_dir , package_subpath , lib ,
298
- universal_lib_dir , toolchain_lib_dir , 'swift-tools-support-core' )
315
+ universal_lib_dir , toolchain_lib_dir , 'swift-tools-support-core' , targets )
299
316
300
317
package_subpath = os .path .join (args .configuration , 'dependencies' , 'swift-argument-parser' )
301
318
install_library (args , build_dir , package_subpath , 'libArgumentParser' ,
302
- universal_lib_dir , toolchain_lib_dir ,'swift-argument-parser' )
319
+ universal_lib_dir , toolchain_lib_dir ,'swift-argument-parser' , targets )
303
320
304
321
package_subpath = os .path .join (args .configuration , 'dependencies' , 'llbuild' )
305
322
for lib in ['libllbuildSwift' , 'libllbuild' ]:
306
323
install_library (args , build_dir , package_subpath , lib ,
307
- universal_lib_dir , toolchain_lib_dir ,'llbuild' )
324
+ universal_lib_dir , toolchain_lib_dir ,'llbuild' , targets )
308
325
309
326
# Create a universal shared-library file and install it into the toolchain lib
310
327
def install_library (args , build_dir , package_subpath , lib_name ,
311
- universal_lib_dir , toolchain_lib_dir , package_name ):
328
+ universal_lib_dir , toolchain_lib_dir , package_name , targets ):
312
329
shared_lib_file = lib_name + shared_lib_ext
313
330
output_dylib_path = os .path .join (universal_lib_dir , shared_lib_file )
314
331
lipo_cmd = ['lipo' ]
315
- for arch in macos_target_architectures :
316
- input_lib_path = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
332
+ for target in targets :
333
+ input_lib_path = os .path .join (build_dir , target ,
317
334
package_subpath , 'lib' , shared_lib_file )
318
335
lipo_cmd .append (input_lib_path )
319
336
lipo_cmd .extend (['-create' , '-output' , output_dylib_path ])
320
337
subprocess .check_call (lipo_cmd )
321
338
install_binary (shared_lib_file , universal_lib_dir , toolchain_lib_dir , args .verbose )
322
339
323
340
# Install binary .swiftmodule files for the driver and its dependencies into the toolchain lib
324
- def install_binary_swift_modules (args , build_dir , toolchain_lib_dir ):
341
+ def install_binary_swift_modules (args , build_dir , toolchain_lib_dir , targets ):
325
342
# The common subpath from a project's build directory to where its build products are found
326
343
product_subpath = 'swift'
327
344
328
345
# swift-driver
329
346
package_subpath = os .path .join (args .configuration , product_subpath )
330
347
for module in ['SwiftDriver' , 'SwiftOptions' ]:
331
- install_module (args , build_dir , package_subpath , toolchain_lib_dir , module )
348
+ install_module (args , build_dir , package_subpath , toolchain_lib_dir , module , targets )
332
349
333
350
# swift-tools-support-core
334
351
package_subpath = os .path .join (args .configuration , 'dependencies' , 'swift-tools-support-core' ,
335
352
product_subpath )
336
353
for module in ['TSCUtility' , 'TSCLibc' , 'TSCBasic' ]:
337
- install_module (args , build_dir , package_subpath , toolchain_lib_dir , module )
354
+ install_module (args , build_dir , package_subpath , toolchain_lib_dir , module , targets )
338
355
339
356
# swift-argument-parser
340
357
package_subpath = os .path .join (args .configuration , 'dependencies' , 'swift-argument-parser' ,
341
358
product_subpath )
342
- install_module (args , build_dir , package_subpath , toolchain_lib_dir , 'ArgumentParser' )
359
+ install_module (args , build_dir , package_subpath , toolchain_lib_dir , 'ArgumentParser' , targets )
343
360
344
361
# Install the modulemaps and headers of the driver's C module dependencies into the toolchain
345
362
# include directory
@@ -349,16 +366,16 @@ def install_c_module_includes(args, build_dir, toolchain_include_dir):
349
366
'TSCclibc' , 'include' )
350
367
install_include_artifacts (args , toolchain_include_dir , tscc_include_dir , 'TSCclibc' )
351
368
352
- def install_module (args , build_dir , package_subpath , toolchain_lib , module_name ):
369
+ def install_module (args , build_dir , package_subpath , toolchain_lib , module_name , targets ):
353
370
toolchain_module_dir = os .path .join (toolchain_lib , module_name + '.swiftmodule' )
354
371
mkdir_p (toolchain_module_dir )
355
- for arch in macos_target_architectures :
356
- swift_dir = os .path .join (build_dir , arch + '-apple-macos' + macos_deployment_target ,
372
+ for target in targets :
373
+ swift_dir = os .path .join (build_dir , target ,
357
374
package_subpath )
358
375
for fileext in ['.swiftmodule' , '.swiftdoc' ]:
359
376
install_binary (module_name + fileext , swift_dir , toolchain_module_dir , args .verbose )
360
377
os .rename (os .path .join (toolchain_module_dir , module_name + fileext ),
361
- os .path .join (toolchain_module_dir , arch + '-apple-macos' + fileext ))
378
+ os .path .join (toolchain_module_dir , target + fileext ))
362
379
363
380
# Copy over the contents of a module's include directory contents (modulemap, headers, etc.)
364
381
def install_include_artifacts (args , toolchain_include_dir , src_include_dir , dst_module_name ):
@@ -367,15 +384,8 @@ def install_include_artifacts(args, toolchain_include_dir, src_include_dir, dst_
367
384
shutil .rmtree (toolchain_module_include_dir , ignore_errors = True )
368
385
shutil .copytree (src_include_dir , toolchain_module_include_dir )
369
386
370
- def build_using_cmake (args , toolchain_bin , build_dir ):
387
+ def build_using_cmake (args , toolchain_bin , build_dir , targets ):
371
388
swiftc_exec = os .path .join (toolchain_bin , 'swiftc' )
372
-
373
- # Platform-specific targets for which we must build swift-driver
374
- if platform .system () == 'Darwin' :
375
- targets = [x + '-apple-macos' + macos_deployment_target for x in macos_target_architectures ]
376
- else :
377
- targets = [get_build_target (swiftc_exec , args )]
378
-
379
389
swift_flags = []
380
390
if args .configuration == 'debug' :
381
391
swift_flags .append ('-Onone' )
@@ -460,9 +470,6 @@ def build_yams_using_cmake(args, target, swiftc_exec, build_dir, base_cmake_flag
460
470
yams_cmake_flags .append ('-DCMAKE_C_FLAGS=-target %s' % target )
461
471
else :
462
472
yams_cmake_flags .append ('-DCMAKE_C_FLAGS=-fPIC -target %s' % target )
463
- if args .dispatch_build_dir :
464
- yams_cmake_flags .append (get_dispatch_cmake_arg (args ))
465
-
466
473
if args .foundation_build_dir :
467
474
yams_cmake_flags .append (get_foundation_cmake_arg (args ))
468
475
yams_swift_flags = swift_flags [:]
@@ -551,6 +558,18 @@ def main():
551
558
def add_common_args (parser ):
552
559
parser .add_argument ('--package-path' , metavar = 'PATH' , help = 'directory of the package to build' , default = '.' )
553
560
parser .add_argument ('--toolchain' , required = True , metavar = 'PATH' , help = 'build using the toolchain at PATH' )
561
+ parser .add_argument (
562
+ '--prefix' ,
563
+ dest = 'install_prefixes' ,
564
+ nargs = '*' ,
565
+ help = 'paths (relative to the project root) where to install build products [%(default)s]' ,
566
+ metavar = 'PATHS' )
567
+ parser .add_argument (
568
+ '--cross-compile-hosts' ,
569
+ dest = 'cross_compile_hosts' ,
570
+ nargs = '*' ,
571
+ help = 'List of cross compile hosts targets.' ,
572
+ default = [])
554
573
parser .add_argument ('--ninja-bin' , metavar = 'PATH' , help = 'ninja binary to use for testing' )
555
574
parser .add_argument ('--cmake-bin' , metavar = 'PATH' , help = 'cmake binary to use for building' )
556
575
parser .add_argument ('--build-path' , metavar = 'PATH' , default = '.build' , help = 'build in the given path' )
@@ -573,8 +592,6 @@ def add_common_args(parser):
573
592
install_parser = subparsers .add_parser ('install' , help = 'build the package' )
574
593
add_common_args (install_parser )
575
594
576
-
577
-
578
595
args = parser .parse_args (sys .argv [1 :])
579
596
580
597
# Canonicalize paths
@@ -587,18 +604,20 @@ def add_common_args(parser):
587
604
else :
588
605
args .sysroot = None
589
606
590
- if args .toolchain :
591
- toolchain_bin = os .path .join (args .toolchain , 'bin' )
592
- else :
593
- toolchain_bin = ''
607
+ if args .cross_compile_hosts and not all ('apple-macos' in target for target in args .cross_compile_hosts ):
608
+ error ('Cross-compilation is currently only supported for the Darwin platform.' )
594
609
595
610
if args .dispatch_build_dir :
596
611
args .dispatch_build_dir = os .path .abspath (args .dispatch_build_dir )
597
612
598
613
if args .foundation_build_dir :
599
614
args .foundation_build_dir = os .path .abspath (args .foundation_build_dir )
600
615
601
- handle_invocation (toolchain_bin , args )
616
+ # If a separate prefix has not been specified, installed into the specified toolchain
617
+ if not args .install_prefixes :
618
+ args .install_prefixes = [args .toolchain ]
619
+
620
+ handle_invocation (args )
602
621
603
622
if __name__ == '__main__' :
604
623
main ()
0 commit comments