I was wondering about how static linking works for Swift today and how that compares to what might be useful and desired. There is a lot of text here to lay the context before getting to the actual question, so please pardon the long message.
My understanding is that there are two flags that are relevant to this today:
-static-stdlib
: statically link the standard library-static-executable
: statically link the entire executable
An aside on `-static-library`
There is a related flag which is -static-library
. However, as static libraries are not linked at build time, this merely changes the compilation to form a static library. This is important for Windows as this changes the compiler behaviour - public symbols will not be exported to ensure that the consumer of the library does not vend the ABI surface.
-static-executable
forms a completely static executable: fully statically linked C, C++, and Swift runtimes and any dependencies. This is a completely static binary. This is reasonable if you do not need any type of dynamic linking at all.
The interesting case however is -static-stdlib
. First of all, this is a misnomer AIUI, it does not control the static linking of the Swift standard library, but rather all Swift SDK libraries, i.e., it controls the static linking of the standard library, the runtime, and the core libraries (which now includes the non-corelibs Foundation). Granted that the runtime is always statically linked to the standard library, the effective point becomes: -static-stdlib
also indirectly controls the static linking of Foundation and dispatch.
With that context, I finally arrive to my motivating question: does it make sense or is it ever useful to consider a statically linked standard library and a dynamically linked core library? That is, could it ever be beneficial to statically link Foundation and dynamically link swiftCore?
I can imagine cases where a host application is written in Swift but does not use Foundation and a plugin would like to use a small portion of Foundation. Similarly, I can see potential applications to the embedded environments. This obviously is unlikely to ever be useful for the Apple platforms given as these libraries are shipped with the OS, but for other platforms, there is the open question. It could also be that I am forgetting some reasoning for this to be prohibited (e.g. the inverted case of static standard library and dynamic core libraries would not be reasonable to support as one runs afoul of the one runtime per address space restriction).
CC: @kubamracek @al45tair