Search code examples
build-processbuild-dependenciesninja

Avoid rebuilding dependencies of unchanged outputs


When I have a rule with multiple outputs in ninja, it assumes that all outputs have been updated and in turn all their dependencies get rebuilt, even if they haven't changed at all.

This often comes up with a code generation step, like parser generators.

E.g. for the LEMON parser generator:

rule lemon
    command = lemon $in

build grammar.c grammar.h grammar.out: lemon grammar.y

If grammar.y gets updated but grammar.h doesn't change after running LEMON. LEMON detects this and doesn't update the file. Still, ninja rebuilds everything that depended on grammar.h.

How do I prevent this?


Solution

  • Add restat to your rule. From the documentation:

    restat
    If present, causes Ninja to re-stat the command’s outputs after execution of the command. Each output whose modification time the command did not change will be treated as though it had never needed to be built. This may cause the output’s reverse dependencies to be removed from the list of pending build actions.

    So the rule becomes:

    rule lemon
        command = lemon $in
        restat = 1