# 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.](#cmd_analyze) * [args: Display or configure arguments declared by the build.](#cmd_args) * [check: Check header dependencies.](#cmd_check) * [clean: Cleans the output directory.](#cmd_clean) * [clean_stale: Cleans the stale output files from the output directory.](#cmd_clean_stale) * [desc: Show lots of insightful information about a target or config.](#cmd_desc) * [format: Format .gn files.](#cmd_format) * [gen: Generate ninja files.](#cmd_gen) * [help: Does what you think.](#cmd_help) * [ls: List matching targets.](#cmd_ls) * [meta: List target metadata collection results.](#cmd_meta) * [outputs: Which files a source/target make.](#cmd_outputs) * [path: Find paths between two targets.](#cmd_path) * [refs: Find stuff referencing a target or file.](#cmd_refs) * [Target declarations](#targets) * [action: Declare a target that runs a script a single time.](#func_action) * [action_foreach: Declare a target that runs a script over a set of files.](#func_action_foreach) * [bundle_data: [iOS/macOS] Declare a target without output.](#func_bundle_data) * [copy: Declare a target that copies files.](#func_copy) * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#func_create_bundle) * [executable: Declare an executable target.](#func_executable) * [generated_file: Declare a generated_file target.](#func_generated_file) * [group: Declare a named group of targets.](#func_group) * [loadable_module: Declare a loadable module target.](#func_loadable_module) * [rust_library: Declare a Rust library target.](#func_rust_library) * [rust_proc_macro: Declare a Rust procedural macro target.](#func_rust_proc_macro) * [shared_library: Declare a shared library target.](#func_shared_library) * [source_set: Declare a source set target.](#func_source_set) * [static_library: Declare a static library target.](#func_static_library) * [target: Declare an target with the given programmatic type.](#func_target) * [Buildfile functions](#functions) * [assert: Assert an expression is true at generation time.](#func_assert) * [config: Defines a configuration object.](#func_config) * [declare_args: Declare build arguments.](#func_declare_args) * [defined: Returns whether an identifier is defined.](#func_defined) * [exec_script: Synchronously run a script and return the output.](#func_exec_script) * [filter_exclude: Remove values that match a set of patterns.](#func_filter_exclude) * [filter_include: Remove values that do not match a set of patterns.](#func_filter_include) * [foreach: Iterate over a list.](#func_foreach) * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from) * [get_label_info: Get an attribute from a target's label.](#func_get_label_info) * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info) * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs) * [getenv: Get an environment variable.](#func_getenv) * [import: Import a file into the current scope.](#func_import) * [not_needed: Mark variables from scope as not needed.](#func_not_needed) * [pool: Defines a pool object.](#func_pool) * [print: Prints to the console.](#func_print) * [process_file_template: Do template expansion over a list of files.](#func_process_file_template) * [read_file: Read a file into a variable.](#func_read_file) * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path) * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain) * [set_defaults: Set default values for a target type.](#func_set_defaults) * [split_list: Splits a list into N different sub-lists.](#func_split_list) * [string_join: Concatenates a list of strings with a separator.](#func_string_join) * [string_replace: Replaces substring in the given string.](#func_string_replace) * [string_split: Split string into a list of strings.](#func_string_split) * [template: Define a template rule.](#func_template) * [tool: Specify arguments to a toolchain tool.](#func_tool) * [toolchain: Defines a toolchain.](#func_toolchain) * [write_file: Write a file to disk.](#func_write_file) * [Built-in predefined variables](#predefined_variables) * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu) * [current_os: [string] The operating system of the current toolchain.](#var_current_os) * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain) * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain) * [gn_version: [number] The version of gn.](#var_gn_version) * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu) * [host_os: [string] The operating system that GN is running on.](#var_host_os) * [invoker: [string] The invoking scope inside a template.](#var_invoker) * [python_path: [string] Absolute path of Python.](#var_python_path) * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir) * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir) * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir) * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu) * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir) * [target_name: [string] The name of the current target.](#var_target_name) * [target_os: [string] The desired operating system for the build.](#var_target_os) * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir) * [Variables you set in targets](#target_variables) * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps) * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs) * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from) * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags) * [args: [string list] Arguments passed to an action.](#var_args) * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags) * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps) * [bridge_header: [string] Path to C/Objective-C compatibility header.](#var_bridge_header) * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir) * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter) * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir) * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir) * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir) * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags) * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c) * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc) * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc) * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc) * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes) * [code_signing_args: [string list] Arguments passed to code signing script.](#var_code_signing_args) * [code_signing_outputs: [file list] Output files for code signing step.](#var_code_signing_outputs) * [code_signing_script: [file name] Script for code signing.](#var_code_signing_script) * [code_signing_sources: [file list] Sources for code signing step.](#var_code_signing_sources) * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib) * [configs: [label list] Configs applying to this target or config.](#var_configs) * [contents: Contents to write to file.](#var_contents) * [crate_name: [string] The name for the compiled crate.](#var_crate_name) * [crate_root: [string] The root source file for a binary or library.](#var_crate_root) * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type) * [data: [file list] Runtime data file dependencies.](#var_data) * [data_deps: [label list] Non-linked dependencies.](#var_data_deps) * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys) * [defines: [string list] C preprocessor defines.](#var_defines) * [depfile: [string] File name for input dependencies for actions.](#var_depfile) * [deps: [label list] Private linked dependencies.](#var_deps) * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs) * [framework_dirs: [directory list] Additional framework search directories.](#var_framework_dirs) * [frameworks: [name list] Name of frameworks that must be linked.](#var_frameworks) * [friend: [label pattern list] Allow targets to include private headers.](#var_friend) * [gen_deps: [label list] Declares targets that should generate when this one does.](#var_gen_deps) * [include_dirs: [directory list] Additional include directories.](#var_include_dirs) * [inputs: [file list] Additional compile-time dependencies.](#var_inputs) * [ldflags: [string list] Flags passed to the linker.](#var_ldflags) * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs) * [libs: [string list] Additional libraries to link.](#var_libs) * [metadata: [scope] Metadata of this target.](#var_metadata) * [module_name: [string] The name for the compiled module.](#var_module_name) * [output_conversion: Data format for generated_file targets.](#var_output_conversion) * [output_dir: [directory] Directory to put output file in.](#var_output_dir) * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension) * [output_name: [string] Name for the output file other than the default.](#var_output_name) * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override) * [outputs: [file list] Output files for actions and copy targets.](#var_outputs) * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist) * [pool: [string] Label of the pool used by the action.](#var_pool) * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header) * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type) * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source) * [product_type: [string] Product type for Xcode projects.](#var_product_type) * [public: [file list] Declare public header files for a target.](#var_public) * [public_configs: [label list] Configs applied to dependents.](#var_public_configs) * [public_deps: [label list] Declare public dependencies.](#var_public_deps) * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase) * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents) * [script: [file name] Script file for actions.](#var_script) * [sources: [file list] Source files for a target.](#var_sources) * [swiftflags: [string list] Flags passed to the swift compiler.](#var_swiftflags) * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly) * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility) * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys) * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks) * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps) * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags) * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes) * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name) * [Other help topics](#other) * all: Print all the help at once * [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.](#io_conversion) * [file_pattern: Matching more than one file.](#file_pattern) * [label_pattern: Matching more than one label.](#label_pattern) * [labels: About labels.](#labels) * [metadata_collection: About metadata and its collection.](#metadata_collection) * [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.](#io_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.](#switch_list) ## 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" (optional): 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. If "additional_compile_targets" is absent, it defaults to the empty list. If input_path is -, input is read from stdin. 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. If output_path is -, output is written to stdout. 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**: (command-line tool) ``` Display or configure arguments declared by the build. gn args [--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] [\--check-generated]** ``` 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** ``` --check-generated Generated files are normally not checked since they do not exist until after a build. With this flag, those generated files that can be found on disk are also checked. --check-system Check system style includes (using ) in addition to "double quote" includes. --default-toolchain Normally wildcard targets are matched in all toolchains. This switch makes wildcard labels with no explicit toolchain reference only match targets in the default toolchain. Non-wildcard inputs with no explicit toolchain specification will always match only a target in the default toolchain if one exists. --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 in the list check_targets (see "gn help dotfile"). Alternatively, the .gn file may specify a list of targets not to be checked in no_check_targets. If a label pattern is specified on the command line, neither check_targets or no_check_targets is used. 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. - Generated files (that might not exist yet) are ignored unless the --check-generated flag is provided. - Includes with a "nogncheck" annotation are skipped (see "gn help nogncheck"). - Includes using "quotes" are always checked. If system style checking is enabled, includes using are also checked. - 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 clean_stale [\--ninja-executable=...] <out_dir>...** ``` Removes the no longer needed output files from the build directory and prunes their records from the ninja build log and dependency database. These are output files that were generated from previous builds, but the current build graph no longer references them. This command requires a ninja executable of at least version 1.10.0. The executable must be provided by the --ninja-executable switch. ``` #### **Options** ``` --ninja-executable= Can be used to specify the ninja executable to use. ``` ### **gn desc** ``` gn desc