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):
Init new project: swift package init --type executable
Add import Framework to the created main.swift
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
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.
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 ).
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.
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.
-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.
After I added a -DBUILD_SHARED_LIBS=NO (so changed OFF to NO) I could find a static library:
find / -type f -name "libdispatch.a"e
/usr/local/lib/libdispatch.a
/builds/ios/swift-corelibs-libdispatch/src/libdispatch.a
Unfortunately, after that I still saw a lot of /usr/bin/ld.gold: error: cannot find -ldispatch.
I also spotted:
Attempt to open /usr/lib/swift_static/linux/libdispatch.a failed
so I copied the generated libdispatch.a to this location.
This fixed the error (/usr/bin/ld.gold: error: cannot find -ldispatch) but unfortunately I still have errors during linking stage:
/builds/ios/azimoSlack/.build/x86_64-unknown-linux/release/Core.build/Dispatch.swift.o:Array.swift.o:function $S4Core10background8functionyyyc_tF: error: undefined reference to '$S8Dispatch0A5QueueCMa'
/builds/ios/azimoSlack/.build/x86_64-unknown-linux/release/Core.build/Dispatch.swift.o:Array.swift.o:function $S4Core10background8functionyyyc_tF: error: undefined reference to '$S8Dispatch0A5QueueC6global3qosAcA0A3QoSV0E6SClassO_tFZ'
/builds/ios/azimoSlack/.build/x86_64-unknown-linux/release/Core.build/Dispatch.swift.o:Array.swift.o:function $S4Core10background8functionyyyc_tF: error: undefined reference to '_NSConcreteStackBlock'
/builds/ios/azimoSlack/.build/x86_64-unknown-linux/release/Core.build/Dispatch.swift.o:Array.swift.o:function $S4Core10background8functionyyyc_tF: error: undefined reference to '_Block_copy'
...
It's not linking in swiftDispatch and BlocksRuntime. Adding -lswiftDispatch -lBlocksRuntime should help with those. I would've expected the autolinking to kick in though...
Thanks again for your time.
Unfortunately adding those parameters do not help. I'm not sure if I used them correctly. Firstly I added them to the swift build command
swift build -c release -Xswiftc -static-stdlib -Xswiftc -lswiftDispatch -Xswiftc -lBlocksRuntime
You need to tell the linker where to fine the build of BlocksRuntime and the swift SDK overlay for libdispatch. They should be part of the libdispatch build.
/usr/bin/ld.gold: Released descriptor 272 for "(null)"
/usr/bin/ld.gold: Attempt to open /usr/lib/swift_static/linux/libBlocksRuntime.so failed
/usr/bin/ld.gold: Attempt to open /usr/lib/swift_static/linux/libBlocksRuntime.a failed
/usr/bin/ld.gold: Attempt to open /builds/ios/azimoSlack/.build/x86_64-unknown-linux/release/libBlocksRuntime.so failed
/usr/bin/ld.gold: Attempt to open /builds/ios/azimoSlack/.build/x86_64-unknown-linux/release/libBlocksRuntime.a failed
/usr/bin/ld.gold: Attempt to open //usr/lib/gcc/x86_64-linux-gnu/5.4.0/libBlocksRuntime.so failed
/usr/bin/ld.gold: Attempt to open //usr/lib/gcc/x86_64-linux-gnu/5.4.0/libBlocksRuntime.a failed
/usr/bin/ld.gold: Opened new descriptor 272 for "//usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/libBlocksRuntime.so"
/usr/bin/ld.gold: Attempt to open //usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/libBlocksRuntime.so succeeded
/usr/bin/ld.gold: Unlocking file "//usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/libBlocksRuntime.so"
/usr/bin/ld.gold: Released descriptor 272 for "(null)"
but errors remain the same .
You mentioned also about linking to the swiftDispatch but I can not find anything related to this library.
Do I miss something?
That is part of the swift-corelibs-libdispatch build, which you cannot find a pre-existing package for. The libblocksruntime dependency should no longer be necessary as we are building that always now. The build output should contain a libswiftDispatch.so.
You're right, the libblocksruntime is not required. I don't know how I could miss that before.
Unfortunately, I can not found this share object after the swift-corelibs-libdispatch is built.
I'm searching it using:
find / -name "*swiftDispatch*"
and there is no output.
I'm building the master branch of the swift-corelibs-libdispatch. Today I also tried the swift-4.2-branch but with the same result.
No, I didn't :(.
After I added this flag to the cmake -G Ninja ... I've got:
CMake Error at CMakeLists.txt:55 (message):
CMAKE_SWIFT_COMPILER must be defined to enable swift
so I added a -DCMAKE_SWIFT_COMPILER=/usr/bin/swiftc
and now I have this error:
CMake Error at CMakeLists.txt:58 (find_package):
Could not find a package configuration file provided by "Swift" with any of
the following names:
SwiftConfig.cmake
swift-config.cmake
Add the installation prefix of "Swift" to CMAKE_PREFIX_PATH or set
"Swift_DIR" to a directory containing one of the above files. If "Swift"
provides a separate development package or SDK, be sure it has been
installed.
Hi,
I've tried to use the build-script but unfortunately I couldn't do this on the docker as it required to much space on disk .
I've also tried to install it on a laptop with the linux but after some time I ended up with this error:
Seems like you ran out of memory during the link step. You will probably need 32G of RAM if you are using ld.bfd, and probably around 16G if you are using gold or lld. As to backporting the changes … you need to find the relevant changes, rewrite them to apply and adjust for semantic changes from the tree for the version of swift that you are trying to use and apply them and rebuild the projects.
I've changed the default linker from ld.bfd to ld.gold but this do not help. The errors occurs definitely later then when using the ld.bfd so most likely memory is the reason (laptop has 16 GB of RAM).
this sounds really complicated . I don't know eaven where to start looking for those changes.
Maybe I will wait for swift 5