Skip to content

Commit 72e6597

Browse files
committed
Switch over to using a CMake toolchain file to cross-compile Testing on the CI
1 parent e9f11e3 commit 72e6597

File tree

5 files changed

+165
-41
lines changed

5 files changed

+165
-41
lines changed

.github/workflows/sdks.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
SWIFT_TAG="swift-DEVELOPMENT-SNAPSHOT-${LATEST_TOOLCHAIN_VERSION}-a"
3232
fi
3333
echo "tag=$SWIFT_TAG" >> $GITHUB_OUTPUT
34-
echo "key=$SWIFT_TAG-ndk-${NDK_VERSION}-get-bundle" >> $GITHUB_OUTPUT
34+
echo "key=$SWIFT_TAG-ndk-${NDK_VERSION}-test-bundle" >> $GITHUB_OUTPUT
3535
- name: Get cached SDK bundle
3636
id: cache-bundle
3737
uses: actions/cache/restore@v4
@@ -144,6 +144,7 @@ jobs:
144144
perl -pi -e 's%33%24%' $ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/execinfo.h
145145
fi
146146
147+
git apply swift-android-testing.patch
147148
perl -pi -e 's%String\(cString: getpass%\"fake\" //%' swiftpm/Sources/PackageRegistryCommand/PackageRegistryCommand+Auth.swift
148149
perl -pi -e "s%swift/android',%swift/android', '-Xlinker', '-landroid-spawn',%" sourcekit-lsp/Utilities/build-script-helper.py
149150
VERSION="$(echo ${{ steps.version.outputs.tag }} | cut -f1,2 -d-)-${{ matrix.version }}"

swift-android-devel.patch

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
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
2+
index 47e7ab79905..d88c3c242ad 100644
3+
--- a/swift/utils/swift_build_support/swift_build_support/products/product.py
4+
+++ b/swift/utils/swift_build_support/swift_build_support/products/product.py
5+
@@ -389,7 +395,7 @@ class Product(object):
6+
sysroot_arch, vendor, abi = self.get_linux_target_components(arch)
7+
return '{}-{}-linux-{}'.format(sysroot_arch, vendor, abi)
8+
9+
- def generate_linux_toolchain_file(self, platform, arch):
10+
+ def generate_linux_toolchain_file(self, platform, arch, crosscompiling=True):
11+
"""
12+
Generates a new CMake tolchain file that specifies Linux as a target
13+
platform.
14+
@@ -402,8 +408,9 @@ class Product(object):
15+
16+
toolchain_args = {}
17+
18+
- toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
19+
- toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
20+
+ if crosscompiling:
21+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
22+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
23+
24+
# We only set the actual sysroot if we are actually cross
25+
# compiling. This is important since otherwise cmake seems to change the
126
diff --git a/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt b/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt
227
index 016bf294..5c42986a 100644
328
--- a/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt

swift-android-testing-release.patch

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ index 16e05052609..7ab8cebfab8 100755
1212
llvm_cmake_options+=(
1313
-DLLVM_HOST_TRIPLE:STRING="${SWIFT_HOST_TRIPLE}"
1414
)
15+
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
16+
index e62eb23c8bb..d88c3c242ad 100644
17+
--- a/swift/utils/swift_build_support/swift_build_support/products/product.py
18+
+++ b/swift/utils/swift_build_support/swift_build_support/products/product.py
19+
@@ -389,7 +395,7 @@ class Product(object):
20+
sysroot_arch, vendor, abi = self.get_linux_target_components(arch)
21+
return '{}-{}-linux-{}'.format(sysroot_arch, vendor, abi)
22+
23+
- def generate_linux_toolchain_file(self, platform, arch):
24+
+ def generate_linux_toolchain_file(self, platform, arch, crosscompiling=True):
25+
"""
26+
Generates a new CMake tolchain file that specifies Linux as a target
27+
platform.
28+
@@ -402,8 +408,9 @@ class Product(object):
29+
30+
toolchain_args = {}
31+
32+
- toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
33+
- toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
34+
+ if crosscompiling:
35+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
36+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
37+
38+
# We only set the actual sysroot if we are actually cross
39+
# compiling. This is important since otherwise cmake seems to change the
1540
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
1641
index b6bfa5760ef..324d1a77eea 100644
1742
--- a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py

swift-android-testing.patch

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
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
2+
index d88c3c242ad..fcafd7676f5 100644
3+
--- a/swift/utils/swift_build_support/swift_build_support/products/product.py
4+
+++ b/swift/utils/swift_build_support/swift_build_support/products/product.py
5+
@@ -409,18 +409,31 @@ class Product(object):
6+
toolchain_args = {}
7+
8+
if crosscompiling:
9+
- toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
10+
- toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
11+
+ if platform == "linux":
12+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
13+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
14+
+ elif platform == "android":
15+
+ toolchain_args['CMAKE_SYSTEM_NAME'] = 'Android'
16+
+ toolchain_args['CMAKE_SYSTEM_VERSION'] = self.args.android_api_level
17+
+ toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = self.args.android_arch if not \
18+
+ self.args.android_arch == 'armv7' else 'armv7-a'
19+
+ toolchain_args['CMAKE_ANDROID_NDK'] = self.args.android_ndk
20+
+ toolchain_args['CMAKE_FIND_ROOT_PATH'] = self.args.cross_compile_deps_path
21+
+ toolchain_args['CMAKE_SHARED_LINKER_FLAGS'] = '\"\"'
22+
23+
# We only set the actual sysroot if we are actually cross
24+
# compiling. This is important since otherwise cmake seems to change the
25+
# RUNPATH to be a relative rather than an absolute path, breaking
26+
# certain cmark tests (and maybe others).
27+
- maybe_sysroot = self.get_linux_sysroot(platform, arch)
28+
- if maybe_sysroot is not None:
29+
- toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
30+
-
31+
- target = self.get_linux_target(platform, arch)
32+
+ if platform == "linux":
33+
+ maybe_sysroot = self.get_linux_sysroot(platform, arch)
34+
+ if maybe_sysroot is not None:
35+
+ toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
36+
+
37+
+ target = self.get_linux_target(platform, arch)
38+
+ elif platform == "android":
39+
+ target = '%s-unknown-linux-android%s' % (self.args.android_arch,
40+
+ self.args.android_api_level)
41+
if self.toolchain.cc.endswith('clang'):
42+
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
43+
if self.toolchain.cxx.endswith('clang++'):
44+
@@ -466,9 +479,19 @@ class Product(object):
45+
platform, arch,
46+
macos_deployment_version=override_macos_deployment_version)
47+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
48+
- elif platform == "linux":
49+
- toolchain_file = self.generate_linux_toolchain_file(platform, arch)
50+
+ elif platform == "linux" or platform == "android":
51+
+ # Always cross-compile for linux, but not on Android, as a native
52+
+ # compile on Android does not use the NDK and the CMake config.
53+
+ cross_compile = platform == "linux" or self.is_cross_compile_target(host_target)
54+
+ toolchain_file = self.generate_linux_toolchain_file(platform, arch, cross_compile)
55+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
56+
+ if cross_compile and platform == "android":
57+
+ resource_dir = None
58+
+ if not self.is_before_build_script_impl_product() and not self.is_build_script_impl_product():
59+
+ install_path = self.host_install_destdir(host_target) + self.args.install_prefix
60+
+ resource_dir = '%s/lib/swift' % install_path
61+
+ flags = targets.StdlibDeploymentTarget.get_target_for_name(host_target).platform.swift_flags(self.args, resource_dir)
62+
+ self.cmake_options.define('CMAKE_Swift_FLAGS', flags)
63+
64+
return toolchain_file
65+
66+
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
67+
index 417056efdd0..177ea9f0623 100644
68+
--- a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
69+
+++ b/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
70+
@@ -127,3 +127,11 @@ class SwiftTestingCMakeShim(cmake_product.CMakeProduct):
71+
install_prefix = install_destdir + self.args.install_prefix
72+
73+
self.install_with_cmake(['install'], install_prefix)
74+
+
75+
+ @classmethod
76+
+ def is_build_script_impl_product(cls):
77+
+ return False
78+
+
79+
+ @classmethod
80+
+ def is_before_build_script_impl_product(cls):
81+
+ return False
82+
diff --git a/swift/utils/swift_build_support/swift_build_support/targets.py b/swift/utils/swift_build_support/swift_build_support/targets.py
83+
index fba09416ddb..67b81daba12 100644
84+
--- a/swift/utils/swift_build_support/swift_build_support/targets.py
85+
+++ b/swift/utils/swift_build_support/swift_build_support/targets.py
86+
@@ -72,7 +72,7 @@ class Platform(object):
87+
return True
88+
return False
89+
90+
- def swift_flags(self, args):
91+
+ def swift_flags(self, args, resource_path=None):
92+
"""
93+
Swift compiler flags for a platform, useful for cross-compiling
94+
"""
95+
@@ -154,12 +154,15 @@ class AndroidPlatform(Platform):
96+
"""
97+
return True
98+
99+
- def swift_flags(self, args):
100+
+ def swift_flags(self, args, resource_path=None):
101+
flags = '-target %s-unknown-linux-android%s ' % (args.android_arch,
102+
args.android_api_level)
103+
104+
- flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
105+
- args.build_root, self.name, args.android_arch)
106+
+ if resource_path is not None:
107+
+ flags += '-resource-dir %s ' % (resource_path)
108+
+ else:
109+
+ flags += '-resource-dir %s/swift-%s-%s/lib/swift ' % (
110+
+ args.build_root, self.name, args.android_arch)
111+
112+
android_toolchain_path = self.ndk_toolchain_path(args)
113+

swift-android.patch

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,6 @@ index 16e05052609..7ab8cebfab8 100755
1010

1111
cmake_options=(
1212
-DENABLE_SWIFT=YES
13-
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
14-
index 324d1a77eea..e88601a8701 100644
15-
--- a/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
16-
+++ b/swift/utils/swift_build_support/swift_build_support/products/swift_testing.py
17-
@@ -13,6 +13,9 @@
18-
import os
19-
20-
from build_swift.build_swift.versions import Version
21-
+from ..host_specific_configuration \
22-
+ import HostSpecificConfiguration
23-
+from ..targets import StdlibDeploymentTarget
24-
25-
from . import cmake_product
26-
from . import product
27-
@@ -115,6 +117,25 @@ class SwiftTestingCMakeShim(cmake_product.CMakeProduct):
28-
# FIXME: If we build macros for the builder, specify the path.
29-
self.cmake_options.define('SwiftTesting_MACRO', 'NO')
30-
31-
+ if host_target.startswith('android') and self.is_cross_compile_target(host_target):
32-
+ host_config = HostSpecificConfiguration(host_target, self.args)
33-
+ self.cmake_options.extend(host_config.cmake_options)
34-
+ triple = '%s-unknown-linux-android%s' % (self.args.android_arch,
35-
+ self.args.android_api_level)
36-
+ flags = '-target %s ' % (triple)
37-
+
38-
+ flags += '-resource-dir %s/lib/swift ' % (
39-
+ self.host_install_destdir(host_target) + self.args.install_prefix)
40-
+
41-
+ ndk_path = StdlibDeploymentTarget.get_target_for_name(host_target).platform.ndk_toolchain_path(self.args)
42-
+ flags += '-sdk %s/sysroot ' % (ndk_path)
43-
+ flags += '-tools-directory %s/bin' % (ndk_path)
44-
+ self.cmake_options.define('CMAKE_Swift_FLAGS', flags)
45-
+ self.cmake_options.define('CMAKE_Swift_COMPILER_TARGET', triple)
46-
+ self.cmake_options.define('CMAKE_CXX_COMPILER_WORKS', 'True')
47-
+ self.cmake_options.define('CMAKE_SHARED_LINKER_FLAGS', '')
48-
+ self.cmake_options.define('CMAKE_FIND_ROOT_PATH', self.args.cross_compile_deps_path)
49-
+
50-
self.generate_toolchain_file_for_darwin_or_linux(
51-
host_target, override_macos_deployment_version=override_deployment_version)
52-
self.build_with_cmake([], self.args.build_variant, [],
5313
diff --git a/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt b/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt
5414
index 016bf294..5c42986a 100644
5515
--- a/swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt

0 commit comments

Comments
 (0)