Skip to content

Commit bf7c03b

Browse files
authored
Support cross compile Xcode toolchain for Apple Silicon
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
1 parent 773eec1 commit bf7c03b

File tree

10 files changed

+80
-28
lines changed

10 files changed

+80
-28
lines changed

utils/build-presets.ini

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

12131213
dash-dash
12141214

1215+
# Cross compile for Apple Silicon
1216+
cross-compile-hosts=macosx-arm64
1217+
12151218
lldb-no-debugserver
12161219
lldb-use-system-debugserver
12171220
lldb-build-type=Release

utils/build-script

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,8 @@ class BuildScriptInvocation(object):
942942
for product_class in impl_product_classes:
943943
self._execute_install_action(host_target, product_class)
944944

945-
# Lipo...
946-
self._execute_merged_host_lipo_action()
945+
# Core Lipo...
946+
self._execute_merged_host_lipo_core_action()
947947

948948
# Non-build-script-impl products...
949949
# Note: currently only supports building for the host.
@@ -987,6 +987,9 @@ class BuildScriptInvocation(object):
987987
for host_target in all_hosts:
988988
self._execute_package_action(host_target)
989989

990+
# Lipo...
991+
self._execute_merged_host_lipo_action()
992+
990993
def _execute_build_action(self, host_target, product_class):
991994
action_name = "{}-{}-build".format(host_target.name,
992995
product_class.product_name())
@@ -1013,6 +1016,9 @@ class BuildScriptInvocation(object):
10131016
def _execute_merged_host_lipo_action(self):
10141017
self._execute_action("merged-hosts-lipo")
10151018

1019+
def _execute_merged_host_lipo_core_action(self):
1020+
self._execute_action("merged-hosts-lipo-core")
1021+
10161022
def _execute_action(self, action_name):
10171023
shell.call_without_sleeping(
10181024
[BUILD_SCRIPT_IMPL_PATH] + self.impl_args +

utils/build-script-impl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ function set_build_options_for_host() {
425425
swift_cmake_options=()
426426
cmark_cmake_options=()
427427
lldb_cmake_options=()
428+
llbuild_cmake_options=()
428429
SWIFT_HOST_VARIANT=
429430
SWIFT_HOST_VARIANT_SDK=
430431
SWIFT_HOST_VARIANT_ARCH=
@@ -646,6 +647,7 @@ function set_build_options_for_host() {
646647
-DCMAKE_CXX_FLAGS="$(cmark_c_flags ${host})"
647648
-DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
648649
-DCMAKE_OSX_DEPLOYMENT_TARGET="${cmake_osx_deployment_target}"
650+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
649651
)
650652
llvm_cmake_options=(
651653
-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING="${cmake_osx_deployment_target}"
@@ -655,6 +657,7 @@ function set_build_options_for_host() {
655657
-DCOMPILER_RT_ENABLE_TVOS:BOOL=FALSE
656658
-DSANITIZER_MIN_OSX_VERSION="${cmake_osx_deployment_target}"
657659
-DLLVM_ENABLE_MODULES:BOOL="$(true_false ${LLVM_ENABLE_MODULES})"
660+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
658661
)
659662
if [[ $(is_llvm_lto_enabled) == "TRUE" ]]; then
660663
llvm_cmake_options+=(
@@ -686,6 +689,10 @@ function set_build_options_for_host() {
686689

687690
lldb_cmake_options+=(
688691
-DCMAKE_OSX_SYSROOT:PATH="${cmake_os_sysroot}"
692+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
693+
)
694+
llbuild_cmake_options+=(
695+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
689696
)
690697
;;
691698
esac
@@ -2025,6 +2032,7 @@ for host in "${ALL_HOSTS[@]}"; do
20252032
llbuild)
20262033
cmake_options=(
20272034
"${cmake_options[@]}"
2035+
"${llbuild_cmake_options[@]}"
20282036

20292037
-DCMAKE_BUILD_TYPE:STRING="${LLBUILD_BUILD_TYPE}"
20302038
-DCMAKE_C_COMPILER:PATH="${CLANG_BIN}/clang"
@@ -3031,11 +3039,7 @@ function build_and_test_installable_package() {
30313039
local host_install_destdir="$(get_host_install_destdir ${host})"
30323040
local host_install_prefix="$(get_host_install_prefix ${host})"
30333041

3034-
if [[ $(has_cross_compile_hosts) ]]; then
3035-
package_for_host="${INSTALLABLE_PACKAGE}-${host}"
3036-
else
3037-
package_for_host="${INSTALLABLE_PACKAGE}"
3038-
fi
3042+
package_for_host="${INSTALLABLE_PACKAGE}"
30393043

30403044
echo "--- Creating installable package ---"
30413045
echo "-- Package file: ${package_for_host} --"
@@ -3110,7 +3114,7 @@ function build_and_test_installable_package() {
31103114
PKG_TESTS_SANDBOX_PARENT="$(build_directory swift_package_sandbox_${host} none)"
31113115
PKG_TESTS_TEMPS="${PKG_TESTS_SANDBOX_PARENT}"/"tests"
31123116

3113-
if [[ "${host}" == "macosx-"* ]] ; then
3117+
if [[ "${host}" == "macosx-"* ]] || [[ "${host}" == "merged-hosts" ]]; then
31143118
PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"/"${TOOLCHAIN_PREFIX}"
31153119
else # Linux
31163120
PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"
@@ -3151,7 +3155,7 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then
31513155
# This is from multiple hosts; Which host should we say it is?
31523156
# Let's call it 'merged-hosts' so that we can identify it.
31533157

3154-
if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then
3158+
if [[ $(should_execute_action "${mergedHost}-lipo") || $(should_execute_action "${mergedHost}-lipo-core") ]]; then
31553159
# Allow passing lipo with --host-lipo
31563160
if [[ -z "${HOST_LIPO}" ]] ; then
31573161
LIPO_PATH=$(xcrun_find_tool lipo)
@@ -3160,8 +3164,10 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then
31603164
fi
31613165
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[@]}
31623166

3163-
# Build and test the lipo-ed package.
3164-
build_and_test_installable_package ${mergedHost}
3167+
if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then
3168+
# Build and test the lipo-ed package.
3169+
build_and_test_installable_package ${mergedHost}
3170+
fi
31653171
fi
31663172
fi
31673173
# 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
@@ -82,7 +82,9 @@ def get_dependencies(cls):
8282

8383

8484
def run_build_script_helper(host_target, product, args):
85-
toolchain_path = args.install_destdir
85+
toolchain_path = swiftpm.SwiftPM.get_install_destdir(args,
86+
host_target,
87+
product.build_dir)
8688
if platform.system() == 'Darwin':
8789
# The prefix is an absolute path, so concatenate without os.path.
8890
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
@@ -76,7 +76,12 @@ def run_build_script_helper(action, host_target, product, args,
7676
script_path = os.path.join(
7777
product.source_dir, 'Utilities', 'build-script-helper.py')
7878

79-
toolchain_path = targets.toolchain_path(args.install_destdir,
79+
install_destdir = args.install_destdir
80+
if swiftpm.SwiftPM.has_cross_compile_hosts(args):
81+
install_destdir = swiftpm.SwiftPM.get_install_destdir(args,
82+
host_target,
83+
product.build_dir)
84+
toolchain_path = targets.toolchain_path(install_destdir,
8085
args.install_prefix)
8186
is_release = product.is_release()
8287
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
@@ -162,13 +163,17 @@ def is_release(self):
162163
"""
163164
return is_release_variant(self.args.build_variant)
164165

165-
def install_toolchain_path(self):
166+
def install_toolchain_path(self, host_target):
166167
"""toolchain_path() -> string
167168
168169
Returns the path to the toolchain that is being created as part of this
169170
build.
170171
"""
171-
return targets.toolchain_path(self.args.install_destdir,
172+
install_destdir = self.args.install_destdir
173+
if self.args.cross_compile_hosts:
174+
build_root = os.path.dirname(self.build_dir)
175+
install_destdir = '%s/intermediate-install/%s' % (build_root, host_target)
176+
return targets.toolchain_path(install_destdir,
172177
self.args.install_prefix)
173178

174179

utils/swift_build_support/swift_build_support/products/skstresstester.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def is_swiftpm_unified_build_product(cls):
5050
def package_name(self):
5151
return 'SourceKitStressTester'
5252

53-
def run_build_script_helper(self, action, additional_params=[]):
53+
def run_build_script_helper(self, action, host_target, additional_params=[]):
5454
script_path = os.path.join(
5555
self.source_dir, 'build-script-helper.py')
5656

@@ -60,7 +60,7 @@ def run_build_script_helper(self, action, additional_params=[]):
6060
script_path,
6161
action,
6262
'--package-dir', self.package_name(),
63-
'--toolchain', self.install_toolchain_path(),
63+
'--toolchain', self.install_toolchain_path(host_target),
6464
'--config', configuration,
6565
'--build-dir', self.build_dir,
6666
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
@@ -84,20 +84,23 @@ def build(self, host_target):
8484
"than Darwin".format(
8585
product=self.package_name()))
8686

87-
self.run_build_script_helper('build')
87+
self.run_build_script_helper('build', host_target)
8888

8989
def should_test(self, host_target):
9090
return self.args.test_skstresstester
9191

9292
def test(self, host_target):
93-
self.run_build_script_helper('test')
93+
self.run_build_script_helper('test', host_target)
9494

9595
def should_install(self, host_target):
9696
return self.args.install_skstresstester
9797

9898
def install(self, host_target):
99-
install_prefix = self.args.install_destdir + self.args.install_prefix
100-
self.run_build_script_helper('install', [
99+
install_destdir = swiftpm.SwiftPM.get_install_destdir(self.args,
100+
host_target,
101+
self.build_dir)
102+
install_prefix = install_destdir + self.args.install_prefix
103+
self.run_build_script_helper('install', host_target, [
101104
'--prefix', install_prefix
102105
])
103106

utils/swift_build_support/swift_build_support/products/swiftformat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def is_build_script_impl_product(cls):
4646
def is_swiftpm_unified_build_product(cls):
4747
return True
4848

49-
def run_build_script_helper(self, action, additional_params=[]):
49+
def run_build_script_helper(self, action, host_target, additional_params=[]):
5050
script_path = os.path.join(
5151
self.source_dir, 'build-script-helper.py')
5252

@@ -55,7 +55,7 @@ def run_build_script_helper(self, action, additional_params=[]):
5555
helper_cmd = [
5656
script_path,
5757
action,
58-
'--toolchain', self.install_toolchain_path(),
58+
'--toolchain', self.install_toolchain_path(host_target),
5959
'--configuration', configuration,
6060
'--build-path', self.build_dir,
6161
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
@@ -74,13 +74,13 @@ def should_build(self, host_target):
7474
return True
7575

7676
def build(self, host_target):
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_swiftformat
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 False

utils/swift_build_support/swift_build_support/products/swiftpm.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def should_build(self, host_target):
4040
def run_bootstrap_script(self, action, host_target, additional_params=[]):
4141
script_path = os.path.join(
4242
self.source_dir, 'Utilities', 'bootstrap')
43-
toolchain_path = self.install_toolchain_path()
43+
toolchain_path = self.install_toolchain_path(host_target)
4444
swiftc = os.path.join(toolchain_path, "bin", "swiftc")
4545

4646
# FIXME: We require llbuild build directory in order to build. Is
@@ -86,6 +86,12 @@ def run_bootstrap_script(self, action, host_target, additional_params=[]):
8686
"--foundation-build-dir", foundation_build_dir
8787
]
8888

89+
# Pass Cross compile host info
90+
if self.has_cross_compile_hosts(self.args):
91+
helper_cmd += ['--cross-compile-hosts']
92+
for cross_compile_host in self.args.cross_compile_hosts:
93+
helper_cmd += [cross_compile_host]
94+
8995
helper_cmd.extend(additional_params)
9096

9197
shell.call(helper_cmd)
@@ -108,8 +114,24 @@ def clean(self, host_target):
108114
def should_install(self, host_target):
109115
return self.args.install_swiftpm
110116

117+
@classmethod
118+
def has_cross_compile_hosts(self, args):
119+
return args.cross_compile_hosts
120+
121+
@classmethod
122+
def get_install_destdir(self, args, host_target, build_dir):
123+
install_destdir = args.install_destdir
124+
if self.has_cross_compile_hosts(args):
125+
build_root = os.path.dirname(build_dir)
126+
install_destdir = '%s/intermediate-install/%s' % (build_root, host_target)
127+
return install_destdir
128+
111129
def install(self, host_target):
112-
install_prefix = self.args.install_destdir + self.args.install_prefix
130+
install_destdir = self.get_install_destdir(self.args,
131+
host_target,
132+
self.build_dir)
133+
install_prefix = install_destdir + self.args.install_prefix
134+
113135
self.run_bootstrap_script('install', host_target, [
114136
'--prefix', install_prefix
115137
])

utils/swift_build_support/swift_build_support/products/swiftsyntax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def run_swiftsyntax_build_script(self, target, additional_params=[]):
5555
script_path,
5656
'--build-dir', self.build_dir,
5757
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
58-
'--toolchain', self.install_toolchain_path(),
58+
'--toolchain', self.install_toolchain_path(target),
5959
'--filecheck-exec', os.path.join(llvm_build_dir, 'bin',
6060
'FileCheck'),
6161
]

0 commit comments

Comments
 (0)