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!