Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule to compile dependency does not receive build variables #2303

Open
stefanv opened this issue Jun 16, 2023 · 1 comment
Open

Rule to compile dependency does not receive build variables #2303

stefanv opened this issue Jun 16, 2023 · 1 comment

Comments

@stefanv
Copy link

stefanv commented Jun 16, 2023

Consider the following build.ninja file:

rule a
  command = echo "a: $lang"

rule b
  command = echo "b: $lang"

build dep.txt: b

build output.txt: a dep.txt
  lang = english

Here, I would expect the lang = english variable definition to apply when dep.txt is built, since that build is triggered from the output.txt build. Instead, it the variable is only used when rule a is executed. This may well be intended behavior, but if so then how to get b to see $lang?

The docs state the following, and it's not clear whether/how 2 & 3 apply:

Variable declarations indented in a build block are scoped to the build block. The full lookup order for a variable expanded in a build block (or the rule is uses) is:

    1. Special built-in variables ($in, $out).
    2. Build-level variables from the build block.
    3. Rule-level variables from the rule block (i.e. $command). (Note from the above discussion on expansion that these are expanded "late", and may make use of in-scope bindings like $in.)
    4. File-level variables from the file that the build line was in.
    5. Variables from the file that included that file using the subninja keyword. 
@digit-google
Copy link
Contributor

build-level variables only apply to the command expansion for that build statement, so what you are seeing is the intended behavior. Ninja never propagates variable definitions through the build graph, intentionally. This kind of computation is complex and should be performed by the generator for your Ninja build plan.

In this specific case, one possibility is to define a rule-level variable whose value will be a default, that can be overridden by specific build statements, as in:

rule a
  lang = english
  command = echo "a: $lang"
  
rule b
  lang = english
  command = echo "b: $lang"
  
build dep.txt: b

build output.txt: a dep.txt
  lang = french

But that is only a suggestion, this really depends on the problem you want to solve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants