Skip to content

Commit 2c1ac12

Browse files
committed
[build][android] Use a CMake toolchain file to cross-compile Testing (#83260)
Also, disable a recently failing test for Android armv7.
1 parent 8aeb9b2 commit 2c1ac12

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
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
@@ -403,18 +403,33 @@ def generate_linux_toolchain_file(self, platform, arch, crosscompiling=True):
403403
toolchain_args = {}
404404

405405
if crosscompiling:
406-
toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
407-
toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
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'] = '\"\"'
408420

409421
# We only set the actual sysroot if we are actually cross
410422
# compiling. This is important since otherwise cmake seems to change the
411423
# RUNPATH to be a relative rather than an absolute path, breaking
412424
# certain cmark tests (and maybe others).
413-
maybe_sysroot = self.get_linux_sysroot(platform, arch)
414-
if maybe_sysroot is not None:
415-
toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
416-
417-
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)
418433
if self.toolchain.cc.endswith('clang'):
419434
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
420435
if self.toolchain.cxx.endswith('clang++'):
@@ -460,10 +475,30 @@ def generate_toolchain_file_for_darwin_or_linux(
460475
platform, arch,
461476
macos_deployment_version=override_macos_deployment_version)
462477
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
463-
elif platform == "linux":
464-
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)
465485
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
466486

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+
467502
return toolchain_file
468503

469504
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# REQUIRES: standalone_build
2-
# REQUIRES: OS=macosx
32

43
# RUN: %empty-directory(%t)
54
# RUN: mkdir -p %t
65
# RUN: SKIP_XCODE_VERSION_CHECK=1 SWIFT_BUILD_ROOT=%t %swift_src_root/utils/build-script --dry-run --install-all --cmake %cmake --skip-build-llvm --skip-build-swift 2>&1 | %FileCheck %s
6+
# RUN: SKIP_XCODE_VERSION_CHECK=1 SWIFT_BUILD_ROOT=%t %swift_src_root/utils/build-script --dry-run --install-all --cmake %cmake --skip-build-llvm --skip-build-swift --cross-compile-hosts=android-aarch64 --skip-local-build --android --android-ndk %t/ndk/ 2>&1 | %FileCheck --check-prefix=ANDROID %s
77

88
# CHECK: DRY_RUN! Writing Toolchain file to path:{{.*}}BuildScriptToolchain.cmake
99
# CHECK: cmake {{.*}}-DCMAKE_TOOLCHAIN_FILE:PATH={{.*}}BuildScriptToolchain.cmake {{.*}}cmark
10+
11+
# ANDROID: DRY_RUN! Writing Toolchain file to path:{{.*}}cmark-android-aarch64/BuildScriptToolchain.cmake
12+
# ANDROID: cmake {{.*}}-DCMAKE_TOOLCHAIN_FILE:PATH={{.*}}cmark-android-aarch64/BuildScriptToolchain.cmake
13+
# ANDROID: -DCMAKE_Swift_FLAGS=-target aarch64-unknown-linux-android

0 commit comments

Comments
 (0)