Any field in your package can be keyed on any fact of the build (target OS, architecture, ABI, build mode, optimization level, sanitizer settings) by using a when clause in TOML. Clauses are structured data, not a DSL1, and an entry with multiple clauses gets them ANDed together. Here's some common examples:
source = [
{ path = "source/backend/win32.c", when = { os = "windows" } },
{ path = "source/backend/posix.c", when = { os = { not = "windows" } } },
]
flags = [
{ value = "-mfpu=neon", when = { os = "linux", arch = "aarch64" } },
]
define = [
{ value = "USE_DEBUG_ALLOC", when = { mode = "debug", sanitize_address = false } },
]
system_deps = [
{ value = "pthread", when = { os = { not = "windows" } } },
]
deps = [
{ pkg = "tracy", when = { mode = "debug" } },
]when clauses
A when is a table on an entry. Each key is either a build fact or one of the package's own options, and each value is a string, a bool, or { not = value }. Anything else is an error, so a typo can't quietly never match.
There's no "or". Every key in a when has to match, so the way to say "x86 or ARM" is to repeat the entry:
define = [
{ value = "USE_SIMD", when = { arch = "x86_64" } },
{ value = "USE_SIMD", when = { arch = "aarch64" } },
]There's no when on a target itself, either; gate its sources instead.
Two places see fewer keys. A profile is what decides most of the facts, so a when inside one can only use os and arch. A metaprogram can use any fact, but not options.
Fact keys
Facts always describe the target you're building for, never the machine doing the build.
| Key | Values |
|---|---|
os | linux, macos, windows, wasi, freestanding |
arch | x86_64, aarch64, wasm32 |
abi | gnu, musl, msvc, apple, none, elf |
driver | gcc, clang, msvc, zig |
linker | gnu, lld, ld64, msvc |
mode | debug, release |
opt | 0, 1, 2, 3, s, z |
sanitize_$name | true or false, one key each for address, thread, undefined, memory, and leak |
The bundled Zig toolchain is driver = "zig", so driver = "clang" won't match it.
Options
Packages provide options. Options can be enumerations or booleans; enums are mutually exclusive, and an unresolvable conflict is a build error. Booleans are additive. For example:
[options.tls]
type = "enum"
values = ["schannel", "openssl", "off"]
default = [
{ when = { os = "windows" }, value = "schannel" },
{ when = { os = { not = "wasi" } }, value = "openssl" },
{ value = "off" },
]
[options.zstd]
type = "bool"
default = falseConsumers can then set these options
Options as keys
Options declared in the manifest work as keys, too, so a feature flag can gate sources and dependencies together:
[options.freetype]
type = "bool"
default = true
define = "UI_FREETYPE"
[[lib]]
name = "ui"
source = [
{ path = "source/text_freetype.c", when = { freetype = true } },
]
deps = [
{ pkg = "freetype", when = { freetype = true } },
]Setting options
- Your own package's options are set in a profile's
optionstable; there is no command line flag. - A dependency's options are set in
[config.foo] optionsin the root manifest, which wins over everything. optionson a dependency entry is a request, and every package that asks has to agree.[config]and[profile]are read only from the root, so a package that needs an option on its own dependency asks on the entry.
[profile.default]
options = { freetype = false }
[config.foo]
options = { tls = "openssl" }
[deps.package]
foo = { version = "6.9.0", options = { zstd = true } }Footnotes
-
I'm just gonna preempt any "ACKSHUALLY THIS IS JUST A DSL TOO" ↩