Skip to content

Commit 69eef12

Browse files
committed
[build][android] Use a CMake toolchain file to cross-compile Testing
1 parent 792f4f3 commit 69eef12

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,34 @@ 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'] = self.args.android_arch \
419+
if not self.args.android_arch == 'armv7' 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' % (self.args.android_arch,
439+
self.args.android_api_level)
424440
if self.toolchain.cc.endswith('clang'):
425441
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
426442
if self.toolchain.cxx.endswith('clang++'):
@@ -466,10 +482,30 @@ def generate_toolchain_file_for_darwin_or_linux(
466482
platform, arch,
467483
macos_deployment_version=override_macos_deployment_version)
468484
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
469-
elif platform == "linux":
470-
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
485+
elif platform == "linux" or platform == "android":
486+
# Always cross-compile for linux, but not on Android, as a native
487+
# compile on Android does not use the NDK and its CMake config.
488+
cross_compile = platform == "linux" or \
489+
self.is_cross_compile_target(host_target)
490+
toolchain_file = self.generate_linux_toolchain_file(platform, arch,
491+
cross_compile)
471492
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
472493

494+
if cross_compile and platform == "android":
495+
resource_dir = None
496+
# build-script-impl products build before the install and use
497+
# the Swift stdlib from the compiler build directory instead,
498+
# while products built even before that currently do not support
499+
# cross-compiling Swift.
500+
if not self.is_before_build_script_impl_product() and \
501+
not self.is_build_script_impl_product():
502+
install_path = self.host_install_destdir(host_target) + \
503+
self.args.install_prefix
504+
resource_dir = '%s/lib/swift' % install_path
505+
flags = targets.StdlibDeploymentTarget.get_target_for_name(
506+
host_target).platform.swift_flags(self.args, resource_dir)
507+
self.cmake_options.define('CMAKE_Swift_FLAGS', flags)
508+
473509
return toolchain_file
474510

475511
def get_openbsd_toolchain_file(self):

utils/swift_build_support/swift_build_support/products/swift_testing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,11 @@ def install(self, host_target):
127127
install_prefix = install_destdir + self.args.install_prefix
128128

129129
self.install_with_cmake(['install'], install_prefix)
130+
131+
@classmethod
132+
def is_build_script_impl_product(cls):
133+
return False
134+
135+
@classmethod
136+
def is_before_build_script_impl_product(cls):
137+
return False

utils/swift_build_support/swift_build_support/targets.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def contains(self, target_name):
7272
return True
7373
return False
7474

75-
def swift_flags(self, args):
75+
def swift_flags(self, args, resource_path=None):
7676
"""
7777
Swift compiler flags for a platform, useful for cross-compiling
7878
"""
@@ -154,12 +154,15 @@ def uses_host_tests(self):
154154
"""
155155
return True
156156

157-
def swift_flags(self, args):
157+
def swift_flags(self, args, resource_path=None):
158158
flags = '-target %s-unknown-linux-android%s ' % (args.android_arch,
159159
args.android_api_level)
160160

161-
flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
162-
args.build_root, self.name, args.android_arch)
161+
if resource_path is not None:
162+
flags += '-resource-dir %s ' % (resource_path)
163+
else:
164+
flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
165+
args.build_root, self.name, args.android_arch)
163166

164167
android_toolchain_path = self.ndk_toolchain_path(args)
165168

0 commit comments

Comments
 (0)