Skip to content

[build] Allow cross-compiling build-script products for non-Darwin hosts too #36917

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 2 commits into from
Dec 11, 2021

Conversation

finagolfin
Copy link
Member

@finagolfin finagolfin commented Apr 14, 2021

To that end, move the --cross-compile-deps-path flag from build-script-impl to a publicly documented build-script flag and use it for build-script products' library dependencies too. Generate a SPM destination JSON file that can be used both for cross-compiling these build-script products and by users for their own Swift packages.

Also, add a native_toolchain_path() method, that uses a prebuilt toolchain if available, and pass an install prefix in to swift-driver and sourcekit-lsp.

I use this patch to cross-compile SPM from linux to Android, along with the since-merged #33724 and swiftlang/swift-package-manager#3403.

@shahmishal, since you added support for cross-compiling SPM from macOS x86_64 to arm64, maybe you could review this? I kept this separate from your macOS config, but one additional change that could be made is to rename the SPM bootstrap script's --cross-compile-hosts flag to --cross-compile-host and only ever pass in one host at a time. That'll work because I only pass in one cross-compilation host at a time for these non-Darwin hosts, and you only support cross-compiling for a single Mac host, ie macOS arm64.

Once this in, I will look into extendinghave also extended it to cross-compile sourcekit-lsp, which I have never cross-compiled.

for cross_compile_host in self.args.cross_compile_hosts:
helper_cmd += [cross_compile_host]
if host_target == self.args.host_target:
for cross_compile_host in self.args.cross_compile_hosts:
Copy link
Member Author

@finagolfin finagolfin Apr 14, 2021

Choose a reason for hiding this comment

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

No need for a loop here if we change the SPM bootstrap flag to --cross-compile-host.

@finagolfin
Copy link
Member Author

Updated this pull to generate a SPM configuration file to cross-compile for Android, which matches the latest version of my pull to cross-compile SPM with itself, swiftlang/swift-package-manager#3403. I'm able to use these two pulls with the latest official trunk snapshot build of the Swift toolchain to cross-compile SPM for Android.

@shahmishal, can you review or recommend someone who might?

@finagolfin finagolfin force-pushed the swiftpm branch 2 times, most recently from ad6a515 to af829b1 Compare June 25, 2021 02:11
@finagolfin
Copy link
Member Author

Rebased and ran this through the Python linter, then tweaked it to pass that.

I used to merely dislike Python and its mandatory indentation, now I hate it and its linter: it's a cruel joke.

@varungandhi-apple, would you run the CI on this?

@varungandhi-apple
Copy link
Contributor

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Jul 1, 2021

Build failed
Swift Test OS X Platform
Git Sha - 2ca0c7c95c160cb0071da14e87f839c4806ac02c

@finagolfin
Copy link
Member Author

Mac CI failure is unrelated, Win CI appears to have been broken then.

@finagolfin
Copy link
Member Author

Rebased and made an ARM change that I had missed when generating the JSON file.

@finagolfin
Copy link
Member Author

Rebased, added support for cross-compiling swift-driver too, and added a check to use an existing SPM JSON file if it was written previously. I just submitted the required patch to use this with swift-driver in swiftlang/swift-driver#743.

@artemcm
Copy link
Contributor

artemcm commented Jul 7, 2021

@swift-ci test

@finagolfin
Copy link
Member Author

finagolfin commented Jul 8, 2021

Rebased and added a new commit with the prefix flag for swift-driver. I also made two changes that will make this pull work in more scenarios. There are three primary scenarios it needs to handle:

  1. Obviously everything should keep working for native compilation, this is tested by the CI.
  2. You could build the native compiler from scratch then build a cross-compiled host toolchain with this pull, this is untested by me or any CI, since I'm only adding this capability for non-Darwin hosts now tested with the July 24 trunk source snapshot and works fine.
  3. Use a prebuilt official snapshot toolchain to cross-compile these build-script products: that's how I initially tested this pull with the July 7 trunk snapshot.

Since 2. wasn't tested, I had made some mistakes in that scenario, which I call out in comments below.

@finagolfin
Copy link
Member Author

@drexin, since you're up, let's see if this still passes the CI.

@drexin
Copy link
Contributor

drexin commented Jul 9, 2021

@swift-ci test

@finagolfin
Copy link
Member Author

@neonichu, since this pull primarily adds a way to cross-compile SPM for non-Darwin platforms, mind reviewing? We should probably also run it through the CI again since it's been more than a month since it passed the CI and was ready for review, I've gone ahead and rebased.

@neonichu
Copy link
Contributor

@swift-ci please smoke test

@neonichu
Copy link
Contributor

@buttaface I'm not really familiar with any of the build scripts in the Swift repo, so not the right person to review.

@finagolfin
Copy link
Member Author

@gottesmm, would you review?

@finagolfin
Copy link
Member Author

Rebased and added support for cross-compiling sourcekit-lsp too, as I said I would when I opened this pull more than seven months ago. I used this pull and the sourcekit-lsp change to successfully cross-compile the last trunk source snapshot of the toolchain before the break for Android.

@finagolfin
Copy link
Member Author

@shahmishal, can we get this in before the 5.6 branch? It's been ready for more than two months now and @gottesmm told me he'd like to get it in, but he was busy a month ago.

@shahmishal
Copy link
Member

@edymtt Can you help review this change?

@shahmishal
Copy link
Member

@swift-ci test

@shahmishal shahmishal requested a review from edymtt December 3, 2021 18:06
@finagolfin
Copy link
Member Author

Windows CI failure is single unrelated concurrency test.

@finagolfin
Copy link
Member Author

@edymtt, do you think we can get this in?

@shahmishal
Copy link
Member

I think @gottesmm is reviewing it now.

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

Looks really good. A few quick questions. I am fine with you answering them after we merge since if the tests pass I am ok with that later explanation.

@@ -669,7 +673,13 @@ def execute(self):
#
# This just maintains current behavior.
if index > last_impl_index:
self._execute(pipeline, [self.args.host_target])
non_darwin_cross_compile_hostnames = [
Copy link
Contributor

Choose a reason for hiding this comment

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

@buttaface can you add a comment here (or fix up the comment above) explaining that we support it for non-darwin platforms. I am fine with a follow on commit fixing the comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do.

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 simply removed the comments above, since it is no longer true. Darwin platforms were already cross-compiling in a different way, and this change adds support for non-Darwin platforms.

toolchain_path = swiftpm.SwiftPM.get_install_destdir(args,
host_target,
product.build_dir)
toolchain_path = product.host_install_destdir(host_target)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am fine with this if the tests pass (which it looks like they did).

resource_path=resource_dir)
]

if action == 'install' and product.product_name() == "sourcekitlsp":
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this also be guarded by the not is_darwin_host since you are trying to not effect the Darwin path?

Copy link
Member Author

@finagolfin finagolfin Dec 8, 2021

Choose a reason for hiding this comment

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

While most of these changes only affect non-Darwin and are guarded by such checks, there are a few changes that are applied generally, ie for Darwin too, which I called out separately in my notes for the first commit and the second commit:

Also, add a native_toolchain_path() method, that uses a prebuilt toolchain if available, and pass an install prefix in to swift-driver and sourcekit-lsp.

This was needed because non-Darwin uses a different install prefix for cross-compilation hosts, so I submitted pulls for those products' build scripts to accept an install prefix or actually start using the prefix passed in, which this pull now passes in for all platforms.

These configuration changes that affect Darwin too appear to be working fine.

if args.verbose_build:
helper_cmd.append('--verbose')

if action == 'install':
helper_cmd += [
'--prefix', install_destdir + args.install_prefix
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as my earlier question about --prefix. I just want to understand what the effect is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same answer as indexstoredb above.

…sts too

To that end, move the --cross-compile-deps-path flag from build-script-impl to a
publicly documented build-script flag and use it for build-script products'
library dependencies too. Generate a SPM destination JSON file that can be used
both for cross-compiling these build-script products and by users for their own
Swift packages.

Also, add a native_toolchain_path() method, that uses a prebuilt toolchain if
available, and pass an install prefix in to swift-driver and sourcekit-lsp.
@finagolfin
Copy link
Member Author

@MaxDesiatov, would you test and merge?

@MaxDesiatov
Copy link
Contributor

@swift-ci please smoke test

@MaxDesiatov
Copy link
Contributor

If CI passes, I hesitate to merge since there were changes requested by one of the reviews that I don't know if they were fully addressed. I'd prefer one of the reviewers or code owners to merge.

@gottesmm
Copy link
Contributor

@DougGregor haven't they since built an ok toolchain? Can you remove your block?

@gottesmm
Copy link
Contributor

I looked above and it seems that Doug's issue was dealt with explicitly here: #36917 (comment).

I am going to override the change request just so we can get this moving.

@gottesmm gottesmm dismissed DougGregor’s stale review December 11, 2021 21:03

Responded to and dealt with here: #36917 (comment)

@gottesmm gottesmm merged commit 1ba8317 into swiftlang:main Dec 11, 2021
@finagolfin
Copy link
Member Author

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.