Skip to content

[Build Script Helper] Add support for non-Darwin cross-compilation hosts, starting with Android #743

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
Jul 7, 2021

Conversation

finagolfin
Copy link
Member

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.

I tested this with the latest trunk source snapshot from June 12. The build-script side of this pull was just added to swiftlang/swift#36917. This pull is the equivalent of swiftlang/swift-package-manager#3403. I plan to use this pull to build and install this swift-driver as part of my upcoming Swift 5.5 toolchain for Android.

@@ -626,6 +640,8 @@ def add_common_args(parser):
args.cross_compile_hosts = [build_target + macos_deployment_target, 'arm64-apple-macosx%s' % macos_deployment_target]
elif (build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args.cross_compile_hosts):
args.cross_compile_hosts = [build_target + macos_deployment_target, 'x86_64-apple-macosx%s' % macos_deployment_target]
elif args.cross_compile_hosts and re.match('android-', args.cross_compile_hosts[0]):
Copy link
Member Author

Choose a reason for hiding this comment

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

Gates this new cross-compilation path for Android only, since it has only been tested for that non-Darwin platform.

Copy link
Member Author

@finagolfin finagolfin Jul 5, 2021

Choose a reason for hiding this comment

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

Also, it only checks the first element of the hosts with args.cross_compile_hosts[0], as swiftlang/swift#36917 only passes in one at a time. I have proposed changing that flag to a single --cross-compile-host instead.

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 just noticed that you're changing the format of the strings in cross_compile_hosts for Darwin in the lines above, whereas I'm keeping the passed-in format for non-Darwin. Since the triple format you use on Darwin is only needed for the CMake build and Darwin install(), both of which are unused with non-Darwin hosts in this pull, that works fine for now with these non-Darwin hosts.

@@ -180,8 +185,13 @@ def handle_invocation(args):
if args.sysroot:
env['SDKROOT'] = args.sysroot

env['SWIFT_EXEC'] = '%sc' % (swift_exec)
Copy link
Member Author

Choose a reason for hiding this comment

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

The only change in this pull that will affect native compilation too, by applying this environment variable when invoking swift-build when installing on non-Darwin platforms. I had to add this when cross-compiling for Android with Swift 5.5 or trunk on a linux host with a Swift 5.4 package installed, as SPM would then try to use that system Swift 5.4 and error because of the Swift stdlib version mismatch.

This fixes that bug in the native build too.

if args.action == 'build':
build_using_cmake(args, toolchain_bin, args.build_path, targets)
if args.cross_compile_hosts:
swiftpm('build', swift_exec, swiftpm_args, env)
Copy link
Member Author

Choose a reason for hiding this comment

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

Since SPM is used for the install step below anyway, I see no reason to cross-compile with CMake first, which would require passing in a separate list of Swift flags to cross-compile with CMake.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd really like to unify these one day, but for now, I agree, this is simpler.

for exe in executables_to_install:
install_binary(exe, swiftpm_bin_path, toolchain_bin, verbose)
def non_darwin_install(args, swiftpm_bin_path):
for prefix in args.install_prefixes:
Copy link
Member Author

Choose a reason for hiding this comment

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

This should keep working as it always has when a --prefix list isn't passed in, since this list is default-initialized to the toolchain path anyway and build-script never passed --prefix in.

…sts, 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.
Copy link
Contributor

@artemcm artemcm left a comment

Choose a reason for hiding this comment

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

Thank you, @buttaface.

if args.action == 'build':
build_using_cmake(args, toolchain_bin, args.build_path, targets)
if args.cross_compile_hosts:
swiftpm('build', swift_exec, swiftpm_args, env)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd really like to unify these one day, but for now, I agree, this is simpler.

@finagolfin
Copy link
Member Author

Just needs to be run through the CI and merged?

@artemcm
Copy link
Contributor

artemcm commented Jul 7, 2021

@swift-ci please test

@artemcm
Copy link
Contributor

artemcm commented Jul 7, 2021

@buttaface does this need to have a cross-repo test from Swift repo side to make sure that this doesn't affect the build-script flow?

@finagolfin
Copy link
Member Author

No, if you're happy with how this works, we can get this in now, then the build-script side can be tested separately for swiftlang/swift#36917. Btw, I asked you a question about that pull over there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants