Build the executable project on Linux using -Xswiftc -static-stdlib failed

linux
spm
build

(Mateusz) #1

Hi.

Is it possible to build a swift project on Linux using the -Xswiftc -static-stdlib flags?
What I want to archive, is to create command line tool which can be executed on other Linux machines without the need to install the swift. As far as I understand the -Xswiftc -static-stdlib flag should allow this but adding this flag produce errors during the Linking stage.

Compile Swift Module 'ExampleProject' (1 sources)
Linking ./.build/x86_64-unknown-linux/release/ExampleProject
/usr/bin/ld.gold: error: cannot find -ldispatch
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
error: terminated(1): /home/mateusz/Developer/swift/4.2.1/usr/bin/swift-build-tool -f /home/mateusz/Developer/ExampleProject/.build/release.yaml main output:

Steps to reproduce (I assume that the Swift has been already installed):

  1. Init new project: swift package init --type executable
  2. Add import Framework to the created main.swift
  3. Build: swift build -c release -Xswiftc -static-stdlib

Additional information
Swift version:

Swift version 4.2.1 (swift-4.2.1-RELEASE)
Target: x86_64-unknown-linux-gnu

Here you can found output from the swift build -c release -Xswiftc -static-stdlib -Xlinker --verbose command

I have read the Building distributable executable using SwiftPM but I'm not sure if it describes the same problem :(.


(Saleem Abdulrasool) #2

Seems that (static) libdispatch is missing. I had fixed that a while ago, but, that was broken on the 4.2 release, so you would need to build from the current master to get that to work.


(Mateusz) #3

Thanks a lot for your response!

I assume that you are talking about swift-corelibs-libdispatch.

Is it a known issue? Do we have an issue that I could follow?
If this is a regression, then I believe, it is a good candidate for a test somewhere (if possible :thinking:).


(Saleem Abdulrasool) #4

Yeah, it is a known issue. It should have been resolved for the 5.0 release along with a test for that in the validation set to ensure that both versions of the library are present.


(Mateusz) #5

Hi,
Thank you for your patience :).

I tried to install manually the swift-corelibs-libdispatch using:

cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ <path-to-source>
ninja
ninja install

but this produces only the libdispatch.so and if I understand it correctly this kind of libraries are linked dynamically.
Do you know how I can build it to get a libdispatch.a? Is there some additional parameter? I tried a -BUILD_SHARED_LIBS:ON/OFF but without luck.

Thanks in advice :)


(Saleem Abdulrasool) #6

-DBUILD_SHARED_LIBS=NO is required if you want to build it statically. How did that fail? That is currently being built by build-script --build-libdispatch, so I would expect this to work.