Skip to content

[embedded] Start building and including lld even in Darwin toolchains #70715

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
Mar 27, 2024

Conversation

kubamracek
Copy link
Contributor

We already include lld in the Linux and Windows toolchains. For embedded Swift development, it's common to (need to) use ELF even on a macOS host, but the Xcode-provided linker is Mach-O only. Let's start including a linker capable of linking ELF binaries (for embedded cross-compilation use cases) even in the Darwin toolchains.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Yes!

@kubamracek
Copy link
Contributor Author

@swift-ci Please Build Toolchain macOS Platform

1 similar comment
@kubamracek
Copy link
Contributor Author

@swift-ci Please Build Toolchain macOS Platform

@kubamracek
Copy link
Contributor Author

@shahmishal This will have Darwin toolchain size impact (should be no change on Linux/Windows toolchains).

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@shahmishal
Copy link
Member

@swift-ci build toolchain macOS

@shahmishal
Copy link
Member

I don't see lld getting installed in the toolchain, not sure if we are missing something.

@shahmishal
Copy link
Member

We will need to change this to include lld

https://github.com/apple/swift/blob/main/utils/build-presets.ini#L22

@shahmishal
Copy link
Member

@kubamracek I am a change to your PR to see if this fixes the issue.

@shahmishal
Copy link
Member

@swift-ci build toolchain macOS

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@ole
Copy link
Contributor

ole commented Feb 9, 2024

@kubamracek If you have the time, could you start another macOS toolchain build for me? Or do you have an idea when this PR will be merged? I have a feeling it's the only remaining blocker to have a working SwiftPM-based build for our RP2040 experiment, and I'd love to try this out.

It seems the last Mac toolchain build is no longer available? Because the CI server only keeps those around for a limited time? (I have no idea how this works.)

Thanks!

@kubamracek
Copy link
Contributor Author

@swift-ci build toolchain macOS

@kubamracek
Copy link
Contributor Author

@kubamracek If you have the time, could you start another macOS toolchain build for me? Or do you have an idea when this PR will be merged? I have a feeling it's the only remaining blocker to have a working SwiftPM-based build for our RP2040 experiment, and I'd love to try this out.

It seems the last Mac toolchain build is no longer available? Because the CI server only keeps those around for a limited time? (I have no idea how this works.)

Kicked off another toolchain build. You're right, the CI server only keeps a limited number of build logs and build artifacts.

I still need to figure out a few things about the inclusion of lld, there might be a bit more delays. I wonder, if all you need is a working lld on macOS, then if a possible short-term workaround for you would be to grab lld from llvm.org? The releases of LLVM for macOS do include it.

@ole
Copy link
Contributor

ole commented Feb 9, 2024

I wonder, if all you need is a working lld on macOS, then if a possible short-term workaround for you would be to grab lld from llvm.org? The releases of LLVM for macOS do include it.

Thank you! Thanks for the llvm.org tip. I've been using the Clang from the ARM embedded LLVM toolchain, and that seems to work too, but it's obviously a little inconvenient for users.

Here's roughly my SwiftPM setup (not public yet, I have to write it all up):

  1. One library target/product for the RP2040 boot2 bootloader. This is a bunch of assembly files, copied from the Pico SDK.

  2. One library target for the RP2040 crt0. This is also some assembly files, plus one or two C files, copied from the Pico SDK.

  3. A SwiftPM static library target/product for the actual RP2040 executable, written in Swift. This target depends on (2), but not on (1) because we need to postprocess the bootloader before linking (see below).

    Perhaps this should be an executable, but I don't think SwiftPM can build embedded Swift executables yet (I forget the exact error message I get when I try that)? And even if we can build an executable in this step, it wouldn't be enough because we need to inject our custom linker scripts etc.

  4. A SwiftPM command plugin that does the following:

    • Build products (1) and (3) separately.
    • Postprocess (1), the bootloader: compute a checksum over the assembled binary, write the checksum into the binary, link everything to the correct location.
    • Call Clang to link the postprocessed bootloader together with (3) into an executable.

I have this working on Linux with the default Swift Docker images, so you can write something like this to build your executable:

swift package --triple armv6m-none-none-eabi link-executable

On macOS, it also works with a current toolchain as long as you have the ld.lld from the ARM embedded LLVM toolchain in your PATH.

@kubamracek
Copy link
Contributor Author

Very cool, please do write it up, or even post this as is to the forums :) There are folks (like me) who are eager to read about doing cool stuff with Embedded Swift.

@MaxDesiatov
Copy link
Contributor

Thank you! Thanks for the llvm.org tip. I've been using the Clang from the ARM embedded LLVM toolchain, and that seems to work too, but it's obviously a little inconvenient for users.

I found lld that comes with a plain brew install llvm is sufficient when cross-compiling from macOS.

@ole
Copy link
Contributor

ole commented Feb 9, 2024

@MaxDesiatov Thank you, I'll try that.

@MaxDesiatov
Copy link
Contributor

Note that the Homebrew install doesn't add it to the usual PATH locations, on Apple Silicon it's /opt/homebrew/opt/llvm/bin/ld.lld and on Intel it should be different from that.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek requested a review from fredriss March 27, 2024 17:33
@kubamracek kubamracek enabled auto-merge March 27, 2024 17:35
@kubamracek kubamracek merged commit e7b6c1d into swiftlang:main Mar 27, 2024
@MaxDesiatov
Copy link
Contributor

Any chance this would be cherry-picked for 6.0?

al45tair pushed a commit to al45tair/swift that referenced this pull request May 29, 2024
[embedded] Start building and including lld even in Darwin toolchains
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
embedded Embedded Swift WebAssembly Platform: WebAssembly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants