How to build a Swift program as a static executable? - swift

I do not have any familiarity with the Swift Package Manager, but I have to compile a Swift program (https://github.com/ltentrup/SafetySynth, if it matters) as a statically linked executable (on Linux).
First of all. Is it possible?
The project uses the Swift Package Manager to build the executable, through the swift build command.
Is there any command line flags to tell the SPM to make a static executable?
This question specifically concerns the Swift Package Manager, because I'm on Linux, so I'm not using Xcode (and the project does not use Xcode neither).

Related

Create macOS application package with swift-package-manager on linux

Is it possible to create a application package (directory {ApplicationName}.app) using the Swift package manager in the linux version of the Swift? With GNUStep, adding a GNUmakefile file to my project, I can do that (and for what I read, I could run this application on a macOS machine? Or am I wrong?). How to do that with Swift and the SPM? After execute swift build, I can see that the binaries are located on directory like .build/x86_64-unknown-linux-gnu/debig (or .build/x86_64-unknown-linux-gnu/release, if I use swift build -c release). How I build the application package with the files located here?

How can I build and test a Swift/C library from the Linux command line

I have Swift code that wraps a C-library.
I want to build and test it under Linux.
How should I build the library, associated tests and run the tests?
I was tempted by the idea of creating a Swift Package but I'm not sure if I can use a C-library as part of the package if I do that.

Does Swift executable binary need the .swiftmodule, .swiftdoc and .build files to run?

I'm writing my Swift app for Ubuntu using Vapor. And my mission is to have the smallest Docker image for production. I've trimmed down my image significantly but I wanted to know, just out of curiosity, does my final executable need all the compiled .module, .doc and .build files in the same directory?
tl;dr: No.
The folders/files you listed are byproducts of the build process and can be safely discarded.
When it comes to distribution, your application is just like any other Linux executable. You must have all dynamically linked libraries available on the target system.
These include the runtime libraries of the Swift toolchain plus any compiled C modules your application (or the framework beneath it) links with (*).
You can check the dependencies of the executable using the ldd command.
Some of them are available as packages, some of them will need to be copied to the target system manually.
(*) In case of a Vapor 2 application, such C modules are libCHTTP.so and libCSQLite.so, which are placed in your build folder.

How do you install Swift package binaries after building them?

After building a package how do you install it on the system?
For example, I'm trying to install Swift language server but I have no idea what to do after swift build. Do I have to copy executables and libraries manually?
In CMake/make world there is always a make install step. Is there anything similar in Swift package manager? There doesn't seem to be an install command or something similar. Am I missing something?
Swift Package Manager produces plain executables, in .build/debug or .build/release directory. You can see the last line in its output Linking .build/debug/<the name of the main module in the Package>.
If not specified otherwise, you can just run the result executable, as any other executable, by typing its path in the command line and providing parameters as needed.
Swift Package Manager does not support custom scripts or targets, like install, deploy etc. If there is a need for installation/deployment automation, it should be done by additional scripts or tools, like Makefile.

How to compile Swift programs for testing on the command line

I am trying to set up a Swift project on the command line, preferably for both OS X and Linux. I managed to compile Swift source to executable binaries, dynamic libraries and Swift modules on OS X. Now I am trying to get testing to work by using the XCTest framework. When I am trying to compile the testing module, the compiler tells me that my module was not "compiled for testing". While there are a lot of hints about "enabling testability" in Xcode, I couldn't find any pointers about doing this with the Swift command line compiler. So, how can do this?