@@ -409,18 +409,33 @@ def generate_linux_toolchain_file(self, platform, arch, crosscompiling=True):
409409 toolchain_args = {}
410410
411411 if crosscompiling :
412- toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Linux'
413- toolchain_args ['CMAKE_SYSTEM_PROCESSOR' ] = arch
412+ if platform == "linux" :
413+ toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Linux'
414+ toolchain_args ['CMAKE_SYSTEM_PROCESSOR' ] = arch
415+ elif platform == "android" :
416+ toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Android'
417+ toolchain_args ['CMAKE_SYSTEM_VERSION' ] = self .args .android_api_level
418+ toolchain_args ['CMAKE_SYSTEM_PROCESSOR' ] = arch if not arch == 'armv7' \
419+ else 'armv7-a'
420+ toolchain_args ['CMAKE_ANDROID_NDK' ] = self .args .android_ndk
421+ toolchain_args ['CMAKE_FIND_ROOT_PATH' ] = self .args .cross_compile_deps_path
422+ # This is a workaround for a CMake 3.30+ bug,
423+ # https://gitlab.kitware.com/cmake/cmake/-/issues/26154, and can
424+ # be removed once that is fixed.
425+ toolchain_args ['CMAKE_SHARED_LINKER_FLAGS' ] = '\" \" '
414426
415427 # We only set the actual sysroot if we are actually cross
416428 # compiling. This is important since otherwise cmake seems to change the
417429 # RUNPATH to be a relative rather than an absolute path, breaking
418430 # certain cmark tests (and maybe others).
419- maybe_sysroot = self .get_linux_sysroot (platform , arch )
420- if maybe_sysroot is not None :
421- toolchain_args ['CMAKE_SYSROOT' ] = maybe_sysroot
422-
423- target = self .get_linux_target (platform , arch )
431+ if platform == "linux" :
432+ maybe_sysroot = self .get_linux_sysroot (platform , arch )
433+ if maybe_sysroot is not None :
434+ toolchain_args ['CMAKE_SYSROOT' ] = maybe_sysroot
435+
436+ target = self .get_linux_target (platform , arch )
437+ elif platform == "android" :
438+ target = '%s-unknown-linux-android%s' % (arch , self .args .android_api_level )
424439 if self .toolchain .cc .endswith ('clang' ):
425440 toolchain_args ['CMAKE_C_COMPILER_TARGET' ] = target
426441 if self .toolchain .cxx .endswith ('clang++' ):
@@ -466,10 +481,30 @@ def generate_toolchain_file_for_darwin_or_linux(
466481 platform , arch ,
467482 macos_deployment_version = override_macos_deployment_version )
468483 self .cmake_options .define ('CMAKE_TOOLCHAIN_FILE:PATH' , toolchain_file )
469- elif platform == "linux" :
470- toolchain_file = self .generate_linux_toolchain_file (platform , arch )
484+ elif platform == "linux" or platform == "android" :
485+ # Always cross-compile for linux, but not on Android, as a native
486+ # compile on Android does not use the NDK and its CMake config.
487+ cross_compile = platform == "linux" or \
488+ self .is_cross_compile_target (host_target )
489+ toolchain_file = self .generate_linux_toolchain_file (platform , arch ,
490+ cross_compile )
471491 self .cmake_options .define ('CMAKE_TOOLCHAIN_FILE:PATH' , toolchain_file )
472492
493+ if cross_compile and platform == "android" :
494+ resource_dir = None
495+ # build-script-impl products build before the install and use
496+ # the Swift stdlib from the compiler build directory instead,
497+ # while products built even before that currently do not support
498+ # cross-compiling Swift.
499+ if not self .is_before_build_script_impl_product () and \
500+ not self .is_build_script_impl_product ():
501+ install_path = self .host_install_destdir (host_target ) + \
502+ self .args .install_prefix
503+ resource_dir = '%s/lib/swift' % install_path
504+ flags = targets .StdlibDeploymentTarget .get_target_for_name (
505+ host_target ).platform .swift_flags (self .args , resource_dir )
506+ self .cmake_options .define ('CMAKE_Swift_FLAGS' , flags )
507+
473508 return toolchain_file
474509
475510 def get_openbsd_toolchain_file (self ):
0 commit comments