Skip to content

Commit 44eb5ec

Browse files
committed
[build] Build sourcekit-lsp for multiple arches and lipo them
Instead of building SourceKit-LSP using SwiftPM's multi-arch xcbuild backend, build it for only one arch at a time and then run `lipo` to merge the two resulting binaries. This should allow us to share build products between building installing and testing and also eliminates other quirks resulting from the xcbuild backend.
1 parent 4d01253 commit 44eb5ec

File tree

2 files changed

+106
-71
lines changed

2 files changed

+106
-71
lines changed

utils/swift_build_support/swift_build_support/products/indexstoredb.py

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ def should_build(self, host_target):
5050
return True
5151

5252
def build(self, host_target):
53-
run_build_script_helper('build', host_target, self, self.args)
53+
self.run_build_script_helper('build', host_target)
5454

5555
def should_test(self, host_target):
5656
return self.args.test_indexstoredb
5757

5858
def test(self, host_target):
59-
run_build_script_helper('test', host_target, self, self.args,
60-
self.args.test_indexstoredb_sanitize_all)
59+
self.run_build_script_helper('test', host_target)
6160

6261
def should_install(self, host_target):
6362
return False
@@ -82,63 +81,33 @@ def get_dependencies(cls):
8281
swiftsyntax.SwiftSyntax]
8382

8483

85-
def run_build_script_helper(action, host_target, product, args,
86-
sanitize_all=False, clean=False):
87-
script_path = os.path.join(
88-
product.source_dir, 'Utilities', 'build-script-helper.py')
89-
90-
install_destdir = product.host_install_destdir(host_target)
91-
toolchain_path = product.native_toolchain_path(host_target)
92-
is_release = product.is_release()
93-
configuration = 'release' if is_release else 'debug'
94-
helper_cmd = [
95-
script_path,
96-
action,
97-
'--package-path', product.source_dir,
98-
'--build-path', product.build_dir,
99-
'--configuration', configuration,
100-
'--toolchain', toolchain_path,
101-
'--ninja-bin', product.toolchain.ninja,
102-
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
103-
]
104-
if args.verbose_build:
105-
helper_cmd.append('--verbose')
106-
107-
if sanitize_all:
108-
helper_cmd.append('--sanitize-all')
109-
elif args.enable_asan:
110-
helper_cmd.extend(['--sanitize', 'address'])
111-
elif args.enable_ubsan:
112-
helper_cmd.extend(['--sanitize', 'undefined'])
113-
elif args.enable_tsan:
114-
helper_cmd.extend(['--sanitize', 'thread'])
115-
116-
if clean:
117-
helper_cmd.append('--clean')
118-
119-
# Pass Cross compile host info unless we're testing.
120-
# It doesn't make sense to run tests of the cross compile host.
121-
if product.has_cross_compile_hosts() and action != 'test':
122-
if product.is_darwin_host(host_target):
123-
if len(args.cross_compile_hosts) != 1:
124-
raise RuntimeError("Cross-Compiling indexstoredb to multiple " +
125-
"targets is not supported")
126-
helper_cmd += ['--cross-compile-host', args.cross_compile_hosts[0]]
127-
elif product.is_cross_compile_target(host_target):
128-
helper_cmd.extend(['--cross-compile-host', host_target])
129-
build_toolchain_path = install_destdir + args.install_prefix
130-
resource_dir = '%s/lib/swift' % build_toolchain_path
131-
helper_cmd += [
132-
'--cross-compile-config',
133-
targets.StdlibDeploymentTarget.get_target_for_name(host_target).platform
134-
.swiftpm_config(args, output_dir=build_toolchain_path,
135-
swift_toolchain=toolchain_path,
136-
resource_path=resource_dir)
137-
]
138-
139-
if action == 'install' and product.product_name() == "sourcekitlsp":
140-
helper_cmd.extend([
141-
'--prefix', install_destdir + args.install_prefix
142-
])
143-
144-
shell.call(helper_cmd)
84+
def run_build_script_helper(self, action, host_target):
85+
script_path = os.path.join(
86+
self.source_dir, 'Utilities', 'build-script-helper.py')
87+
88+
install_destdir = self.host_install_destdir(host_target)
89+
toolchain_path = self.native_toolchain_path(host_target)
90+
configuration = 'release' if self.is_release() else 'debug'
91+
helper_cmd = [
92+
script_path,
93+
action,
94+
'--package-path', self.source_dir,
95+
'--build-path', self.build_dir,
96+
'--configuration', configuration,
97+
'--toolchain', toolchain_path,
98+
'--ninja-bin', self.toolchain.ninja,
99+
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
100+
]
101+
if self.args.verbose_build:
102+
helper_cmd.append('--verbose')
103+
104+
if self.args.test_indexstoredb_sanitize_all:
105+
helper_cmd.append('--sanitize-all')
106+
elif self.args.enable_asan:
107+
helper_cmd.extend(['--sanitize', 'address'])
108+
elif self.args.enable_ubsan:
109+
helper_cmd.extend(['--sanitize', 'undefined'])
110+
elif self.args.enable_tsan:
111+
helper_cmd.extend(['--sanitize', 'thread'])
112+
113+
shell.call(helper_cmd)

utils/swift_build_support/swift_build_support/products/sourcekitlsp.py

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#
1111
# ----------------------------------------------------------------------------
1212

13+
import os
14+
15+
from build_swift.build_swift.constants import MULTIROOT_DATA_FILE_PATH
16+
1317
from . import cmark
1418
from . import foundation
15-
from . import indexstoredb
1619
from . import libcxx
1720
from . import libdispatch
1821
from . import llbuild
@@ -21,6 +24,8 @@
2124
from . import swift
2225
from . import swiftpm
2326
from . import xctest
27+
from .. import shell
28+
from .. import targets
2429

2530

2631
class SourceKitLSP(product.Product):
@@ -43,24 +48,36 @@ def is_swiftpm_unified_build_product(cls):
4348
def should_build(self, host_target):
4449
return True
4550

51+
def _for_each_host_target(self, base_target, body):
52+
body(base_target)
53+
54+
# For Darwin host, 'build' is only called for the builder.
55+
# Manually iterate the cross compile hosts.
56+
if self.has_cross_compile_hosts() and self.is_darwin_host(base_target):
57+
for target in self.args.cross_compile_hosts:
58+
body(target)
59+
60+
4661
def build(self, host_target):
47-
indexstoredb.run_build_script_helper(
48-
'build', host_target, self, self.args)
62+
self._for_each_host_target(
63+
host_target,
64+
lambda target: self.run_build_script_helper('build', host_target, target)
65+
)
4966

5067
def should_test(self, host_target):
5168
return self.args.test_sourcekitlsp
5269

5370
def test(self, host_target):
54-
indexstoredb.run_build_script_helper(
55-
'test', host_target, self, self.args,
56-
self.args.test_sourcekitlsp_sanitize_all)
71+
self.run_build_script_helper('test', host_target, host_target)
5772

5873
def should_install(self, host_target):
5974
return self.args.install_sourcekitlsp
6075

6176
def install(self, host_target):
62-
indexstoredb.run_build_script_helper(
63-
'install', host_target, self, self.args)
77+
self._for_each_host_target(
78+
host_target,
79+
lambda target: self.run_build_script_helper('install', host_target, target)
80+
)
6481

6582
@classmethod
6683
def get_dependencies(cls):
@@ -73,3 +90,52 @@ def get_dependencies(cls):
7390
xctest.XCTest,
7491
llbuild.LLBuild,
7592
swiftpm.SwiftPM]
93+
94+
def run_build_script_helper(self, action, base_target, host_target):
95+
# base_target is the machine that's driving the build.
96+
# host_target is the target we are bulding for.
97+
script_path = os.path.join(
98+
self.source_dir, 'Utilities', 'build-script-helper.py')
99+
100+
install_destdir = self.host_install_destdir(host_target)
101+
toolchain_path = self.native_toolchain_path(base_target)
102+
configuration = 'release' if self.is_release() else 'debug'
103+
helper_cmd = [
104+
script_path,
105+
action,
106+
'--package-path', self.source_dir,
107+
'--build-path', self.build_dir,
108+
'--configuration', configuration,
109+
'--toolchain', toolchain_path,
110+
'--ninja-bin', self.toolchain.ninja,
111+
'--multiroot-data-file', MULTIROOT_DATA_FILE_PATH,
112+
]
113+
if self.args.verbose_build:
114+
helper_cmd.append('--verbose')
115+
116+
if self.args.test_sourcekitlsp_sanitize_all:
117+
helper_cmd.append('--sanitize-all')
118+
elif self.args.enable_asan:
119+
helper_cmd += ['--sanitize', 'address']
120+
elif self.args.enable_ubsan:
121+
helper_cmd += ['--sanitize', 'undefined']
122+
elif self.args.enable_tsan:
123+
helper_cmd += ['--sanitize', 'thread']
124+
125+
if self.has_cross_compile_hosts():
126+
helper_cmd += ['--cross-compile-host', host_target]
127+
if self.is_cross_compile_target(host_target) and not self.is_darwin_host(host_target):
128+
build_toolchain_path = install_destdir + self.args.install_prefix
129+
resource_dir = '%s/lib/swift' % build_toolchain_path
130+
helper_cmd += [
131+
'--cross-compile-config',
132+
targets.StdlibDeploymentTarget.get_target_for_name(host_target).platform
133+
.swiftpm_config(self.args, output_dir=build_toolchain_path,
134+
swift_toolchain=toolchain_path,
135+
resource_path=resource_dir)
136+
]
137+
138+
if action == 'install':
139+
helper_cmd += ['--prefix', install_destdir + self.args.install_prefix]
140+
141+
shell.call(helper_cmd)

0 commit comments

Comments
 (0)