Distributing binaries for linuxes (static linking?)

What is the best way to distribute apps written in Swift for random glibc-based linux distributions? I found an issue which indicates that static linking does not work yet, maybe it will work with Swift 5? Alternatively, is there a way to package a binary together with all the foundation libraries and libdispatch?

As far as I know, there is no correct way to distribute Swift binaries yet. They are not portable. @Aciid for sure has better information here.

I see... Is there a division between SDK and runtime libraries? To at least not include dev libraries into docker image...

IIRC there are hardcoded paths etc… in the binary. That's one problem at least.

If static linking worked on Linux (which it may with the Swift 5 snapshots, I haven't tried) distributing binaries on Linux would be fine since everything Swift related would be self contained in the binary. With Swift 4.x there's at least one other issue still being tracked here [SR-7039] can't use -static-stdlib (or -static-executable) with any Dispatch using app · Issue #662 · apple/swift-corelibs-libdispatch · GitHub

Right, SwiftPM doesn't have a binary distribution story yet. You can package up things together using scripts but there is no builtin way to do that yet.

And, if I've been following the discussions correctly, you need Swift ABI stability before binary distribution is possible. If I understand correctly, Swift ABI stability has been achieved for only Darwin (MacOSX, iOS, tvOS, watchOS) in Swift 5, not Linux, FreeBSD, Windows, etc..

This shouldn't matter for distributing binaries. No ABI stability just means that distributed binaries must be compiled with the same version of swift that is installed on the machine. ABI stability makes binaries future proof (ie: A binary compiled with swift 5 will still work if swift 6 is installed).

And if you're building statically then none of this should matter anyways since all the stdlib and everything will be included in the binary itself.

3 Likes

If static linking worked on Linux (which it may with the Swift 5 snapshots, I haven't tried)

I just tried, latest 5.0 snapshot on Ubuntu 18.04. Does not work still. Seems like Foundation, BlockRuntime and dispatch are not distributed as .a files.

I just wanted to check-in to see if there is any way in the current version of Swift to build static binaries on Linux? When I tried the option, it seemed to just be ignored. Thanks in advance.

This is an old thread, but it's probably worth pointing out that we now have a downloadable SDK for building fully statically linked binaries for Linux. You can read about it in Getting Started with the Static Linux SDK.

4 Likes

Hi @al45tair

I just downloaded the toolchain for 5.10.1 (Mac) and tried to install the linux static sdk, but it fails due so something missing in the archive. I may be doing something wrong, but I fail to see what it is. Here's what I'm doing:

xcrun --toolchain swift  swift experimental-sdk install https://download.swift.org/development/static-sdk/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1.artifactbundle.tar.gz
Downloading a Swift SDK bundle archive from `https://download.swift.org/development/static-sdk/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1.artifactbundle.tar.gz`...
Swift SDK bundle archive successfully downloaded from `https://download.swift.org/development/static-sdk/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1.artifactbundle.tar.gz`.
Swift SDK bundle at `https://download.swift.org/development/static-sdk/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1.artifactbundle.tar.gz` is assumed to be an archive, unpacking...
Error: failed parsing ArtifactsArchive info.json at '/var/folders/k7/nxcvrqvj069205_kp2lr169r0000gn/T/TemporaryDirectory.XoDyy0/extraction-results/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1.artifactbundle/info.json': keyNotFound(CodingKeys(stringValue: "supportedTriples", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "artifacts", intValue: nil), _JSONKey(stringValue: "swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1", intValue: nil), CodingKeys(stringValue: "variants", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"supportedTriples\", intValue: nil) (\"supportedTriples\").", underlyingError: nil))

You need to use a Swift 6 OSS nightly with it. You can’t use 5.10, which I think is what you’re using there, because the bundle format has changed slightly and it isn’t compatible, as you’ve found.

1 Like

Oh, I see —Thanks!

Still, tried this combination, too with no luck either. In this case the error was different, though (segmentation fault)

after originally posting this, I downloaded the "main" snapshot and tried again:

xcrun --toolchain swift -toolchain org.swift.59202406081a swift --version
Apple Swift version 6.0-dev (LLVM 579155491d559cc, Swift 64869e5a42221b2)
Target: arm64-apple-macosx14.0

xcrun --toolchain swift -toolchain org.swift.59202406081a swift sdk install /Volumes/Data_EXT/downloads/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1.artifactbundle.tar.gz 
Swift SDK bundle at `/Volumes/my-external-drive/downloads/swift-DEVELOPMENT-SNAPSHOT-2024-06-06-a_static-linux-0.0.1.artifactbundle.tar.gz` is assumed to be an archive, unpacking...
zsh: segmentation fault  xcrun --toolchain swift -toolchain org.swift.59202406081a swift sdk install 

arch
arm64

Hmmm. That obviously should not be happening. Could you possibly send the crash logs to us so we can figure out what's going on? Feedback Assistant would be a good way to do it, if possible.

Just did, thanks! Hope "system diagnostics" contains the crash logs.

It should do. Thanks. Just so I know which thing I'm looking at, could you share the Feedback number? (FB…?)

I reported this problem under swift sdk install crashes in URLSessionHTTPClient.execute and Archiver.extract when built with Swift 6.0 · Issue #7648 · apple/swift-package-manager · GitHub.

SPM built with Swift 6 toolchain has crashes; but building with Swift 5.10 doesn't have this crash.

Hi! I currently can't compile a static hello binary on Ubuntu 24.04 with Static Linux SDK.

I downloaded and installed:

swift-6.0-DEVELOPMENT-SNAPSHOT-2024-06-08-a-ubuntu22.04.tar.gz

and

swift-6.0-DEVELOPMENT-SNAPSHOT-2024-06-08-a_static-linux-0.0.1.artifactbundle.tar.gz

$ swift --version
Swift version 6.0-dev (LLVM bf8bbb910874156, Swift a0bb875256e41ae)
Target: x86_64-unknown-linux-gnu
$ swift sdk list
swift-6.0-DEVELOPMENT-SNAPSHOT-2024-06-08-a_static-linux-0.0.1

When I try to repeat the build steps listed on Swift.org - Getting Started with the Static Linux SDK

I get the following folders:

$ ls -1 .build/
artifacts
checkouts
debug
debug.yaml
repositories
workspace-state.json
x86_64-unknown-linux-gnu

There is no x86_64-swift-linux-musl folder, and all the compiled binaries are not statically linked.

$ objdump -x .build/x86_64-unknown-linux-gnu/debug/hello | grep NEEDED
  NEEDED               libswiftSwiftOnoneSupport.so
  NEEDED               libswiftCore.so
  NEEDED               libswift_Concurrency.so
  NEEDED               libswift_StringProcessing.so
  NEEDED               libswift_RegexParser.so
  NEEDED               libc.so.6

Any idea why that is? Thanks!

What build command did you use? It should have been

swift build --swift-sdk x86_64-swift-linux-musl

Mostly we've been testing this by cross-building from macOS, whereas I think you're using Linux here as the host, but it should still work.

1 Like