Is there a way to get the final value of an arg?

46 views
Skip to first unread message

Cheng Zhao

unread,
Nov 15, 2023, 9:01:06 PM11/15/23
to gn-dev
For example in v8/BUILD.gn, the value of "v8_enable_pointer_compression" printed by "gn args" is an empty string, but the actual value of it is "true" because the arg is modified in v8/BUILD.gn.

Is there a way to get the actual value of the arg with gn command line?

The use case is Node.js, where we need to print the values of V8 configurations into an external file, which will then be read by node-gyp to decide how to configure the native modules. For example, when "v8_enable_pointer_compression" is set to true, node-gyp will add the "V8_COMPRESS_POINTERS" define when compiling native modules.

David Turner

unread,
Nov 16, 2023, 6:35:01 AM11/16/23
to Cheng Zhao, gn-dev
On Thu, Nov 16, 2023 at 3:01 AM Cheng Zhao <zcb...@gmail.com> wrote:
For example in v8/BUILD.gn, the value of "v8_enable_pointer_compression" printed by "gn args" is an empty string, but the actual value of it is "true" because the arg is modified in v8/BUILD.gn.

Is there a way to get the actual value of the arg with gn command line?

In general there is no single "actual value" in general, since BUILD.gn files may be evaluated several times in the context of different GN toolchain, and each one may result in a different value.
For example, here the value of v8_enable_pointer_compression depends on v8_target_cpu,

If you can however create a generated_file() target whose content contains values you are interested in, and whose output is under "$root_out_dir" (which is toolchain dependent) instead of "$root_build_dir", as in:

generated_file("toolchain_args") {
  outputs = [ "toolchain_args.json" ]
  content = {
    target_cpu = v8_target_cpu
    enable_pointer_compression = v8_enable_pointer_compression
  }
  output_conversion = "json"
}

Then after `gn gen`, and even before building, you can look at $BUILD_DIR/toolchain_args.json (for the default toolchain) or $BUILD_DIR/<toolchain_name>/toolchain_args.json (for other toolchains)

Another useful thing we do in the Fuchsia build is write the expanded content of args.gn into a JSON file as well, with something that looks like:

if (current_toolchain == default_toolchain) {
  generated_file("args_json") {
    outputs = [ "args.json" ]
    content = read_file("$root_build_dir/args.gn", "scope")
    output_conversion = "json"
  }
}

This only exports the variables defined in args.gn (and the file it imports), but is useful for our infra scripts.

Hope this helps,

- Digit

The use case is Node.js, where we need to print the values of V8 configurations into an external file, which will then be read by node-gyp to decide how to configure the native modules. For example, when "v8_enable_pointer_compression" is set to true, node-gyp will add the "V8_COMPRESS_POINTERS" define when compiling native modules.

--
To unsubscribe from this group and stop receiving emails from it, send an email to gn-dev+un...@chromium.org.

David Turner

unread,
Nov 16, 2023, 6:36:07 AM11/16/23
to Cheng Zhao, gn-dev
Sorry, the first example missed the right output prefix, correct version is:

generated_file("toolchain_args") {
  outputs = [ "$root_out_dir/toolchain_args.json" ]
  content = {
    target_cpu = v8_target_cpu
    enable_pointer_compression = v8_enable_pointer_compression
  }
  output_conversion = "json"
}

Cheng Zhao

unread,
Nov 16, 2023, 7:01:16 PM11/16/23
to gn-dev, David Turner, gn-dev, Cheng Zhao
This method should work for our use case, thank you so much!
Reply all
Reply to author
Forward
0 new messages