Kayavak / The Backend / BE·43 Build Systems / Deeper
Deeper · The Backend
Build Systems
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Stuart Feldman wrote make at Bell Labs in 1976 after a colleague wasted a morning debugging a program that hadn't been recompiled: make rebuilds only what's out of date, by comparing file timestamps along declared dependencies. Java projects used Ant (2000), essentially make in XML, then Maven (1.0 in 2004), which replaced scripts with conventions (a standard directory layout and life cycle) and added dependency download from repositories. Gradle (2008–2012) combined conventions with a programmable build and incremental task execution.
Large companies needed more. Google's internal Blaze, open-sourced as Bazel in 2015, treated builds as pure functions of their inputs: hermetic, sandboxed actions whose outputs can be cached and shared across every machine. Those ideas (content-addressed caches, remote execution) have since spread to Gradle and other tools.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
A build is a graph of tasks from inputs to outputs. A good build tool knows the whole graph, runs only the tasks whose inputs changed, and gets the same outputs from the same inputs anywhere.
| Question | Answer |
|---|---|
| What is it? | Tools that resolve dependencies, compile, test, package and publish software |
| Problem it solves | Turning source into tested artifacts quickly and repeatably, on any machine |
| Parts | Dependency resolution, a task or life-cycle graph, plugins, incremental execution, caches, reproducibility settings |
| How it works | Order tasks by dependencies (topologically), skip any whose inputs and outputs are unchanged, cache the rest |
| Assumptions | Every input of a task is declared |
| When they change | An undeclared input (an environment variable, the current time) makes caching serve wrong results or builds differ between machines |
| Where it fails | Slow builds from tasks that can't be cached; builds that only work on one laptop |
| Related ideas | Dependency resolution (ch42); CI pipelines (DevOps); reproducible builds and supply chain (Security) |
What if?
What if a build step read the current date?
Reason it through, then open
Two builds of the same commit would produce different artifacts, so their checksums wouldn't match and caches would never hit. You couldn't prove a released binary came from a given source. Reproducible builds fix timestamps (for example to the commit time), sort file orders and remove machine-specific paths, so anyone can rebuild and compare.
What if the build didn't know the full dependency graph?
Reason it through, then open
It couldn't tell which tasks a change affects, so it would either rebuild everything (slow) or miss something (wrong): the stale-binary problem make was invented to stop. Declared dependencies are what make incremental and cached builds both fast and correct.
Where the boundary is
| Aspect | Incremental build | Cached (hermetic) build |
|---|---|---|
| Reuses | Outputs from your last build, on your machine | Outputs from any build, anywhere, with the same inputs |
| Needs | Changed-file detection | Every input declared and hashed |
| Fails when | State on disk is stale or corrupted | An input isn't declared |
Derive it
Why a build graph can be run in an order at all, and which order.
- Make each task a node and draw an edge from A to B when B needs A's output.
- A valid order runs every task after all its inputs: a topological order of the graph.
- Such an order exists exactly when the graph has no cycle. If A needs B and B needs A, no order works, which is why build tools reject circular dependencies between modules.
- Kahn's algorithm finds one: repeatedly run any task with no unfinished inputs. Tasks that become ready at the same time are independent, so they can run in parallel.
- When a source file changes, only tasks reachable from it in the graph need re-running; everything else is up to date.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Make was written by Stuart Feldman in 1976 | Historical | Feldman's 1979 paper and later accounts |
| A dependency graph has a valid order iff it has no cycles | Theorem | Graph theory (Foundations, ch05) |
| Remote caches speed up large builds | Measured | Reports from Bazel and Gradle users; depends on cache hit rate |
Still open
Whether hermetic build systems like Bazel are worth their complexity outside very large codebases is debated; many teams find Gradle or Maven with caching enough.
What would change your mind? If your clean builds were as fast as incremental ones, build caching wouldn't matter for you. It matters as soon as they aren't.
Recall
What problem did make solve?
Rebuilding only what's out of date, so nobody runs stale binaries or rebuilds everything.
When can a build graph be ordered?
When it has no cycles; any topological order then works.
Why must every input of a cached task be declared?
Otherwise the cache can serve outputs built from different inputs.
What makes a build reproducible?
The same inputs give byte-identical outputs: fixed timestamps, ordering and paths.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.