Skip to content

Commit 9b88f7b

Browse files
incertumal45tairetcwilde
authored
[static-linux-sdk] Update "Getting Started with the Static Linux SDK" (#1120)
* [authors] add incertum Signed-off-by: Melissa Kilby <[email protected]> * [static-linux-sdk] Update "Getting Started with the Static Linux SDK" Enhance clarity and transparency by adding more details about dependency management when using the Static Linux SDK, especially regarding the dependencies bundled with it. Co-authored-by: Alastair Houghton <[email protected]> Signed-off-by: Melissa Kilby <[email protected]> * [static-linux-sdk] Update "Getting Started with the Static Linux SDK" Add a comment contrasting the Linux SDK with Apple platforms. Co-authored-by: Evan Wilde <[email protected]> Signed-off-by: Melissa Kilby <[email protected]> --------- Signed-off-by: Melissa Kilby <[email protected]> Co-authored-by: Alastair Houghton <[email protected]> Co-authored-by: Evan Wilde <[email protected]>
1 parent b2619b8 commit 9b88f7b

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

_data/authors.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,9 @@ umeshbatra13:
533533
name: Umesh Batra
534534
github: umeshbatra13
535535
about: "Umesh Batra is a member of the engineering team at Apple working on Password Monitoring and other Security Services."
536+
537+
incertum:
538+
name: Melissa Kilby
539+
540+
github: incertum
541+
about: "Melissa Kilby works on the Swift for Linux Server team at Apple."

documentation/articles/static-linux-getting-started.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: page
33
date: 2024-06-04 12:00:00
44
title: Getting Started with the Static Linux SDK
5-
author: [al45tair]
5+
author: [al45tair, incertum, etcwilde]
66
---
77

88
It's well known that Swift can be used to build software for Apple
@@ -24,12 +24,30 @@ with no external dependencies at all (not even the C library), which
2424
means that it will run on _any_ Linux distribution as the only thing
2525
it depends on is the Linux system call interface.
2626

27+
This portability comes at a cost, namely that everything your program
28+
depends on must be statically linked. There is no support for dynamic
29+
linking whatsoever — even the `dlopen()` function will not work.
30+
31+
A result of this design choice is that the Static Linux SDK uses a
32+
“bring your own dependencies” model, similar to that you might be used
33+
to with the Swift Package Manager. You cannot use system libraries,
34+
but must either rely on the handful of common libraries supplied with
35+
the Static SDK (see below), or build any extras yourself.
36+
2737
Additionally, the Static Linux SDK can be used from any platform
2838
supported by the Swift compiler and package manager; this means that
2939
you can develop and test your program on macOS before building and
3040
deploying it to a Linux-based server, whether running locally or
3141
somewhere in the cloud.
3242

43+
Finally, for those wondering about an equivalent for Apple platforms,
44+
no such static SDK exists. Building a fully static executable is not
45+
possible on Apple's operating systems because, unlike Linux, the
46+
Darwin kernel's system call table is not part of the ABI. This design
47+
requires all system calls to be routed through the dynamic library
48+
`libsystem.dylib`, fundamentally preventing a 100% statically linked
49+
binary.
50+
3351
### Static vs Dynamic Linking
3452

3553
_Linking_ is the process of taking different pieces of a computer
@@ -255,7 +273,7 @@ in another, or a pointer type will be imported as `OpaquePointer`
255273
rather than `UnsafePointer<FOO>`.
256274

257275
If you do find yourself needing to make these kinds of adjustments,
258-
you can make your local copy of the package dependency editable by
276+
you can make your [local copy](https://developer.apple.com/documentation/xcode/editing-a-package-dependency-as-a-local-package) of the package dependency editable by
259277
doing
260278

261279
```console
@@ -265,3 +283,69 @@ $ swift package edit SomePackage
265283
and then editing the files in the `Packages` directory that appears in
266284
your program's source directory. You may wish to consider raising PRs
267285
upstream with any fixes you may have.
286+
287+
If your project makes use of C or C++ language libraries, you may need
288+
to take additional steps. The Static SDK for Linux includes a small
289+
handful of very common dependencies (e.g.
290+
[libxml2](https://gitlab.gnome.org/GNOME/libxml2/-/wikis/home),
291+
[zlib](https://www.zlib.net/) and [curl](https://curl.se/)). There is
292+
a high bar for adding dependencies to the SDK itself, because it makes
293+
the SDK image larger, and means the SDK must be updated to track the
294+
versions of those dependencies.
295+
296+
The Static SDK includes an SBOM, in [SPDX format](https://spdx.dev/),
297+
that you can use to determine exactly what is present in any given
298+
release of the Static SDK for Linux. For instance, using the `bom`
299+
tool, you can display the SBOM using a command like:
300+
301+
```console
302+
$ bom document outline ~/.swiftpm/swift-sdks/swift-6.1.2-RELEASE-static-linux-0.0.1.artifactbundle/sbom.spdx.json
303+
_
304+
___ _ __ __| |_ __
305+
/ __| '_ \ / _` \ \/ /
306+
\__ \ |_) | (_| |> <
307+
|___/ .__/ \__,_/_/\_\
308+
|_|
309+
310+
📂 SPDX Document SBOM-SPDX-648fa59a-9d9d-476f-9183-78d57d847c31
311+
312+
│ 📦 DESCRIBES 1 Packages
313+
314+
├ Swift statically linked SDK for [email protected]
315+
│ │ 🔗 7 Relationships
316+
│ ├ GENERATED_FROM PACKAGE [email protected]
317+
│ ├ GENERATED_FROM PACKAGE [email protected]
318+
│ ├ GENERATED_FROM PACKAGE [email protected]
319+
│ ├ GENERATED_FROM PACKAGE [email protected]
320+
│ ├ GENERATED_FROM PACKAGE [email protected]
321+
│ ├ GENERATED_FROM PACKAGE boringssl@fips-20220613
322+
│ └ GENERATED_FROM PACKAGE [email protected]
323+
324+
└ 📄 DESCRIBES 0 Files
325+
```
326+
327+
If your project has additional C/C++ dependencies, the process is the
328+
same as using any static library you’ve built yourself in any other
329+
context. You must ensure the static library (`.a` file) is in the
330+
linker’s search path. Additionally, if you intend to call the
331+
library's functions directly from your Swift code, you must also add
332+
its header files to the compiler's include path. The only
333+
Swift-specific part is that you will need a module map for the
334+
library, but this is also true outside of the Static SDK for Linux
335+
(see [Mixing Swift and
336+
C++](https://www.swift.org/documentation/cxx-interop/)).
337+
338+
Some of the dependencies bundled in the Static SDK may be pulled in by
339+
Swift’s runtime libraries, if you use the functionality that requires
340+
them — for instance, Foundation Networking uses `libcurl` and
341+
`libcurl` uses `libz` — but because of the way static linking works,
342+
you will generally only “pay for what you use”.
343+
344+
You may be able to override the versions of libraries that ship with
345+
the Static SDK by placing a newer build of the library earlier in the
346+
linker’s search path. Note however that where other libraries that
347+
ship with the Static SDK have been built against the library in
348+
question, your new build will need to be ABI compatible with the
349+
version that shipped in the Static SDK, since the other libraries in
350+
the Static SDK will have been built against the headers from the
351+
version that they ship with.

0 commit comments

Comments
 (0)