
Ninja
- 350 installs
- 155 repo stars
- Updated June 27, 2026
- mohitmishra786/low-level-dev-skills
Generate and maintain Ninja build graphs for fast incremental native compiles in CMake, Meson, or custom pipelines with correct dependency edges and parallel jobs.
About
Explains how to structure Ninja build files and integrate them with CMake or Meson so agents produce fast, reliable incremental native builds for CLI utilities and API services in local dev and CI.
- build.ninja graph structure
- Incremental rebuild correctness
- Parallel -j tuning
- CMake/Meson Ninja backends
- CI cache-friendly builds
Ninja by the numbers
- 350 all-time installs (skills.sh)
- +27 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #319 of 1,435 DevOps & CI/CD skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill ninjaAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 350 |
|---|---|
| repo stars | ★ 155 |
| Last updated | June 27, 2026 |
| Repository | mohitmishra786/low-level-dev-skills ↗ |
What it does
Generate and maintain Ninja build graphs for fast incremental native compiles in CMake, Meson, or custom pipelines with correct dependency edges and parallel jobs.
Files
Ninja
Purpose
Guide agents through Ninja as a build executor: diagnosing failures, controlling parallelism, generating from CMake, and understanding the .ninja file format when needed.
Triggers
- "Ninja is failing — how do I get more output?"
- "How do I use Ninja with CMake?"
- "How many parallel jobs does Ninja use?"
- "How do I add a custom build step in Ninja?"
- "What is a
build.ninjafile?"
Workflow
1. Ninja as a CMake generator
The most common use of Ninja is as the build executor for CMake:
# Configure with Ninja
cmake -S . -B build -G Ninja
cmake --build build # uses ninja internally
# Or invoke ninja directly
cd build && ninja
# Specify parallelism
ninja -j4
ninja -j$(nproc)
# Build specific target
ninja myapp
ninja installCMake also supports Ninja Multi-Config:
cmake -S . -B build -G "Ninja Multi-Config"
cmake --build build --config Release
cmake --build build --config Debug2. Verbose output and diagnostics
# Show full commands (not just [CC] foo.c)
ninja -v
# Dry run (show what would be built)
ninja -n
# Show why a target needs rebuilding
ninja -d explain myapp
# Print all targets
ninja -t targets all
# Print targets grouped by rule
ninja -t targets rule cc
# Dependency graph (graphviz)
ninja -t graph myapp | dot -Tsvg -o deps.svg3. Common Ninja flags
| Flag | Effect |
|---|---|
-j N | Parallel jobs (default: CPUs + 2) |
-l N | Don't start new jobs if load average > N |
-k N | Keep going after N failures (default 1) |
-v | Verbose: show full command lines |
-n | Dry run |
-C dir | Change to dir before doing anything |
-t tool | Run a sub-tool (clean, query, targets, graph, compdb) |
4. Cleaning
ninja -t clean # remove build outputs
ninja -t clean -g # also remove generated filesOr via CMake:
cmake --build build --target clean5. compile_commands.json
Ninja (via CMake) can generate a compile_commands.json for IDE integration and clang-tidy:
cmake -S . -B build -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
ln -sf build/compile_commands.json .6. build.ninja format (reference)
Rarely hand-written, but useful to understand for debugging:
# Variable
cflags = -Wall -O2
# Rule
rule cc
command = gcc $cflags -c $in -o $out
description = CC $in
# Build edge
build foo.o: cc foo.c
# Phony target
build all: phony foo.o
# Default target
default allKey concepts:
rule: defines how to produce outputs from inputsbuild: instantiates a rule with specific files$in/$out: automatic variables for inputs/outputsphony: a target that is always considered out of date (like.PHONYin make)
7. Ninja sub-tools
# List all build targets
ninja -t targets
# Query dependencies of a target
ninja -t query myapp
# Clean (already mentioned)
ninja -t clean
# Generate compile_commands.json (if supported by generator)
ninja -t compdb cc cxx > compile_commands.json
# List rules
ninja -t rules8. Common issues
| Issue | Cause | Fix |
|---|---|---|
ninja: error: 'foo.o', needed by 'prog', missing and no known rule to make it | Missing build rule | Regenerate with CMake; check add_executable source list |
| Build not picking up changes | Stale build.ninja | Re-run cmake -S . -B build |
| Very slow parallel build | -j too high for I/O-bound build | Use -l$(nproc) to limit by load |
| Circular dependency | Rule depends on itself | Check CMake target dependencies |
For the full Ninja command reference, build.ninja format details, and CMake integration patterns, see references/cheatsheet.md.
Related skills
- Use
skills/build-systems/cmakefor CMake configuration that generates Ninja files - Use
skills/build-systems/makefor Make-based projects
Ninja Cheatsheet
Source: <https://ninja-build.org/manual.html>
Table of Contents
1. Command line 2. Sub-tools (-t) 3. build.ninja format 4. Variables and rules 5. Integration with CMake 6. Troubleshooting
---
Command line
ninja # build default target
ninja myapp # build specific target
ninja install # run install target
ninja clean # remove outputs (via -t clean)
ninja -j4 # exactly 4 parallel jobs
ninja -j$(nproc) # all available CPUs
ninja -l4.0 # don't start jobs if load > 4.0
ninja -k5 # keep going after up to 5 failures
ninja -k0 # keep going indefinitely
ninja -v # verbose: show full commands
ninja -n # dry run: show what would run
ninja -C build/ # change to build/ directory first
ninja -d explain myapp # explain why target needs rebuild
ninja -d keepdepfile # keep .d files after use
ninja -d keeprsp # keep .rsp response files
ninja -d stats # print build statistics at end---
Sub-tools (-t)
# List all targets
ninja -t targets
ninja -t targets all # all targets (including intermediate)
ninja -t targets rule cc # targets using rule 'cc'
# Clean build outputs
ninja -t clean # remove all built files
ninja -t clean myapp # clean specific target only
ninja -t clean -g # also remove generated files
# Query a target's dependencies
ninja -t query myapp
# Generate dependency graph (graphviz)
ninja -t graph myapp | dot -Tsvg -o deps.svg
ninja -t graph | dot -Tsvg -o all_deps.svg
# Generate compile_commands.json (if rules support it)
ninja -t compdb cc cxx > compile_commands.json
# List all rules
ninja -t rules
# Browse (requires curses)
ninja -t browse --port=8080 myapp---
build.ninja format
Ninja files are almost always generated by CMake or other tools. Understanding the format helps debug generated builds.
Minimal example
# Variable declaration
cc = gcc
cflags = -Wall -O2
# Rule: how to transform inputs to outputs
rule compile_c
command = $cc $cflags -c $in -o $out
description = CC $in
depfile = $out.d
deps = gcc # tells ninja to read gcc-style .d files
rule link
command = $cc -o $out $in
description = LINK $out
# Build edges: instantiate rules with specific files
build src/foo.o: compile_c src/foo.c
build src/bar.o: compile_c src/bar.c
# Link
build myapp: link src/foo.o src/bar.o
# Phony: always out of date
build clean: phony
build all: phony myapp
# Default targets (built when none specified)
default allKey concepts
| Concept | Description |
|---|---|
rule | Defines a build command template |
build | Instantiates a rule with specific files |
$in | Space-separated list of input files |
$out | Space-separated list of output files |
$depfile | Path to a Makefile-style dependency file (.d) |
deps = gcc | Tells ninja to parse gcc-style depfiles and manage them |
deps = msvc | Parse MSVC-style /showIncludes output |
phony | Pseudo-target; always considered out of date |
default | Targets built when none specified on command line |
---
Variables and rules
Special variables
| Variable | When it expands |
|---|---|
$in | Input files for the build edge |
$out | Output files for the build edge |
$depfile | The dependency file path |
Variable scoping
# Global variable
cflags = -O2
# Rule-level variable (overrides global inside the rule)
rule compile_c
command = gcc $cflags -c $in -o $out
cflags = -O0 # this shadows the global inside the rule
# Build-edge variable (overrides for this specific build edge)
build slow.o: compile_c slow.c
cflags = -O0 -g # only for this edgeResponse files (for long command lines)
rule link
command = gcc @$out.rsp -o $out
rspfile = $out.rsp
rspfile_content = $in $LINK_FLAGS---
Integration with CMake
# Generate Ninja build files
cmake -S . -B build -G Ninja
# Build
cmake --build build
# Or: cd build && ninja
# Parallel build
cmake --build build -- -j$(nproc)
# Verbose (see actual commands)
cmake --build build -- -v
# Or: VERBOSE=1 ninja -C build
# Build a specific target
cmake --build build --target myapp
# Clean
cmake --build build --target clean
# Multi-config Ninja
cmake -S . -B build -G "Ninja Multi-Config"
cmake --build build --config Release
cmake --build build --config Debuggenerate compile_commands.json
cmake -S . -B build -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
ln -sf build/compile_commands.json .---
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
ninja: error: 'foo.o', needed by 'prog', missing and no known rule | File missing / rule not generated | Re-run cmake; check add_executable sources |
| Stale build, not picking up changes | build.ninja outdated | Re-run cmake -S . -B build |
ninja: build stopped: subcommand failed | A compile/link failed | Read the error above this line; use -v for full command |
| Parallel build produces race output | Many jobs writing stdout | Use ninja -j1 to serialize; or add pool console |
| Circular dependency | Target depends on itself indirectly | Check CMake target_link_libraries for cycles |
| Extremely slow incremental build | Too many redundant edges | Check if globs or generated files are causing unnecessary rebuilds |
Debugging why a target rebuilds
ninja -d explain myapp 2>&1 | head -30Output will show which dependency is newer than the target, triggering the rebuild.