@@ -389,7 +389,7 @@ def get_linux_target(self, platform, arch):
389
389
sysroot_arch , vendor , abi = self .get_linux_target_components (arch )
390
390
return '{}-{}-linux-{}' .format (sysroot_arch , vendor , abi )
391
391
392
- def generate_linux_toolchain_file (self , platform , arch ):
392
+ def generate_linux_toolchain_file (self , platform , arch , crosscompiling = True ):
393
393
"""
394
394
Generates a new CMake tolchain file that specifies Linux as a target
395
395
platform.
@@ -402,18 +402,34 @@ def generate_linux_toolchain_file(self, platform, arch):
402
402
403
403
toolchain_args = {}
404
404
405
- toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Linux'
406
- toolchain_args ['CMAKE_SYSTEM_PROCESSOR' ] = arch
405
+ if crosscompiling :
406
+ if platform == "linux" :
407
+ toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Linux'
408
+ toolchain_args ['CMAKE_SYSTEM_PROCESSOR' ] = arch
409
+ elif platform == "android" :
410
+ toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Android'
411
+ toolchain_args ['CMAKE_SYSTEM_VERSION' ] = self .args .android_api_level
412
+ toolchain_args ['CMAKE_SYSTEM_PROCESSOR' ] = arch if not arch == 'armv7' \
413
+ else 'armv7-a'
414
+ toolchain_args ['CMAKE_ANDROID_NDK' ] = self .args .android_ndk
415
+ toolchain_args ['CMAKE_FIND_ROOT_PATH' ] = self .args .cross_compile_deps_path
416
+ # This is a workaround for a CMake 3.30+ bug,
417
+ # https://gitlab.kitware.com/cmake/cmake/-/issues/26154, and can
418
+ # be removed once that is fixed.
419
+ toolchain_args ['CMAKE_SHARED_LINKER_FLAGS' ] = '\" \" '
407
420
408
421
# We only set the actual sysroot if we are actually cross
409
422
# compiling. This is important since otherwise cmake seems to change the
410
423
# RUNPATH to be a relative rather than an absolute path, breaking
411
424
# certain cmark tests (and maybe others).
412
- maybe_sysroot = self .get_linux_sysroot (platform , arch )
413
- if maybe_sysroot is not None :
414
- toolchain_args ['CMAKE_SYSROOT' ] = maybe_sysroot
415
-
416
- target = self .get_linux_target (platform , arch )
425
+ if platform == "linux" :
426
+ maybe_sysroot = self .get_linux_sysroot (platform , arch )
427
+ if maybe_sysroot is not None :
428
+ toolchain_args ['CMAKE_SYSROOT' ] = maybe_sysroot
429
+
430
+ target = self .get_linux_target (platform , arch )
431
+ elif platform == "android" :
432
+ target = '%s-unknown-linux-android%s' % (arch , self .args .android_api_level )
417
433
if self .toolchain .cc .endswith ('clang' ):
418
434
toolchain_args ['CMAKE_C_COMPILER_TARGET' ] = target
419
435
if self .toolchain .cxx .endswith ('clang++' ):
@@ -459,10 +475,30 @@ def generate_toolchain_file_for_darwin_or_linux(
459
475
platform , arch ,
460
476
macos_deployment_version = override_macos_deployment_version )
461
477
self .cmake_options .define ('CMAKE_TOOLCHAIN_FILE:PATH' , toolchain_file )
462
- elif platform == "linux" :
463
- toolchain_file = self .generate_linux_toolchain_file (platform , arch )
478
+ elif platform == "linux" or platform == "android" :
479
+ # Always cross-compile for linux, but not on Android, as a native
480
+ # compile on Android does not use the NDK and its CMake config.
481
+ cross_compile = platform == "linux" or \
482
+ self .is_cross_compile_target (host_target )
483
+ toolchain_file = self .generate_linux_toolchain_file (platform , arch ,
484
+ cross_compile )
464
485
self .cmake_options .define ('CMAKE_TOOLCHAIN_FILE:PATH' , toolchain_file )
465
486
487
+ if cross_compile and platform == "android" :
488
+ resource_dir = None
489
+ # build-script-impl products build before the install and use
490
+ # the Swift stdlib from the compiler build directory instead,
491
+ # while products built even before that currently do not support
492
+ # cross-compiling Swift.
493
+ if not self .is_before_build_script_impl_product () and \
494
+ not self .is_build_script_impl_product ():
495
+ install_path = self .host_install_destdir (host_target ) + \
496
+ self .args .install_prefix
497
+ resource_dir = '%s/lib/swift' % install_path
498
+ flags = targets .StdlibDeploymentTarget .get_target_for_name (
499
+ host_target ).platform .swift_flags (self .args , resource_dir )
500
+ self .cmake_options .define ('CMAKE_Swift_FLAGS' , flags )
501
+
466
502
return toolchain_file
467
503
468
504
def get_openbsd_toolchain_file (self ):
0 commit comments