spn build compiles your project and all dependencies. Compilation targets a triple (e.g. x86_64-windows-gnu) and uses a profile (e.g. release, O2, statically linked).
Build output
By default, spn outputs to build/. If you explicitly pass a triple, it uses build/triple/profile. Otherwise, it uses build/profile. If you were to run this:
spn build
spn build --target x86_64-windows-gnuIt would produce this:
build/
├── debug/
└── x86_64-windows-gnu/
└── debug/Inside a given build, each executable is placed at the top level. The shared libraries it needs (itself or transitively) are staged next to it. The executable is relocatable.
Tests and examples get the same treatment under test/ and example/. Every package, including your dependencies, has its own prefix under store/ holding the headers, libraries, and binaries it produced. Finally, a JSONL file with a detailed trace of the build is in .spn/build.jsonl
build/debug/
├── foo.exe
├── bar.dll
├── store/
│ ├── foo/
│ │ └── bin/
│ │ └── foo.exe
│ └── bar/
│ ├── lib/
│ │ └── bar.dll
│ └── include/
│ ├── bar.h
│ └── ...
└── .spn/
└── build.jsonlProfiles
Packages are compiled against a profile, which contains the target triple, toolchain, build mode, optimization level, sanitizers, etc. spn bakes in two profiles, debug and release, and reserves [profile.default] as the base fields which every profile uses unless overridden.
[profile.default]
toolchain = "zig"
static_deps = true
standard = "c11"
mode = "debug"
[profile.debug]
opt = "2"
[profile.foo]
abi = { value = "gnu", when = { os = "linux" } }
toolchain = [
{ value = "clang", when = { os = "macos" } },
{ value = "gcc" },
]Here, debug applies optimization on top of the default profile, and foo overrides the toolchain and ABI. Lists are tried in order such that the first match wins and an ungated entry is the fallback.
When building:
spn buildusesdebugspn build --mode releaseusesreleasespn build --profile NAMEuses the named[[profile]]
Optimization
opt in the profile overrides the mode's default, and can be 0, 1, 2, 3, s, or z. When provided by the toolchain, s optimizes for size, and z does so more aggressively.
Stripping
strip controls what is stripped from binaries: none does nothing, debuginfo strips debug information but otherwise nothing, and symbols also strips unused symbols (which may be undesirable for some kinds of libraries)
Build modes
mode is a field of the profile which controls whether you build a debug or release binary. By default, it is debug. The field determines whether your binary contains debug info and whether NDEBUG is defined, plus defaults for optimization and stripping:
| Mode | Debug info? | NDEBUG | Optimization | Strip |
|---|---|---|---|---|
debug | yes | not defined | -O0 | none |
release | no | defined | -O2 | debuginfo |
Sanitizers
spn has first class support for sanitized builds. It supports address, thread, undefined, memory, and leak.
[profile.safe]
sanitize = ["address", "undefined"]spn build --sanitize thread,leak
# --sanitize always overrides the profile
spn build --sanitize thread,leak --profile safe
spn build --sanitize none --profile safeAvailability
spn doesn't bundle sanitizers. It uses what's provided by the toolchain. spn never probes to discover what a toolchain supports, but rather expects it to be declared. We therefore provide a reasonable default support matrix for system toolchains.
| Toolchain | Linux | macOS | Windows |
|---|---|---|---|
zig | undefined, thread | undefined, thread | undefined |
clang, llvm | address, thread, undefined, memory, leak | address, thread, undefined, leak | none |
gcc | address, thread, undefined, leak1 | address, thread, undefined | none |
msvc, llvm | address2 |
Of course, without brittle probing, spn can't provide a default that covers every real toolchain installed on a user's machine. If your system's toolchain is different, or if you're pinning to an external toolchain, it's easy to declare what sanitizers it supports.
Toolchain selection
When you don't explicitly pass a toolchain, spn may pick a toolchain that can support the sanitizers you asked for. For example, --sanitize address can't be satisfied by the default Zig toolchain, so spn might fall back to your system clang. If this is undesirable, pass a toolchain explicitly.
Everything except undefined needs a dynamic loader. A native Linux build switches from static musl to glibc, and libc = "static" is an error.
Errata
The built in Zig toolchain enables UBSan in debug builds by default. Thank you, Zig!
Cross compilation
Cross compilation is one of the fundamental principles of spn, and setting aside draconian practices like copyrighting your platform's headers3 there's no reason that any piece of software shouldn't be able to be cross compiled.
To cross compile, pass --target $triple. A triple is of the format $arch-$os-$abi, and the ABI can be omitted when the OS only supports one ABI (like macos and apple). The following values are supported:
- arch:
x86_64,aarch64,wasm32 - os:
linux,macos,windows,wasi,freestanding - abi:
gnu,musl,msvc,apple,none,elf
Here are a few sample triples of note:
| Triple | Notes |
|---|---|
aarch64-macos | Equal to aarch64-macos-apple |
wasm32-wasi | Equal to wasm32-wasi-musl |
wasm32-freestanding | Equal to wasm32-wasi-none |
x86_64-freestanding-none | Raw machine code, like a kernel |
x86_64-linux-none | Raw ELF binary, no libc |
x86_64-windows-gnu | MinGW |
You must give a full triple (e.g. x86_64-linux-gnu) when cross compiling. We never infer facts about a cross compile from the host that builds it, because we have no information as to which ABI is preferred.
zig cc
Out of the box, spn uses the Zig toolchain, which means that it can cross compile your code to Linux, Windows, macOS, and WebAssembly from any host, with no external tools or compilers needed. This is largely thanks to the truly, truly excellent zig cc, which does the work of bundling together and unkinking countless toolchains and provides a build of clang that's preconfigured to use them4.
macOS
A macOS program that links frameworks needs a macOS SDK, and Apple's licensing prevents us (or anyone) from redistributing the headers and .tbd stubs needed to compile and link from a non-macOS host5.
--os,--arch, and--abiset one part each.os,arch, andabiin a profile make the target permanent.- The bundled zig cross compiles to Linux, Windows (MinGW), macOS, and WebAssembly from any host, with nothing else installed.
- System gcc, clang, and MSVC only build for the machine they run on, unless you declare a cross toolchain.
Windows
Because the bundled Zig toolchain ships with MinGW sources, Windows builds by default target MinGW. MSVC has first class support, and spn will detect your Visual Studio installations and Windows SDKs automatically; just pass --toolchain msvc, or see the toolchain docs for how to pick between installations or use e.g. xwin.
Runtime and libc
Besides their dependencies, there are two things that programs control that affect their portability6:
runtime. Libraries linked by the compiler, like the unwinder, low level math routines, the C++ standard library (e.g.libstdc++,libgcc,winpthreads)libc. The C standard library (e.g.libc.so,libSystem.dylib, the UCRT)
Specify them like this; both fields can be static or shared:
[profile.whatever]
runtime = "static"
libc = "shared"Defaults
Different platforms have different stances on some or all of these knobs, but by default spn build produces the most self contained binary that it can. Here's the values that get picked for different combinations of target:
| OS | ABI | runtime | libc | Notes |
|---|---|---|---|---|
linux | musl | static | static | The default on any Linux box |
linux | gnu | shared | shared | spn will implicitly switch to the host's libc when shared libraries are allowed, some dependency or sanitizer runtime cannot be statically linked, and a target wasn't specified |
macos | apple | shared | shared | macOS forces programs to link dynamically to libSystem.dylib (i.e. libc) |
windows | gnu | static | shared | MinGW does not provide a libc to statically link, and regardless ucrtbase.dll ships with Windows |
windows | msvc | static | static | Avoid requiring Visual C++ Redistributables, by default[^9] |
Dependencies
By default, dependencies are linked statically, and if a package doesn't support static linkage it will be linked dynamically when your target supports it. For example, if you're building against glibc and producing a dynamically linked binary, spn will build and link to libfoo.so if libfoo can't be compiled statically. But if you're building a statically linked musl binary, it will fail.
If you don't want this fallback to dynamic linkage, setting static_deps on your profile will fail when static linkage isn't available7:
[profile.whatever]
static_deps = trueThis isn't an override. Rather, it's a check. It tells the build system "Unless I explicitly asked for a shared library, do not allow one". You can control how individual dependencies link in the [config] section:
[config.foo]
kind = "shared"Taking the two snippets above, here's what would happen with three hypothetical dependencies:
| Library | Supports | Result |
|---|---|---|
libfoo | static, shared | libfoo.so, because you explicitly asked for it |
libbar | static, shared | libbar.a, because dependencies link statically by default |
libqux | shared | Failure, because you told spn not to fall back to dynamic linkage |
Libraries
When you build the package itself, a library is built as static when it offers it. A profile's emit lists the kinds to build instead:
[profile.release]
emit = ["static", "shared"]emit only chooses for a library that offers both kinds. An archive that an executable in the same build links is built either way.
A shared kind needs a dynamic loader, so asking for one moves the default Linux build off its static libc, and executables in that build link libc dynamically too. Where there is no loader, as with libc = "static", the shared kind is skipped.
WASM
WASM has no concept of a shared library in the same sense as a traditional OS. When compiling a target for WASM:
- If it's your top level target,
sharedmeans to link it as a module,foo.wasm. On WASI, that's a reactor. - If it's a dependency,
sharedandstaticare equivalent, and it's just a regular static archive
Portability
runtime and linkage are what most often produce unportable binaries. If you're experienced with C/++, feel free to skip these examples. If you aren't, I've provided them to show why these knobs aren't arbitrary.
Gotcha: Why doesn't my program work on Ubuntu?
linux + gnu + shared libc
The classic Linux trap. Compile on a machine with, say, glibc 2.69, and unknowingly pull in some function that was just added. Ubuntu is known for having a slow, fixed release cycle8, which means it might come with, say, glibc 2.34, which doesn't have that function. Your program crashes because it can't load the function it needs.
Gotcha: Why can't I open a window?
linux + static libc
Static linkage means libc is inside your binary. You ask the user's machine for nothing. But, by definition, it means there's no loader for shared libraries9, so if you need to load a graphics driver, X, Wayland, or any kind of plugin, you're F'd.
Gotcha: What the heck is a redistributable?
windows + msvc + shared runtime and libc
Probably the classic Windows trap, and fundamentally the same as its Linux counterpart, except instead of versioning individual symbols the version is for the entire binary. The user therefore needs the correct version binary at runtime, and if they don't they get the dreaded VCRUNTIME140.dll not found.
Gotcha: Freeing a std::string crashed my program
windows + msvc + static runtime/libc
If you've ever freed a std::string allocated in a DLL and crashed your program, it's because static gives everyone their own heap and CRT state. It's as if you tried to free a string that was allocated by taskmgr.exe.
Gotcha: Nothing I build with MinGW works
windows + gnu + shared runtime
shared by definition means "load at runtime", which means the library must exist at runtime. For MSVC, that's usually OK, because redistributables are commonly installed. But here, you're requiring the user to have e.g. libstdc++-6.dll installed, which here's a secret: They don't.
Gotcha: macOS
Joke's on you. There are no gotchas on macOS here. A walled garden has its perks, eh?
Cheat Sheet
Here are a few examples of useful combinations of these knobs. This is, of course, not exhaustive. If you need help understanding any of these options, or if you know what you want but aren't sure how to tell spn to do it, stop by our Discord or send me an email.
| Build | Deps | Runtime | Libc | ABI | |
|---|---|---|---|---|---|
| A Linux binary that runs anywhere | static | static | static | musl | This is a completely self contained binary that will run on any Linux system you'll find in the wild; spn does this by default |
| A portable Linux video game | any | static | shared | gnu | Games need Vulkan or OpenGL, which must be used from a shared library, but otherwise bundle everything else they need inside the binary |
| A Windows binary which doesn't need redistributables | any | static | static | msvc | Like /MT; The UCRT, vcruntime, and the C++ STL all live inside the executable |
| A plugin that gets loaded by a DAW | any | shared | shared | You want to share the host's runtime, so that an e.g. std::string can cross between your DLL and the host cleanly | |
| A fully sanitized build (ASan, TSan, MSan) | shared | any | These sanitizers don't work without a loader |
JSON event stream
When given --json, every command writes the JSONL event stream instead of normal, human readable output. .spn/build.jsonl contains the last build's events, regardless of whether you passed the flag.
Each event in the stream is a tagged union:
{
"event": "target_build_passed",
"level": "debug",
"data": { ... }
}For a full list of events emitted and their shape, see the schema on GitHub, but some useful data emitted in this event stream is:
- Every invocation of the compiler or linker, with full command line and timing
- Every call to the guest API
- Every package resolve and download, with URL and timing
The event stream is a simple rendering of the events delivered to any in process consumer of libspn.
Footnotes
-
Only for the
gnuABI; stock GCC's sanitizer runtimes are generally incompatible with musl. These are broad defaults. They are not intended to model arbitrary, unknown systems. They're meant to make the common case of downloadingspnand using it against whatever you have installed work reasonably well, with no configuration or probing. If they don't work for you, simply write down what your toolchain can do andspnwill happily oblige. ↩ -
x86_64 only. ↩
-
Get bent, Apple. Seriously. ↩
-
spnwouldn't exist without Zig. Part of that is the massive amount of work and care taken to createzig ccand gift it to the world, gratis. And part of it is the attitude that simply refuses to accept the status quo of systems programming, the love and care they put into fixing it, and the simple existence proof that you can build software like this. Thank you, Zig! ↩ -
From my research, this isn't exactly correct. Many such headers are released under Apple's open source license, and from my understanding could be bundled with
spn. However, it's an incomplete set, and more importantly they differ from the actual headers you might find in a real SDK. Even though it's incomplete, it's not trivial. But, most importantly, it doesn't cover Cocoa which is Objective-C. It is more complicated than that, since you can write C headers againstlibobjc, but very few upstream libraries actually compile against such an interface and in general I have no idea how feasible that is3. I'm sure my research is scattershot compared to someone who actually understands all of this, so if you are that person, hit me up on Discord or send me an email. ↩ -
There are more, of course, like the mess of versioned symbols in glibc. But for our purposes, three is fine. ↩
-
It is often desirable to statically link to all of your dependencies while linking dynamically to libc. For example, a program which wants to be hermetic against the versions of libraries on the user's machine, but which wants to load plugins at runtime. To this end, you'd like to compile your dependencies statically or fail the build. ↩
-
I don't mean to pick on Ubuntu; this is a univeral problem in the Linux world. Folks may be more familiar with it on Ubuntu given its popularity and release cadence. ↩
-
If there were, you'd need to ensure that the libraries it loads are ABI compatible with your binary, which means you need to be using the same libc, which means you need to dynamically link to it! ↩