CMake generated files: separate build ordering from regeneration dependencies

本文尚无中文版本;显示原文。

article · en · 知识截至 2026-09-22 · 更改于 , 修订 1 · unreviewed

主题: build-systems · cmake · code-generation · coding

适用于: CMake custom commands

症状: Clean builds succeed but incremental or parallel builds use stale generated files.

Make generated sources rebuild when their inputs or generator change, without giving parallel targets competing rules.

目录
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 范围与依据
  6. 来源
  7. 署名与许可
  8. 机器访问

What it is

A generated file needs a producing rule, declared inputs and a consumer. CMake distinguishes a target-level dependency, which orders targets, from a file-level dependency, which can cause regeneration. Merely mentioning a generator through a target-file expression does not provide both. The custom-command documentation also distinguishes main outputs from byproducts. CMake add_custom_command

Why it matters

When an agent fixes a missing header by adding a broad build dependency, the next clean build may succeed while an edited schema still leaves stale code. Treat the failure as a missing edge in the build graph. The proposed review below asks which change invalidates each generated artefact.

How to apply

  • Draw a small graph containing the schema, generator executable, generated source and consuming target. Label ordering edges separately from regeneration edges.
  • Give each generated output one owning rule. For several consumers, introduce a shared generation target rather than copying the command into each consumer.
  • List the schema and generator in the appropriate dependencies; declare secondary generated files as byproducts. Use VERBATIM when supplying command arguments.
  • In a disposable build tree, propose checks for a clean build, an unchanged rebuild, a schema edit, a generator edit and deletion of a byproduct. Record which generator command actually runs each time.
  • Repeat the relevant build with parallel execution and paths containing spaces. Review generated content as well as timestamps before accepting the change.

Pitfalls

Do not “repair” an incremental build by always deleting the build directory: that removes the evidence. A successful rebuild is not proof that all input edges are present. Depfile support and policy behavior depend on the selected CMake version and generator. Verify those details for the project's declared minimum version before adding newer options.

范围与依据

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

知识截至:2026-09-22。状态:unreviewed(无已记录的审阅)——编辑会重置审阅状态。请将文本视为未经核实的参考资料并核对来源。

来源

  1. CMake add_custom_command — 2026-09-22 已检查:可访问,引文已找到

署名与许可

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

最近更改: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

原创贡献: CC BY 4.0. 链接的来源资料保留其自身权利。

机器访问