Introduction
Ninja is a build system created by Evan Martin while working on Google Chrome. Unlike Make, which is designed for humans to write by hand, Ninja is designed to have its build files generated by meta-build systems like CMake or Meson. This design choice allows Ninja to be extremely simple and fast — it does one thing (run builds) and does it with minimal overhead.
What Ninja Does
- Executes incremental builds with minimal startup time and maximum parallelism
- Tracks file dependencies using timestamps and a build log for accurate rebuilds
- Runs build commands in parallel up to the number of available CPU cores
- Provides a compact status line showing build progress and elapsed time
- Supports build file generation from CMake, Meson, GN, and other meta-build tools
Architecture Overview
Ninja reads a build.ninja file containing explicit dependency edges and build commands. On startup, it loads the dependency graph, checks which outputs are stale by comparing timestamps and the command hash stored in .ninja_log, and executes only the necessary build steps in parallel. The build file format is intentionally minimal — no variables, conditionals, or glob patterns — because those features belong in the generator. This simplicity is what makes Ninja fast: startup time is dominated by stat() calls, not file parsing.
Self-Hosting & Configuration
- Install via package manager:
apt install ninja-build,brew install ninja, orpip install ninja - Generate
build.ninjafiles from CMake (cmake -G Ninja ..) or Meson (meson setup builddir) - Control parallelism with
-j Nflag (defaults to number of CPU cores) - Use
ninja -t compdbto generate a compilation database (compile_commands.json) for IDE integration - Clean build artifacts with
ninja -t clean
Key Features
- Sub-second startup time even for large projects with thousands of build edges
- Automatic parallelism based on available CPU cores
- Precise incremental builds using both timestamps and command hashes
- Built-in compilation database generation for clangd, ccls, and other language servers
- Tiny binary with zero runtime dependencies
Comparison with Similar Tools
- Make — general-purpose build tool with a human-writable syntax; Ninja is faster but requires a generator
- CMake — meta-build system that generates Ninja, Make, or Visual Studio files; Ninja is the recommended backend for CMake
- Meson — modern meta-build system that uses Ninja as its default backend
- Bazel — full-featured build system with caching and remote execution; Ninja is simpler and faster for local builds
FAQ
Q: Should I write build.ninja files by hand? A: Generally no. Use CMake or Meson to generate them. Hand-written Ninja files are only practical for very small projects.
Q: How much faster is Ninja than Make? A: For incremental builds on large projects, Ninja can be 10-20x faster due to lower startup overhead and better dependency tracking.
Q: Does Ninja support distributed builds? A: Not directly. Use tools like distcc or Icecream alongside Ninja for distributed compilation.
Q: Can I use Ninja with Visual Studio projects? A: Yes. CMake can generate Ninja files on Windows, and Ninja works with MSVC, Clang, and GCC on all platforms.