Skip to content

Commit c6acc42

Browse files
committed
Support cross compile Xcode toolchain for Apple Silicon
* Add CMake flag DCMAKE_OSX_ARCHITECTURES to LLVM * Add CMake flag DCMAKE_OSX_ARCHITECTURES to cmark * Add CMake flag DCMAKE_OSX_ARCHITECTURES to lldb * Add CMake flag DCMAKE_OSX_ARCHITECTURES to llbuild * Add llbuild CMake options array to provide DCMAKE_OSX_ARCHITECTURES * [Build System] Use one install package for cross compile hosts * Remove Lipo before non-build-script-impl products * Add support to only lipo without running installable package tests * [Build System] Support cross compile install prefix for SwiftPM product in Swift Build Support * Use cross compile toolchain path for indexstoredb and swift-driver * Use cross compile toolchain path for swiftpm, swiftsyntax, swiftformat, and skstresstester * Add cross compile toolchain support to Benchmarks, and fix the python lint issue in skstresstester.py * [SwiftPM] Add support for cross-compile-hosts flag to build swiftpm using bootstrap script (cherry picked from commit bf7c03b)
1 parent 7e770fd commit c6acc42

File tree

9 files changed

+79
-22
lines changed

9 files changed

+79
-22
lines changed

utils/build-presets.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,9 @@ compiler-vendor=apple
12141214

12151215
dash-dash
12161216

1217+
# Cross compile for Apple Silicon
1218+
cross-compile-hosts=macosx-arm64
1219+
12171220
lldb-no-debugserver
12181221
lldb-use-system-debugserver
12191222
lldb-build-type=Release

utils/build-script

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,9 @@ class BuildScriptInvocation(object):
919919
for product_class in impl_product_classes:
920920
self._execute_install_action(host_target, product_class)
921921

922+
# Core Lipo...
923+
self._execute_merged_host_lipo_core_action()
924+
922925
# Non-build-script-impl products...
923926
# Note: currently only supports building for the host.
924927
for host_target in [self.args.host_target]:
@@ -986,6 +989,9 @@ class BuildScriptInvocation(object):
986989
def _execute_merged_host_lipo_action(self):
987990
self._execute_action("merged-hosts-lipo")
988991

992+
def _execute_merged_host_lipo_core_action(self):
993+
self._execute_action("merged-hosts-lipo-core")
994+
989995
def _execute_action(self, action_name):
990996
shell.call_without_sleeping(
991997
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +

utils/build-script-impl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ function set_build_options_for_host() {
446446
swift_cmake_options=()
447447
cmark_cmake_options=()
448448
lldb_cmake_options=()
449+
llbuild_cmake_options=()
449450
SWIFT_HOST_VARIANT=
450451
SWIFT_HOST_VARIANT_SDK=
451452
SWIFT_HOST_VARIANT_ARCH=
@@ -656,6 +657,7 @@ function set_build_options_for_host() {
656657
-DCMAKE_CXX_FLAGS="$(cmark_c_flags ${host})"
657658
-DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
658659
-DCMAKE_OSX_DEPLOYMENT_TARGET="${cmake_osx_deployment_target}"
660+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
659661
)
660662
llvm_cmake_options=(
661663
-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING="${cmake_osx_deployment_target}"
@@ -665,6 +667,7 @@ function set_build_options_for_host() {
665667
-DCOMPILER_RT_ENABLE_TVOS:BOOL=FALSE
666668
-DSANITIZER_MIN_OSX_VERSION="${cmake_osx_deployment_target}"
667669
-DLLVM_ENABLE_MODULES:BOOL="$(true_false ${LLVM_ENABLE_MODULES})"
670+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
668671
)
669672
if [[ $(is_llvm_lto_enabled) == "TRUE" ]]; then
670673
llvm_cmake_options+=(
@@ -693,6 +696,14 @@ function set_build_options_for_host() {
693696
# in the compiler checks CMake performs
694697
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
695698
)
699+
700+
lldb_cmake_options+=(
701+
-DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
702+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
703+
)
704+
llbuild_cmake_options+=(
705+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
706+
)
696707
;;
697708
esac
698709

@@ -1992,6 +2003,7 @@ for host in "${ALL_HOSTS[@]}"; do
19922003
llbuild)
19932004
cmake_options=(
19942005
"${cmake_options[@]}"
2006+
"${llbuild_cmake_options[@]}"
19952007

19962008
-DCMAKE_BUILD_TYPE:STRING="${LLBUILD_BUILD_TYPE}"
19972009
-DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
@@ -2986,11 +2998,7 @@ function build_and_test_installable_package() {
29862998
local host_install_destdir="$(get_host_install_destdir ${host})"
29872999
local host_install_prefix="$(get_host_install_prefix ${host})"
29883000

2989-
if [[ $(has_cross_compile_hosts) ]]; then
2990-
package_for_host="${INSTALLABLE_PACKAGE}-${host}"
2991-
else
2992-
package_for_host="${INSTALLABLE_PACKAGE}"
2993-
fi
3001+
package_for_host="${INSTALLABLE_PACKAGE}"
29943002

29953003
echo "--- Creating installable package ---"
29963004
echo "-- Package file: ${package_for_host} --"
@@ -3060,7 +3068,7 @@ function build_and_test_installable_package() {
30603068
PKG_TESTS_SANDBOX_PARENT="$(build_directory swift_package_sandbox_${host} none)"
30613069
PKG_TESTS_TEMPS="${PKG_TESTS_SANDBOX_PARENT}"/"tests"
30623070

3063-
if [[ "${host}" == "macosx-"* ]] ; then
3071+
if [[ "${host}" == "macosx-"* ]] || [[ "${host}" == "merged-hosts" ]]; then
30643072
PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"/"${TOOLCHAIN_PREFIX}"
30653073
else # Linux
30663074
PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"
@@ -3101,7 +3109,7 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then
31013109
# This is from multiple hosts; Which host should we say it is?
31023110
# Let's call it 'merged-hosts' so that we can identify it.
31033111

3104-
if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then
3112+
if [[ $(should_execute_action "${mergedHost}-lipo") || $(should_execute_action "${mergedHost}-lipo-core") ]]; then
31053113
# Allow passing lipo with --host-lipo
31063114
if [[ -z "${HOST_LIPO}" ]] ; then
31073115
LIPO_PATH=$(xcrun_find_tool lipo)
@@ -3110,8 +3118,10 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then
31103118
fi
31113119
call "${SWIFT_SOURCE_DIR}"/utils/recursive-lipo --lipo=${LIPO_PATH} --copy-subdirs="$(get_host_install_prefix ${host})lib/swift $(get_host_install_prefix ${host})lib/swift_static" --destination="$(get_host_install_destdir ${mergedHost})" ${LIPO_SRC_DIRS[@]}
31123120

3113-
# Build and test the lipo-ed package.
3114-
build_and_test_installable_package ${mergedHost}
3121+
if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then
3122+
# Build and test the lipo-ed package.
3123+
build_and_test_installable_package ${mergedHost}
3124+
fi
31153125
fi
31163126
fi
31173127
# END

utils/swift_build_support/swift_build_support/products/benchmarks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def install(self, host_target):
5959

6060

6161
def run_build_script_helper(host_target, product, args):
62-
toolchain_path = args.install_destdir
62+
toolchain_path = swiftpm.SwiftPM.get_install_destdir(args,
63+
host_target,
64+
product.build_dir)
6365
if platform.system() == 'Darwin':
6466
# The prefix is an absolute path, so concatenate without os.path.
6567
toolchain_path += \

utils/swift_build_support/swift_build_support/products/indexstoredb.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ def run_build_script_helper(action, host_target, product, args,
5151
script_path = os.path.join(
5252
product.source_dir, 'Utilities', 'build-script-helper.py')
5353

54-
toolchain_path = targets.toolchain_path(args.install_destdir,
54+
install_destdir = args.install_destdir
55+
if swiftpm.SwiftPM.has_cross_compile_hosts(args):
56+
install_destdir = swiftpm.SwiftPM.get_install_destdir(args,
57+
host_target,
58+
product.build_dir)
59+
toolchain_path = targets.toolchain_path(install_destdir,
5560
args.install_prefix)
5661
is_release = product.is_release()
5762
configuration = 'release' if is_release else 'debug'

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# ----------------------------------------------------------------------------
1212

1313
import abc
14+
import os
1415

1516
from .. import cmake
1617
from .. import targets
@@ -136,13 +137,17 @@ def is_release(self):
136137
"""
137138
return is_release_variant(self.args.build_variant)
138139

139-
def install_toolchain_path(self):
140+
def install_toolchain_path(self, host_target):
140141
"""toolchain_path() -> string
141142
142143
Returns the path to the toolchain that is being created as part of this
143144
build.
144145
"""
145-
return targets.toolchain_path(self.args.install_destdir,
146+
install_destdir = self.args.install_destdir
147+
if self.args.cross_compile_hosts:
148+
build_root = os.path.dirname(self.build_dir)
149+
install_destdir = '%s/intermediate-install/%s' % (build_root, host_target)
150+
return targets.toolchain_path(install_destdir,
146151
self.args.install_prefix)
147152

148153

utils/swift_build_support/swift_build_support/products/skstresstester.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def is_swiftpm_unified_build_product(cls):
4040
def package_name(self):
4141
return 'SourceKitStressTester'
4242

43-
def run_build_script_helper(self, action, additional_params=[]):
43+
def run_build_script_helper(self, action, host_target, additional_params=[]):
4444
script_path = os.path.join(
4545
self.source_dir, 'build-script-helper.py')
4646

@@ -50,7 +50,7 @@ def run_build_script_helper(self, action, additional_params=[]):
5050
script_path,
5151
action,
5252
'--package-dir', self.package_name(),
53-
'--toolchain', self.install_toolchain_path(),
53+
'--toolchain', self.install_toolchain_path(host_target),
5454
'--config', configuration,
5555
'--build-dir', self.build_dir,
5656
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
@@ -74,19 +74,22 @@ def build(self, host_target):
7474
"than Darwin".format(
7575
product=self.package_name()))
7676

77-
self.run_build_script_helper('build')
77+
self.run_build_script_helper('build', host_target)
7878

7979
def should_test(self, host_target):
8080
return self.args.test_skstresstester
8181

8282
def test(self, host_target):
83-
self.run_build_script_helper('test')
83+
self.run_build_script_helper('test', host_target)
8484

8585
def should_install(self, host_target):
8686
return self.args.install_skstresstester
8787

8888
def install(self, host_target):
89-
install_prefix = self.args.install_destdir + self.args.install_prefix
90-
self.run_build_script_helper('install', [
89+
install_destdir = swiftpm.SwiftPM.get_install_destdir(self.args,
90+
host_target,
91+
self.build_dir)
92+
install_prefix = install_destdir + self.args.install_prefix
93+
self.run_build_script_helper('install', host_target, [
9194
'--prefix', install_prefix
9295
])

utils/swift_build_support/swift_build_support/products/swiftpm.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def should_build(self, host_target):
3131
def run_bootstrap_script(self, action, host_target, additional_params=[]):
3232
script_path = os.path.join(
3333
self.source_dir, 'Utilities', 'bootstrap')
34-
toolchain_path = self.install_toolchain_path()
34+
toolchain_path = self.install_toolchain_path(host_target)
3535
swiftc = os.path.join(toolchain_path, "bin", "swiftc")
3636

3737
# FIXME: We require llbuild build directory in order to build. Is
@@ -53,6 +53,13 @@ def run_bootstrap_script(self, action, host_target, additional_params=[]):
5353
"--build-dir", self.build_dir,
5454
"--llbuild-build-dir", llbuild_build_dir
5555
]
56+
57+
# Pass Cross compile host info
58+
if self.has_cross_compile_hosts(self.args):
59+
helper_cmd += ['--cross-compile-hosts']
60+
for cross_compile_host in self.args.cross_compile_hosts:
61+
helper_cmd += [cross_compile_host]
62+
5663
helper_cmd.extend(additional_params)
5764

5865
shell.call(helper_cmd)
@@ -69,8 +76,24 @@ def test(self, host_target):
6976
def should_install(self, host_target):
7077
return self.args.install_swiftpm
7178

79+
@classmethod
80+
def has_cross_compile_hosts(self, args):
81+
return args.cross_compile_hosts
82+
83+
@classmethod
84+
def get_install_destdir(self, args, host_target, build_dir):
85+
install_destdir = args.install_destdir
86+
if self.has_cross_compile_hosts(args):
87+
build_root = os.path.dirname(build_dir)
88+
install_destdir = '%s/intermediate-install/%s' % (build_root, host_target)
89+
return install_destdir
90+
7291
def install(self, host_target):
73-
install_prefix = self.args.install_destdir + self.args.install_prefix
92+
install_destdir = self.get_install_destdir(self.args,
93+
host_target,
94+
self.build_dir)
95+
install_prefix = install_destdir + self.args.install_prefix
96+
7497
self.run_bootstrap_script('install', host_target, [
7598
'--prefix', install_prefix
7699
])

utils/swift_build_support/swift_build_support/products/swiftsyntax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def run_swiftsyntax_build_script(self, target, additional_params=[]):
4545
script_path,
4646
'--build-dir', self.build_dir,
4747
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
48-
'--toolchain', self.install_toolchain_path(),
48+
'--toolchain', self.install_toolchain_path(target),
4949
'--filecheck-exec', os.path.join(llvm_build_dir, 'bin',
5050
'FileCheck'),
5151
]

0 commit comments

Comments
 (0)