You can define a target like this:
outputs = [ "$target_gen_dir/flag.txt" ]
public_configs = [ ":use-flag-input" ]
inputs = [ "$target_gen_dir/flag.txt" ]
cflags = [ "--use-input-file=" + rebase_path(inputs, root_build_dir) ]
Then every target you want to use the flag, you add `":generate-flag-input" to its deps list. The presence of the action target in the deps graph is what ensures that it gets generated before it's needed. Then the presence of the same file name in the inputs list makes sure that everything that uses the flag gets a ninja file dependency on the input file to ensure that those things always get recompiled when the file is regenerated. Note you can make the action()'s script not actually touch the output file when it didn't change, and that will prevent things from getting recompiled unnecessarily after some input to the flag-generating script changed that didn't affect its output.
Note also that many compilers support the "@file" response file syntax, so you can do this for generating files containing flags (and other command-line arguments) as well as for flags that accept the name of an input file (just replace "--use-input-file=" with "@").
I hope this helps!