Packages

A package is a collection of executables and libraries, tests, and examples, called targets. Targets have fields like source, or include, or system_deps, and most fields can be configured at the package level for all targets or for an individual target.

We'll cover conditional compilation on the next page, but for now it's enough to know that nearly every field can be set conditionally with a when clause:

[[lib]]
system_deps = {
  { lib = "advapi32", when = { os = "windows" } },
}
source = [
  { path = "src/audio/pipewire/*.c", when = { os = "linux", pipewire = true } },
]

Targets

There are five kinds of targets, as shown in the example above.

[package]
name = "foo"
include = ['common']

[[lib]]
name = "foo"
source = ['source/lib.c']
include = ['source/lib']

[[example]]
name = "kram"
source = ['example/kram.c']

[[test]]
name = "unit"
source = ['test/main.c', 'test/foo.c']
define = ['BAR', 'BAZ=69']
system_deps = ['m']

[[tool]]
name = "serve"
source = ['tools/server.c']
deps = ['foo']

Executables

[[bin]]. These are executables which are exported by your package. They're placed at your build's top level, $build.

Libraries

[[lib]]. These are libraries which are exported by your package. Every [[lib]] declared by a dependency is compiled unconditionally, and no executables are compiled.

A library declares the linkages it supports with the kinds field. For example, a library that can't work when compiled statically:

[[lib]]
kinds = ["shared"]

Examples

``[[example]]. These are examples, placed in $build/example/`

Tests

[[test]]. These are tests, for some definition of the word test. spn has no test framework or integration with one. Instead, [[test]] executables are reguler executables which:

  • Are not exported
  • Have their own dependencies in [deps.test].

spn test runs all such executables and treats a nonzero exit code as failure. For more information, read about the task runner features of spn.

Tools

[[tool]]. These are executables that aren't exported by your package. They're used like scripts in a package.json, (usually) simple, native tools that you run locally. They have their own dependencies in [deps.tool].

Metaprograms

[package.configure] and [package.build]. Metaprograms are just like any other executable, except that they're compiled to WebAssembly and run inside a sandbox in your build. For more information, check out the documentation on metaprograms.

Unlike other targets, metaprograms are restricted to one per package (i.e. one configure metaprogram, one build metaprogram).

Shared fields

Several target fields can also be declared at the package's top level (include, define, system_deps, host_deps, and macos). These are merged in to each target. For example, the following executable would get common/ and special/ as its include directories:

[package]
include = ["common"]

[[bin]]
include = ["special"]

For a full list of fields, see the manifest reference.

Sources

The source field lists a target's sources. Each entry is either a plain string, for common files and globs, or a table, when the entry is conditional or not in the repository (more on these in a bit).

Paths are always relative, must use forward slashes, and cannot contain . or .. segments. Code outside the package's directory

Finally, language is inferred from the entry's extension:

  • .cpp, .cc, .cxx, .c++, and .C compile as C++
  • .s, .S, and .asm compile as assembly
  • Everything else compiles as C (e.g. .m files)

Globs

Paths can be written as globs. spn implements shell-style globs, plus **. That means you can use *, **, ?, [a-z], [!a-z], [^a-z], and {a,b}. Wildcards also match directory components. It differs from how the shell usually works in a few ways:

  • * matches dotfiles
  • ** matches at any depth
  • * and ? never cross a separator
  • There's no escaping. \ is just rewritten to /
  • Matching is byte-exact on every OS, so e.g. *.c doesn't match FOO.C
  • Unlike .gitignore, where foo/ matches a/b/foo, our patterns are checked from the top level

Trees

Sometimes, a file doesn't live in your repository:

  • Files generated by a metaprogram
  • Source-relative files when packaging an upstream repository (e.g. you want to refer to example/foo.c from libfoo, but your repository only contains a manifest)

To this end, you can specify which tree a path is relative to by using the table form:

[[example]]
source = [
  { path = "example/foo.c", tree = "source" },
  { path = "src/whatever.c", tree = "manifest" },
  "example/common/*.c" # Implicitly source-rooted
]

This lets you mix between files you wrote (whatever.c) and files that upstream wrote (foo.c), without forking or patching.

Headers

[package.include] is to include directories as source is to sources; they can be single entries or globs, and they can be plain strings or tables. Unlike sources, . is allowed as a special case to mean the top level.

[package]
include = [
  ".",
  "common/"
  { path = "include/", tree = "source" },
  { path = "my_special_includes/", tree = "manifest" },
  { path = "generated/", tree = "work" },
]

Paths listed under [package.include] are not exported, just shared between your targets. If you're looking to publish headers to packages that consume you, see here.

Preprocessor

[package.define] is an array of entries of the form NAME or NAME=VALUE. Each definition is private to this package's compilation. To publish preprocessor definitions to consumers, see here.

Flags

You can forward custom flags verbatim to the compiler and linker with flags and link_flags, respectively. Use when clauses with driver as the predicate to limit the flags to where they're needed.

link_flags only apply to the target's own link. They aren't added to a consumer's link line. Moreover, since static libraries are not linked, merely archives, these flags only apply to shared libraries.

C standard

C is compiled as strict C11 (-std=c11) by default. spn supports the following: c89, c99, c11, gnu89, gnu99, gnu11. Until newer standards are added, pass them as flags.

Many functions which one might assume are standard parts of C, like strdup or clock_gettime, are actually POSIX or GNU extensions. spn doesn't yet have a way for packages to communicate the standard they require to their consumers, so fix this by defining the corresponding feature macro (e.g. _GNU_SOURCE or _POSIX_C_SOURCE=200809L) until then.

C++

The cxx field on a target controls C++ settings. spn exposes standard, exceptions, and rtti; for a full list of values and defaults, see the manifest reference.

C programs that link a static library containing C++ get the C++ runtime automatically.

Exceptions

Under WASM, exceptions are off by default. They're also enabled at the toolchain level, since driver support is spotty and differs between the same driver across different toolchains.

Dependencies

Each kind of executable (bin, test, example, tool, plus metaprogram) can have its own dependencies. An executable's deps name other packages or targets within your own package. When a package is named, the executable links to every library in that package.

system_deps

system_deps are libraries provided by the target, like m, or ws2_32, or macOS frameworks like Cocoa. They are always spelled as a table:

[package]
system_deps = [
  { lib = "advapi32", when = { os = "windows" } },
  { lib = "m", when = { os = "linux" } },
  { framework = "Cocoa" },
]

These can of course be specified as raw link_flags, but system_deps will automatically spell them for the linker in use. Since system_deps are by definition platform specific, you'll usually use them with a when clause (except framework, which is automatically ignored when not targeting macOS).

A static library's system_deps follow it to whatever links it. You don't need to re-declare your dependencies' system_deps.

host_deps

host_deps are libraries provided by the host i.e. the build machine. They are discouraged, and they disable cross compilation. However, tools exist to serve, not to artificially restrict or get in the way, so when you just want to link to something on the build machine this field is available.

Exports

[package.publish]

Let's say that you have these headers:

foo/
  bar/
    b.h
  baz/
    qux/
      c.h
    spum/
      d.h
  a.h

And you publish them like so, flattening out baz and otherwise treating foo as the top level:

[package.publish]
include = [
  { from = "foo/*.h"},
  { from = "foo/bar/*.h", to = "bar" },
  { from = "baz/**/.*.h", to = "baz" },
]

You'd end up with this:

include/
  a.h
  bar/
    b.h
  baz/
    c.h
    d.h

Publishing defines

  • [package.publish] define lists macros that dependents compile with.
  • A bool option with define and public = true does the same while the option is on.
  • Publish any macro that changes your public headers, like a static linking switch or one that changes a struct layout.
  • Without it, the package and its dependents silently compile the same header differently.
  • [package.publish] system_deps lists system libraries that dependents link.

Library kinds

  • kinds is static, shared, or both, and ["static"] when omitted.
  • Offer shared only when upstream supports a shared build.
  • A dependent links the static kind when the library offers it, unless the root sets [config.foo] kind.
  • Which kinds are built for your own package is the profile's emit.

Targets declare what kinds of linkage they support:

  • static
  • shared

kinds lists what a library can be built as. The default is ["static"].

KindOutput
staticAn archive (libfoo.a, or foo.lib for MSVC)
sharedlibfoo.so, libfoo.dylib, or foo.dll with foo.dll.lib to link against. On WebAssembly, a module (foo.wasm) that exports the library's functions

A WebAssembly module can't be linked against, so a library that other targets link needs static there.

Header-only packages

spn is perfectly happy with header only packages. That includes single header libraries, like sokol, as well as packages that provide headers for a system library like X11 or PulseAudio.

For true header only libraries, you don't need a [[lib]] target. Such a library is just:

[package.publish]
include = [...]

With no library to carry them, the package's system_deps and define don't reach dependents; put them in [package.publish].

Platform specific

macOS

[package.macos]
min_os = { major = 12 }

[lib.macos]
frameworks = ["Cocoa", "IOKit", "CoreVideo", "OpenGL"]

When linking, your binary's min_os is the max across everything in it. If a dependency needs macOS 12, your binary targets macOS 12.

Windows

[[bin]]
# ...
windows = { subsystem = "windows" }

Embedding files in binaries

spn can embed arbitrary files and bytes in your binary. This is a very common way to ship a program with fonts, shaders, assets, etc. without depending on a filesystem at runtime.

Embedded files are specified in TOML. spn creates:

  • An object file containing the literal bytes
  • A header file with metadata for use in your program

Each entry names a file (or, with dir = true, every file under a directory) and the path it is embedded as; the symbol is that path with /, \\, ., and - replaced by _. For example, this configuration:

[[bin]]
name = "whatever"
source = ["main.c"]
embed = [
  "asset/fonts/inconsolata.ttf",
  { path = "asset/data.json", dest = "data.json", data_type = "u8", size_type = "u64" },
  { path = "asset/shaders", dir = true, dest = "shaders" },
]

Produces this header:

extern const unsigned char asset_fonts_inconsolata_ttf [109384];
extern const unsigned long long asset_fonts_inconsolata_ttf_size;

extern const u8 data_json [2213];
extern const u64 data_json_size;

extern const unsigned char shaders_quad_frag [412];
extern const unsigned long long shaders_quad_frag_size;

extern const unsigned char shaders_quad_vert [287];
extern const unsigned long long shaders_quad_vert_size;

typedef struct {
  const char* path;
  const void* data;
  unsigned long long size;
} spn_embed_entry_t;
static const unsigned int spn_embed_count = 4;
static const spn_embed_entry_t spn_embed_manifest[] = {
  { "asset/fonts/inconsolata.ttf", asset_fonts_inconsolata_ttf, 109384 },
  { "asset/data.json", data_json, 2213 },
  { "shaders/quad.frag", shaders_quad_frag, 412 },
  { "shaders/quad.vert", shaders_quad_vert, 287 },
};