Skip to content

[build-script] Add installation support for sourcekit-lsp #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions Utilities/build-script-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def swiftpm(action, swift_exec, swiftpm_args, env=None):
subprocess.check_call(cmd, env=env)

def swiftpm_bin_path(swift_exec, swiftpm_args, env=None):
swiftpm_args = filter(lambda arg: arg != '-v' and arg != '--verbose', swiftpm_args)
cmd = [swift_exec, 'build', '--show-bin-path'] + swiftpm_args
print(' '.join(cmd))
return subprocess.check_output(cmd, env=env).strip()
Expand All @@ -29,18 +30,46 @@ def get_swiftpm_options(args):
if args.verbose:
swiftpm_args += ['--verbose']

if platform.system() != 'Darwin':
if platform.system() == 'Darwin':
swiftpm_args += [
# Relative library rpath for swift; will only be used when /usr/lib/swift
# is not available.
'-Xlinker', '-rpath', '-Xlinker', '@executable_path/../lib/swift/macosx',
]
else:
swiftpm_args += [
# Dispatch headers
'-Xcxx', '-I', '-Xcxx',
os.path.join(args.toolchain, 'usr', 'lib', 'swift'),
# For <Block.h>
'-Xcxx', '-I', '-Xcxx',
os.path.join(args.toolchain, 'usr', 'lib', 'swift', 'Block'),
# Library rpath for swift, dispatch, Foundation, etc. when installing
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/linux',
]

return swiftpm_args

def install(swiftpm_bin_path, toolchain):
toolchain_bin = os.path.join(toolchain, 'usr', 'bin')
for exe in ['sourcekit-lsp']:
install_binary(exe, swiftpm_bin_path, toolchain_bin, toolchain)

def install_binary(exe, source_dir, install_dir, toolchain):
cmd = ['rsync', '-a', os.path.join(source_dir, exe), install_dir]
print(' '.join(cmd))
subprocess.check_call(cmd)

if platform.system() == 'Darwin':
result_path = os.path.join(install_dir, exe)
stdlib_rpath = os.path.join(toolchain, 'usr', 'lib', 'swift', 'macosx')
delete_rpath(stdlib_rpath, result_path)

def delete_rpath(rpath, binary):
cmd = ["install_name_tool", "-delete_rpath", rpath, binary]
print(' '.join(cmd))
subprocess.check_call(cmd)

def main():
parser = argparse.ArgumentParser(description='Build along with the Swift build-script.')
def add_common_args(parser):
Expand All @@ -49,6 +78,7 @@ def add_common_args(parser):
parser.add_argument('--ninja-bin', metavar='PATH', help='ninja binary to use for testing')
parser.add_argument('--build-path', metavar='PATH', default='.build', help='build in the given path')
parser.add_argument('--configuration', '-c', default='debug', help='build using configuration (release|debug)')
parser.add_argument('--no-local-deps', action='store_true', help='use normal remote dependencies when building')
parser.add_argument('--verbose', '-v', action='store_true', help='enable verbose output')

subparsers = parser.add_subparsers(title='subcommands', dest='action', metavar='action')
Expand All @@ -58,6 +88,9 @@ def add_common_args(parser):
test_parser = subparsers.add_parser('test', help='test the package')
add_common_args(test_parser)

install_parser = subparsers.add_parser('install', help='build the package')
add_common_args(install_parser)

args = parser.parse_args(sys.argv[1:])

# Canonicalize paths
Expand All @@ -76,7 +109,8 @@ def add_common_args(parser):
# Set the toolchain used in tests at runtime
env['SOURCEKIT_TOOLCHAIN_PATH'] = args.toolchain
# Use local dependencies (i.e. checked out next sourcekit-lsp).
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"
if not args.no_local_deps:
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"

if args.ninja_bin:
env['NINJA_BIN'] = args.ninja_bin
Expand All @@ -89,6 +123,10 @@ def add_common_args(parser):
print('Cleaning ' + tests)
shutil.rmtree(tests, ignore_errors=True)
swiftpm('test', swift_exec, swiftpm_args, env)
elif args.action == 'install':
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
swiftpm('build', swift_exec, swiftpm_args, env)
install(bin_path, args.toolchain)
else:
assert False, 'unknown action \'{}\''.format(args.action)

Expand Down