Skip to content

Commit 927da2a

Browse files
committed
Create an Android CMake toolchain file instead to cross-compile Testing
1 parent 3f36409 commit 927da2a

File tree

3 files changed

+133
-35
lines changed

3 files changed

+133
-35
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,28 @@ index bfb69965890..b5e9f5349c2 100644
1515
define("CMAKE_Swift_COMPILER:PATH", cmake_swiftc_path)
1616
else:
1717
cmake_swiftc_path = os.getenv('CMAKE_Swift_COMPILER', toolchain.swiftc)
18+
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
19+
index 47e7ab79905..d88c3c242ad 100644
20+
--- a/swift/utils/swift_build_support/swift_build_support/products/product.py
21+
+++ b/swift/utils/swift_build_support/swift_build_support/products/product.py
22+
@@ -389,7 +395,7 @@ class Product(object):
23+
sysroot_arch, vendor, abi = self.get_linux_target_components(arch)
24+
return '{}-{}-linux-{}'.format(sysroot_arch, vendor, abi)
25+
26+
- def generate_linux_toolchain_file(self, platform, arch):
27+
+ def generate_linux_toolchain_file(self, platform, arch, crosscompiling=True):
28+
"""
29+
Generates a new CMake tolchain file that specifies Linux as a target
30+
platform.
31+
@@ -402,8 +408,9 @@ class Product(object):
32+
33+
toolchain_args = {}
34+
35+
- toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
36+
- toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
37+
+ if crosscompiling:
38+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
39+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
40+
41+
# We only set the actual sysroot if we are actually cross
42+
# compiling. This is important since otherwise cmake seems to change the

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

Lines changed: 107 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,116 @@ index 16e05052609..7ab8cebfab8 100755
2222

2323
cmake_options=(
2424
-DENABLE_SWIFT=YES
25+
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
26+
index d88c3c242ad..fcafd7676f5 100644
27+
--- a/swift/utils/swift_build_support/swift_build_support/products/product.py
28+
+++ b/swift/utils/swift_build_support/swift_build_support/products/product.py
29+
@@ -409,18 +409,31 @@ class Product(object):
30+
toolchain_args = {}
31+
32+
if crosscompiling:
33+
- toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
34+
- toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
35+
+ if platform == "linux":
36+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
37+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
38+
+ elif platform == "android":
39+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Android'
40+
+ toolchain_args['CMAKE_SYSTEM_VERSION'] = self.args.android_api_level
41+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = self.args.android_arch if not \
42+
+ self.args.android_arch == 'armv7' else 'armv7-a'
43+
+ toolchain_args['CMAKE_ANDROID_NDK'] = self.args.android_ndk
44+
+ toolchain_args['CMAKE_FIND_ROOT_PATH'] = self.args.cross_compile_deps_path
45+
+ toolchain_args['CMAKE_SHARED_LINKER_FLAGS'] = '\"\"'
46+
47+
# We only set the actual sysroot if we are actually cross
48+
# compiling. This is important since otherwise cmake seems to change the
49+
# RUNPATH to be a relative rather than an absolute path, breaking
50+
# certain cmark tests (and maybe others).
51+
- maybe_sysroot = self.get_linux_sysroot(platform, arch)
52+
- if maybe_sysroot is not None:
53+
- toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
54+
-
55+
- target = self.get_linux_target(platform, arch)
56+
+ if platform == "linux":
57+
+ maybe_sysroot = self.get_linux_sysroot(platform, arch)
58+
+ if maybe_sysroot is not None:
59+
+ toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
60+
+
61+
+ target = self.get_linux_target(platform, arch)
62+
+ elif platform == "android":
63+
+ target = '%s-unknown-linux-android%s' % (self.args.android_arch,
64+
+ self.args.android_api_level)
65+
if self.toolchain.cc.endswith('clang'):
66+
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
67+
if self.toolchain.cxx.endswith('clang++'):
68+
@@ -466,9 +479,19 @@ class Product(object):
69+
platform, arch,
70+
macos_deployment_version=override_macos_deployment_version)
71+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
72+
- elif platform == "linux":
73+
- toolchain_file = self.generate_linux_toolchain_file(platform, arch)
74+
+ elif platform == "linux" or platform == "android":
75+
+ # Always cross-compile for linux, but not on Android, as a native
76+
+ # compile on Android does not use the NDK and the CMake config.
77+
+ cross_compile = platform == "linux" or self.is_cross_compile_target(host_target)
78+
+ toolchain_file = self.generate_linux_toolchain_file(platform, arch, cross_compile)
79+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
80+
+ if cross_compile and platform == "android":
81+
+ resource_dir = None
82+
+ if not self.is_before_build_script_impl_product() and not self.is_build_script_impl_product():
83+
+ install_path = self.host_install_destdir(host_target) + self.args.install_prefix
84+
+ resource_dir = '%s/lib/swift' % install_path
85+
+ flags = targets.StdlibDeploymentTarget.get_target_for_name(host_target).platform.swift_flags(self.args, resource_dir)
86+
+ self.cmake_options.define('CMAKE_Swift_FLAGS', flags)
87+
88+
return toolchain_file
89+
2590
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
91+
index 417056efdd0..177ea9f0623 100644
2792
--- a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
2893
+++ 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')
94+
@@ -127,3 +127,11 @@ class SwiftTestingCMakeShim(cmake_product.CMakeProduct):
95+
install_prefix = install_destdir + self.args.install_prefix
4296

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)
97+
self.install_with_cmake(['install'], install_prefix)
5298
+
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)
99+
+ @classmethod
100+
+ def is_build_script_impl_product(cls):
101+
+ return False
61102
+
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, [],
103+
+ @classmethod
104+
+ def is_before_build_script_impl_product(cls):
105+
+ return False
106+
diff --git a/swift/utils/swift_build_support/swift_build_support/targets.py b/swift/utils/swift_build_support/swift_build_support/targets.py
107+
index fba09416ddb..67b81daba12 100644
108+
--- a/swift/utils/swift_build_support/swift_build_support/targets.py
109+
+++ b/swift/utils/swift_build_support/swift_build_support/targets.py
110+
@@ -72,7 +72,7 @@ class Platform(object):
111+
return True
112+
return False
113+
114+
- def swift_flags(self, args):
115+
+ def swift_flags(self, args, resource_path=None):
116+
"""
117+
Swift compiler flags for a platform, useful for cross-compiling
118+
"""
119+
@@ -154,12 +154,15 @@ class AndroidPlatform(Platform):
120+
"""
121+
return True
122+
123+
- def swift_flags(self, args):
124+
+ def swift_flags(self, args, resource_path=None):
125+
flags = '-target %s-unknown-linux-android%s ' % (args.android_arch,
126+
args.android_api_level)
127+
128+
- flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
129+
- args.build_root, self.name, args.android_arch)
130+
+ if resource_path is not None:
131+
+ flags += '-resource-dir %s ' % (resource_path)
132+
+ else:
133+
+ flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
134+
+ args.build_root, self.name, args.android_arch)
135+
136+
android_toolchain_path = self.ndk_toolchain_path(args)
137+

0 commit comments

Comments
 (0)