Ninja

mohitmishra786/low-level-dev-skills/skills/build-systems/ninja

作者 mohitmishra786bdc58472fa9f无许可证253 个星标收录于 2026年10月9日更新于 2026年10月9日仓库3个月前更新

Ninja 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 about ninja errors, ninja parallelism, ninja verbose output, build.ninja format, or ninja as a CMake generator.

AI 生成的概览

指导代理使用 Ninja 构建执行器、诊断问题、调整并行度并理解 build.ninja 文件。

功能
该技能提供关于将 Ninja 用作底层构建执行器的指导,通常由 CMake 生成。内容涵盖使用 CMake 的 Ninja 生成器配置构建、详细输出与试运行输出、并行度参数、清理、生成 compile_commands.json,以及 build.ninja 文件格式。它还列出常用的 Ninja 子工具,并用表格说明常见构建错误的原因与修复方法,同时指向速查参考以获取更完整的命令细节。
适用场景
适用于诊断 Ninja 构建失败、解读 Ninja 输出或调整 Ninja 并行度的场景。也适合关于将 Ninja 与 CMake 配合使用、Ninja Multi-Config 生成器或 build.ninja 文件格式的问题。
运行要求
不需要脚本或软件包,仅为说明性内容。所述工作流假定已安装 Ninja 和 CMake,部分命令可选使用 graphviz(dot)或 clang-tidy。

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.ninja file?"

Workflow

1. Ninja as a CMake generator

The most common use of Ninja is as the build executor for CMake:

bash
# Configure with Ninjacmake -S . -B build -G Ninjacmake --build build                    # uses ninja internally
# Or invoke ninja directlycd build && ninja
# Specify parallelismninja -j4ninja -j$(nproc)
# Build specific targetninja myappninja install

CMake also supports Ninja Multi-Config:

bash
cmake -S . -B build -G "Ninja Multi-Config"cmake --build build --config Releasecmake --build build --config Debug

2. Verbose output and diagnostics

bash
# Show full commands (not just [CC] foo.c)ninja -v
# Dry run (show what would be built)ninja -n
# Show why a target needs rebuildingninja -d explain myapp
# Print all targetsninja -t targets all
# Print targets grouped by ruleninja -t targets rule cc
# Dependency graph (graphviz)ninja -t graph myapp | dot -Tsvg -o deps.svg

3. Common Ninja flags

FlagEffect
-j NParallel jobs (default: CPUs + 2)
-l NDon't start new jobs if load average > N
-k NKeep going after N failures (default 1)
-vVerbose: show full command lines
-nDry run
-C dirChange to dir before doing anything
-t toolRun a sub-tool (clean, query, targets, graph, compdb)

4. Cleaning

bash
ninja -t clean          # remove build outputsninja -t clean -g       # also remove generated files

Or via CMake:

bash
cmake --build build --target clean

5. compile_commands.json

Ninja (via CMake) can generate a compile_commands.json for IDE integration and clang-tidy:

bash
cmake -S . -B build -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ONln -sf build/compile_commands.json .

6. build.ninja format (reference)

Rarely hand-written, but useful to understand for debugging:

ninja
# Variablecflags = -Wall -O2
# Rulerule cc  command = gcc $cflags -c $in -o $out  description = CC $in
# Build edgebuild foo.o: cc foo.c
# Phony targetbuild all: phony foo.o
# Default targetdefault all

Key concepts:

  • rule: defines how to produce outputs from inputs
  • build: instantiates a rule with specific files
  • $in / $out: automatic variables for inputs/outputs
  • phony: a target that is always considered out of date (like .PHONY in make)

7. Ninja sub-tools

bash
# List all build targetsninja -t targets
# Query dependencies of a targetninja -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 rulesninja -t rules

8. Common issues

IssueCauseFix
ninja: error: 'foo.o', needed by 'prog', missing and no known rule to make itMissing build ruleRegenerate with CMake; check add_executable source list
Build not picking up changesStale build.ninjaRe-run cmake -S . -B build
Very slow parallel build-j too high for I/O-bound buildUse -l$(nproc) to limit by load
Circular dependencyRule depends on itselfCheck CMake target dependencies

For the full Ninja command reference, build.ninja format details, and CMake integration patterns, see references/cheatsheet.md [blocked].

Related skills

  • Use skills/build-systems/cmake for CMake configuration that generates Ninja files
  • Use skills/build-systems/make for Make-based projects

来源与署名

来源:mohitmishra786/low-level-dev-skills位于skills/build-systems/ninja提交bdc5847

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架