Compiling Swift with statically linked standard library on Linux

I've been trying to compile a command line application I wrote, inside the official Swift docker container. However, when I run swift build -v --static-swift-stdlib -c release, I get the following:

lsb_release -r
/usr/bin/swiftc --driver-mode=swift -L /usr/lib/swift/pm/4_2 -lPackageDescription -swift-version 5 -I /usr/lib/swift/pm/4_2 -sdk / /code/Package.swift -fileno 7
/usr/bin/swiftc -module-name tree2latex -incremental -emit-dependencies -emit-module -emit-module-path /code/.build/x86_64-unknown-linux/release/tree2latex.swiftmodule -output-file-map /code/.build/x86_64-unknown-linux/release/tree2latex.build/output-file-map.json -whole-module-optimization -num-threads 2 -c /code/Sources/tree2latex/Tree.swift /code/Sources/tree2latex/helpers.swift /code/Sources/tree2latex/main.swift /code/Sources/tree2latex/template.swift -I /code/.build/x86_64-unknown-linux/release -target x86_64-unknown-linux -swift-version 5 -sdk / -O -j2 -DSWIFT_PACKAGE -module-cache-path /code/.build/x86_64-unknown-linux/release/ModuleCache -Xfrontend -color-diagnostics
/usr/bin/swiftc -sdk / -L /code/.build/x86_64-unknown-linux/release -o /code/.build/x86_64-unknown-linux/release/tree2latex -module-name tree2latex -emit-executable -Xlinker '-rpath=$ORIGIN' @/code/.build/x86_64-unknown-linux/release/tree2latex.product/Objects.LinkFileList

Upon further inspection, ldd shows that, indeed, the binary is not statically linked:

[...]
libswiftCore.so => /usr/lib/swift/linux/libswiftCore.so (0x00007f6d242b3000)
libFoundation.so => /usr/lib/swift/linux/libFoundation.so (0x00007f6d23979000)
libswiftGlibc.so => /usr/lib/swift/linux/libswiftGlibc.so (0x00007f6d249de000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
[...]

Shouldn't the swiftc command have an -static-stdlib option when running swift build or am I missing something?

Thank you!

2 Likes

It doesn't work on Linux right now: [SR-648] swift package manager should have the option to produce statically linked binaries · Issue #5432 · apple/swift-package-manager · GitHub

2 Likes

Thank you for pointing me to the bug report! I had not seen that one.