compiler flag depends on action generated file

20 views
Skip to first unread message

Zequan Wu

unread,
Apr 28, 2023, 6:06:59 PM4/28/23
to gn-dev
Hi all,
I want to add a compiler flag which takes a generated file as input. How can I have all the targets with this flag config depends on the action that generates the generated file?

Roland McGrath

unread,
Apr 28, 2023, 10:48:15 PM4/28/23
to Zequan Wu, gn-dev
You can define a target like this:

action("generate-flag-input") {
  outputs = [ "$target_gen_dir/flag.txt" ]
  ...
  public_configs = [ ":use-flag-input" ]
}

config("use-flag-input") {
  inputs = [ "$target_gen_dir/flag.txt" ]
  cflags = [ "--use-input-file=" + rebase_path(inputs[0], 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!

Zequan Wu

unread,
May 1, 2023, 12:47:33 PM5/1/23
to Roland McGrath, gn-dev
Roland, thanks for your reply.

I ended up having the following:

action("generate-flag-input") {
  outputs = [ "$target_gen_dir/flag.txt" ]
  ...
}
config("use-flag-input") {
  cflags = [ "--use-input-file=" + rebase_path(inputs[0], root_build_dir) ]
}
group("a-group") {
  public_configs = [ ":use-flag-input" ]
  public_deps = [ ":generate-flag-input" ]
}

And adding ":a-group" to the target's deps makes the target depend on the group.
Reply all
Reply to author
Forward
0 new messages