Skip to content

Commit 5620347

Browse files
committed
[build] Support cross-compilation of swift-format
This should build swift-format as a fat binary containing both an x86_64 and an arm64 slice in the open source toolchains. rdar://121326143
1 parent acf4e28 commit 5620347

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

utils/swift_build_support/swift_build_support/products/swiftformat.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from . import swiftsyntax
2828
from . import xctest
2929
from .. import shell
30+
from .. import targets
3031

3132

3233
class SwiftFormat(product.Product):
@@ -57,8 +58,6 @@ def run_build_script_helper(self, action, host_target, additional_params=[]):
5758
script_path = os.path.join(
5859
self.source_dir, 'build-script-helper.py')
5960

60-
install_destdir = self.host_install_destdir(host_target)
61-
6261
helper_cmd = [
6362
script_path,
6463
action,
@@ -71,9 +70,33 @@ def run_build_script_helper(self, action, host_target, additional_params=[]):
7170
# reset the dependencies to be local.
7271
'--update'
7372
]
74-
helper_cmd.extend([
75-
'--prefix', install_destdir + self.args.install_prefix
76-
])
73+
74+
install_destdir = self.host_install_destdir(host_target)
75+
toolchain_path = self.native_toolchain_path(host_target)
76+
77+
# Pass Cross compile host info unless we're testing.
78+
# It doesn't make sense to run tests of the cross compile host.
79+
if self.has_cross_compile_hosts() and action != 'test':
80+
if self.is_darwin_host(host_target):
81+
if len(self.args.cross_compile_hosts) != 1:
82+
raise RuntimeError("Cross-Compiling swift-format to multiple " +
83+
"targets is not supported")
84+
helper_cmd += ['--cross-compile-host', self.args.cross_compile_hosts[0]]
85+
elif self.is_cross_compile_target(host_target):
86+
helper_cmd.extend(['--cross-compile-host', host_target])
87+
build_toolchain_path = install_destdir + self.args.install_prefix
88+
resource_dir = f'{build_toolchain_path}/lib/swift'
89+
cross_compile_config = targets.StdlibDeploymentTarget \
90+
.get_target_for_name(host_target) \
91+
.platform \
92+
.swiftpm_config(
93+
self.args,
94+
output_dir=build_toolchain_path,
95+
swift_toolchain=toolchain_path,
96+
resource_path=resource_dir
97+
)
98+
helper_cmd += ['--cross-compile-config', cross_compile_config]
99+
77100
if self.args.verbose_build:
78101
helper_cmd.append('--verbose')
79102
helper_cmd.extend(additional_params)
@@ -139,7 +162,12 @@ def should_install(self, host_target):
139162
return self.args.install_swiftformat
140163

141164
def install(self, host_target):
142-
self.run_build_script_helper('install', host_target)
165+
install_destdir = self.host_install_destdir(host_target)
166+
self.run_build_script_helper(
167+
'install',
168+
host_target,
169+
additional_params=['--prefix', install_destdir + self.args.install_prefix]
170+
)
143171

144172
@classmethod
145173
def get_dependencies(cls):

0 commit comments

Comments
 (0)