Skip to content

Commit 7e2d7ba

Browse files
committed
[build-script] Add installation support for sourcekit-lsp
When invoked with the `install` action, build-script-helper.py will rsync the produced sourcekit-lsp to the toolchain's bin directory. On Linux, we add an extra relative rpath to find the swift corelibs (unfotunately we currently have no way to remove the absolute rpath; the same is true for all other swift-built binaries in the toolchain). On macOS, we replace the rpath.
1 parent 697d02a commit 7e2d7ba

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

Utilities/build-script-helper.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,46 @@ def get_swiftpm_options(args):
2929
if args.verbose:
3030
swiftpm_args += ['--verbose']
3131

32-
if platform.system() != 'Darwin':
32+
if platform.system() == 'Darwin':
33+
swiftpm_args += [
34+
# Relative library rpath for swift; will only be used when /usr/lib/swift
35+
# is not available.
36+
'-Xlinker', '-rpath', '-Xlinker', '@executable_path/../lib/swift/macosx',
37+
]
38+
else:
3339
swiftpm_args += [
3440
# Dispatch headers
3541
'-Xcxx', '-I', '-Xcxx',
3642
os.path.join(args.toolchain, 'usr', 'lib', 'swift'),
3743
# For <Block.h>
3844
'-Xcxx', '-I', '-Xcxx',
3945
os.path.join(args.toolchain, 'usr', 'lib', 'swift', 'Block'),
46+
# Library rpath for swift, dispatch, Foundation, etc. when installing
47+
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/linux',
4048
]
4149

4250
return swiftpm_args
4351

52+
def install(swiftpm_bin_path, toolchain):
53+
toolchain_bin = os.path.join(toolchain, 'usr', 'bin')
54+
for exe in ['sourcekit-lsp']:
55+
install_binary(exe, swiftpm_bin_path, toolchain_bin, toolchain)
56+
57+
def install_binary(exe, source_dir, install_dir, toolchain):
58+
cmd = ['rsync', '-a', os.path.join(source_dir, exe), install_dir]
59+
print(' '.join(cmd))
60+
subprocess.check_call(cmd)
61+
62+
if platform.system() == 'Darwin':
63+
result_path = os.path.join(install_dir, exe)
64+
stdlib_rpath = os.path.join(toolchain, 'usr', 'lib', 'swift', 'macosx')
65+
delete_rpath(stdlib_rpath, result_path)
66+
67+
def delete_rpath(rpath, binary):
68+
cmd = ["install_name_tool", "-delete_rpath", rpath, binary]
69+
print(' '.join(cmd))
70+
subprocess.check_call(cmd)
71+
4472
def main():
4573
parser = argparse.ArgumentParser(description='Build along with the Swift build-script.')
4674
def add_common_args(parser):
@@ -58,6 +86,9 @@ def add_common_args(parser):
5886
test_parser = subparsers.add_parser('test', help='test the package')
5987
add_common_args(test_parser)
6088

89+
install_parser = subparsers.add_parser('install', help='build the package')
90+
add_common_args(install_parser)
91+
6192
args = parser.parse_args(sys.argv[1:])
6293

6394
# Canonicalize paths
@@ -89,6 +120,10 @@ def add_common_args(parser):
89120
print('Cleaning ' + tests)
90121
shutil.rmtree(tests, ignore_errors=True)
91122
swiftpm('test', swift_exec, swiftpm_args, env)
123+
elif args.action == 'install':
124+
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
125+
swiftpm('build', swift_exec, swiftpm_args, env)
126+
install(bin_path, args.toolchain)
92127
else:
93128
assert False, 'unknown action \'{}\''.format(args.action)
94129

0 commit comments

Comments
 (0)