Dependencies

Consuming

Let's look at the three ways to consume a dependency:

  • Vendor both its source and its package manifest
  • Vendor its manifest, but fetch its source from Git
  • Fetch both source and manifest from Git

Vendor everything

The simplest dependency is a path to a directory with an spn.toml:

[deps.package]
foo = { path = "packages/json" }

And this is what the package might look like:

[package]
name = "json"
version = "6.9.0"

[[lib]]
name = "json"
source = ["json.c"] # Relative to this manifest, not top level

Vendor the manifest

More often, a dependency is a library that lives in another repository, and instead of literally vendoring its source code in your repository you just want to point at a commit and say "build that".

Let's say that json.c actually lives in another repository, but you'd still like to write your own manifest. Take the exact same manifest, and point it at a commit!

[package.upstream]
url = "https://github.com/tspader/foo.git"
commit = "6937fa02243da7b693c5692cea84a696950d4669"

Vendor nothing

In most other languages, it's uncommon to vendor either of these things. If json.c were a Rust library, you'd just cargo add json and vendor nothing. spn treats this as first class, too.

Take the same manifest and put it in a package index, which is as simple as putting it in a Git repository (or even just a local directory) and adding it as an index:

spn index add https://github.com/you/index
spn index add ./index # Alternatively, for simple local use

spn add json

spn add writes the newest resolvable version to [deps.package]. Use --test, --tool, or --build to add a dependency for other kinds of executables. If you need a specific version:

spn add json@6.9.1

Kinds

Each kind of executable target your package can produce can have its own dependencies:

  • [deps.package] is seen by libraries, binaries, examples, and tests.
  • [deps.test] is seen by tests only
  • [deps.build] is seen only by the build metaprogram.
  • [deps.tool] is seen only by tools, which don't see [deps.package].
  • [deps.test] and [deps.tool] are read only from the root manifest.

The same package can be in [deps.build] and [deps.package] at different versions.

Conditional and public dependencies

  • A dependency entry takes when, keyed on build facts or this package's options.
  • A dependency whose condition is off isn't fetched, resolved, or locked, so different profiles and targets can have different dependencies.
  • Dependencies are private: your dependents don't see a dependency's headers or defines.
  • public = true passes them on.
  • Mark a dependency public when your published headers include its headers, or every dependent fails on a missing header.
  • A dependent sees through a chain only if every link in it is public; linking is transitive either way.
[deps.package]
foo = { version = "6.9.0", public = true }
bar = { version = "1.0.0", when = { os = "linux" } }

SemVer

spn implements most of the SemVer spec:

  • A bare version means "compatible with":
    • "1.4.2" allows >=1.4.2, <2.0.0
    • "0.3.1" allows >=0.3.1, <0.4.0
  • "=1.4.2" pins exactly
  • ^, ~, >, >=, <, <=, and * work too
  • Major, minor, and patch are all required, so 6.9 is rejected

It does not implement:

  • Multiple comparators, so ">=1.2, <1.5" is an error
  • Prerelease versions, like 6.9.0-rc.1 or 6.9.0+kram

What is a package?

When you "depend" on a package, what does that actually mean?

  • Its published headers are on your include path.
  • Its published defines, and those of its public options, are in your compiles.
  • Every one of its libraries is linked in, along with their static dependencies and system libraries.
  • Nothing else of it is built: no binaries, tests, examples, or tools.

Configuration

Options

Packages can provide options, either as an enumeration or a boolean. Options for foo are set in [config.foo], like this:

[config.foo]
options = { tls = "openssl", zstd = true }

Scalars

It's often convenient to configure a package by giving it a value, like a number or a string, rather than a toggle or an enumeration. For example, cpptrace provides CPPTRACE_HARD_MAX_FRAMES or CPPTRACE_ADDR2LINE_PATH. We don't support this yet; it's well needed, and it's on the roadmap.

Linkage

Libraries declare the linkages they support:

  • shared
  • static
[config.foo]
kind = "shared"

Anything that links the library uses one kind: static when the library offers it, unless the root manifest sets [config.foo] kind = "shared".

Patching

What if you need to patch foo? You don't want to go back to the technology of the ancients, vendoring or submoduling entire repositories just to change a few lines. Do this in your spn.toml:

[deps.package]
foo = "6.9.0"

[patch.foo]
files = ["patches/foo.patch"]

That's it! Patches fold into the build system like everything else. If you change the contents of a patch, add or remove patches, you'll still get a correct, incremental build.

For security and sanity, dependencies can't patch.

Root-only tables

[config], [profile], [[toolchain]], [[index]], [stage], [deps.test], and [deps.tool] take effect only in the project being built; in a manifest used as a dependency they are ignored without a warning.

A package intended to be consumed can still use e.g. [profile.default] and [config] to build its own examples, but these settings have no effect downstream.

Version resolution

  • Each package gets one version across the whole graph: the newest that satisfies every requirement, preferring what spn.lock holds.
  • [deps.test] shares that graph, so a test dependency can hold back a package dependency's version.
  • When no version satisfies everyone, the build fails with X is already selected at 2.0.0, but Y 1.0.0 requires ^1.0.0.
  • There is no override; the only fix is changing a requirement.
  • [deps.build] and [deps.tool] resolve separately, so the same package can appear there at another version.
  • Two versions of a package are never linked into one binary.
  • A name comes from one place per build: the same package from a path and an index, or from two paths, is an error.
  • So you can't swap an index dependency for a local copy; patch it.

The lockfile

  • spn.lock sits next to spn.toml, and is written by spn build, spn test, and spn path.
  • It records the exact version and commit of every index dependency, build and tool dependencies included.
  • Path dependencies aren't recorded.
  • Commit it.
  • Locked versions are sticky: a build keeps a locked version when newer releases appear, or when you widen the requirement.
  • Builds only add entries; removing a dependency leaves its entry in place.
  • If a locked version no longer fits the manifest, the build picks a new one and adds it.
  • For a repository of manifests, it records the index's commit too.

--locked

  • --locked on spn build, spn test, and spn path fails instead of writing spn.lock.
  • It fails when the file is missing, too.
  • A profile or target with conditional dependencies that were never locked fails with "spn.lock is out of date".
  • So build each profile and target that CI uses once without --locked, and commit the result.

System dependencies

  • system_deps and host_deps both put -l<name> on the link line; neither one fetches or builds anything.
  • They differ in where the library is expected to come from at link time.
[package]
system_deps = [{ lib = "ws2_32", when = { os = "windows" } }]
host_deps = ["curl"]

system_deps

  • The target's SDK or sysroot provides the library: m, pthread, ws2_32, a macOS framework.
  • It works when cross compiling, and with the bundled Zig.
  • The field itself is covered in packages.

host_deps

  • The build machine provides the library, like a distro's libcurl.so.
  • It needs a system toolchain building for the machine it runs on.
  • With the bundled Zig, or a cross target, the build fails with an error listing the toolchains that would work.