Skip to content

Commit 75f1fbb

Browse files
committed
[build][android] Use a CMake toolchain file to cross-compile Testing
Also, disable a recently failing test for Android armv7.
1 parent 792f4f3 commit 75f1fbb

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

test/SILOptimizer/concat_string_literals.32.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// We have a separate test for 64-bit architectures.
66
// REQUIRES: PTRSIZE=32
7+
// XFAIL: OS=linux-androideabi
78

89
// NOTE: 25185.byteSwapped = 0x62 'a', 0x61 'b'
910
// CHECK-LABEL: test_ascii_scalar_scalar2

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

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)