Skip to content

[build-script-helper] Add support for Android cross-compilation #424

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 1 commit into from
Aug 31, 2021
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
51 changes: 34 additions & 17 deletions Utilities/build-script-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import os
import platform
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -50,25 +51,28 @@ def get_swiftpm_options(args):
os.path.join(args.toolchain, 'lib', 'swift', 'Block'),
]

if 'ANDROID_DATA' in os.environ:
swiftpm_args += [
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android',
# SwiftPM will otherwise try to compile against GNU strerror_r on
# Android and fail.
'-Xswiftc', '-Xcc', '-Xswiftc', '-U_GNU_SOURCE',
]
else:
# Library rpath for swift, dispatch, Foundation, etc. when installing
swiftpm_args += [
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/linux',
]
if 'ANDROID_DATA' in os.environ or (args.cross_compile_host and re.match(
'android-', args.cross_compile_host)):
swiftpm_args += [
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android',
# SwiftPM will otherwise try to compile against GNU strerror_r on
# Android and fail.
'-Xswiftc', '-Xcc', '-Xswiftc', '-U_GNU_SOURCE',
]
elif platform.system() == 'Linux':
# Library rpath for swift, dispatch, Foundation, etc. when installing
swiftpm_args += [
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/linux',
]

if args.cross_compile_host:
swiftpm_args += ['--destination', args.cross_compile_config]

return swiftpm_args

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

def install_binary(exe, source_dir, install_dir, toolchain):
cmd = ['rsync', '-a', os.path.join(source_dir, exe), install_dir]
Expand Down Expand Up @@ -112,6 +116,8 @@ def handle_invocation(swift_exec, args):
print('Cleaning ' + args.build_path)
shutil.rmtree(args.build_path, ignore_errors=True)

env['SWIFT_EXEC'] = '%sc' % (swift_exec)

if args.action == 'build':
swiftpm('build', swift_exec, swiftpm_args, env)
elif args.action == 'test':
Expand All @@ -131,7 +137,10 @@ def handle_invocation(swift_exec, args):
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
swiftpm_args += ['-Xswiftc', '-no-toolchain-stdlib-rpath']
swiftpm('build', swift_exec, swiftpm_args, env)
install(bin_path, args.toolchain)

if not args.install_prefixes:
args.install_prefixes = [args.toolchain]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tested this default case like this initially, then modified it to just passing args.install_prefixes if not None else [args.toolchain] to the line below and not tested the default case again. For whatever reason, that doesn't work, so going back to this.

install(bin_path, args.install_prefixes, args.toolchain)
else:
assert False, 'unknown action \'{}\''.format(args.action)

Expand All @@ -148,6 +157,8 @@ def add_common_args(parser):
parser.add_argument('--sanitize', action='append', help='build using the given sanitizer(s) (address|thread|undefined)')
parser.add_argument('--sanitize-all', action='store_true', help='build using every available sanitizer in sub-directories of build path')
parser.add_argument('--verbose', '-v', action='store_true', help='enable verbose output')
parser.add_argument('--cross-compile-host', help='cross-compile for another host instead')
parser.add_argument('--cross-compile-config', help='an SPM JSON destination file containing Swift cross-compilation flags')

subparsers = parser.add_subparsers(title='subcommands', dest='action', metavar='action')
build_parser = subparsers.add_parser('build', help='build the package')
Expand All @@ -159,6 +170,7 @@ def add_common_args(parser):

install_parser = subparsers.add_parser('install', help='build the package')
add_common_args(install_parser)
install_parser.add_argument('--prefix', dest='install_prefixes', nargs='*', metavar='PATHS', help="paths to install sourcekit-lsp, default: 'toolchain/bin'")

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

Expand All @@ -175,6 +187,11 @@ def add_common_args(parser):
else:
swift_exec = 'swift'

if args.cross_compile_host and re.match('android-', args.cross_compile_host):
print('Cross-compiling for %s' % args.cross_compile_host)
elif args.cross_compile_host:
error("cannot cross-compile for %s" % args.cross_compile_host)

handle_invocation(swift_exec, args)
if args.sanitize_all:
base = args.build_path
Expand Down