Reputation: 170825
GN Reference about inputs for actions:
For action and action_foreach targets, inputs should be the inputs to script that don't vary. These should be all .py files that the script uses via imports (the main script itself will be an implicit dependency of the action so need not be listed).
For action targets, inputs and sources are treated the same, but from a style perspective, it's recommended to follow the same rule as action_foreach and put helper files in the inputs, and the data used by the script (if any) in sources.
Note that another way to declare input dependencies from an action is to have the action write a depfile (see "gn help depfile"). This allows the script to dynamically write input dependencies, that might not be known until actually executing the script. This is more efficient than doing processing while running GN to determine the inputs, and is easier to keep in-sync than hardcoding the list.
In my case one of inputs is optional, so I want to add it to the list only if it exists. I don't see a way to test for this in GN itself. I can use depfiles, but is this the only option?
Upvotes: 0
Views: 513
Reputation: 1154
Build args are one solution, if whatever's invoking GN "knows" whether the optional input file is present:
BUILD.gn
:
declare_args() {
have_some_optional_input_file = false
}
action("my_action") {
inputs = [ ... ] # list of your required inputs
if (have_some_optional_input_file) {
inputs += [ "my_optional_input.whatever" ]
}
...
}
If it's truly unknowable, there's always the exec_script
"escape hatch": https://gn.googlesource.com/gn/+/master/docs/reference.md#func_exec_script
This lets you run an arbitrary script at configuration time, and GN will parse the output as if you'd written it in the BUILd.gn file directly.
Since GN needs to know your exact inputs for every build, you could do something like this:
BUILD.gn
:
action("my_action") {
inputs = [ ... ] + exec_script("find_optional_inputs.py", [], "list lines")
}
Write your find_optional_inputs.py
such that it returns a newline-delimited list of all files you want it to add to the input set.
Be careful, though, your exec_script
statements will run every time you invoke GN, even if it's just for a non-mutating subcommand like desc
or ls
. Additionally, if your script is in a template, it will run once per instantiation. Your script will also run once per toolchain where it is referenced. You can add the --time
argument to GN itself to see how long it takes, so at least you can keep an eye on it!
Upvotes: 1