@@ -389,7 +389,7 @@ def get_linux_target(self, platform, arch):
389389 sysroot_arch , vendor , abi = self .get_linux_target_components (arch )
390390 return '{}-{}-linux-{}' .format (sysroot_arch , vendor , abi )
391391
392- def generate_linux_toolchain_file (self , platform , arch ):
392+ def generate_linux_toolchain_file (self , platform , arch , crosscompiling = True ):
393393 """
394394 Generates a new CMake tolchain file that specifies Linux as a target
395395 platform.
@@ -402,18 +402,34 @@ def generate_linux_toolchain_file(self, platform, arch):
402402
403403 toolchain_args = {}
404404
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' ] = '\" \" '
407420
408421 # We only set the actual sysroot if we are actually cross
409422 # compiling. This is important since otherwise cmake seems to change the
410423 # RUNPATH to be a relative rather than an absolute path, breaking
411424 # 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 )
417433 if self .toolchain .cc .endswith ('clang' ):
418434 toolchain_args ['CMAKE_C_COMPILER_TARGET' ] = target
419435 if self .toolchain .cxx .endswith ('clang++' ):
@@ -459,10 +475,30 @@ def generate_toolchain_file_for_darwin_or_linux(
459475 platform , arch ,
460476 macos_deployment_version = override_macos_deployment_version )
461477 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 )
464485 self .cmake_options .define ('CMAKE_TOOLCHAIN_FILE:PATH' , toolchain_file )
465486
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+
466502 return toolchain_file
467503
468504 def get_openbsd_toolchain_file (self ):
0 commit comments