Skip to content

Commit 45e46eb

Browse files
shahmishalDavide Italiano
andauthored
[5.3] Support cross compile Xcode toolchain for Apple Silicon (#33780)
* 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) * Remove DCMAKE_OSX_SYSROOT from LLDB CMake flag The cherry-picked added DCMAKE_OSX_SYSROOT flag when it should not have. * Don't build SwiftUnitTests for cross compile host (cherry picked from commit ceeb724) * Don't run LLDB cross compile host test suite (cherry picked from commit bb356d3) * Remove compiler-rt from install targets for cross compile host (cherry picked from commit 9adbace) * [Package] When building a toolchain, don't copy the Resource directory. Should dramatically improve the size of the snapshost produced, as we don't have stdlib et similia included twice. Also, it helps when producing fat binaries, as we don't have to implement any mechanism for sandwiching all the slices together. (cherry picked from commit 8337f96) * Add missing imports for swiftpm in the products class Co-authored-by: Davide Italiano <[email protected]>
1 parent 377a3bd commit 45e46eb

File tree

9 files changed

+107
-33
lines changed

9 files changed

+107
-33
lines changed

utils/build-presets.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,13 +1214,21 @@ 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
12201223
verbose-build
12211224
build-ninja
12221225
build-swift-stdlib-unittest-extra
12231226

1227+
# When producing a package, don't copy the Swift Resource/ directory.
1228+
# This is mainly a space optimization.
1229+
extra-cmake-options=
1230+
-DLLDB_FRAMEWORK_COPY_SWIFT_RESOURCES=0
1231+
12241232
install-swift
12251233
install-lldb
12261234
install-llbuild

utils/build-script

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

922-
# Lipo...
923-
self._execute_merged_host_lipo_action()
922+
# Core Lipo...
923+
self._execute_merged_host_lipo_core_action()
924924

925925
# Non-build-script-impl products...
926926
# Note: currently only supports building for the host.
@@ -960,6 +960,9 @@ class BuildScriptInvocation(object):
960960
for host_target in all_hosts:
961961
self._execute_package_action(host_target)
962962

963+
# Lipo...
964+
self._execute_merged_host_lipo_action()
965+
963966
def _execute_build_action(self, host_target, product_class):
964967
action_name = "{}-{}-build".format(host_target.name,
965968
product_class.product_name())
@@ -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: 37 additions & 18 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,13 @@ 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_ARCHITECTURES="${architecture}"
702+
)
703+
llbuild_cmake_options+=(
704+
-DCMAKE_OSX_ARCHITECTURES="${architecture}"
705+
)
696706
;;
697707
esac
698708

@@ -1992,6 +2002,7 @@ for host in "${ALL_HOSTS[@]}"; do
19922002
llbuild)
19932003
cmake_options=(
19942004
"${cmake_options[@]}"
2005+
"${llbuild_cmake_options[@]}"
19952006

19962007
-DCMAKE_BUILD_TYPE:STRING="${LLBUILD_BUILD_TYPE}"
19972008
-DCMAKE_INSTALL_PREFIX:PATH="$(get_host_install_prefix ${host})"
@@ -2433,13 +2444,15 @@ for host in "${ALL_HOSTS[@]}"; do
24332444
executable_target=
24342445
results_targets=
24352446
if ! [[ "${SKIP_TEST_SWIFT}" ]]; then
2436-
executable_target=SwiftUnitTests
2437-
results_targets=("${SWIFT_TEST_TARGETS[@]}")
2438-
if [[ "${STRESS_TEST_SOURCEKIT}" ]]; then
2439-
results_targets=(
2440-
"${results_targets[@]}"
2441-
stress-SourceKit
2442-
)
2447+
if ! [[ $(is_cross_tools_host ${host}) ]] ; then
2448+
executable_target=SwiftUnitTests
2449+
results_targets=("${SWIFT_TEST_TARGETS[@]}")
2450+
if [[ "${STRESS_TEST_SOURCEKIT}" ]]; then
2451+
results_targets=(
2452+
"${results_targets[@]}"
2453+
stress-SourceKit
2454+
)
2455+
fi
24432456
fi
24442457
fi
24452458
if ! [[ "${SKIP_TEST_BENCHMARKS}" ]]; then
@@ -2456,6 +2469,10 @@ for host in "${ALL_HOSTS[@]}"; do
24562469
if [[ "${SKIP_TEST_LLDB}" ]]; then
24572470
continue
24582471
fi
2472+
if [[ $(is_cross_tools_host ${host}) ]]; then
2473+
echo "--- Can't execute tests for ${host}, skipping... ---"
2474+
continue
2475+
fi
24592476
llvm_build_dir=$(build_directory ${host} llvm)
24602477
lldb_build_dir=$(build_directory ${host} lldb)
24612478
results_dir="${lldb_build_dir}/test-results"
@@ -2758,8 +2775,12 @@ for host in "${ALL_HOSTS[@]}"; do
27582775
fi
27592776
if [[ "${LLVM_INSTALL_COMPONENTS}" == "all" ]]; then
27602777
INSTALL_TARGETS=install
2761-
else
2762-
INSTALL_TARGETS=install-$(echo ${LLVM_INSTALL_COMPONENTS} | sed -E 's/;/ install-/g')
2778+
elif [[ -n "${LLVM_INSTALL_COMPONENTS}" ]] ; then
2779+
if [[ $(is_cross_tools_host ${host}) && "${LLVM_INSTALL_COMPONENTS}" == *"compiler-rt"* ]]; then
2780+
INSTALL_TARGETS=install-$(echo ${LLVM_INSTALL_COMPONENTS} | sed -E 's/compiler-rt;//g' |sed -E 's/;/ install-/g')
2781+
else
2782+
INSTALL_TARGETS=install-$(echo ${LLVM_INSTALL_COMPONENTS} | sed -E 's/;/ install-/g')
2783+
fi
27632784
fi
27642785
;;
27652786
libcxx)
@@ -2986,11 +3007,7 @@ function build_and_test_installable_package() {
29863007
local host_install_destdir="$(get_host_install_destdir ${host})"
29873008
local host_install_prefix="$(get_host_install_prefix ${host})"
29883009

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

29953012
echo "--- Creating installable package ---"
29963013
echo "-- Package file: ${package_for_host} --"
@@ -3060,7 +3077,7 @@ function build_and_test_installable_package() {
30603077
PKG_TESTS_SANDBOX_PARENT="$(build_directory swift_package_sandbox_${host} none)"
30613078
PKG_TESTS_TEMPS="${PKG_TESTS_SANDBOX_PARENT}"/"tests"
30623079

3063-
if [[ "${host}" == "macosx-"* ]] ; then
3080+
if [[ "${host}" == "macosx-"* ]] || [[ "${host}" == "merged-hosts" ]]; then
30643081
PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"/"${TOOLCHAIN_PREFIX}"
30653082
else # Linux
30663083
PKG_TESTS_SANDBOX="${PKG_TESTS_SANDBOX_PARENT}"
@@ -3101,7 +3118,7 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then
31013118
# This is from multiple hosts; Which host should we say it is?
31023119
# Let's call it 'merged-hosts' so that we can identify it.
31033120

3104-
if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then
3121+
if [[ $(should_execute_action "${mergedHost}-lipo") || $(should_execute_action "${mergedHost}-lipo-core") ]]; then
31053122
# Allow passing lipo with --host-lipo
31063123
if [[ -z "${HOST_LIPO}" ]] ; then
31073124
LIPO_PATH=$(xcrun_find_tool lipo)
@@ -3110,8 +3127,10 @@ if [[ ${#LIPO_SRC_DIRS[@]} -gt 0 ]]; then
31103127
fi
31113128
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[@]}
31123129

3113-
# Build and test the lipo-ed package.
3114-
build_and_test_installable_package ${mergedHost}
3130+
if [[ $(should_execute_action "${mergedHost}-lipo") ]]; then
3131+
# Build and test the lipo-ed package.
3132+
build_and_test_installable_package ${mergedHost}
3133+
fi
31153134
fi
31163135
fi
31173136
# END

utils/swift_build_support/swift_build_support/products/benchmarks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import platform
1515

1616
from . import product
17+
from . import swiftpm
1718
from .. import shell
1819
from .. import targets
1920

@@ -59,7 +60,9 @@ def install(self, host_target):
5960

6061

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

utils/swift_build_support/swift_build_support/products/indexstoredb.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414

1515
from . import product
16+
from . import swiftpm
1617
from .. import shell
1718
from .. import targets
1819

@@ -51,7 +52,12 @@ def run_build_script_helper(action, host_target, product, args,
5152
script_path = os.path.join(
5253
product.source_dir, 'Utilities', 'build-script-helper.py')
5354

54-
toolchain_path = targets.toolchain_path(args.install_destdir,
55+
install_destdir = args.install_destdir
56+
if swiftpm.SwiftPM.has_cross_compile_hosts(args):
57+
install_destdir = swiftpm.SwiftPM.get_install_destdir(args,
58+
host_target,
59+
product.build_dir)
60+
toolchain_path = targets.toolchain_path(install_destdir,
5561
args.install_prefix)
5662
is_release = product.is_release()
5763
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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from build_swift.build_swift.constants import MULTIROOT_DATA_FILE_PATH
1818

1919
from . import product
20+
from . import swiftpm
2021
from .. import shell
2122

2223

@@ -40,7 +41,7 @@ def is_swiftpm_unified_build_product(cls):
4041
def package_name(self):
4142
return 'SourceKitStressTester'
4243

43-
def run_build_script_helper(self, action, additional_params=[]):
44+
def run_build_script_helper(self, action, host_target, additional_params=[]):
4445
script_path = os.path.join(
4546
self.source_dir, 'build-script-helper.py')
4647

@@ -50,7 +51,7 @@ def run_build_script_helper(self, action, additional_params=[]):
5051
script_path,
5152
action,
5253
'--package-dir', self.package_name(),
53-
'--toolchain', self.install_toolchain_path(),
54+
'--toolchain', self.install_toolchain_path(host_target),
5455
'--config', configuration,
5556
'--build-dir', self.build_dir,
5657
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
@@ -74,19 +75,22 @@ def build(self, host_target):
7475
"than Darwin".format(
7576
product=self.package_name()))
7677

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

7980
def should_test(self, host_target):
8081
return self.args.test_skstresstester
8182

8283
def test(self, host_target):
83-
self.run_build_script_helper('test')
84+
self.run_build_script_helper('test', host_target)
8485

8586
def should_install(self, host_target):
8687
return self.args.install_skstresstester
8788

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

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)