Skip to content

Commit f658604

Browse files
committed
Create an Android CMake toolchain file instead to cross-compile Testing and add
16 KB page linker flag to 6.2 branch
1 parent 3f36409 commit f658604

File tree

3 files changed

+169
-53
lines changed

3 files changed

+169
-53
lines changed

swift-ci/sdks/android/patches/apply-patches.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ cd ${1}
55

66
case "${BUILD_SCHEME}" in
77
swift-*-branch)
8-
git apply -v -C1 ${patches_dir}/swift-android.patch
98
git apply -v -C1 ${patches_dir}/swift-android-devel.patch
9+
git apply -v -C1 ${patches_dir}/swift-android.patch
1010
;;
1111
development)
1212
git apply -v -C1 ${patches_dir}/swift-android.patch

swift-ci/sdks/android/patches/swift-android-devel.patch

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,171 @@
1+
diff --git a/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake b/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake
2+
index 0b18957eadb..5021409ba06 100644
3+
--- a/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake
4+
+++ b/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake
5+
@@ -2504,6 +2504,8 @@ function(add_swift_target_library name)
6+
list(APPEND swiftlib_link_flags_all "-shared")
7+
# TODO: Instead of `lib${name}.so` find variable or target property which already have this value.
8+
list(APPEND swiftlib_link_flags_all "-Wl,-soname,lib${name}.so")
9+
+ # Ensure compatibility with Android 15+ devices using 16KB memory pages.
10+
+ list(APPEND swiftlib_link_flags_all "-Wl,-z,max-page-size=16384")
11+
endif()
12+
13+
if (SWIFTLIB_BACK_DEPLOYMENT_LIBRARY)
14+
diff --git a/swift/test/SILOptimizer/concat_string_literals.32.swift b/swift/test/SILOptimizer/concat_string_literals.32.swift
15+
index 01390761c8c..d84c142397c 100644
16+
--- a/swift/test/SILOptimizer/concat_string_literals.32.swift
17+
+++ b/swift/test/SILOptimizer/concat_string_literals.32.swift
18+
@@ -4,6 +4,7 @@
19+
20+
// We have a separate test for 64-bit architectures.
21+
// REQUIRES: PTRSIZE=32
22+
+// XFAIL: OS=linux-androideabi
23+
24+
// NOTE: 25185.byteSwapped = 0x62 'a', 0x61 'b'
25+
// CHECK-LABEL: test_ascii_scalar_scalar2
26+
diff --git a/swift/utils/swift_build_support/swift_build_support/products/product.py b/swift/utils/swift_build_support/swift_build_support/products/product.py
27+
index 47e7ab79905..6bd94c3cad8 100644
28+
--- a/swift/utils/swift_build_support/swift_build_support/products/product.py
29+
+++ b/swift/utils/swift_build_support/swift_build_support/products/product.py
30+
@@ -389,7 +389,7 @@ class Product(object):
31+
sysroot_arch, vendor, abi = self.get_linux_target_components(arch)
32+
return '{}-{}-linux-{}'.format(sysroot_arch, vendor, abi)
33+
34+
- def generate_linux_toolchain_file(self, platform, arch):
35+
+ def generate_linux_toolchain_file(self, platform, arch, crosscompiling=True):
36+
"""
37+
Generates a new CMake tolchain file that specifies Linux as a target
38+
platform.
39+
@@ -402,18 +402,34 @@ class Product(object):
40+
41+
toolchain_args = {}
42+
43+
- toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
44+
- toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
45+
+ if crosscompiling:
46+
+ if platform == "linux":
47+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
48+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
49+
+ elif platform == "android":
50+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Android'
51+
+ toolchain_args['CMAKE_SYSTEM_VERSION'] = self.args.android_api_level
52+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch if not arch == 'armv7' \
53+
+ else 'armv7-a'
54+
+ toolchain_args['CMAKE_ANDROID_NDK'] = self.args.android_ndk
55+
+ toolchain_args['CMAKE_FIND_ROOT_PATH'] = self.args.cross_compile_deps_path
56+
+ # This is a workaround for a CMake 3.30+ bug,
57+
+ # https://gitlab.kitware.com/cmake/cmake/-/issues/26154, and can
58+
+ # be removed once that is fixed.
59+
+ toolchain_args['CMAKE_SHARED_LINKER_FLAGS'] = '\"\"'
60+
61+
# We only set the actual sysroot if we are actually cross
62+
# compiling. This is important since otherwise cmake seems to change the
63+
# RUNPATH to be a relative rather than an absolute path, breaking
64+
# certain cmark tests (and maybe others).
65+
- maybe_sysroot = self.get_linux_sysroot(platform, arch)
66+
- if maybe_sysroot is not None:
67+
- toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
68+
-
69+
- target = self.get_linux_target(platform, arch)
70+
+ if platform == "linux":
71+
+ maybe_sysroot = self.get_linux_sysroot(platform, arch)
72+
+ if maybe_sysroot is not None:
73+
+ toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
74+
+
75+
+ target = self.get_linux_target(platform, arch)
76+
+ elif platform == "android":
77+
+ target = '%s-unknown-linux-android%s' % (arch, self.args.android_api_level)
78+
if self.toolchain.cc.endswith('clang'):
79+
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
80+
if self.toolchain.cxx.endswith('clang++'):
81+
@@ -459,10 +475,30 @@ class Product(object):
82+
platform, arch,
83+
macos_deployment_version=override_macos_deployment_version)
84+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
85+
- elif platform == "linux":
86+
- toolchain_file = self.generate_linux_toolchain_file(platform, arch)
87+
+ elif platform == "linux" or platform == "android":
88+
+ # Always cross-compile for linux, but not on Android, as a native
89+
+ # compile on Android does not use the NDK and its CMake config.
90+
+ cross_compile = platform == "linux" or \
91+
+ self.is_cross_compile_target(host_target)
92+
+ toolchain_file = self.generate_linux_toolchain_file(platform, arch,
93+
+ cross_compile)
94+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
95+
96+
+ if cross_compile and platform == "android":
97+
+ resource_dir = None
98+
+ # build-script-impl products build before the install and use
99+
+ # the Swift stdlib from the compiler build directory instead,
100+
+ # while products built even before that currently do not support
101+
+ # cross-compiling Swift.
102+
+ if not self.is_before_build_script_impl_product() and \
103+
+ not self.is_build_script_impl_product():
104+
+ install_path = self.host_install_destdir(host_target) + \
105+
+ self.args.install_prefix
106+
+ resource_dir = '%s/lib/swift' % install_path
107+
+ flags = targets.StdlibDeploymentTarget.get_target_for_name(
108+
+ host_target).platform.swift_flags(self.args, resource_dir)
109+
+ self.cmake_options.define('CMAKE_Swift_FLAGS', flags)
110+
+
111+
return toolchain_file
112+
113+
def get_openbsd_toolchain_file(self):
114+
diff --git a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py b/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
115+
index 417056efdd0..177ea9f0623 100644
116+
--- a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
117+
+++ b/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
118+
@@ -127,3 +127,11 @@ class SwiftTestingCMakeShim(cmake_product.CMakeProduct):
119+
install_prefix = install_destdir + self.args.install_prefix
120+
121+
self.install_with_cmake(['install'], install_prefix)
122+
+
123+
+ @classmethod
124+
+ def is_build_script_impl_product(cls):
125+
+ return False
126+
+
127+
+ @classmethod
128+
+ def is_before_build_script_impl_product(cls):
129+
+ return False
130+
diff --git a/swift/utils/swift_build_support/swift_build_support/targets.py b/swift/utils/swift_build_support/swift_build_support/targets.py
131+
index 508d6204eaf..64a862eaf0e 100644
132+
--- a/swift/utils/swift_build_support/swift_build_support/targets.py
133+
+++ b/swift/utils/swift_build_support/swift_build_support/targets.py
134+
@@ -72,7 +72,7 @@ class Platform(object):
135+
return True
136+
return False
137+
138+
- def swift_flags(self, args):
139+
+ def swift_flags(self, args, resource_path=None):
140+
"""
141+
Swift compiler flags for a platform, useful for cross-compiling
142+
"""
143+
@@ -154,17 +154,21 @@ class AndroidPlatform(Platform):
144+
"""
145+
return True
146+
147+
- def swift_flags(self, args):
148+
+ def swift_flags(self, args, resource_path=None):
149+
flags = '-target %s-unknown-linux-android%s ' % (args.android_arch,
150+
args.android_api_level)
151+
152+
- flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
153+
- args.build_root, self.name, args.android_arch)
154+
+ if resource_path is not None:
155+
+ flags += '-resource-dir %s ' % (resource_path)
156+
+ else:
157+
+ flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
158+
+ args.build_root, self.name, args.android_arch)
159+
160+
android_toolchain_path = self.ndk_toolchain_path(args)
161+
162+
flags += '-sdk %s/sysroot ' % (android_toolchain_path)
163+
- flags += '-tools-directory %s/bin' % (android_toolchain_path)
164+
+ flags += '-tools-directory %s/bin ' % (android_toolchain_path)
165+
+ flags += '-Xclang-linker -Wl,-z,max-page-size=16384'
166+
return flags
167+
168+
def cmake_options(self, args):
1169
diff --git a/swift/utils/swift_build_support/swift_build_support/cmake.py b/swift/utils/swift_build_support/swift_build_support/cmake.py
2170
index bfb69965890..b5e9f5349c2 100644
3171
--- a/swift/utils/swift_build_support/swift_build_support/cmake.py
Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
diff --git a/swift/test/SILOptimizer/concat_string_literals.32.swift b/swift/test/SILOptimizer/concat_string_literals.32.swift
2-
index 01390761c8c..d84c142397c 100644
3-
--- a/swift/test/SILOptimizer/concat_string_literals.32.swift
4-
+++ b/swift/test/SILOptimizer/concat_string_literals.32.swift
5-
@@ -4,6 +4,7 @@
6-
7-
// We have a separate test for 64-bit architectures.
8-
// REQUIRES: PTRSIZE=32
9-
+// XFAIL: OS=linux-androideabi
10-
11-
// NOTE: 25185.byteSwapped = 0x62 'a', 0x61 'b'
12-
// CHECK-LABEL: test_ascii_scalar_scalar2
131
diff --git a/swift/utils/build-script-impl b/swift/utils/build-script-impl
142
index 16e05052609..7ab8cebfab8 100755
153
--- a/swift/utils/build-script-impl
@@ -22,43 +10,3 @@ index 16e05052609..7ab8cebfab8 100755
2210

2311
cmake_options=(
2412
-DENABLE_SWIFT=YES
25-
diff --git a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py b/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
26-
index 324d1a77eea..e88601a8701 100644
27-
--- a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
28-
+++ b/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
29-
@@ -13,6 +13,9 @@
30-
import os
31-
32-
from build_swift.build_swift.versions import Version
33-
+from ..host_specific_configuration \
34-
+ import HostSpecificConfiguration
35-
+from ..targets import StdlibDeploymentTarget
36-
37-
from . import cmake_product
38-
from . import product
39-
@@ -115,6 +117,25 @@ class SwiftTestingCMakeShim(cmake_product.CMakeProduct):
40-
# FIXME: If we build macros for the builder, specify the path.
41-
self.cmake_options.define('SwiftTesting_MACRO', 'NO')
42-
43-
+ if host_target.startswith('android') and self.is_cross_compile_target(host_target):
44-
+ host_config = HostSpecificConfiguration(host_target, self.args)
45-
+ self.cmake_options.extend(host_config.cmake_options)
46-
+ triple = '%s-unknown-linux-android%s' % (self.args.android_arch,
47-
+ self.args.android_api_level)
48-
+ flags = '-target %s ' % (triple)
49-
+
50-
+ flags += '-resource-dir %s/lib/swift ' % (
51-
+ self.host_install_destdir(host_target) + self.args.install_prefix)
52-
+
53-
+ ndk_path = StdlibDeploymentTarget.get_target_for_name(host_target).platform.ndk_toolchain_path(self.args)
54-
+ flags += '-sdk %s/sysroot ' % (ndk_path)
55-
+ flags += '-tools-directory %s/bin' % (ndk_path)
56-
+ self.cmake_options.define('CMAKE_Swift_FLAGS', flags)
57-
+ self.cmake_options.define('CMAKE_Swift_COMPILER_TARGET', triple)
58-
+ self.cmake_options.define('CMAKE_CXX_COMPILER_WORKS', 'True')
59-
+ self.cmake_options.define('CMAKE_SHARED_LINKER_FLAGS', '')
60-
+ self.cmake_options.define('CMAKE_FIND_ROOT_PATH', self.args.cross_compile_deps_path)
61-
+
62-
self.generate_toolchain_file_for_darwin_or_linux(
63-
host_target, override_macos_deployment_version=override_deployment_version)
64-
self.build_with_cmake([], self.args.build_variant, [],

0 commit comments

Comments
 (0)