# GN Reference *This page is automatically generated from* `gn help --markdown all`. ## Contents * [Commands](#commands) * [analyze: Analyze which targets are affected by a list of files.](#analyze) * [args: Display or configure arguments declared by the build.](#args) * [check: Check header dependencies.](#check) * [clean: Cleans the output directory.](#clean) * [desc: Show lots of insightful information about a target or config.](#desc) * [format: Format .gn file.](#format) * [gen: Generate ninja files.](#gen) * [help: Does what you think.](#help) * [ls: List matching targets.](#ls) * [path: Find paths between two targets.](#path) * [refs: Find stuff referencing a target or file.](#refs) * [Target declarations](#targets) * [action: Declare a target that runs a script a single time.](#action) * [action_foreach: Declare a target that runs a script over a set of files.](#action_foreach) * [bundle_data: [iOS/macOS] Declare a target without output.](#bundle_data) * [copy: Declare a target that copies files.](#copy) * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#create_bundle) * [executable: Declare an executable target.](#executable) * [group: Declare a named group of targets.](#group) * [loadable_module: Declare a loadable module target.](#loadable_module) * [shared_library: Declare a shared library target.](#shared_library) * [source_set: Declare a source set target.](#source_set) * [static_library: Declare a static library target.](#static_library) * [target: Declare an target with the given programmatic type.](#target) * [Buildfile functions](#functions) * [assert: Assert an expression is true at generation time.](#assert) * [config: Defines a configuration object.](#config) * [declare_args: Declare build arguments.](#declare_args) * [defined: Returns whether an identifier is defined.](#defined) * [exec_script: Synchronously run a script and return the output.](#exec_script) * [foreach: Iterate over a list.](#foreach) * [forward_variables_from: Copies variables from a different scope.](#forward_variables_from) * [get_label_info: Get an attribute from a target's label.](#get_label_info) * [get_path_info: Extract parts of a file or directory name.](#get_path_info) * [get_target_outputs: [file list] Get the list of outputs from a target.](#get_target_outputs) * [getenv: Get an environment variable.](#getenv) * [import: Import a file into the current scope.](#import) * [not_needed: Mark variables from scope as not needed.](#not_needed) * [pool: Defines a pool object.](#pool) * [print: Prints to the console.](#print) * [process_file_template: Do template expansion over a list of files.](#process_file_template) * [read_file: Read a file into a variable.](#read_file) * [rebase_path: Rebase a file or directory to another location.](#rebase_path) * [set_default_toolchain: Sets the default toolchain name.](#set_default_toolchain) * [set_defaults: Set default values for a target type.](#set_defaults) * [set_sources_assignment_filter: Set a pattern to filter source files.](#set_sources_assignment_filter) * [split_list: Splits a list into N different sub-lists.](#split_list) * [string_replace: Replaces substring in the given string.](#string_replace) * [template: Define a template rule.](#template) * [tool: Specify arguments to a toolchain tool.](#tool) * [toolchain: Defines a toolchain.](#toolchain) * [write_file: Write a file to disk.](#write_file) * [Built-in predefined variables](#predefined_variables) * [current_cpu: [string] The processor architecture of the current toolchain.](#current_cpu) * [current_os: [string] The operating system of the current toolchain.](#current_os) * [current_toolchain: [string] Label of the current toolchain.](#current_toolchain) * [default_toolchain: [string] Label of the default toolchain.](#default_toolchain) * [host_cpu: [string] The processor architecture that GN is running on.](#host_cpu) * [host_os: [string] The operating system that GN is running on.](#host_os) * [invoker: [string] The invoking scope inside a template.](#invoker) * [python_path: [string] Absolute path of Python.](#python_path) * [root_build_dir: [string] Directory where build commands are run.](#root_build_dir) * [root_gen_dir: [string] Directory for the toolchain's generated files.](#root_gen_dir) * [root_out_dir: [string] Root directory for toolchain output files.](#root_out_dir) * [target_cpu: [string] The desired cpu architecture for the build.](#target_cpu) * [target_gen_dir: [string] Directory for a target's generated files.](#target_gen_dir) * [target_name: [string] The name of the current target.](#target_name) * [target_os: [string] The desired operating system for the build.](#target_os) * [target_out_dir: [string] Directory for target output files.](#target_out_dir) * [Variables you set in targets](#target_variables) * [all_dependent_configs: [label list] Configs to be forced on dependents.](#all_dependent_configs) * [allow_circular_includes_from: [label list] Permit includes from deps.](#allow_circular_includes_from) * [arflags: [string list] Arguments passed to static_library archiver.](#arflags) * [args: [string list] Arguments passed to an action.](#args) * [asmflags: [string list] Flags passed to the assembler.](#asmflags) * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#assert_no_deps) * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#bundle_contents_dir) * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#bundle_deps_filter) * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#bundle_executable_dir) * [bundle_plugins_dir: Expansion of {{bundle_plugins_dir}} in create_bundle.](#bundle_plugins_dir) * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#bundle_resources_dir) * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#bundle_root_dir) * [cflags: [string list] Flags passed to all C compiler variants.](#cflags) * [cflags_c: [string list] Flags passed to the C compiler.](#cflags_c) * [cflags_cc: [string list] Flags passed to the C++ compiler.](#cflags_cc) * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#cflags_objc) * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#cflags_objcc) * [check_includes: [boolean] Controls whether a target's files are checked.](#check_includes) * [code_signing_args: [string list] Arguments passed to code signing script.](#code_signing_args) * [code_signing_outputs: [file list] Output files for code signing step.](#code_signing_outputs) * [code_signing_script: [file name] Script for code signing.](#code_signing_script) * [code_signing_sources: [file list] Sources for code signing step.](#code_signing_sources) * [complete_static_lib: [boolean] Links all deps into a static library.](#complete_static_lib) * [configs: [label list] Configs applying to this target or config.](#configs) * [data: [file list] Runtime data file dependencies.](#data) * [data_deps: [label list] Non-linked dependencies.](#data_deps) * [defines: [string list] C preprocessor defines.](#defines) * [depfile: [string] File name for input dependencies for actions.](#depfile) * [deps: [label list] Private linked dependencies.](#deps) * [friend: [label pattern list] Allow targets to include private headers.](#friend) * [include_dirs: [directory list] Additional include directories.](#include_dirs) * [inputs: [file list] Additional compile-time dependencies.](#inputs) * [ldflags: [string list] Flags passed to the linker.](#ldflags) * [lib_dirs: [directory list] Additional library directories.](#lib_dirs) * [libs: [string list] Additional libraries to link.](#libs) * [output_dir: [directory] Directory to put output file in.](#output_dir) * [output_extension: [string] Value to use for the output's file extension.](#output_extension) * [output_name: [string] Name for the output file other than the default.](#output_name) * [output_prefix_override: [boolean] Don't use prefix for output name.](#output_prefix_override) * [outputs: [file list] Output files for actions and copy targets.](#outputs) * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#partial_info_plist) * [pool: [string] Label of the pool used by the action.](#pool) * [precompiled_header: [string] Header file to precompile.](#precompiled_header) * [precompiled_header_type: [string] "gcc" or "msvc".](#precompiled_header_type) * [precompiled_source: [file name] Source file to precompile.](#precompiled_source) * [product_type: [string] Product type for Xcode projects.](#product_type) * [public: [file list] Declare public header files for a target.](#public) * [public_configs: [label list] Configs applied to dependents.](#public_configs) * [public_deps: [label list] Declare public dependencies.](#public_deps) * [response_file_contents: [string list] Contents of .rsp file for actions.](#response_file_contents) * [script: [file name] Script file for actions.](#script) * [sources: [file list] Source files for a target.](#sources) * [testonly: [boolean] Declares a target must only be used for testing.](#testonly) * [visibility: [label list] A list of labels that can depend on a target.](#visibility) * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#write_runtime_deps) * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#xcode_extra_attributes) * [test_application_name: [string] Test application name for unit or ui test target.](#test_application_name) * [Other help topics](#other) * [all: Print all the help at once](#all) * [buildargs: How build arguments work.](#buildargs) * [dotfile: Info about the toplevel .gn file.](#dotfile) * [execution: Build graph and execution overview.](#execution) * [grammar: Language and grammar for GN build files.](#grammar) * [input_conversion: Processing input from exec_script and read_file.](#input_conversion) * [label_pattern: Matching more than one label.](#label_pattern) * [labels: About labels.](#labels) * [ninja_rules: How Ninja build rules are named.](#ninja_rules) * [nogncheck: Annotating includes for checking.](#nogncheck) * [output_conversion: Specifies how to transform a value to output.](#output_conversion) * [runtime_deps: How runtime dependency computation works.](#runtime_deps) * [source_expansion: Map sources to outputs for scripts.](#source_expansion) * [switches: Show available command-line switches.](#switches) ## Commands ### **gn analyze <out_dir> <input_path> <output_path>** ``` 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 <out_dir> [\--list] [\--short] [\--args] [\--overrides-only]** ``` 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] [--overrides-only] [--json] 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 preceding the declaration. If --short is specified, only the names and current values will be printed. If --overrides-only is specified, only the names and current values of arguments that have been overridden (i.e. non-default arguments) will be printed. Overrides come from the /args.gn file and //.gn If --json is specified, the output will be emitted in json format. JSON schema for output: [ { "name": variable_name, "current": { "value": overridden_value, "file": file_name, "line": line_no }, "default": { "value": default_value, "file": file_name, "line": line_no }, "comment": comment_string }, ... ] ``` #### **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 --short --overrides-only Prints overridden arguments 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 <out_dir> [<label_pattern>] [\--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 any of the "include_dirs" for the target (including the implicit current dir). - 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. - Outputs 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 is difficult to fix or doesn't care about include checks 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 <out_dir>** ``` Deletes the contents of the output directory except for args.gn and creates a Ninja build environment sufficient to regenerate the build. ``` ### **gn desc <out_dir> <label or pattern> [<what to show>] [\--blame] "** #### **[\--format=json]** ``` Displays information about a given target or config. The build parameters will be taken for the build in the given . The