Skip to content

Commit 4672dbc

Browse files
committed
[Build Script Helper] Add support for non-Darwin cross-compilation hosts, starting with Android
Add a flag, '--cross-compile-config', to pass in a SPM cross-compilation file and cross-compile with that instead of CMake. Also, fix non-Darwin installs to use the '--prefix' list passed in, rather than simply installing into the build toolchain.
1 parent 4f4e853 commit 4672dbc

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

Utilities/build-script-helper.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import subprocess
1212
import sys
1313
import errno
14+
import re
1415

1516
if platform.system() == 'Darwin':
1617
shared_lib_ext = '.dylib'
@@ -96,7 +97,11 @@ def get_swiftpm_options(args):
9697
os.path.join(args.toolchain, 'lib', 'swift', 'Block'),
9798
]
9899

99-
if 'ANDROID_DATA' in os.environ:
100+
if args.cross_compile_hosts:
101+
swiftpm_args += ['--destination', args.cross_compile_config]
102+
103+
if 'ANDROID_DATA' in os.environ or (args.cross_compile_hosts and re.match(
104+
'android-', args.cross_compile_hosts[0])):
100105
swiftpm_args += [
101106
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android',
102107
# SwiftPM will otherwise try to compile against GNU strerror_r on
@@ -180,8 +185,13 @@ def handle_invocation(args):
180185
if args.sysroot:
181186
env['SDKROOT'] = args.sysroot
182187

188+
env['SWIFT_EXEC'] = '%sc' % (swift_exec)
189+
183190
if args.action == 'build':
184-
build_using_cmake(args, toolchain_bin, args.build_path, targets)
191+
if args.cross_compile_hosts and not re.match('-macosx', args.cross_compile_hosts[0]):
192+
swiftpm('build', swift_exec, swiftpm_args, env)
193+
else:
194+
build_using_cmake(args, toolchain_bin, args.build_path, targets)
185195

186196
elif args.action == 'clean':
187197
print('Cleaning ' + args.build_path)
@@ -191,7 +201,6 @@ def handle_invocation(args):
191201
tool_path = os.path.join(toolchain_bin, tool)
192202
if os.path.exists(tool_path):
193203
env['SWIFT_DRIVER_' + tool.upper().replace('-','_') + '_EXEC'] = '%s' % (tool_path)
194-
env['SWIFT_EXEC'] = '%sc' % (swift_exec)
195204
test_args = swiftpm_args
196205
test_args += ['-Xswiftc', '-enable-testing']
197206
if should_test_parallel():
@@ -208,16 +217,17 @@ def handle_invocation(args):
208217
else:
209218
bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env)
210219
swiftpm('build', swift_exec, swiftpm_args, env)
211-
non_darwin_install(bin_path, args.toolchain, args.verbose)
220+
non_darwin_install(args, bin_path)
212221
else:
213222
assert False, 'unknown action \'{}\''.format(args.action)
214223

215224
# Installation flow for non-darwin platforms, only copies over swift-driver and swift-help
216225
# TODO: Unify CMake-based installation flow used on Darwin with this
217-
def non_darwin_install(swiftpm_bin_path, toolchain, verbose):
218-
toolchain_bin = os.path.join(toolchain, 'bin')
219-
for exe in executables_to_install:
220-
install_binary(exe, swiftpm_bin_path, toolchain_bin, verbose)
226+
def non_darwin_install(args, swiftpm_bin_path):
227+
for prefix in args.install_prefixes:
228+
prefix_bin = os.path.join(prefix, 'bin')
229+
for exe in executables_to_install:
230+
install_binary(exe, swiftpm_bin_path, prefix_bin, args.verbose)
221231

222232
def install(args, build_dir, targets):
223233
# Construct and install universal swift-driver, swift-help executables
@@ -584,6 +594,10 @@ def add_common_args(parser):
584594
nargs='*',
585595
help='List of cross compile hosts targets.',
586596
default=[])
597+
parser.add_argument(
598+
'--cross-compile-config',
599+
metavar='PATH',
600+
help="A JSON SPM config file with Swift flags for cross-compilation")
587601
parser.add_argument('--ninja-bin', metavar='PATH', help='ninja binary to use for testing')
588602
parser.add_argument('--cmake-bin', metavar='PATH', help='cmake binary to use for building')
589603
parser.add_argument('--build-path', metavar='PATH', default='.build', help='build in the given path')
@@ -626,6 +640,8 @@ def add_common_args(parser):
626640
args.cross_compile_hosts = [build_target + macos_deployment_target, 'arm64-apple-macosx%s' % macos_deployment_target]
627641
elif (build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args.cross_compile_hosts):
628642
args.cross_compile_hosts = [build_target + macos_deployment_target, 'x86_64-apple-macosx%s' % macos_deployment_target]
643+
elif args.cross_compile_hosts and re.match('android-', args.cross_compile_hosts[0]):
644+
print('Cross-compiling for %s' % args.cross_compile_hosts[0])
629645
elif args.cross_compile_hosts:
630646
error("cannot cross-compile for %s" % cross_compile_hosts)
631647

0 commit comments

Comments
 (0)