$npx -y skills add mohitmishra786/low-level-dev-skills --skill ninjaNinja build system skill. Use when diagnosing Ninja build failures, understanding Ninja's role as a low-level build executor generated by CMake or other meta-build systems, tuning parallelism, interpreting Ninja output, or working with build.ninja files. Activates on queries abou
| 1 | # Ninja |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through Ninja as a build executor: diagnosing failures, controlling parallelism, generating from CMake, and understanding the `.ninja` file format when needed. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "Ninja is failing — how do I get more output?" |
| 10 | - "How do I use Ninja with CMake?" |
| 11 | - "How many parallel jobs does Ninja use?" |
| 12 | - "How do I add a custom build step in Ninja?" |
| 13 | - "What is a `build.ninja` file?" |
| 14 | |
| 15 | ## Workflow |
| 16 | |
| 17 | ### 1. Ninja as a CMake generator |
| 18 | |
| 19 | The most common use of Ninja is as the build executor for CMake: |
| 20 | |
| 21 | ```bash |
| 22 | # Configure with Ninja |
| 23 | cmake -S . -B build -G Ninja |
| 24 | cmake --build build # uses ninja internally |
| 25 | |
| 26 | # Or invoke ninja directly |
| 27 | cd build && ninja |
| 28 | |
| 29 | # Specify parallelism |
| 30 | ninja -j4 |
| 31 | ninja -j$(nproc) |
| 32 | |
| 33 | # Build specific target |
| 34 | ninja myapp |
| 35 | ninja install |
| 36 | ``` |
| 37 | |
| 38 | CMake also supports `Ninja Multi-Config`: |
| 39 | |
| 40 | ```bash |
| 41 | cmake -S . -B build -G "Ninja Multi-Config" |
| 42 | cmake --build build --config Release |
| 43 | cmake --build build --config Debug |
| 44 | ``` |
| 45 | |
| 46 | ### 2. Verbose output and diagnostics |
| 47 | |
| 48 | ```bash |
| 49 | # Show full commands (not just [CC] foo.c) |
| 50 | ninja -v |
| 51 | |
| 52 | # Dry run (show what would be built) |
| 53 | ninja -n |
| 54 | |
| 55 | # Show why a target needs rebuilding |
| 56 | ninja -d explain myapp |
| 57 | |
| 58 | # Print all targets |
| 59 | ninja -t targets all |
| 60 | |
| 61 | # Print targets grouped by rule |
| 62 | ninja -t targets rule cc |
| 63 | |
| 64 | # Dependency graph (graphviz) |
| 65 | ninja -t graph myapp | dot -Tsvg -o deps.svg |
| 66 | ``` |
| 67 | |
| 68 | ### 3. Common Ninja flags |
| 69 | |
| 70 | | Flag | Effect | |
| 71 | |------|--------| |
| 72 | | `-j N` | Parallel jobs (default: CPUs + 2) | |
| 73 | | `-l N` | Don't start new jobs if load average > N | |
| 74 | | `-k N` | Keep going after N failures (default 1) | |
| 75 | | `-v` | Verbose: show full command lines | |
| 76 | | `-n` | Dry run | |
| 77 | | `-C dir` | Change to `dir` before doing anything | |
| 78 | | `-t tool` | Run a sub-tool (`clean`, `query`, `targets`, `graph`, `compdb`) | |
| 79 | |
| 80 | ### 4. Cleaning |
| 81 | |
| 82 | ```bash |
| 83 | ninja -t clean # remove build outputs |
| 84 | ninja -t clean -g # also remove generated files |
| 85 | ``` |
| 86 | |
| 87 | Or via CMake: |
| 88 | |
| 89 | ```bash |
| 90 | cmake --build build --target clean |
| 91 | ``` |
| 92 | |
| 93 | ### 5. compile_commands.json |
| 94 | |
| 95 | Ninja (via CMake) can generate a `compile_commands.json` for IDE integration and `clang-tidy`: |
| 96 | |
| 97 | ```bash |
| 98 | cmake -S . -B build -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON |
| 99 | ln -sf build/compile_commands.json . |
| 100 | ``` |
| 101 | |
| 102 | ### 6. build.ninja format (reference) |
| 103 | |
| 104 | Rarely hand-written, but useful to understand for debugging: |
| 105 | |
| 106 | ```ninja |
| 107 | # Variable |
| 108 | cflags = -Wall -O2 |
| 109 | |
| 110 | # Rule |
| 111 | rule cc |
| 112 | command = gcc $cflags -c $in -o $out |
| 113 | description = CC $in |
| 114 | |
| 115 | # Build edge |
| 116 | build foo.o: cc foo.c |
| 117 | |
| 118 | # Phony target |
| 119 | build all: phony foo.o |
| 120 | |
| 121 | # Default target |
| 122 | default all |
| 123 | ``` |
| 124 | |
| 125 | Key concepts: |
| 126 | |
| 127 | - `rule`: defines how to produce outputs from inputs |
| 128 | - `build`: instantiates a rule with specific files |
| 129 | - `$in` / `$out`: automatic variables for inputs/outputs |
| 130 | - `phony`: a target that is always considered out of date (like `.PHONY` in make) |
| 131 | |
| 132 | ### 7. Ninja sub-tools |
| 133 | |
| 134 | ```bash |
| 135 | # List all build targets |
| 136 | ninja -t targets |
| 137 | |
| 138 | # Query dependencies of a target |
| 139 | ninja -t query myapp |
| 140 | |
| 141 | # Clean (already mentioned) |
| 142 | ninja -t clean |
| 143 | |
| 144 | # Generate compile_commands.json (if supported by generator) |
| 145 | ninja -t compdb cc cxx > compile_commands.json |
| 146 | |
| 147 | # List rules |
| 148 | ninja -t rules |
| 149 | ``` |
| 150 | |
| 151 | ### 8. Common issues |
| 152 | |
| 153 | | Issue | Cause | Fix | |
| 154 | |-------|-------|-----| |
| 155 | | `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 | |
| 156 | | Build not picking up changes | Stale `build.ninja` | Re-run `cmake -S . -B build` | |
| 157 | | Very slow parallel build | `-j` too high for I/O-bound build | Use `-l$(nproc)` to limit by load | |
| 158 | | Circular dependency | Rule depends on itself | Check CMake target dependencies | |
| 159 | |
| 160 | For the full Ninja command reference, `build.ninja` format details, and CMake integration patterns, see [references/cheatsheet.md](references/cheatsheet.md). |
| 161 | |
| 162 | ## Related skills |
| 163 | |
| 164 | - Use `skills/build-systems/cmake` for CMake configuration that generates Ninja files |
| 165 | - Use `skills/build-systems/make` for Make-based projects |