# GN Reference *This page is automatically generated from* `gn help --markdown all`. ## **\--args**: Specifies build arguments overrides. ``` See "gn help buildargs" for an overview of how build arguments work. Most operations take a build directory. The build arguments are taken from the previous build done in that directory. If a command specifies --args, it will override the previous arguments stored in the build directory, and use the specified ones. The args specified will be saved to the build directory for subsequent commands. Specifying --args="" will clear all build arguments. ``` ### **Formatting** ``` The value of the switch is interpreted in GN syntax. For typical usage of string arguments, you will need to be careful about escaping of quotes. ``` ### **Examples** ``` gn gen out/Default --args="foo=\"bar\"" gn gen out/Default --args='foo="bar" enable=true blah=7' gn check out/Default --args="" Clears existing build args from the directory. gn desc out/Default --args="some_list=[1, false, \"foo\"]" ``` ## **\--[no]color**: Forces colored output on or off. ``` Normally GN will try to detect whether it is outputting to a terminal and will enable or disable color accordingly. Use of these switches will override the default. ``` ### **Examples** ``` gn gen out/Default --color gn gen out/Default --nocolor ``` ## **\--dotfile**: Override the name of the ".gn" file. ``` Normally GN loads the ".gn"file from the source root for some basic configuration (see "gn help dotfile"). This flag allows you to use a different file. Note that this interacts with "--root" in a possibly incorrect way. It would be nice to test the edge cases and document or fix. ``` ## **\--fail-on-unused-args**: Treat unused build args as fatal errors. ``` If you set a value in a build's "gn args" and never use it in the build (in a declare_args() block), GN will normally print an error but not fail the build. In many cases engineers would use build args to enable or disable features that would sometimes get removed. It would by annoying to block work for typically benign problems. In Chrome in particular, flags might be configured for build bots in a separate infrastructure repository, or a declare_args block might be changed in a third party repository. Treating these errors as blocking forced complex multi- way patches to land what would otherwise be simple changes. In some cases, such concerns are not as important, and a mismatch in build flags between the invoker of the build and the build files represents a critical mismatch that should be immediately fixed. Such users can set this flag to force GN to fail in that case. ``` ## **\--markdown**: Write help output in the Markdown format. ## **\--[no]color**: Forces colored output on or off. ``` Normally GN will try to detect whether it is outputting to a terminal and will enable or disable color accordingly. Use of these switches will override the default. ``` ### **Examples** ``` gn gen out/Default --color gn gen out/Default --nocolor ``` ## **-q**: Quiet mode. Don't print output on success. ``` This is useful when running as a part of another script. ``` ## **\--root**: Explicitly specify source root. ``` Normally GN will look up in the directory tree from the current directory to find a ".gn" file. The source root directory specifies the meaning of "//" beginning with paths, and the BUILD.gn file in that directory will be the first thing loaded. Specifying --root allows GN to do builds in a specific directory regardless of the current directory. ``` ### **Examples** ``` gn gen //out/Default --root=/home/baracko/src gn desc //out/Default --root="C:\Users\BObama\My Documents\foo" ``` ## **\--runtime-deps-list-file**: Save runtime dependencies for targets in file. ``` --runtime-deps-list-file= Where is a text file consisting of the labels, one per line, of the targets for which runtime dependencies are desired. See "gn help runtime_deps" for a description of how runtime dependencies are computed. ``` ### **Runtime deps output file** ``` For each target requested, GN will write a separate runtime dependency file. The runtime dependency file will be in the output directory alongside the output file of the target, with a ".runtime_deps" extension. For example, if the target "//foo:bar" is listed in the input file, and that target produces an output file "bar.so", GN will create a file "bar.so.runtime_deps" in the build directory. If a source set, action, copy, or group is listed, the runtime deps file will correspond to the .stamp file corresponding to that target. This is probably not useful; the use-case for this feature is generally executable targets. The runtime dependency file will list one file per line, with no escaping. The files will be relative to the root_build_dir. The first line of the file will be the main output file of the target itself (in the above example, "bar.so"). ``` ## **\--script-executable**: Set the executable used to execute scripts. ``` By default GN searches the PATH for Python to execute scripts in action targets and exec_script calls. This flag allows the specification of a specific Python executable or potentially a different language interpreter. ``` ## **\--threads**: Specify number of worker threads. ``` GN runs many threads to load and run build files. This can make debugging challenging. Or you may want to experiment with different values to see how it affects performance. The parameter is the number of worker threads. This does not count the main thread (so there are always at least two). ``` ### **Examples** ``` gen gen out/Default --threads=1 ``` ## **\--time**: Outputs a summary of how long everything took. ``` Hopefully self-explanatory. ``` ### **Examples** ``` gn gen out/Default --time ``` ## **\--tracelog**: Writes a Chrome-compatible trace log to the given file. ``` The trace log will show file loads, executions, scripts, and writes. This allows performance analysis of the generation step. To view the trace, open Chrome and navigate to "chrome://tracing/", then press "Load" and specify the file you passed to this parameter. ``` ### **Examples** ``` gn gen out/Default --tracelog=mytrace.trace ``` ## **-v**: Verbose logging. ``` This will spew logging events to the console for debugging issues. Good luck! ``` ## **gn analyze ** ``` Analyze which targets are affected by a list of files. This command takes three arguments: out_dir is the path to the build directory. input_path is a path to a file containing a JSON object with three fields: - "files": A list of the filenames to check. - "test_targets": A list of the labels for targets that are needed to run the tests we wish to run. - "additional_compile_targets": A list of the labels for targets that we wish to rebuild, but aren't necessarily needed for testing. The important difference between this field and "test_targets" is that if an item in the additional_compile_targets list refers to a group, then any dependencies of that group will be returned if they are out of date, but the group itself does not need to be. If the dependencies themselves are groups, the same filtering is repeated. This filtering can be used to avoid rebuilding dependencies of a group that are unaffected by the input files. The list may also contain the string "all" to refer to a pseudo-group that contains every root target in the build graph. This filtering behavior is also known as "pruning" the list of compile targets. output_path is a path indicating where the results of the command are to be written. The results will be a file containing a JSON object with one or more of following fields: - "compile_targets": A list of the labels derived from the input compile_targets list that are affected by the input files. Due to the way the filtering works for compile targets as described above, this list may contain targets that do not appear in the input list. - "test_targets": A list of the labels from the input test_targets list that are affected by the input files. This list will be a proper subset of the input list. - "invalid_targets": A list of any names from the input that do not exist in the build graph. If this list is non-empty, the "error" field will also be set to "Invalid targets". - "status": A string containing one of three values: - "Found dependency" - "No dependency" - "Found dependency (all) " In the first case, the lists returned in compile_targets and test_targets should be passed to ninja to build. In the second case, nothing was affected and no build is necessary. In the third case, GN could not determine the correct answer and returned the input as the output in order to be safe. - "error": This will only be present if an error occurred, and will contain a string describing the error. This includes cases where the input file is not in the right format, or contains invalid targets. The command returns 1 if it is unable to read the input file or write the output file, or if there is something wrong with the build such that gen would also fail, and 0 otherwise. In particular, it returns 0 even if the "error" key is non-empty and a non-fatal error occurred. In other words, it tries really hard to always write something to the output JSON and convey errors that way rather than via return codes. ``` ## **gn args [\--list] [\--short] [\--args]** ``` See also "gn help buildargs" for a more high-level overview of how build arguments work. ``` ### **Usage** ``` gn args Open the arguments for the given build directory in an editor. If the given build directory doesn't exist, it will be created and an empty args file will be opened in the editor. You would type something like this into that file: enable_doom_melon=false os="android" To find your editor on Posix, GN will search the environment variables in order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command associated with .txt files. Note: you can edit the build args manually by editing the file "args.gn" in the build directory and then running "gn gen ". gn args --list[=] [--short] Lists all build arguments available in the current configuration, or, if an exact_arg is specified for the list flag, just that one build argument. The output will list the declaration location, current value for the build, default value (if different than the current value), and comment preceeding the declaration. If --short is specified, only the names and current values will be printed. ``` ### **Examples** ``` gn args out/Debug Opens an editor with the args for out/Debug. gn args out/Debug --list --short Prints all arguments with their default values for the out/Debug build. gn args out/Debug --list=target_cpu Prints information about the "target_cpu" argument for the " "out/Debug build. gn args --list --args="os=\"android\" enable_doom_melon=true" Prints all arguments with the default values for a build with the given arguments set (which may affect the values of other arguments). ``` ## **gn check [] [\--force]** ``` GN's include header checker validates that the includes for C-like source files match the build dependency graph. "gn check" is the same thing as "gn gen" with the "--check" flag except that this command does not write out any build files. It's intended to be an easy way to manually trigger include file checking. The can take exact labels or patterns that match more than one (although not general regular expressions). If specified, only those matching targets will be checked. See "gn help label_pattern" for details. ``` ### **Command-specific switches** ``` --force Ignores specifications of "check_includes = false" and checks all target's files that match the target label. ``` ### **What gets checked** ``` The .gn file may specify a list of targets to be checked. Only these targets will be checked if no label_pattern is specified on the command line. Otherwise, the command-line list is used instead. See "gn help dotfile". Targets can opt-out from checking with "check_includes = false" (see "gn help check_includes"). For targets being checked: - GN opens all C-like source files in the targets to be checked and scans the top for includes. - Includes with a "nogncheck" annotation are skipped (see "gn help nogncheck"). - Only includes using "quotes" are checked. are assumed to be system includes. - Include paths are assumed to be relative to either the source root or the "root_gen_dir" and must include all the path components. (It might be nice in the future to incorporate GN's knowledge of the include path to handle other include styles.) - GN does not run the preprocessor so will not understand conditional includes. - Only includes matching known files in the build are checked: includes matching unknown paths are ignored. For an include to be valid: - The included file must be in the current target, or there must be a path following only public dependencies to a target with the file in it ("gn path" is a good way to diagnose problems). - There can be multiple targets with an included file: only one needs to be valid for the include to be allowed. - If there are only "sources" in a target, all are considered to be public and can be included by other targets with a valid public dependency path. - If a target lists files as "public", only those files are able to be included by other targets. Anything in the sources will be considered private and will not be includable regardless of dependency paths. - Ouptuts from actions are treated like public sources on that target. - A target can include headers from a target that depends on it if the other target is annotated accordingly. See "gn help allow_circular_includes_from". ``` ### **Advice on fixing problems** ``` If you have a third party project that uses relative includes, it's generally best to exclude that target from checking altogether via "check_includes = false". If you have conditional includes, make sure the build conditions and the preprocessor conditions match, and annotate the line with "nogncheck" (see "gn help nogncheck" for an example). If two targets are hopelessly intertwined, use the "allow_circular_includes_from" annotation. Ideally each should have identical dependencies so configs inherited from those dependencies are consistent (see "gn help allow_circular_includes_from"). If you have a standalone header file or files that need to be shared between a few targets, you can consider making a source_set listing only those headers as public sources. With only header files, the source set will be a no-op from a build perspective, but will give a central place to refer to those headers. That source set's files will still need to pass "gn check" in isolation. In rare cases it makes sense to list a header in more than one target if it could be considered conceptually a member of both. ``` ### **Examples** ``` gn check out/Debug Check everything. gn check out/Default //foo:bar Check only the files in the //foo:bar target. gn check out/Default "//foo/* Check only the files in targets in the //foo directory tree. ``` ## **gn clean ** ``` Deletes the contents of the output directory except for args.gn and creates a Ninja build environment sufficient to regenerate the build. ``` ## **gn desc