16d528ed9Sopenharmony_ci# GN Reference 26d528ed9Sopenharmony_ci 36d528ed9Sopenharmony_ci*This page is automatically generated from* `gn help --markdown all`. 46d528ed9Sopenharmony_ci 56d528ed9Sopenharmony_ci## Contents 66d528ed9Sopenharmony_ci 76d528ed9Sopenharmony_ci* [Commands](#commands) 86d528ed9Sopenharmony_ci * [analyze: Analyze which targets are affected by a list of files.](#cmd_analyze) 96d528ed9Sopenharmony_ci * [args: Display or configure arguments declared by the build.](#cmd_args) 106d528ed9Sopenharmony_ci * [check: Check header dependencies.](#cmd_check) 116d528ed9Sopenharmony_ci * [clean: Cleans the output directory.](#cmd_clean) 126d528ed9Sopenharmony_ci * [clean_stale: Cleans the stale output files from the output directory.](#cmd_clean_stale) 136d528ed9Sopenharmony_ci * [desc: Show lots of insightful information about a target or config.](#cmd_desc) 146d528ed9Sopenharmony_ci * [format: Format .gn files.](#cmd_format) 156d528ed9Sopenharmony_ci * [gen: Generate ninja files.](#cmd_gen) 166d528ed9Sopenharmony_ci * [help: Does what you think.](#cmd_help) 176d528ed9Sopenharmony_ci * [ls: List matching targets.](#cmd_ls) 186d528ed9Sopenharmony_ci * [meta: List target metadata collection results.](#cmd_meta) 196d528ed9Sopenharmony_ci * [outputs: Which files a source/target make.](#cmd_outputs) 206d528ed9Sopenharmony_ci * [path: Find paths between two targets.](#cmd_path) 216d528ed9Sopenharmony_ci * [refs: Find stuff referencing a target or file.](#cmd_refs) 226d528ed9Sopenharmony_ci* [Target declarations](#targets) 236d528ed9Sopenharmony_ci * [action: Declare a target that runs a script a single time.](#func_action) 246d528ed9Sopenharmony_ci * [action_foreach: Declare a target that runs a script over a set of files.](#func_action_foreach) 256d528ed9Sopenharmony_ci * [bundle_data: [iOS/macOS] Declare a target without output.](#func_bundle_data) 266d528ed9Sopenharmony_ci * [copy: Declare a target that copies files.](#func_copy) 276d528ed9Sopenharmony_ci * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#func_create_bundle) 286d528ed9Sopenharmony_ci * [executable: Declare an executable target.](#func_executable) 296d528ed9Sopenharmony_ci * [generated_file: Declare a generated_file target.](#func_generated_file) 306d528ed9Sopenharmony_ci * [group: Declare a named group of targets.](#func_group) 316d528ed9Sopenharmony_ci * [loadable_module: Declare a loadable module target.](#func_loadable_module) 326d528ed9Sopenharmony_ci * [rust_library: Declare a Rust library target.](#func_rust_library) 336d528ed9Sopenharmony_ci * [rust_proc_macro: Declare a Rust procedural macro target.](#func_rust_proc_macro) 346d528ed9Sopenharmony_ci * [shared_library: Declare a shared library target.](#func_shared_library) 356d528ed9Sopenharmony_ci * [source_set: Declare a source set target.](#func_source_set) 366d528ed9Sopenharmony_ci * [static_library: Declare a static library target.](#func_static_library) 376d528ed9Sopenharmony_ci * [target: Declare a target with the given programmatic type.](#func_target) 386d528ed9Sopenharmony_ci* [Buildfile functions](#functions) 396d528ed9Sopenharmony_ci * [assert: Assert an expression is true at generation time.](#func_assert) 406d528ed9Sopenharmony_ci * [config: Defines a configuration object.](#func_config) 416d528ed9Sopenharmony_ci * [declare_args: Declare build arguments.](#func_declare_args) 426d528ed9Sopenharmony_ci * [defined: Returns whether an identifier is defined.](#func_defined) 436d528ed9Sopenharmony_ci * [exec_script: Synchronously run a script and return the output.](#func_exec_script) 446d528ed9Sopenharmony_ci * [filter_exclude: Remove values that match a set of patterns.](#func_filter_exclude) 456d528ed9Sopenharmony_ci * [filter_include: Remove values that do not match a set of patterns.](#func_filter_include) 466d528ed9Sopenharmony_ci * [filter_labels_exclude: Remove labels that match a set of patterns.](#func_filter_labels_exclude) 476d528ed9Sopenharmony_ci * [filter_labels_include: Remove labels that do not match a set of patterns.](#func_filter_labels_include) 486d528ed9Sopenharmony_ci * [foreach: Iterate over a list.](#func_foreach) 496d528ed9Sopenharmony_ci * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from) 506d528ed9Sopenharmony_ci * [get_label_info: Get an attribute from a target's label.](#func_get_label_info) 516d528ed9Sopenharmony_ci * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info) 526d528ed9Sopenharmony_ci * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs) 536d528ed9Sopenharmony_ci * [getenv: Get an environment variable.](#func_getenv) 546d528ed9Sopenharmony_ci * [import: Import a file into the current scope.](#func_import) 556d528ed9Sopenharmony_ci * [label_matches: Returns whether a label matches any of a list of patterns.](#func_label_matches) 566d528ed9Sopenharmony_ci * [not_needed: Mark variables from scope as not needed.](#func_not_needed) 576d528ed9Sopenharmony_ci * [pool: Defines a pool object.](#func_pool) 586d528ed9Sopenharmony_ci * [print: Prints to the console.](#func_print) 596d528ed9Sopenharmony_ci * [print_stack_trace: Prints a stack trace.](#func_print_stack_trace) 606d528ed9Sopenharmony_ci * [process_file_template: Do template expansion over a list of files.](#func_process_file_template) 616d528ed9Sopenharmony_ci * [read_file: Read a file into a variable.](#func_read_file) 626d528ed9Sopenharmony_ci * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path) 636d528ed9Sopenharmony_ci * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain) 646d528ed9Sopenharmony_ci * [set_defaults: Set default values for a target type.](#func_set_defaults) 656d528ed9Sopenharmony_ci * [split_list: Splits a list into N different sub-lists.](#func_split_list) 666d528ed9Sopenharmony_ci * [string_join: Concatenates a list of strings with a separator.](#func_string_join) 676d528ed9Sopenharmony_ci * [string_replace: Replaces substring in the given string.](#func_string_replace) 686d528ed9Sopenharmony_ci * [string_split: Split string into a list of strings.](#func_string_split) 696d528ed9Sopenharmony_ci * [template: Define a template rule.](#func_template) 706d528ed9Sopenharmony_ci * [tool: Specify arguments to a toolchain tool.](#func_tool) 716d528ed9Sopenharmony_ci * [toolchain: Defines a toolchain.](#func_toolchain) 726d528ed9Sopenharmony_ci * [write_file: Write a file to disk.](#func_write_file) 736d528ed9Sopenharmony_ci* [Built-in predefined variables](#predefined_variables) 746d528ed9Sopenharmony_ci * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu) 756d528ed9Sopenharmony_ci * [current_os: [string] The operating system of the current toolchain.](#var_current_os) 766d528ed9Sopenharmony_ci * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain) 776d528ed9Sopenharmony_ci * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain) 786d528ed9Sopenharmony_ci * [gn_version: [number] The version of gn.](#var_gn_version) 796d528ed9Sopenharmony_ci * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu) 806d528ed9Sopenharmony_ci * [host_os: [string] The operating system that GN is running on.](#var_host_os) 816d528ed9Sopenharmony_ci * [invoker: [string] The invoking scope inside a template.](#var_invoker) 826d528ed9Sopenharmony_ci * [python_path: [string] Absolute path of Python.](#var_python_path) 836d528ed9Sopenharmony_ci * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir) 846d528ed9Sopenharmony_ci * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir) 856d528ed9Sopenharmony_ci * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir) 866d528ed9Sopenharmony_ci * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu) 876d528ed9Sopenharmony_ci * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir) 886d528ed9Sopenharmony_ci * [target_name: [string] The name of the current target.](#var_target_name) 896d528ed9Sopenharmony_ci * [target_os: [string] The desired operating system for the build.](#var_target_os) 906d528ed9Sopenharmony_ci * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir) 916d528ed9Sopenharmony_ci* [Variables you set in targets](#target_variables) 926d528ed9Sopenharmony_ci * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps) 936d528ed9Sopenharmony_ci * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs) 946d528ed9Sopenharmony_ci * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from) 956d528ed9Sopenharmony_ci * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags) 966d528ed9Sopenharmony_ci * [args: [string list] Arguments passed to an action.](#var_args) 976d528ed9Sopenharmony_ci * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags) 986d528ed9Sopenharmony_ci * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps) 996d528ed9Sopenharmony_ci * [bridge_header: [string] Path to C/Objective-C compatibility header.](#var_bridge_header) 1006d528ed9Sopenharmony_ci * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir) 1016d528ed9Sopenharmony_ci * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter) 1026d528ed9Sopenharmony_ci * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir) 1036d528ed9Sopenharmony_ci * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir) 1046d528ed9Sopenharmony_ci * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir) 1056d528ed9Sopenharmony_ci * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags) 1066d528ed9Sopenharmony_ci * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c) 1076d528ed9Sopenharmony_ci * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc) 1086d528ed9Sopenharmony_ci * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc) 1096d528ed9Sopenharmony_ci * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc) 1106d528ed9Sopenharmony_ci * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes) 1116d528ed9Sopenharmony_ci * [code_signing_args: [string list] [deprecated] Args for the post-processing script.](#var_code_signing_args) 1126d528ed9Sopenharmony_ci * [code_signing_outputs: [file list] [deprecated] Outputs of the post-processing step.](#var_code_signing_outputs) 1136d528ed9Sopenharmony_ci * [code_signing_script: [file name] [deprecated] Script for the post-processing step.](#var_code_signing_script) 1146d528ed9Sopenharmony_ci * [code_signing_sources: [file list] [deprecated] Sources for the post-processing step.](#var_code_signing_sources) 1156d528ed9Sopenharmony_ci * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib) 1166d528ed9Sopenharmony_ci * [configs: [label list] Configs applying to this target or config.](#var_configs) 1176d528ed9Sopenharmony_ci * [contents: Contents to write to file.](#var_contents) 1186d528ed9Sopenharmony_ci * [crate_name: [string] The name for the compiled crate.](#var_crate_name) 1196d528ed9Sopenharmony_ci * [crate_root: [string] The root source file for a binary or library.](#var_crate_root) 1206d528ed9Sopenharmony_ci * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type) 1216d528ed9Sopenharmony_ci * [data: [file list] Runtime data file dependencies.](#var_data) 1226d528ed9Sopenharmony_ci * [data_deps: [label list] Non-linked dependencies.](#var_data_deps) 1236d528ed9Sopenharmony_ci * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys) 1246d528ed9Sopenharmony_ci * [defines: [string list] C preprocessor defines.](#var_defines) 1256d528ed9Sopenharmony_ci * [depfile: [string] File name for input dependencies for actions.](#var_depfile) 1266d528ed9Sopenharmony_ci * [deps: [label list] Private linked dependencies.](#var_deps) 1276d528ed9Sopenharmony_ci * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs) 1286d528ed9Sopenharmony_ci * [framework_dirs: [directory list] Additional framework search directories.](#var_framework_dirs) 1296d528ed9Sopenharmony_ci * [frameworks: [name list] Name of frameworks that must be linked.](#var_frameworks) 1306d528ed9Sopenharmony_ci * [friend: [label pattern list] Allow targets to include private headers.](#var_friend) 1316d528ed9Sopenharmony_ci * [gen_deps: [label list] Declares targets that should generate when this one does.](#var_gen_deps) 1326d528ed9Sopenharmony_ci * [include_dirs: [directory list] Additional include directories.](#var_include_dirs) 1336d528ed9Sopenharmony_ci * [inputs: [file list] Additional compile-time dependencies.](#var_inputs) 1346d528ed9Sopenharmony_ci * [ldflags: [string list] Flags passed to the linker.](#var_ldflags) 1356d528ed9Sopenharmony_ci * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs) 1366d528ed9Sopenharmony_ci * [libs: [string list] Additional libraries to link.](#var_libs) 1376d528ed9Sopenharmony_ci * [metadata: [scope] Metadata of this target.](#var_metadata) 1386d528ed9Sopenharmony_ci * [mnemonic: [string] Prefix displayed when ninja runs this action.](#var_mnemonic) 1396d528ed9Sopenharmony_ci * [module_name: [string] The name for the compiled module.](#var_module_name) 1406d528ed9Sopenharmony_ci * [output_conversion: Data format for generated_file targets.](#var_output_conversion) 1416d528ed9Sopenharmony_ci * [output_dir: [directory] Directory to put output file in.](#var_output_dir) 1426d528ed9Sopenharmony_ci * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension) 1436d528ed9Sopenharmony_ci * [output_name: [string] Name for the output file other than the default.](#var_output_name) 1446d528ed9Sopenharmony_ci * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override) 1456d528ed9Sopenharmony_ci * [outputs: [file list] Output files for actions and copy targets.](#var_outputs) 1466d528ed9Sopenharmony_ci * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist) 1476d528ed9Sopenharmony_ci * [pool: [string] Label of the pool used by binary targets and actions.](#var_pool) 1486d528ed9Sopenharmony_ci * [post_processing_args: [string list] Args for the post-processing script.](#var_post_processing_args) 1496d528ed9Sopenharmony_ci * [post_processing_outputs: [file list] Outputs of the post-processing step.](#var_post_processing_outputs) 1506d528ed9Sopenharmony_ci * [post_processing_script: [file name] Script for the post-processing step.](#var_post_processing_script) 1516d528ed9Sopenharmony_ci * [post_processing_sources: [file list] Sources for the post-processing step.](#var_post_processing_sources) 1526d528ed9Sopenharmony_ci * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header) 1536d528ed9Sopenharmony_ci * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type) 1546d528ed9Sopenharmony_ci * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source) 1556d528ed9Sopenharmony_ci * [product_type: [string] Product type for the bundle.](#var_product_type) 1566d528ed9Sopenharmony_ci * [public: [file list] Declare public header files for a target.](#var_public) 1576d528ed9Sopenharmony_ci * [public_configs: [label list] Configs applied to dependents.](#var_public_configs) 1586d528ed9Sopenharmony_ci * [public_deps: [label list] Declare public dependencies.](#var_public_deps) 1596d528ed9Sopenharmony_ci * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase) 1606d528ed9Sopenharmony_ci * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents) 1616d528ed9Sopenharmony_ci * [rustflags: [string list] Flags passed to the Rust compiler.](#var_rustflags) 1626d528ed9Sopenharmony_ci * [script: [file name] Script file for actions.](#var_script) 1636d528ed9Sopenharmony_ci * [sources: [file list] Source files for a target.](#var_sources) 1646d528ed9Sopenharmony_ci * [swiftflags: [string list] Flags passed to the swift compiler.](#var_swiftflags) 1656d528ed9Sopenharmony_ci * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly) 1666d528ed9Sopenharmony_ci * [transparent: [bool] True if the bundle is transparent.](#var_transparent) 1676d528ed9Sopenharmony_ci * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility) 1686d528ed9Sopenharmony_ci * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys) 1696d528ed9Sopenharmony_ci * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks) 1706d528ed9Sopenharmony_ci * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps) 1716d528ed9Sopenharmony_ci * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags) 1726d528ed9Sopenharmony_ci * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes) 1736d528ed9Sopenharmony_ci * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name) 1746d528ed9Sopenharmony_ci* [Other help topics](#other) 1756d528ed9Sopenharmony_ci * all: Print all the help at once 1766d528ed9Sopenharmony_ci * [buildargs: How build arguments work.](#buildargs) 1776d528ed9Sopenharmony_ci * [dotfile: Info about the toplevel .gn file.](#dotfile) 1786d528ed9Sopenharmony_ci * [execution: Build graph and execution overview.](#execution) 1796d528ed9Sopenharmony_ci * [grammar: Language and grammar for GN build files.](#grammar) 1806d528ed9Sopenharmony_ci * [input_conversion: Processing input from exec_script and read_file.](#io_conversion) 1816d528ed9Sopenharmony_ci * [file_pattern: Matching more than one file.](#file_pattern) 1826d528ed9Sopenharmony_ci * [label_pattern: Matching more than one label.](#label_pattern) 1836d528ed9Sopenharmony_ci * [labels: About labels.](#labels) 1846d528ed9Sopenharmony_ci * [metadata_collection: About metadata and its collection.](#metadata_collection) 1856d528ed9Sopenharmony_ci * [ninja_rules: How Ninja build rules are named.](#ninja_rules) 1866d528ed9Sopenharmony_ci * [nogncheck: Annotating includes for checking.](#nogncheck) 1876d528ed9Sopenharmony_ci * [output_conversion: Specifies how to transform a value to output.](#io_conversion) 1886d528ed9Sopenharmony_ci * [runtime_deps: How runtime dependency computation works.](#runtime_deps) 1896d528ed9Sopenharmony_ci * [source_expansion: Map sources to outputs for scripts.](#source_expansion) 1906d528ed9Sopenharmony_ci * [switches: Show available command-line switches.](#switch_list) 1916d528ed9Sopenharmony_ci 1926d528ed9Sopenharmony_ci## <a name="commands"></a>Commands 1936d528ed9Sopenharmony_ci 1946d528ed9Sopenharmony_ci### <a name="cmd_analyze"></a>**gn analyze <out_dir> <input_path> <output_path>** 1956d528ed9Sopenharmony_ci 1966d528ed9Sopenharmony_ci``` 1976d528ed9Sopenharmony_ci Analyze which targets are affected by a list of files. 1986d528ed9Sopenharmony_ci 1996d528ed9Sopenharmony_ci This command takes three arguments: 2006d528ed9Sopenharmony_ci 2016d528ed9Sopenharmony_ci out_dir is the path to the build directory. 2026d528ed9Sopenharmony_ci 2036d528ed9Sopenharmony_ci input_path is a path to a file containing a JSON object with three fields: 2046d528ed9Sopenharmony_ci 2056d528ed9Sopenharmony_ci - "files": A list of the filenames to check. 2066d528ed9Sopenharmony_ci 2076d528ed9Sopenharmony_ci - "test_targets": A list of the labels for targets that are needed to run 2086d528ed9Sopenharmony_ci the tests we wish to run. 2096d528ed9Sopenharmony_ci 2106d528ed9Sopenharmony_ci - "additional_compile_targets" (optional): A list of the labels for targets 2116d528ed9Sopenharmony_ci that we wish to rebuild, but aren't necessarily needed for testing. The 2126d528ed9Sopenharmony_ci important difference between this field and "test_targets" is that if an 2136d528ed9Sopenharmony_ci item in the additional_compile_targets list refers to a group, then any 2146d528ed9Sopenharmony_ci dependencies of that group will be returned if they are out of date, but 2156d528ed9Sopenharmony_ci the group itself does not need to be. If the dependencies themselves are 2166d528ed9Sopenharmony_ci groups, the same filtering is repeated. This filtering can be used to 2176d528ed9Sopenharmony_ci avoid rebuilding dependencies of a group that are unaffected by the input 2186d528ed9Sopenharmony_ci files. The list may also contain the string "all" to refer to a 2196d528ed9Sopenharmony_ci pseudo-group that contains every root target in the build graph. 2206d528ed9Sopenharmony_ci 2216d528ed9Sopenharmony_ci This filtering behavior is also known as "pruning" the list of compile 2226d528ed9Sopenharmony_ci targets. 2236d528ed9Sopenharmony_ci 2246d528ed9Sopenharmony_ci If "additional_compile_targets" is absent, it defaults to the empty list. 2256d528ed9Sopenharmony_ci 2266d528ed9Sopenharmony_ci If input_path is -, input is read from stdin. 2276d528ed9Sopenharmony_ci 2286d528ed9Sopenharmony_ci output_path is a path indicating where the results of the command are to be 2296d528ed9Sopenharmony_ci written. The results will be a file containing a JSON object with one or more 2306d528ed9Sopenharmony_ci of following fields: 2316d528ed9Sopenharmony_ci 2326d528ed9Sopenharmony_ci - "compile_targets": A list of the labels derived from the input 2336d528ed9Sopenharmony_ci compile_targets list that are affected by the input files. Due to the way 2346d528ed9Sopenharmony_ci the filtering works for compile targets as described above, this list may 2356d528ed9Sopenharmony_ci contain targets that do not appear in the input list. 2366d528ed9Sopenharmony_ci 2376d528ed9Sopenharmony_ci - "test_targets": A list of the labels from the input test_targets list that 2386d528ed9Sopenharmony_ci are affected by the input files. This list will be a proper subset of the 2396d528ed9Sopenharmony_ci input list. 2406d528ed9Sopenharmony_ci 2416d528ed9Sopenharmony_ci - "invalid_targets": A list of any names from the input that do not exist in 2426d528ed9Sopenharmony_ci the build graph. If this list is non-empty, the "error" field will also be 2436d528ed9Sopenharmony_ci set to "Invalid targets". 2446d528ed9Sopenharmony_ci 2456d528ed9Sopenharmony_ci - "status": A string containing one of three values: 2466d528ed9Sopenharmony_ci 2476d528ed9Sopenharmony_ci - "Found dependency" 2486d528ed9Sopenharmony_ci - "No dependency" 2496d528ed9Sopenharmony_ci - "Found dependency (all)" 2506d528ed9Sopenharmony_ci 2516d528ed9Sopenharmony_ci In the first case, the lists returned in compile_targets and test_targets 2526d528ed9Sopenharmony_ci should be passed to ninja to build. In the second case, nothing was 2536d528ed9Sopenharmony_ci affected and no build is necessary. In the third case, GN could not 2546d528ed9Sopenharmony_ci determine the correct answer and returned the input as the output in order 2556d528ed9Sopenharmony_ci to be safe. 2566d528ed9Sopenharmony_ci 2576d528ed9Sopenharmony_ci - "error": This will only be present if an error occurred, and will contain 2586d528ed9Sopenharmony_ci a string describing the error. This includes cases where the input file is 2596d528ed9Sopenharmony_ci not in the right format, or contains invalid targets. 2606d528ed9Sopenharmony_ci 2616d528ed9Sopenharmony_ci If output_path is -, output is written to stdout. 2626d528ed9Sopenharmony_ci 2636d528ed9Sopenharmony_ci The command returns 1 if it is unable to read the input file or write the 2646d528ed9Sopenharmony_ci output file, or if there is something wrong with the build such that gen 2656d528ed9Sopenharmony_ci would also fail, and 0 otherwise. In particular, it returns 0 even if the 2666d528ed9Sopenharmony_ci "error" key is non-empty and a non-fatal error occurred. In other words, it 2676d528ed9Sopenharmony_ci tries really hard to always write something to the output JSON and convey 2686d528ed9Sopenharmony_ci errors that way rather than via return codes. 2696d528ed9Sopenharmony_ci``` 2706d528ed9Sopenharmony_ci### <a name="cmd_args"></a>**gn args**: (command-line tool) 2716d528ed9Sopenharmony_ci 2726d528ed9Sopenharmony_ci``` 2736d528ed9Sopenharmony_ci Display or configure arguments declared by the build. 2746d528ed9Sopenharmony_ci 2756d528ed9Sopenharmony_ci gn args <out_dir> [--list] [--short] [--args] [--overrides-only] 2766d528ed9Sopenharmony_ci 2776d528ed9Sopenharmony_ci See also "gn help buildargs" for a more high-level overview of how 2786d528ed9Sopenharmony_ci build arguments work. 2796d528ed9Sopenharmony_ci``` 2806d528ed9Sopenharmony_ci 2816d528ed9Sopenharmony_ci#### **Usage** 2826d528ed9Sopenharmony_ci 2836d528ed9Sopenharmony_ci``` 2846d528ed9Sopenharmony_ci gn args <out_dir> 2856d528ed9Sopenharmony_ci Open the arguments for the given build directory in an editor. If the 2866d528ed9Sopenharmony_ci given build directory doesn't exist, it will be created and an empty args 2876d528ed9Sopenharmony_ci file will be opened in the editor. You would type something like this 2886d528ed9Sopenharmony_ci into that file: 2896d528ed9Sopenharmony_ci enable_doom_melon=false 2906d528ed9Sopenharmony_ci os="android" 2916d528ed9Sopenharmony_ci 2926d528ed9Sopenharmony_ci To find your editor on Posix, GN will search the environment variables in 2936d528ed9Sopenharmony_ci order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command 2946d528ed9Sopenharmony_ci associated with .txt files. 2956d528ed9Sopenharmony_ci 2966d528ed9Sopenharmony_ci Note: you can edit the build args manually by editing the file "args.gn" 2976d528ed9Sopenharmony_ci in the build directory and then running "gn gen <out_dir>". 2986d528ed9Sopenharmony_ci 2996d528ed9Sopenharmony_ci gn args <out_dir> --list[=<exact_arg>] [--short] [--overrides-only] [--json] 3006d528ed9Sopenharmony_ci Lists all build arguments available in the current configuration, or, if 3016d528ed9Sopenharmony_ci an exact_arg is specified for the list flag, just that one build 3026d528ed9Sopenharmony_ci argument. 3036d528ed9Sopenharmony_ci 3046d528ed9Sopenharmony_ci The output will list the declaration location, current value for the 3056d528ed9Sopenharmony_ci build, default value (if different than the current value), and comment 3066d528ed9Sopenharmony_ci preceding the declaration. 3076d528ed9Sopenharmony_ci 3086d528ed9Sopenharmony_ci If --short is specified, only the names and current values will be 3096d528ed9Sopenharmony_ci printed. 3106d528ed9Sopenharmony_ci 3116d528ed9Sopenharmony_ci If --overrides-only is specified, only the names and current values of 3126d528ed9Sopenharmony_ci arguments that have been overridden (i.e. non-default arguments) will 3136d528ed9Sopenharmony_ci be printed. Overrides come from the <out_dir>/args.gn file and //.gn 3146d528ed9Sopenharmony_ci 3156d528ed9Sopenharmony_ci If --json is specified, the output will be emitted in json format. 3166d528ed9Sopenharmony_ci JSON schema for output: 3176d528ed9Sopenharmony_ci [ 3186d528ed9Sopenharmony_ci { 3196d528ed9Sopenharmony_ci "name": variable_name, 3206d528ed9Sopenharmony_ci "current": { 3216d528ed9Sopenharmony_ci "value": overridden_value, 3226d528ed9Sopenharmony_ci "file": file_name, 3236d528ed9Sopenharmony_ci "line": line_no 3246d528ed9Sopenharmony_ci }, 3256d528ed9Sopenharmony_ci "default": { 3266d528ed9Sopenharmony_ci "value": default_value, 3276d528ed9Sopenharmony_ci "file": file_name, 3286d528ed9Sopenharmony_ci "line": line_no 3296d528ed9Sopenharmony_ci }, 3306d528ed9Sopenharmony_ci "comment": comment_string 3316d528ed9Sopenharmony_ci }, 3326d528ed9Sopenharmony_ci ... 3336d528ed9Sopenharmony_ci ] 3346d528ed9Sopenharmony_ci``` 3356d528ed9Sopenharmony_ci 3366d528ed9Sopenharmony_ci#### **Examples** 3376d528ed9Sopenharmony_ci 3386d528ed9Sopenharmony_ci``` 3396d528ed9Sopenharmony_ci gn args out/Debug 3406d528ed9Sopenharmony_ci Opens an editor with the args for out/Debug. 3416d528ed9Sopenharmony_ci 3426d528ed9Sopenharmony_ci gn args out/Debug --list --short 3436d528ed9Sopenharmony_ci Prints all arguments with their default values for the out/Debug 3446d528ed9Sopenharmony_ci build. 3456d528ed9Sopenharmony_ci 3466d528ed9Sopenharmony_ci gn args out/Debug --list --short --overrides-only 3476d528ed9Sopenharmony_ci Prints overridden arguments for the out/Debug build. 3486d528ed9Sopenharmony_ci 3496d528ed9Sopenharmony_ci gn args out/Debug --list=target_cpu 3506d528ed9Sopenharmony_ci Prints information about the "target_cpu" argument for the " 3516d528ed9Sopenharmony_ci "out/Debug 3526d528ed9Sopenharmony_ci build. 3536d528ed9Sopenharmony_ci 3546d528ed9Sopenharmony_ci gn args --list --args="os=\"android\" enable_doom_melon=true" 3556d528ed9Sopenharmony_ci Prints all arguments with the default values for a build with the 3566d528ed9Sopenharmony_ci given arguments set (which may affect the values of other 3576d528ed9Sopenharmony_ci arguments). 3586d528ed9Sopenharmony_ci``` 3596d528ed9Sopenharmony_ci### <a name="cmd_check"></a>**gn check <out_dir> [<label_pattern>] [\--force] [\--check-generated]** 3606d528ed9Sopenharmony_ci 3616d528ed9Sopenharmony_ci``` 3626d528ed9Sopenharmony_ci GN's include header checker validates that the includes for C-like source 3636d528ed9Sopenharmony_ci files match the build dependency graph. 3646d528ed9Sopenharmony_ci 3656d528ed9Sopenharmony_ci "gn check" is the same thing as "gn gen" with the "--check" flag except that 3666d528ed9Sopenharmony_ci this command does not write out any build files. It's intended to be an easy 3676d528ed9Sopenharmony_ci way to manually trigger include file checking. 3686d528ed9Sopenharmony_ci 3696d528ed9Sopenharmony_ci The <label_pattern> can take exact labels or patterns that match more than 3706d528ed9Sopenharmony_ci one (although not general regular expressions). If specified, only those 3716d528ed9Sopenharmony_ci matching targets will be checked. See "gn help label_pattern" for details. 3726d528ed9Sopenharmony_ci``` 3736d528ed9Sopenharmony_ci 3746d528ed9Sopenharmony_ci#### **Command-specific switches** 3756d528ed9Sopenharmony_ci 3766d528ed9Sopenharmony_ci``` 3776d528ed9Sopenharmony_ci --check-generated 3786d528ed9Sopenharmony_ci Generated files are normally not checked since they do not exist 3796d528ed9Sopenharmony_ci until after a build. With this flag, those generated files that 3806d528ed9Sopenharmony_ci can be found on disk are also checked. 3816d528ed9Sopenharmony_ci 3826d528ed9Sopenharmony_ci --check-system 3836d528ed9Sopenharmony_ci Check system style includes (using <angle brackets>) in addition to 3846d528ed9Sopenharmony_ci "double quote" includes. 3856d528ed9Sopenharmony_ci 3866d528ed9Sopenharmony_ci --default-toolchain 3876d528ed9Sopenharmony_ci Normally wildcard targets are matched in all toolchains. This 3886d528ed9Sopenharmony_ci switch makes wildcard labels with no explicit toolchain reference 3896d528ed9Sopenharmony_ci only match targets in the default toolchain. 3906d528ed9Sopenharmony_ci 3916d528ed9Sopenharmony_ci Non-wildcard inputs with no explicit toolchain specification will 3926d528ed9Sopenharmony_ci always match only a target in the default toolchain if one exists. 3936d528ed9Sopenharmony_ci 3946d528ed9Sopenharmony_ci --force 3956d528ed9Sopenharmony_ci Ignores specifications of "check_includes = false" and checks all 3966d528ed9Sopenharmony_ci target's files that match the target label. 3976d528ed9Sopenharmony_ci``` 3986d528ed9Sopenharmony_ci 3996d528ed9Sopenharmony_ci#### **What gets checked** 4006d528ed9Sopenharmony_ci 4016d528ed9Sopenharmony_ci``` 4026d528ed9Sopenharmony_ci The .gn file may specify a list of targets to be checked in the list 4036d528ed9Sopenharmony_ci check_targets (see "gn help dotfile"). Alternatively, the .gn file may 4046d528ed9Sopenharmony_ci specify a list of targets not to be checked in no_check_targets. If a label 4056d528ed9Sopenharmony_ci pattern is specified on the command line, neither check_targets or 4066d528ed9Sopenharmony_ci no_check_targets is used. 4076d528ed9Sopenharmony_ci 4086d528ed9Sopenharmony_ci Targets can opt-out from checking with "check_includes = false" (see 4096d528ed9Sopenharmony_ci "gn help check_includes"). 4106d528ed9Sopenharmony_ci 4116d528ed9Sopenharmony_ci For targets being checked: 4126d528ed9Sopenharmony_ci 4136d528ed9Sopenharmony_ci - GN opens all C-like source files in the targets to be checked and scans 4146d528ed9Sopenharmony_ci the top for includes. 4156d528ed9Sopenharmony_ci 4166d528ed9Sopenharmony_ci - Generated files (that might not exist yet) are ignored unless 4176d528ed9Sopenharmony_ci the --check-generated flag is provided. 4186d528ed9Sopenharmony_ci 4196d528ed9Sopenharmony_ci - Includes with a "nogncheck" annotation are skipped (see 4206d528ed9Sopenharmony_ci "gn help nogncheck"). 4216d528ed9Sopenharmony_ci 4226d528ed9Sopenharmony_ci - Includes using "quotes" are always checked. 4236d528ed9Sopenharmony_ci If system style checking is enabled, includes using <angle brackets> 4246d528ed9Sopenharmony_ci are also checked. 4256d528ed9Sopenharmony_ci 4266d528ed9Sopenharmony_ci - Include paths are assumed to be relative to any of the "include_dirs" for 4276d528ed9Sopenharmony_ci the target (including the implicit current dir). 4286d528ed9Sopenharmony_ci 4296d528ed9Sopenharmony_ci - GN does not run the preprocessor so will not understand conditional 4306d528ed9Sopenharmony_ci includes. 4316d528ed9Sopenharmony_ci 4326d528ed9Sopenharmony_ci - Only includes matching known files in the build are checked: includes 4336d528ed9Sopenharmony_ci matching unknown paths are ignored. 4346d528ed9Sopenharmony_ci 4356d528ed9Sopenharmony_ci For an include to be valid: 4366d528ed9Sopenharmony_ci 4376d528ed9Sopenharmony_ci - The included file must be in the current target, or there must be a path 4386d528ed9Sopenharmony_ci following only public dependencies to a target with the file in it 4396d528ed9Sopenharmony_ci ("gn path" is a good way to diagnose problems). 4406d528ed9Sopenharmony_ci 4416d528ed9Sopenharmony_ci - There can be multiple targets with an included file: only one needs to be 4426d528ed9Sopenharmony_ci valid for the include to be allowed. 4436d528ed9Sopenharmony_ci 4446d528ed9Sopenharmony_ci - If there are only "sources" in a target, all are considered to be public 4456d528ed9Sopenharmony_ci and can be included by other targets with a valid public dependency path. 4466d528ed9Sopenharmony_ci 4476d528ed9Sopenharmony_ci - If a target lists files as "public", only those files are able to be 4486d528ed9Sopenharmony_ci included by other targets. Anything in the sources will be considered 4496d528ed9Sopenharmony_ci private and will not be includable regardless of dependency paths. 4506d528ed9Sopenharmony_ci 4516d528ed9Sopenharmony_ci - Outputs from actions are treated like public sources on that target. 4526d528ed9Sopenharmony_ci 4536d528ed9Sopenharmony_ci - A target can include headers from a target that depends on it if the 4546d528ed9Sopenharmony_ci other target is annotated accordingly. See "gn help 4556d528ed9Sopenharmony_ci allow_circular_includes_from". 4566d528ed9Sopenharmony_ci``` 4576d528ed9Sopenharmony_ci 4586d528ed9Sopenharmony_ci#### **Advice on fixing problems** 4596d528ed9Sopenharmony_ci 4606d528ed9Sopenharmony_ci``` 4616d528ed9Sopenharmony_ci If you have a third party project that is difficult to fix or doesn't care 4626d528ed9Sopenharmony_ci about include checks it's generally best to exclude that target from checking 4636d528ed9Sopenharmony_ci altogether via "check_includes = false". 4646d528ed9Sopenharmony_ci 4656d528ed9Sopenharmony_ci If you have conditional includes, make sure the build conditions and the 4666d528ed9Sopenharmony_ci preprocessor conditions match, and annotate the line with "nogncheck" (see 4676d528ed9Sopenharmony_ci "gn help nogncheck" for an example). 4686d528ed9Sopenharmony_ci 4696d528ed9Sopenharmony_ci If two targets are hopelessly intertwined, use the 4706d528ed9Sopenharmony_ci "allow_circular_includes_from" annotation. Ideally each should have identical 4716d528ed9Sopenharmony_ci dependencies so configs inherited from those dependencies are consistent (see 4726d528ed9Sopenharmony_ci "gn help allow_circular_includes_from"). 4736d528ed9Sopenharmony_ci 4746d528ed9Sopenharmony_ci If you have a standalone header file or files that need to be shared between 4756d528ed9Sopenharmony_ci a few targets, you can consider making a source_set listing only those 4766d528ed9Sopenharmony_ci headers as public sources. With only header files, the source set will be a 4776d528ed9Sopenharmony_ci no-op from a build perspective, but will give a central place to refer to 4786d528ed9Sopenharmony_ci those headers. That source set's files will still need to pass "gn check" in 4796d528ed9Sopenharmony_ci isolation. 4806d528ed9Sopenharmony_ci 4816d528ed9Sopenharmony_ci In rare cases it makes sense to list a header in more than one target if it 4826d528ed9Sopenharmony_ci could be considered conceptually a member of both. 4836d528ed9Sopenharmony_ci``` 4846d528ed9Sopenharmony_ci 4856d528ed9Sopenharmony_ci#### **Examples** 4866d528ed9Sopenharmony_ci 4876d528ed9Sopenharmony_ci``` 4886d528ed9Sopenharmony_ci gn check out/Debug 4896d528ed9Sopenharmony_ci Check everything. 4906d528ed9Sopenharmony_ci 4916d528ed9Sopenharmony_ci gn check out/Default //foo:bar 4926d528ed9Sopenharmony_ci Check only the files in the //foo:bar target. 4936d528ed9Sopenharmony_ci 4946d528ed9Sopenharmony_ci gn check out/Default "//foo/* 4956d528ed9Sopenharmony_ci Check only the files in targets in the //foo directory tree. 4966d528ed9Sopenharmony_ci``` 4976d528ed9Sopenharmony_ci### <a name="cmd_clean"></a>**gn clean <out_dir>...** 4986d528ed9Sopenharmony_ci 4996d528ed9Sopenharmony_ci``` 5006d528ed9Sopenharmony_ci Deletes the contents of the output directory except for args.gn and 5016d528ed9Sopenharmony_ci creates a Ninja build environment sufficient to regenerate the build. 5026d528ed9Sopenharmony_ci``` 5036d528ed9Sopenharmony_ci### <a name="cmd_clean_stale"></a>**gn clean_stale [\--ninja-executable=...] <out_dir>...** 5046d528ed9Sopenharmony_ci 5056d528ed9Sopenharmony_ci``` 5066d528ed9Sopenharmony_ci Removes the no longer needed output files from the build directory and prunes 5076d528ed9Sopenharmony_ci their records from the ninja build log and dependency database. These are 5086d528ed9Sopenharmony_ci output files that were generated from previous builds, but the current build 5096d528ed9Sopenharmony_ci graph no longer references them. 5106d528ed9Sopenharmony_ci 5116d528ed9Sopenharmony_ci This command requires a ninja executable of at least version 1.10.0. The 5126d528ed9Sopenharmony_ci executable must be provided by the --ninja-executable switch. 5136d528ed9Sopenharmony_ci``` 5146d528ed9Sopenharmony_ci 5156d528ed9Sopenharmony_ci#### **Options** 5166d528ed9Sopenharmony_ci 5176d528ed9Sopenharmony_ci``` 5186d528ed9Sopenharmony_ci --ninja-executable=<string> 5196d528ed9Sopenharmony_ci Can be used to specify the ninja executable to use. 5206d528ed9Sopenharmony_ci``` 5216d528ed9Sopenharmony_ci### <a name="cmd_desc"></a>**gn desc** 5226d528ed9Sopenharmony_ci 5236d528ed9Sopenharmony_ci``` 5246d528ed9Sopenharmony_ci gn desc <out_dir> <label or pattern> [<what to show>] [--blame] 5256d528ed9Sopenharmony_ci [--format=json] 5266d528ed9Sopenharmony_ci 5276d528ed9Sopenharmony_ci Displays information about a given target or config. The build parameters 5286d528ed9Sopenharmony_ci will be taken for the build in the given <out_dir>. 5296d528ed9Sopenharmony_ci 5306d528ed9Sopenharmony_ci The <label or pattern> can be a target label, a config label, or a label 5316d528ed9Sopenharmony_ci pattern (see "gn help label_pattern"). A label pattern will only match 5326d528ed9Sopenharmony_ci targets. 5336d528ed9Sopenharmony_ci``` 5346d528ed9Sopenharmony_ci 5356d528ed9Sopenharmony_ci#### **Possibilities for <what to show>** 5366d528ed9Sopenharmony_ci 5376d528ed9Sopenharmony_ci``` 5386d528ed9Sopenharmony_ci (If unspecified an overall summary will be displayed.) 5396d528ed9Sopenharmony_ci 5406d528ed9Sopenharmony_ci all_dependent_configs 5416d528ed9Sopenharmony_ci allow_circular_includes_from 5426d528ed9Sopenharmony_ci arflags [--blame] 5436d528ed9Sopenharmony_ci args 5446d528ed9Sopenharmony_ci cflags [--blame] 5456d528ed9Sopenharmony_ci cflags_c [--blame] 5466d528ed9Sopenharmony_ci cflags_cc [--blame] 5476d528ed9Sopenharmony_ci check_includes 5486d528ed9Sopenharmony_ci configs [--tree] (see below) 5496d528ed9Sopenharmony_ci data_keys 5506d528ed9Sopenharmony_ci defines [--blame] 5516d528ed9Sopenharmony_ci depfile 5526d528ed9Sopenharmony_ci deps [--all] [--tree] (see below) 5536d528ed9Sopenharmony_ci framework_dirs 5546d528ed9Sopenharmony_ci frameworks 5556d528ed9Sopenharmony_ci include_dirs [--blame] 5566d528ed9Sopenharmony_ci inputs 5576d528ed9Sopenharmony_ci ldflags [--blame] 5586d528ed9Sopenharmony_ci lib_dirs 5596d528ed9Sopenharmony_ci libs 5606d528ed9Sopenharmony_ci metadata 5616d528ed9Sopenharmony_ci output_conversion 5626d528ed9Sopenharmony_ci outputs 5636d528ed9Sopenharmony_ci public_configs 5646d528ed9Sopenharmony_ci public 5656d528ed9Sopenharmony_ci rebase 5666d528ed9Sopenharmony_ci script 5676d528ed9Sopenharmony_ci sources 5686d528ed9Sopenharmony_ci testonly 5696d528ed9Sopenharmony_ci visibility 5706d528ed9Sopenharmony_ci walk_keys 5716d528ed9Sopenharmony_ci weak_frameworks 5726d528ed9Sopenharmony_ci 5736d528ed9Sopenharmony_ci runtime_deps 5746d528ed9Sopenharmony_ci Compute all runtime deps for the given target. This is a computed list 5756d528ed9Sopenharmony_ci and does not correspond to any GN variable, unlike most other values 5766d528ed9Sopenharmony_ci here. 5776d528ed9Sopenharmony_ci 5786d528ed9Sopenharmony_ci The output is a list of file names relative to the build directory. See 5796d528ed9Sopenharmony_ci "gn help runtime_deps" for how this is computed. This also works with 5806d528ed9Sopenharmony_ci "--blame" to see the source of the dependency. 5816d528ed9Sopenharmony_ci``` 5826d528ed9Sopenharmony_ci 5836d528ed9Sopenharmony_ci#### **Shared flags** 5846d528ed9Sopenharmony_ci 5856d528ed9Sopenharmony_ci``` 5866d528ed9Sopenharmony_ci --default-toolchain 5876d528ed9Sopenharmony_ci Normally wildcard targets are matched in all toolchains. This 5886d528ed9Sopenharmony_ci switch makes wildcard labels with no explicit toolchain reference 5896d528ed9Sopenharmony_ci only match targets in the default toolchain. 5906d528ed9Sopenharmony_ci 5916d528ed9Sopenharmony_ci Non-wildcard inputs with no explicit toolchain specification will 5926d528ed9Sopenharmony_ci always match only a target in the default toolchain if one exists. 5936d528ed9Sopenharmony_ci 5946d528ed9Sopenharmony_ci --format=json 5956d528ed9Sopenharmony_ci Format the output as JSON instead of text. 5966d528ed9Sopenharmony_ci``` 5976d528ed9Sopenharmony_ci 5986d528ed9Sopenharmony_ci#### **Target flags** 5996d528ed9Sopenharmony_ci 6006d528ed9Sopenharmony_ci``` 6016d528ed9Sopenharmony_ci --blame 6026d528ed9Sopenharmony_ci Used with any value specified on a config, this will name the config that 6036d528ed9Sopenharmony_ci causes that target to get the flag. This doesn't currently work for libs, 6046d528ed9Sopenharmony_ci lib_dirs, frameworks, weak_frameworks and framework_dirs because those are 6056d528ed9Sopenharmony_ci inherited and are more complicated to figure out the blame (patches 6066d528ed9Sopenharmony_ci welcome). 6076d528ed9Sopenharmony_ci``` 6086d528ed9Sopenharmony_ci 6096d528ed9Sopenharmony_ci#### **Configs** 6106d528ed9Sopenharmony_ci 6116d528ed9Sopenharmony_ci``` 6126d528ed9Sopenharmony_ci The "configs" section will list all configs that apply. For targets this will 6136d528ed9Sopenharmony_ci include configs specified in the "configs" variable of the target, and also 6146d528ed9Sopenharmony_ci configs pushed onto this target via public or "all dependent" configs. 6156d528ed9Sopenharmony_ci 6166d528ed9Sopenharmony_ci Configs can have child configs. Specifying --tree will show the hierarchy. 6176d528ed9Sopenharmony_ci``` 6186d528ed9Sopenharmony_ci 6196d528ed9Sopenharmony_ci#### **Printing outputs** 6206d528ed9Sopenharmony_ci 6216d528ed9Sopenharmony_ci``` 6226d528ed9Sopenharmony_ci The "outputs" section will list all outputs that apply, including the outputs 6236d528ed9Sopenharmony_ci computed from the tool definition (eg for "executable", "static_library", ... 6246d528ed9Sopenharmony_ci targets). 6256d528ed9Sopenharmony_ci``` 6266d528ed9Sopenharmony_ci 6276d528ed9Sopenharmony_ci#### **Printing deps** 6286d528ed9Sopenharmony_ci 6296d528ed9Sopenharmony_ci``` 6306d528ed9Sopenharmony_ci Deps will include all public, private, and data deps (TODO this could be 6316d528ed9Sopenharmony_ci clarified and enhanced) sorted in order applying. The following may be used: 6326d528ed9Sopenharmony_ci 6336d528ed9Sopenharmony_ci --all 6346d528ed9Sopenharmony_ci Collects all recursive dependencies and prints a sorted flat list. Also 6356d528ed9Sopenharmony_ci usable with --tree (see below). 6366d528ed9Sopenharmony_ci 6376d528ed9Sopenharmony_ci --as=(buildfile|label|output) 6386d528ed9Sopenharmony_ci How to print targets. 6396d528ed9Sopenharmony_ci 6406d528ed9Sopenharmony_ci buildfile 6416d528ed9Sopenharmony_ci Prints the build files where the given target was declared as 6426d528ed9Sopenharmony_ci file names. 6436d528ed9Sopenharmony_ci label (default) 6446d528ed9Sopenharmony_ci Prints the label of the target. 6456d528ed9Sopenharmony_ci output 6466d528ed9Sopenharmony_ci Prints the first output file for the target relative to the 6476d528ed9Sopenharmony_ci root build directory. 6486d528ed9Sopenharmony_ci 6496d528ed9Sopenharmony_ci --testonly=(true|false) 6506d528ed9Sopenharmony_ci Restrict outputs to targets with the testonly flag set 6516d528ed9Sopenharmony_ci accordingly. When unspecified, the target's testonly flags are 6526d528ed9Sopenharmony_ci ignored. 6536d528ed9Sopenharmony_ci 6546d528ed9Sopenharmony_ci --tree 6556d528ed9Sopenharmony_ci Print a dependency tree. By default, duplicates will be elided with "..." 6566d528ed9Sopenharmony_ci but when --all and -tree are used together, no eliding will be performed. 6576d528ed9Sopenharmony_ci 6586d528ed9Sopenharmony_ci The "deps", "public_deps", and "data_deps" will all be included in the 6596d528ed9Sopenharmony_ci tree. 6606d528ed9Sopenharmony_ci 6616d528ed9Sopenharmony_ci Tree output can not be used with the filtering or output flags: --as, 6626d528ed9Sopenharmony_ci --type, --testonly. 6636d528ed9Sopenharmony_ci 6646d528ed9Sopenharmony_ci --type=(action|copy|executable|group|loadable_module|shared_library| 6656d528ed9Sopenharmony_ci source_set|static_library) 6666d528ed9Sopenharmony_ci Restrict outputs to targets matching the given type. If 6676d528ed9Sopenharmony_ci unspecified, no filtering will be performed. 6686d528ed9Sopenharmony_ci``` 6696d528ed9Sopenharmony_ci 6706d528ed9Sopenharmony_ci#### **Note** 6716d528ed9Sopenharmony_ci 6726d528ed9Sopenharmony_ci``` 6736d528ed9Sopenharmony_ci This command will show the full name of directories and source files, but 6746d528ed9Sopenharmony_ci when directories and source paths are written to the build file, they will be 6756d528ed9Sopenharmony_ci adjusted to be relative to the build directory. So the values for paths 6766d528ed9Sopenharmony_ci displayed by this command won't match (but should mean the same thing). 6776d528ed9Sopenharmony_ci``` 6786d528ed9Sopenharmony_ci 6796d528ed9Sopenharmony_ci#### **Examples** 6806d528ed9Sopenharmony_ci 6816d528ed9Sopenharmony_ci``` 6826d528ed9Sopenharmony_ci gn desc out/Debug //base:base 6836d528ed9Sopenharmony_ci Summarizes the given target. 6846d528ed9Sopenharmony_ci 6856d528ed9Sopenharmony_ci gn desc out/Foo :base_unittests deps --tree 6866d528ed9Sopenharmony_ci Shows a dependency tree of the "base_unittests" project in 6876d528ed9Sopenharmony_ci the current directory. 6886d528ed9Sopenharmony_ci 6896d528ed9Sopenharmony_ci gn desc out/Debug //base defines --blame 6906d528ed9Sopenharmony_ci Shows defines set for the //base:base target, annotated by where 6916d528ed9Sopenharmony_ci each one was set from. 6926d528ed9Sopenharmony_ci``` 6936d528ed9Sopenharmony_ci### <a name="cmd_format"></a>**gn format [\--dump-tree] (\--stdin | <list of build_files...>)** 6946d528ed9Sopenharmony_ci 6956d528ed9Sopenharmony_ci``` 6966d528ed9Sopenharmony_ci Formats .gn file to a standard format. 6976d528ed9Sopenharmony_ci 6986d528ed9Sopenharmony_ci The contents of some lists ('sources', 'deps', etc.) will be sorted to a 6996d528ed9Sopenharmony_ci canonical order. To suppress this, you can add a comment of the form "# 7006d528ed9Sopenharmony_ci NOSORT" immediately preceding the assignment. e.g. 7016d528ed9Sopenharmony_ci 7026d528ed9Sopenharmony_ci # NOSORT 7036d528ed9Sopenharmony_ci sources = [ 7046d528ed9Sopenharmony_ci "z.cc", 7056d528ed9Sopenharmony_ci "a.cc", 7066d528ed9Sopenharmony_ci ] 7076d528ed9Sopenharmony_ci``` 7086d528ed9Sopenharmony_ci 7096d528ed9Sopenharmony_ci#### **Arguments** 7106d528ed9Sopenharmony_ci 7116d528ed9Sopenharmony_ci``` 7126d528ed9Sopenharmony_ci --dry-run 7136d528ed9Sopenharmony_ci Prints the list of files that would be reformatted but does not write 7146d528ed9Sopenharmony_ci anything to disk. This is useful for presubmit/lint-type checks. 7156d528ed9Sopenharmony_ci - Exit code 0: successful format, matches on disk. 7166d528ed9Sopenharmony_ci - Exit code 1: general failure (parse error, etc.) 7176d528ed9Sopenharmony_ci - Exit code 2: successful format, but differs from on disk. 7186d528ed9Sopenharmony_ci 7196d528ed9Sopenharmony_ci --dump-tree[=( text | json )] 7206d528ed9Sopenharmony_ci Dumps the parse tree to stdout and does not update the file or print 7216d528ed9Sopenharmony_ci formatted output. If no format is specified, text format will be used. 7226d528ed9Sopenharmony_ci 7236d528ed9Sopenharmony_ci --stdin 7246d528ed9Sopenharmony_ci Read input from stdin and write to stdout rather than update a file 7256d528ed9Sopenharmony_ci in-place. 7266d528ed9Sopenharmony_ci 7276d528ed9Sopenharmony_ci --read-tree=json 7286d528ed9Sopenharmony_ci Reads an AST from stdin in the format output by --dump-tree=json and 7296d528ed9Sopenharmony_ci uses that as the parse tree. (The only read-tree format currently 7306d528ed9Sopenharmony_ci supported is json.) The given .gn file will be overwritten. This can be 7316d528ed9Sopenharmony_ci used to programmatically transform .gn files. 7326d528ed9Sopenharmony_ci``` 7336d528ed9Sopenharmony_ci 7346d528ed9Sopenharmony_ci#### **Examples** 7356d528ed9Sopenharmony_ci``` 7366d528ed9Sopenharmony_ci gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn 7376d528ed9Sopenharmony_ci gn format some\\BUILD.gn 7386d528ed9Sopenharmony_ci gn format /abspath/some/BUILD.gn 7396d528ed9Sopenharmony_ci gn format --stdin 7406d528ed9Sopenharmony_ci gn format --read-tree=json //rewritten/BUILD.gn 7416d528ed9Sopenharmony_ci``` 7426d528ed9Sopenharmony_ci### <a name="cmd_gen"></a>**gn gen [\--check] [<ide options>] <out_dir>** 7436d528ed9Sopenharmony_ci 7446d528ed9Sopenharmony_ci``` 7456d528ed9Sopenharmony_ci Generates ninja files from the current tree and puts them in the given output 7466d528ed9Sopenharmony_ci directory. 7476d528ed9Sopenharmony_ci 7486d528ed9Sopenharmony_ci The output directory can be a source-repo-absolute path name such as: 7496d528ed9Sopenharmony_ci //out/foo 7506d528ed9Sopenharmony_ci Or it can be a directory relative to the current directory such as: 7516d528ed9Sopenharmony_ci out/foo 7526d528ed9Sopenharmony_ci 7536d528ed9Sopenharmony_ci "gn gen --check" is the same as running "gn check". "gn gen --check=system" is 7546d528ed9Sopenharmony_ci the same as running "gn check --check-system". See "gn help check" for 7556d528ed9Sopenharmony_ci documentation on that mode. 7566d528ed9Sopenharmony_ci 7576d528ed9Sopenharmony_ci See "gn help switches" for the common command-line switches. 7586d528ed9Sopenharmony_ci``` 7596d528ed9Sopenharmony_ci 7606d528ed9Sopenharmony_ci#### **General options** 7616d528ed9Sopenharmony_ci 7626d528ed9Sopenharmony_ci``` 7636d528ed9Sopenharmony_ci --ninja-executable=<string> 7646d528ed9Sopenharmony_ci Can be used to specify the ninja executable to use. This executable will 7656d528ed9Sopenharmony_ci be used as an IDE option to indicate which ninja to use for building. This 7666d528ed9Sopenharmony_ci executable will also be used as part of the gen process for triggering a 7676d528ed9Sopenharmony_ci restat on generated ninja files and for use with --clean-stale. 7686d528ed9Sopenharmony_ci 7696d528ed9Sopenharmony_ci --clean-stale 7706d528ed9Sopenharmony_ci This option will cause no longer needed output files to be removed from 7716d528ed9Sopenharmony_ci the build directory, and their records pruned from the ninja build log and 7726d528ed9Sopenharmony_ci dependency database after the ninja build graph has been generated. This 7736d528ed9Sopenharmony_ci option requires a ninja executable of at least version 1.10.0. It can be 7746d528ed9Sopenharmony_ci provided by the --ninja-executable switch. Also see "gn help clean_stale". 7756d528ed9Sopenharmony_ci``` 7766d528ed9Sopenharmony_ci 7776d528ed9Sopenharmony_ci#### **IDE options** 7786d528ed9Sopenharmony_ci 7796d528ed9Sopenharmony_ci``` 7806d528ed9Sopenharmony_ci GN optionally generates files for IDE. Files won't be overwritten if their 7816d528ed9Sopenharmony_ci contents don't change. Possibilities for <ide options> 7826d528ed9Sopenharmony_ci 7836d528ed9Sopenharmony_ci --ide=<ide_name> 7846d528ed9Sopenharmony_ci Generate files for an IDE. Currently supported values: 7856d528ed9Sopenharmony_ci "eclipse" - Eclipse CDT settings file. 7866d528ed9Sopenharmony_ci "vs" - Visual Studio project/solution files. 7876d528ed9Sopenharmony_ci (default Visual Studio version: 2019) 7886d528ed9Sopenharmony_ci "vs2013" - Visual Studio 2013 project/solution files. 7896d528ed9Sopenharmony_ci "vs2015" - Visual Studio 2015 project/solution files. 7906d528ed9Sopenharmony_ci "vs2017" - Visual Studio 2017 project/solution files. 7916d528ed9Sopenharmony_ci "vs2019" - Visual Studio 2019 project/solution files. 7926d528ed9Sopenharmony_ci "vs2022" - Visual Studio 2022 project/solution files. 7936d528ed9Sopenharmony_ci "xcode" - Xcode workspace/solution files. 7946d528ed9Sopenharmony_ci "qtcreator" - QtCreator project files. 7956d528ed9Sopenharmony_ci "json" - JSON file containing target information 7966d528ed9Sopenharmony_ci 7976d528ed9Sopenharmony_ci --filters=<path_prefixes> 7986d528ed9Sopenharmony_ci Semicolon-separated list of label patterns used to limit the set of 7996d528ed9Sopenharmony_ci generated projects (see "gn help label_pattern"). Only matching targets 8006d528ed9Sopenharmony_ci and their dependencies will be included in the solution. Only used for 8016d528ed9Sopenharmony_ci Visual Studio, Xcode and JSON. 8026d528ed9Sopenharmony_ci``` 8036d528ed9Sopenharmony_ci 8046d528ed9Sopenharmony_ci#### **Visual Studio Flags** 8056d528ed9Sopenharmony_ci 8066d528ed9Sopenharmony_ci``` 8076d528ed9Sopenharmony_ci --sln=<file_name> 8086d528ed9Sopenharmony_ci Override default sln file name ("all"). Solution file is written to the 8096d528ed9Sopenharmony_ci root build directory. 8106d528ed9Sopenharmony_ci 8116d528ed9Sopenharmony_ci --no-deps 8126d528ed9Sopenharmony_ci Don't include targets dependencies to the solution. Changes the way how 8136d528ed9Sopenharmony_ci --filters option works. Only directly matching targets are included. 8146d528ed9Sopenharmony_ci 8156d528ed9Sopenharmony_ci --winsdk=<sdk_version> 8166d528ed9Sopenharmony_ci Use the specified Windows 10 SDK version to generate project files. 8176d528ed9Sopenharmony_ci As an example, "10.0.15063.0" can be specified to use Creators Update SDK 8186d528ed9Sopenharmony_ci instead of the default one. 8196d528ed9Sopenharmony_ci 8206d528ed9Sopenharmony_ci --ninja-executable=<string> 8216d528ed9Sopenharmony_ci Can be used to specify the ninja executable to use when building. 8226d528ed9Sopenharmony_ci 8236d528ed9Sopenharmony_ci --ninja-extra-args=<string> 8246d528ed9Sopenharmony_ci This string is passed without any quoting to the ninja invocation 8256d528ed9Sopenharmony_ci command-line. Can be used to configure ninja flags, like "-j". 8266d528ed9Sopenharmony_ci``` 8276d528ed9Sopenharmony_ci 8286d528ed9Sopenharmony_ci#### **Xcode Flags** 8296d528ed9Sopenharmony_ci 8306d528ed9Sopenharmony_ci``` 8316d528ed9Sopenharmony_ci --xcode-project=<file_name> 8326d528ed9Sopenharmony_ci Override default Xcode project file name ("all"). The project file is 8336d528ed9Sopenharmony_ci written to the root build directory. 8346d528ed9Sopenharmony_ci 8356d528ed9Sopenharmony_ci --xcode-build-system=<value> 8366d528ed9Sopenharmony_ci Configure the build system to use for the Xcode project. Supported 8376d528ed9Sopenharmony_ci values are (default to "legacy"): 8386d528ed9Sopenharmony_ci "legacy" - Legacy Build system 8396d528ed9Sopenharmony_ci "new" - New Build System 8406d528ed9Sopenharmony_ci 8416d528ed9Sopenharmony_ci --xcode-configs=<config_name_list> 8426d528ed9Sopenharmony_ci Configure the list of build configuration supported by the generated 8436d528ed9Sopenharmony_ci project. If specified, must be a list of semicolon-separated strings. 8446d528ed9Sopenharmony_ci If omitted, a single configuration will be used in the generated 8456d528ed9Sopenharmony_ci project derived from the build directory. 8466d528ed9Sopenharmony_ci 8476d528ed9Sopenharmony_ci --xcode-config-build-dir=<string> 8486d528ed9Sopenharmony_ci If present, must be a path relative to the source directory. It will 8496d528ed9Sopenharmony_ci default to $root_out_dir if omitted. The path is assumed to point to 8506d528ed9Sopenharmony_ci the directory where ninja needs to be invoked. This variable can be 8516d528ed9Sopenharmony_ci used to build for multiple configuration / platform / environment from 8526d528ed9Sopenharmony_ci the same generated Xcode project (assuming that the user has created a 8536d528ed9Sopenharmony_ci gn build directory with the correct args.gn for each). 8546d528ed9Sopenharmony_ci 8556d528ed9Sopenharmony_ci One useful value is to use Xcode variables such as '${CONFIGURATION}' 8566d528ed9Sopenharmony_ci or '${EFFECTIVE_PLATFORM}'. 8576d528ed9Sopenharmony_ci 8586d528ed9Sopenharmony_ci --xcode-additional-files-patterns=<pattern_list> 8596d528ed9Sopenharmony_ci If present, must be a list of semicolon-separated file patterns. It 8606d528ed9Sopenharmony_ci will be used to add all files matching the pattern located in the 8616d528ed9Sopenharmony_ci source tree to the project. It can be used to add, e.g. documentation 8626d528ed9Sopenharmony_ci files to the project to allow easily edit them. 8636d528ed9Sopenharmony_ci 8646d528ed9Sopenharmony_ci --xcode-additional-files-roots=<path_list> 8656d528ed9Sopenharmony_ci If present, must be a list of semicolon-separated paths. It will be used 8666d528ed9Sopenharmony_ci as roots when looking for additional files to add. If omitted, defaults 8676d528ed9Sopenharmony_ci to "//". 8686d528ed9Sopenharmony_ci 8696d528ed9Sopenharmony_ci --ninja-executable=<string> 8706d528ed9Sopenharmony_ci Can be used to specify the ninja executable to use when building. 8716d528ed9Sopenharmony_ci 8726d528ed9Sopenharmony_ci --ninja-extra-args=<string> 8736d528ed9Sopenharmony_ci This string is passed without any quoting to the ninja invocation 8746d528ed9Sopenharmony_ci command-line. Can be used to configure ninja flags, like "-j". 8756d528ed9Sopenharmony_ci 8766d528ed9Sopenharmony_ci --ide-root-target=<target_name> 8776d528ed9Sopenharmony_ci Name of the target corresponding to "All" target in Xcode. If unset, 8786d528ed9Sopenharmony_ci "All" invokes ninja without any target and builds everything. 8796d528ed9Sopenharmony_ci``` 8806d528ed9Sopenharmony_ci 8816d528ed9Sopenharmony_ci#### **QtCreator Flags** 8826d528ed9Sopenharmony_ci 8836d528ed9Sopenharmony_ci``` 8846d528ed9Sopenharmony_ci --ide-root-target=<target_name> 8856d528ed9Sopenharmony_ci Name of the root target for which the QtCreator project will be generated 8866d528ed9Sopenharmony_ci to contain files of it and its dependencies. If unset, the whole build 8876d528ed9Sopenharmony_ci graph will be emitted. 8886d528ed9Sopenharmony_ci``` 8896d528ed9Sopenharmony_ci 8906d528ed9Sopenharmony_ci#### **Eclipse IDE Support** 8916d528ed9Sopenharmony_ci 8926d528ed9Sopenharmony_ci``` 8936d528ed9Sopenharmony_ci GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings 8946d528ed9Sopenharmony_ci file which can be imported into an Eclipse CDT project. The XML file contains 8956d528ed9Sopenharmony_ci a list of include paths and defines. Because GN does not generate a full 8966d528ed9Sopenharmony_ci .cproject definition, it is not possible to properly define includes/defines 8976d528ed9Sopenharmony_ci for each file individually. Instead, one set of includes/defines is generated 8986d528ed9Sopenharmony_ci for the entire project. This works fairly well but may still result in a few 8996d528ed9Sopenharmony_ci indexer issues here and there. 9006d528ed9Sopenharmony_ci``` 9016d528ed9Sopenharmony_ci 9026d528ed9Sopenharmony_ci#### **Generic JSON Output** 9036d528ed9Sopenharmony_ci 9046d528ed9Sopenharmony_ci``` 9056d528ed9Sopenharmony_ci Dumps target information to a JSON file and optionally invokes a 9066d528ed9Sopenharmony_ci python script on the generated file. See the comments at the beginning 9076d528ed9Sopenharmony_ci of json_project_writer.cc and desc_builder.cc for an overview of the JSON 9086d528ed9Sopenharmony_ci file format. 9096d528ed9Sopenharmony_ci 9106d528ed9Sopenharmony_ci --json-file-name=<json_file_name> 9116d528ed9Sopenharmony_ci Overrides default file name (project.json) of generated JSON file. 9126d528ed9Sopenharmony_ci 9136d528ed9Sopenharmony_ci --json-ide-script=<path_to_python_script> 9146d528ed9Sopenharmony_ci Executes python script after the JSON file is generated or updated with 9156d528ed9Sopenharmony_ci new content. Path can be project absolute (//), system absolute (/) or 9166d528ed9Sopenharmony_ci relative, in which case the output directory will be base. Path to 9176d528ed9Sopenharmony_ci generated JSON file will be first argument when invoking script. 9186d528ed9Sopenharmony_ci 9196d528ed9Sopenharmony_ci --json-ide-script-args=<argument> 9206d528ed9Sopenharmony_ci Optional second argument that will be passed to executed script. 9216d528ed9Sopenharmony_ci``` 9226d528ed9Sopenharmony_ci 9236d528ed9Sopenharmony_ci#### **Ninja Outputs** 9246d528ed9Sopenharmony_ci 9256d528ed9Sopenharmony_ci``` 9266d528ed9Sopenharmony_ci The --ninja-outputs-file=<FILE> option dumps a JSON file that maps GN labels 9276d528ed9Sopenharmony_ci to their Ninja output paths. This can be later processed to build an index 9286d528ed9Sopenharmony_ci to convert between Ninja targets and GN ones before or after the build itself. 9296d528ed9Sopenharmony_ci It looks like: 9306d528ed9Sopenharmony_ci 9316d528ed9Sopenharmony_ci { 9326d528ed9Sopenharmony_ci "label1": [ 9336d528ed9Sopenharmony_ci "path1", 9346d528ed9Sopenharmony_ci "path2" 9356d528ed9Sopenharmony_ci ], 9366d528ed9Sopenharmony_ci "label2": [ 9376d528ed9Sopenharmony_ci "path3" 9386d528ed9Sopenharmony_ci ] 9396d528ed9Sopenharmony_ci } 9406d528ed9Sopenharmony_ci 9416d528ed9Sopenharmony_ci --ninja-outputs-script=<path_to_python_script> 9426d528ed9Sopenharmony_ci Executes python script after the outputs file is generated or updated 9436d528ed9Sopenharmony_ci with new content. Path can be project absolute (//), system absolute (/) or 9446d528ed9Sopenharmony_ci relative, in which case the output directory will be base. Path to 9456d528ed9Sopenharmony_ci generated file will be first argument when invoking script. 9466d528ed9Sopenharmony_ci 9476d528ed9Sopenharmony_ci --ninja-outputs-script-args=<argument> 9486d528ed9Sopenharmony_ci Optional second argument that will be passed to executed script. 9496d528ed9Sopenharmony_ci``` 9506d528ed9Sopenharmony_ci 9516d528ed9Sopenharmony_ci#### **Compilation Database** 9526d528ed9Sopenharmony_ci 9536d528ed9Sopenharmony_ci``` 9546d528ed9Sopenharmony_ci --export-rust-project 9556d528ed9Sopenharmony_ci Produces a rust-project.json file in the root of the build directory 9566d528ed9Sopenharmony_ci This is used for various tools in the Rust ecosystem allowing for the 9576d528ed9Sopenharmony_ci replay of individual compilations independent of the build system. 9586d528ed9Sopenharmony_ci This is an unstable format and likely to change without warning. 9596d528ed9Sopenharmony_ci 9606d528ed9Sopenharmony_ci --add-export-compile-commands=<label_pattern> 9616d528ed9Sopenharmony_ci Adds an additional label pattern (see "gn help label_pattern") of a 9626d528ed9Sopenharmony_ci target to add to the compilation database. This pattern is appended to any 9636d528ed9Sopenharmony_ci list values specified in the export_compile_commands variable in the 9646d528ed9Sopenharmony_ci .gn file (see "gn help dotfile"). This allows the user to add additional 9656d528ed9Sopenharmony_ci targets to the compilation database that the project doesn't add by default. 9666d528ed9Sopenharmony_ci 9676d528ed9Sopenharmony_ci To add more than one value, specify this switch more than once. Each 9686d528ed9Sopenharmony_ci invocation adds an additional label pattern. 9696d528ed9Sopenharmony_ci 9706d528ed9Sopenharmony_ci Example: 9716d528ed9Sopenharmony_ci --add-export-compile-commands=//tools:my_tool 9726d528ed9Sopenharmony_ci --add-export-compile-commands="//base/*" 9736d528ed9Sopenharmony_ci 9746d528ed9Sopenharmony_ci --export-compile-commands[=<target_name1,target_name2...>] 9756d528ed9Sopenharmony_ci DEPRECATED https://bugs.chromium.org/p/gn/issues/detail?id=302. 9766d528ed9Sopenharmony_ci Please use --add-export-compile-commands for per-user configuration, and 9776d528ed9Sopenharmony_ci the "export_compile_commands" value in the project-level .gn file (see 9786d528ed9Sopenharmony_ci "gn help dotfile") for per-project configuration. 9796d528ed9Sopenharmony_ci 9806d528ed9Sopenharmony_ci Overrides the value of the export_compile_commands in the .gn file (see 9816d528ed9Sopenharmony_ci "gn help dotfile") as well as the --add-export-compile-commands switch. 9826d528ed9Sopenharmony_ci 9836d528ed9Sopenharmony_ci Unlike the .gn setting, this switch takes a legacy format which is a list 9846d528ed9Sopenharmony_ci of target names that are matched in any directory. For example, "foo" will 9856d528ed9Sopenharmony_ci match: 9866d528ed9Sopenharmony_ci - "//path/to/src:foo" 9876d528ed9Sopenharmony_ci - "//other/path:foo" 9886d528ed9Sopenharmony_ci - "//foo:foo" 9896d528ed9Sopenharmony_ci and not match: 9906d528ed9Sopenharmony_ci - "//foo:bar" 9916d528ed9Sopenharmony_ci``` 9926d528ed9Sopenharmony_ci### <a name="cmd_help"></a>**gn help <anything>** 9936d528ed9Sopenharmony_ci 9946d528ed9Sopenharmony_ci``` 9956d528ed9Sopenharmony_ci Yo dawg, I heard you like help on your help so I put help on the help in the 9966d528ed9Sopenharmony_ci help. 9976d528ed9Sopenharmony_ci 9986d528ed9Sopenharmony_ci You can also use "all" as the parameter to get all help at once. 9996d528ed9Sopenharmony_ci``` 10006d528ed9Sopenharmony_ci 10016d528ed9Sopenharmony_ci#### **Switches** 10026d528ed9Sopenharmony_ci 10036d528ed9Sopenharmony_ci``` 10046d528ed9Sopenharmony_ci --markdown 10056d528ed9Sopenharmony_ci Format output in markdown syntax. 10066d528ed9Sopenharmony_ci``` 10076d528ed9Sopenharmony_ci 10086d528ed9Sopenharmony_ci#### **Example** 10096d528ed9Sopenharmony_ci 10106d528ed9Sopenharmony_ci``` 10116d528ed9Sopenharmony_ci gn help --markdown all 10126d528ed9Sopenharmony_ci Dump all help to stdout in markdown format. 10136d528ed9Sopenharmony_ci``` 10146d528ed9Sopenharmony_ci### <a name="cmd_ls"></a>**gn ls <out_dir> [<label_pattern>] [\--default-toolchain] [\--as=...]** 10156d528ed9Sopenharmony_ci``` 10166d528ed9Sopenharmony_ci [--type=...] [--testonly=...] 10176d528ed9Sopenharmony_ci 10186d528ed9Sopenharmony_ci Lists all targets matching the given pattern for the given build directory. 10196d528ed9Sopenharmony_ci By default, only targets in the default toolchain will be matched unless a 10206d528ed9Sopenharmony_ci toolchain is explicitly supplied. 10216d528ed9Sopenharmony_ci 10226d528ed9Sopenharmony_ci If the label pattern is unspecified, list all targets. The label pattern is 10236d528ed9Sopenharmony_ci not a general regular expression (see "gn help label_pattern"). If you need 10246d528ed9Sopenharmony_ci more complex expressions, pipe the result through grep. 10256d528ed9Sopenharmony_ci``` 10266d528ed9Sopenharmony_ci 10276d528ed9Sopenharmony_ci#### **Options** 10286d528ed9Sopenharmony_ci 10296d528ed9Sopenharmony_ci``` 10306d528ed9Sopenharmony_ci --as=(buildfile|label|output) 10316d528ed9Sopenharmony_ci How to print targets. 10326d528ed9Sopenharmony_ci 10336d528ed9Sopenharmony_ci buildfile 10346d528ed9Sopenharmony_ci Prints the build files where the given target was declared as 10356d528ed9Sopenharmony_ci file names. 10366d528ed9Sopenharmony_ci label (default) 10376d528ed9Sopenharmony_ci Prints the label of the target. 10386d528ed9Sopenharmony_ci output 10396d528ed9Sopenharmony_ci Prints the first output file for the target relative to the 10406d528ed9Sopenharmony_ci root build directory. 10416d528ed9Sopenharmony_ci 10426d528ed9Sopenharmony_ci --default-toolchain 10436d528ed9Sopenharmony_ci Normally wildcard targets are matched in all toolchains. This 10446d528ed9Sopenharmony_ci switch makes wildcard labels with no explicit toolchain reference 10456d528ed9Sopenharmony_ci only match targets in the default toolchain. 10466d528ed9Sopenharmony_ci 10476d528ed9Sopenharmony_ci Non-wildcard inputs with no explicit toolchain specification will 10486d528ed9Sopenharmony_ci always match only a target in the default toolchain if one exists. 10496d528ed9Sopenharmony_ci 10506d528ed9Sopenharmony_ci --testonly=(true|false) 10516d528ed9Sopenharmony_ci Restrict outputs to targets with the testonly flag set 10526d528ed9Sopenharmony_ci accordingly. When unspecified, the target's testonly flags are 10536d528ed9Sopenharmony_ci ignored. 10546d528ed9Sopenharmony_ci 10556d528ed9Sopenharmony_ci --type=(action|copy|executable|group|loadable_module|shared_library| 10566d528ed9Sopenharmony_ci source_set|static_library) 10576d528ed9Sopenharmony_ci Restrict outputs to targets matching the given type. If 10586d528ed9Sopenharmony_ci unspecified, no filtering will be performed. 10596d528ed9Sopenharmony_ci``` 10606d528ed9Sopenharmony_ci 10616d528ed9Sopenharmony_ci#### **Examples** 10626d528ed9Sopenharmony_ci 10636d528ed9Sopenharmony_ci``` 10646d528ed9Sopenharmony_ci gn ls out/Debug 10656d528ed9Sopenharmony_ci Lists all targets in the default toolchain. 10666d528ed9Sopenharmony_ci 10676d528ed9Sopenharmony_ci gn ls out/Debug "//base/*" 10686d528ed9Sopenharmony_ci Lists all targets in the directory base and all subdirectories. 10696d528ed9Sopenharmony_ci 10706d528ed9Sopenharmony_ci gn ls out/Debug "//base:*" 10716d528ed9Sopenharmony_ci Lists all targets defined in //base/BUILD.gn. 10726d528ed9Sopenharmony_ci 10736d528ed9Sopenharmony_ci gn ls out/Debug //base --as=output 10746d528ed9Sopenharmony_ci Lists the build output file for //base:base 10756d528ed9Sopenharmony_ci 10766d528ed9Sopenharmony_ci gn ls out/Debug --type=executable 10776d528ed9Sopenharmony_ci Lists all executables produced by the build. 10786d528ed9Sopenharmony_ci 10796d528ed9Sopenharmony_ci gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug 10806d528ed9Sopenharmony_ci Builds all targets in //base and all subdirectories. 10816d528ed9Sopenharmony_ci``` 10826d528ed9Sopenharmony_ci### <a name="cmd_meta"></a>**gn meta** 10836d528ed9Sopenharmony_ci 10846d528ed9Sopenharmony_ci``` 10856d528ed9Sopenharmony_ci gn meta <out_dir> <target>* --data=<key>[,<key>*]* [--walk=<key>[,<key>*]*] 10866d528ed9Sopenharmony_ci [--rebase=<dest dir>] 10876d528ed9Sopenharmony_ci 10886d528ed9Sopenharmony_ci Lists collected metaresults of all given targets for the given data key(s), 10896d528ed9Sopenharmony_ci collecting metadata dependencies as specified by the given walk key(s). 10906d528ed9Sopenharmony_ci 10916d528ed9Sopenharmony_ci See `gn help generated_file` for more information on the walk. 10926d528ed9Sopenharmony_ci``` 10936d528ed9Sopenharmony_ci 10946d528ed9Sopenharmony_ci#### **Arguments** 10956d528ed9Sopenharmony_ci 10966d528ed9Sopenharmony_ci``` 10976d528ed9Sopenharmony_ci <target(s)> 10986d528ed9Sopenharmony_ci A list of target labels from which to initiate the walk. 10996d528ed9Sopenharmony_ci 11006d528ed9Sopenharmony_ci --data 11016d528ed9Sopenharmony_ci A comma-separated list of keys from which to extract data. In each target 11026d528ed9Sopenharmony_ci walked, its metadata scope is checked for the presence of these keys. If 11036d528ed9Sopenharmony_ci present, the contents of those variable in the scope are appended to the 11046d528ed9Sopenharmony_ci results list. 11056d528ed9Sopenharmony_ci 11066d528ed9Sopenharmony_ci --walk (optional) 11076d528ed9Sopenharmony_ci A comma-separated list of keys from which to control the walk. In each 11086d528ed9Sopenharmony_ci target walked, its metadata scope is checked for the presence of any of 11096d528ed9Sopenharmony_ci these keys. If present, the contents of those variables is checked to ensure 11106d528ed9Sopenharmony_ci that it is a label of a valid dependency of the target and then added to the 11116d528ed9Sopenharmony_ci set of targets to walk. If the empty string ("") is present in any of these 11126d528ed9Sopenharmony_ci keys, all deps and data_deps are added to the walk set. 11136d528ed9Sopenharmony_ci 11146d528ed9Sopenharmony_ci --rebase (optional) 11156d528ed9Sopenharmony_ci A destination directory onto which to rebase any paths found. If set, all 11166d528ed9Sopenharmony_ci collected metadata will be rebased onto this path. This option will throw errors 11176d528ed9Sopenharmony_ci if collected metadata is not a list of strings. 11186d528ed9Sopenharmony_ci``` 11196d528ed9Sopenharmony_ci 11206d528ed9Sopenharmony_ci#### **Examples** 11216d528ed9Sopenharmony_ci 11226d528ed9Sopenharmony_ci``` 11236d528ed9Sopenharmony_ci gn meta out/Debug "//base/foo" --data=files 11246d528ed9Sopenharmony_ci Lists collected metaresults for the `files` key in the //base/foo:foo 11256d528ed9Sopenharmony_ci target and all of its dependency tree. 11266d528ed9Sopenharmony_ci 11276d528ed9Sopenharmony_ci gn meta out/Debug "//base/foo" --data=files,other 11286d528ed9Sopenharmony_ci Lists collected metaresults for the `files` and `other` keys in the 11296d528ed9Sopenharmony_ci //base/foo:foo target and all of its dependency tree. 11306d528ed9Sopenharmony_ci 11316d528ed9Sopenharmony_ci gn meta out/Debug "//base/foo" --data=files --walk=stop 11326d528ed9Sopenharmony_ci Lists collected metaresults for the `files` key in the //base/foo:foo 11336d528ed9Sopenharmony_ci target and all of the dependencies listed in the `stop` key (and so on). 11346d528ed9Sopenharmony_ci 11356d528ed9Sopenharmony_ci gn meta out/Debug "//base/foo" --data=files --rebase="/" 11366d528ed9Sopenharmony_ci Lists collected metaresults for the `files` key in the //base/foo:foo 11376d528ed9Sopenharmony_ci target and all of its dependency tree, rebasing the strings in the `files` 11386d528ed9Sopenharmony_ci key onto the source directory of the target's declaration relative to "/". 11396d528ed9Sopenharmony_ci``` 11406d528ed9Sopenharmony_ci### <a name="cmd_outputs"></a>**gn outputs <out_dir> <list of target or file names...>** 11416d528ed9Sopenharmony_ci 11426d528ed9Sopenharmony_ci``` 11436d528ed9Sopenharmony_ci Lists the output files corresponding to the given target(s) or file name(s). 11446d528ed9Sopenharmony_ci There can be multiple outputs because there can be more than one output 11456d528ed9Sopenharmony_ci generated by a build step, and there can be more than one toolchain matched. 11466d528ed9Sopenharmony_ci You can also list multiple inputs which will generate a union of all the 11476d528ed9Sopenharmony_ci outputs from those inputs. 11486d528ed9Sopenharmony_ci 11496d528ed9Sopenharmony_ci - The input target/file names are relative to the current directory. 11506d528ed9Sopenharmony_ci 11516d528ed9Sopenharmony_ci - The output file names are relative to the root build directory. 11526d528ed9Sopenharmony_ci 11536d528ed9Sopenharmony_ci This command is useful for finding a ninja command that will build only a 11546d528ed9Sopenharmony_ci portion of the build. 11556d528ed9Sopenharmony_ci``` 11566d528ed9Sopenharmony_ci 11576d528ed9Sopenharmony_ci#### **Target outputs** 11586d528ed9Sopenharmony_ci 11596d528ed9Sopenharmony_ci``` 11606d528ed9Sopenharmony_ci If the parameter is a target name that includes a toolchain, it will match 11616d528ed9Sopenharmony_ci only that target in that toolchain. If no toolchain is specified, it will 11626d528ed9Sopenharmony_ci match all targets with that name in any toolchain. 11636d528ed9Sopenharmony_ci 11646d528ed9Sopenharmony_ci The result will be the outputs specified by that target which could be a 11656d528ed9Sopenharmony_ci library, executable, output of an action, a stamp file, etc. 11666d528ed9Sopenharmony_ci``` 11676d528ed9Sopenharmony_ci 11686d528ed9Sopenharmony_ci#### **File outputs** 11696d528ed9Sopenharmony_ci 11706d528ed9Sopenharmony_ci``` 11716d528ed9Sopenharmony_ci If the parameter is a file name it will compute the output for that compile 11726d528ed9Sopenharmony_ci step for all targets in all toolchains that contain that file as a source 11736d528ed9Sopenharmony_ci file. 11746d528ed9Sopenharmony_ci 11756d528ed9Sopenharmony_ci If the source is not compiled (e.g. a header or text file), the command will 11766d528ed9Sopenharmony_ci produce no output. 11776d528ed9Sopenharmony_ci 11786d528ed9Sopenharmony_ci If the source is listed as an "input" to a binary target or action will 11796d528ed9Sopenharmony_ci resolve to that target's outputs. 11806d528ed9Sopenharmony_ci``` 11816d528ed9Sopenharmony_ci 11826d528ed9Sopenharmony_ci#### **Example** 11836d528ed9Sopenharmony_ci 11846d528ed9Sopenharmony_ci``` 11856d528ed9Sopenharmony_ci gn outputs out/debug some/directory:some_target 11866d528ed9Sopenharmony_ci Find the outputs of a given target. 11876d528ed9Sopenharmony_ci 11886d528ed9Sopenharmony_ci gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug 11896d528ed9Sopenharmony_ci Compiles just the given source file in all toolchains it's referenced in. 11906d528ed9Sopenharmony_ci 11916d528ed9Sopenharmony_ci git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64 11926d528ed9Sopenharmony_ci Compiles all files changed in git. 11936d528ed9Sopenharmony_ci``` 11946d528ed9Sopenharmony_ci### <a name="cmd_path"></a>**gn path <out_dir> <target_one> <target_two>** 11956d528ed9Sopenharmony_ci 11966d528ed9Sopenharmony_ci``` 11976d528ed9Sopenharmony_ci Finds paths of dependencies between two targets. Each unique path will be 11986d528ed9Sopenharmony_ci printed in one group, and groups will be separate by newlines. The two 11996d528ed9Sopenharmony_ci targets can appear in either order (paths will be found going in either 12006d528ed9Sopenharmony_ci direction). 12016d528ed9Sopenharmony_ci 12026d528ed9Sopenharmony_ci By default, a single path will be printed. If there is a path with only 12036d528ed9Sopenharmony_ci public dependencies, the shortest public path will be printed. Otherwise, the 12046d528ed9Sopenharmony_ci shortest path using either public or private dependencies will be printed. If 12056d528ed9Sopenharmony_ci --with-data is specified, data deps will also be considered. If there are 12066d528ed9Sopenharmony_ci multiple shortest paths, an arbitrary one will be selected. 12076d528ed9Sopenharmony_ci``` 12086d528ed9Sopenharmony_ci 12096d528ed9Sopenharmony_ci#### **Interesting paths** 12106d528ed9Sopenharmony_ci 12116d528ed9Sopenharmony_ci``` 12126d528ed9Sopenharmony_ci In a large project, there can be 100's of millions of unique paths between a 12136d528ed9Sopenharmony_ci very high level and a common low-level target. To make the output more useful 12146d528ed9Sopenharmony_ci (and terminate in a reasonable time), GN will not revisit sub-paths 12156d528ed9Sopenharmony_ci previously known to lead to the target. 12166d528ed9Sopenharmony_ci``` 12176d528ed9Sopenharmony_ci 12186d528ed9Sopenharmony_ci#### **Options** 12196d528ed9Sopenharmony_ci 12206d528ed9Sopenharmony_ci``` 12216d528ed9Sopenharmony_ci --all 12226d528ed9Sopenharmony_ci Prints all "interesting" paths found rather than just the first one. 12236d528ed9Sopenharmony_ci Public paths will be printed first in order of increasing length, followed 12246d528ed9Sopenharmony_ci by non-public paths in order of increasing length. 12256d528ed9Sopenharmony_ci 12266d528ed9Sopenharmony_ci --public 12276d528ed9Sopenharmony_ci Considers only public paths. Can't be used with --with-data. 12286d528ed9Sopenharmony_ci 12296d528ed9Sopenharmony_ci --with-data 12306d528ed9Sopenharmony_ci Additionally follows data deps. Without this flag, only public and private 12316d528ed9Sopenharmony_ci linked deps will be followed. Can't be used with --public. 12326d528ed9Sopenharmony_ci``` 12336d528ed9Sopenharmony_ci 12346d528ed9Sopenharmony_ci#### **Example** 12356d528ed9Sopenharmony_ci 12366d528ed9Sopenharmony_ci``` 12376d528ed9Sopenharmony_ci gn path out/Default //base //gn 12386d528ed9Sopenharmony_ci``` 12396d528ed9Sopenharmony_ci### <a name="cmd_refs"></a>**gn refs** 12406d528ed9Sopenharmony_ci 12416d528ed9Sopenharmony_ci``` 12426d528ed9Sopenharmony_ci gn refs <out_dir> (<label_pattern>|<label>|<file>|@<response_file>)* [--all] 12436d528ed9Sopenharmony_ci [--default-toolchain] [--as=...] [--testonly=...] [--type=...] 12446d528ed9Sopenharmony_ci 12456d528ed9Sopenharmony_ci Finds reverse dependencies (which targets reference something). The input is 12466d528ed9Sopenharmony_ci a list containing: 12476d528ed9Sopenharmony_ci 12486d528ed9Sopenharmony_ci - Target label: The result will be which targets depend on it. 12496d528ed9Sopenharmony_ci 12506d528ed9Sopenharmony_ci - Config label: The result will be which targets list the given config in 12516d528ed9Sopenharmony_ci its "configs" or "public_configs" list. 12526d528ed9Sopenharmony_ci 12536d528ed9Sopenharmony_ci - Label pattern: The result will be which targets depend on any target 12546d528ed9Sopenharmony_ci matching the given pattern. Patterns will not match configs. These are not 12556d528ed9Sopenharmony_ci general regular expressions, see "gn help label_pattern" for details. 12566d528ed9Sopenharmony_ci 12576d528ed9Sopenharmony_ci - File name: The result will be which targets list the given file in its 12586d528ed9Sopenharmony_ci "inputs", "sources", "public", "data", or "outputs". Any input that does 12596d528ed9Sopenharmony_ci not contain wildcards and does not match a target or a config will be 12606d528ed9Sopenharmony_ci treated as a file. 12616d528ed9Sopenharmony_ci 12626d528ed9Sopenharmony_ci - Response file: If the input starts with an "@", it will be interpreted as 12636d528ed9Sopenharmony_ci a path to a file containing a list of labels or file names, one per line. 12646d528ed9Sopenharmony_ci This allows us to handle long lists of inputs without worrying about 12656d528ed9Sopenharmony_ci command line limits. 12666d528ed9Sopenharmony_ci``` 12676d528ed9Sopenharmony_ci 12686d528ed9Sopenharmony_ci#### **Options** 12696d528ed9Sopenharmony_ci 12706d528ed9Sopenharmony_ci``` 12716d528ed9Sopenharmony_ci --all 12726d528ed9Sopenharmony_ci When used without --tree, will recurse and display all unique 12736d528ed9Sopenharmony_ci dependencies of the given targets. For example, if the input is a target, 12746d528ed9Sopenharmony_ci this will output all targets that depend directly or indirectly on the 12756d528ed9Sopenharmony_ci input. If the input is a file, this will output all targets that depend 12766d528ed9Sopenharmony_ci directly or indirectly on that file. 12776d528ed9Sopenharmony_ci 12786d528ed9Sopenharmony_ci When used with --tree, turns off eliding to show a complete tree. 12796d528ed9Sopenharmony_ci 12806d528ed9Sopenharmony_ci --as=(buildfile|label|output) 12816d528ed9Sopenharmony_ci How to print targets. 12826d528ed9Sopenharmony_ci 12836d528ed9Sopenharmony_ci buildfile 12846d528ed9Sopenharmony_ci Prints the build files where the given target was declared as 12856d528ed9Sopenharmony_ci file names. 12866d528ed9Sopenharmony_ci label (default) 12876d528ed9Sopenharmony_ci Prints the label of the target. 12886d528ed9Sopenharmony_ci output 12896d528ed9Sopenharmony_ci Prints the first output file for the target relative to the 12906d528ed9Sopenharmony_ci root build directory. 12916d528ed9Sopenharmony_ci 12926d528ed9Sopenharmony_ci --default-toolchain 12936d528ed9Sopenharmony_ci Normally wildcard targets are matched in all toolchains. This 12946d528ed9Sopenharmony_ci switch makes wildcard labels with no explicit toolchain reference 12956d528ed9Sopenharmony_ci only match targets in the default toolchain. 12966d528ed9Sopenharmony_ci 12976d528ed9Sopenharmony_ci Non-wildcard inputs with no explicit toolchain specification will 12986d528ed9Sopenharmony_ci always match only a target in the default toolchain if one exists. 12996d528ed9Sopenharmony_ci 13006d528ed9Sopenharmony_ci -q 13016d528ed9Sopenharmony_ci Quiet. If nothing matches, don't print any output. Without this option, if 13026d528ed9Sopenharmony_ci there are no matches there will be an informational message printed which 13036d528ed9Sopenharmony_ci might interfere with scripts processing the output. 13046d528ed9Sopenharmony_ci 13056d528ed9Sopenharmony_ci --testonly=(true|false) 13066d528ed9Sopenharmony_ci Restrict outputs to targets with the testonly flag set 13076d528ed9Sopenharmony_ci accordingly. When unspecified, the target's testonly flags are 13086d528ed9Sopenharmony_ci ignored. 13096d528ed9Sopenharmony_ci 13106d528ed9Sopenharmony_ci --tree 13116d528ed9Sopenharmony_ci Outputs a reverse dependency tree from the given target. Duplicates will 13126d528ed9Sopenharmony_ci be elided. Combine with --all to see a full dependency tree. 13136d528ed9Sopenharmony_ci 13146d528ed9Sopenharmony_ci Tree output can not be used with the filtering or output flags: --as, 13156d528ed9Sopenharmony_ci --type, --testonly. 13166d528ed9Sopenharmony_ci 13176d528ed9Sopenharmony_ci --type=(action|copy|executable|group|loadable_module|shared_library| 13186d528ed9Sopenharmony_ci source_set|static_library) 13196d528ed9Sopenharmony_ci Restrict outputs to targets matching the given type. If 13206d528ed9Sopenharmony_ci unspecified, no filtering will be performed. 13216d528ed9Sopenharmony_ci``` 13226d528ed9Sopenharmony_ci 13236d528ed9Sopenharmony_ci#### **Examples (target input)** 13246d528ed9Sopenharmony_ci 13256d528ed9Sopenharmony_ci``` 13266d528ed9Sopenharmony_ci gn refs out/Debug //gn:gn 13276d528ed9Sopenharmony_ci Find all targets depending on the given exact target name. 13286d528ed9Sopenharmony_ci 13296d528ed9Sopenharmony_ci gn refs out/Debug //base:i18n --as=buildfile | xargs gvim 13306d528ed9Sopenharmony_ci Edit all .gn files containing references to //base:i18n 13316d528ed9Sopenharmony_ci 13326d528ed9Sopenharmony_ci gn refs out/Debug //base --all 13336d528ed9Sopenharmony_ci List all targets depending directly or indirectly on //base:base. 13346d528ed9Sopenharmony_ci 13356d528ed9Sopenharmony_ci gn refs out/Debug "//base/*" 13366d528ed9Sopenharmony_ci List all targets depending directly on any target in //base or 13376d528ed9Sopenharmony_ci its subdirectories. 13386d528ed9Sopenharmony_ci 13396d528ed9Sopenharmony_ci gn refs out/Debug "//base:*" 13406d528ed9Sopenharmony_ci List all targets depending directly on any target in 13416d528ed9Sopenharmony_ci //base/BUILD.gn. 13426d528ed9Sopenharmony_ci 13436d528ed9Sopenharmony_ci gn refs out/Debug //base --tree 13446d528ed9Sopenharmony_ci Print a reverse dependency tree of //base:base 13456d528ed9Sopenharmony_ci``` 13466d528ed9Sopenharmony_ci 13476d528ed9Sopenharmony_ci#### **Examples (file input)** 13486d528ed9Sopenharmony_ci 13496d528ed9Sopenharmony_ci``` 13506d528ed9Sopenharmony_ci gn refs out/Debug //base/macros.h 13516d528ed9Sopenharmony_ci Print target(s) listing //base/macros.h as a source. 13526d528ed9Sopenharmony_ci 13536d528ed9Sopenharmony_ci gn refs out/Debug //base/macros.h --tree 13546d528ed9Sopenharmony_ci Display a reverse dependency tree to get to the given file. This 13556d528ed9Sopenharmony_ci will show how dependencies will reference that file. 13566d528ed9Sopenharmony_ci 13576d528ed9Sopenharmony_ci gn refs out/Debug //base/macros.h //base/at_exit.h --all 13586d528ed9Sopenharmony_ci Display all unique targets with some dependency path to a target 13596d528ed9Sopenharmony_ci containing either of the given files as a source. 13606d528ed9Sopenharmony_ci 13616d528ed9Sopenharmony_ci gn refs out/Debug //base/macros.h --testonly=true --type=executable 13626d528ed9Sopenharmony_ci --all --as=output 13636d528ed9Sopenharmony_ci Display the executable file names of all test executables 13646d528ed9Sopenharmony_ci potentially affected by a change to the given file. 13656d528ed9Sopenharmony_ci``` 13666d528ed9Sopenharmony_ci## <a name="targets"></a>Target declarations 13676d528ed9Sopenharmony_ci 13686d528ed9Sopenharmony_ci### <a name="func_action"></a>**action**: Declare a target that runs a script a single time. 13696d528ed9Sopenharmony_ci 13706d528ed9Sopenharmony_ci``` 13716d528ed9Sopenharmony_ci This target type allows you to run a script a single time to produce one or 13726d528ed9Sopenharmony_ci more output files. If you want to run a script once for each of a set of 13736d528ed9Sopenharmony_ci input files, see "gn help action_foreach". 13746d528ed9Sopenharmony_ci``` 13756d528ed9Sopenharmony_ci 13766d528ed9Sopenharmony_ci#### **Inputs** 13776d528ed9Sopenharmony_ci 13786d528ed9Sopenharmony_ci``` 13796d528ed9Sopenharmony_ci In an action the "sources" and "inputs" are treated the same: they're both 13806d528ed9Sopenharmony_ci input dependencies on script execution with no special handling. If you want 13816d528ed9Sopenharmony_ci to pass the sources to your script, you must do so explicitly by including 13826d528ed9Sopenharmony_ci them in the "args". Note also that this means there is no special handling of 13836d528ed9Sopenharmony_ci paths since GN doesn't know which of the args are paths and not. You will 13846d528ed9Sopenharmony_ci want to use rebase_path() to convert paths to be relative to the 13856d528ed9Sopenharmony_ci root_build_dir. 13866d528ed9Sopenharmony_ci 13876d528ed9Sopenharmony_ci You can dynamically write input dependencies (for incremental rebuilds if an 13886d528ed9Sopenharmony_ci input file changes) by writing a depfile when the script is run (see "gn help 13896d528ed9Sopenharmony_ci depfile"). This is more flexible than "inputs". 13906d528ed9Sopenharmony_ci 13916d528ed9Sopenharmony_ci If the command line length is very long, you can use response files to pass 13926d528ed9Sopenharmony_ci args to your script. See "gn help response_file_contents". 13936d528ed9Sopenharmony_ci 13946d528ed9Sopenharmony_ci It is recommended you put inputs to your script in the "sources" variable, 13956d528ed9Sopenharmony_ci and stuff like other Python files required to run your script in the "inputs" 13966d528ed9Sopenharmony_ci variable. 13976d528ed9Sopenharmony_ci 13986d528ed9Sopenharmony_ci Actions can take the configs and public_configs lists, as well as any of the 13996d528ed9Sopenharmony_ci configs variables (defines, include_dirs, etc.) set directly on the target. 14006d528ed9Sopenharmony_ci These behave exactly as they would on a binary target and can be accessed 14016d528ed9Sopenharmony_ci using substitution patterns in the script args (see "gn help args") to 14026d528ed9Sopenharmony_ci implement custom compiler-like tools. 14036d528ed9Sopenharmony_ci 14046d528ed9Sopenharmony_ci The "deps" and "public_deps" for an action will always be 14056d528ed9Sopenharmony_ci completed before any part of the action is run so it can depend on 14066d528ed9Sopenharmony_ci the output of previous steps. The "data_deps" will be built if the 14076d528ed9Sopenharmony_ci action is built, but may not have completed before all steps of the 14086d528ed9Sopenharmony_ci action are started. This can give additional parallelism in the build 14096d528ed9Sopenharmony_ci for runtime-only dependencies. 14106d528ed9Sopenharmony_ci``` 14116d528ed9Sopenharmony_ci 14126d528ed9Sopenharmony_ci#### **Outputs** 14136d528ed9Sopenharmony_ci 14146d528ed9Sopenharmony_ci``` 14156d528ed9Sopenharmony_ci You should specify files created by your script by specifying them in the 14166d528ed9Sopenharmony_ci "outputs". 14176d528ed9Sopenharmony_ci 14186d528ed9Sopenharmony_ci The script will be executed with the given arguments with the current 14196d528ed9Sopenharmony_ci directory being that of the root build directory. If you pass files 14206d528ed9Sopenharmony_ci to your script, see "gn help rebase_path" for how to convert 14216d528ed9Sopenharmony_ci file names to be relative to the build directory (file names in the 14226d528ed9Sopenharmony_ci sources, outputs, and inputs will be all treated as relative to the 14236d528ed9Sopenharmony_ci current build file and converted as needed automatically). 14246d528ed9Sopenharmony_ci 14256d528ed9Sopenharmony_ci GN sets Ninja's flag 'restat = 1` for all action commands. This means 14266d528ed9Sopenharmony_ci that Ninja will check the timestamp of the output after the action 14276d528ed9Sopenharmony_ci completes. If output timestamp is unchanged, the step will be treated 14286d528ed9Sopenharmony_ci as if it never needed to be rebuilt, potentially eliminating some 14296d528ed9Sopenharmony_ci downstream steps for incremental builds. Scripts can improve build 14306d528ed9Sopenharmony_ci performance by taking care not to change the timstamp of the output 14316d528ed9Sopenharmony_ci file(s) if the contents have not changed. 14326d528ed9Sopenharmony_ci``` 14336d528ed9Sopenharmony_ci 14346d528ed9Sopenharmony_ci#### **File name handling** 14356d528ed9Sopenharmony_ci 14366d528ed9Sopenharmony_ci``` 14376d528ed9Sopenharmony_ci All output files must be inside the output directory of the build. 14386d528ed9Sopenharmony_ci You would generally use |$target_out_dir| or |$target_gen_dir| to 14396d528ed9Sopenharmony_ci reference the output or generated intermediate file directories, 14406d528ed9Sopenharmony_ci respectively. 14416d528ed9Sopenharmony_ci``` 14426d528ed9Sopenharmony_ci 14436d528ed9Sopenharmony_ci#### **Variables** 14446d528ed9Sopenharmony_ci 14456d528ed9Sopenharmony_ci``` 14466d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 14476d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 14486d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 14496d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 14506d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 14516d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 14526d528ed9Sopenharmony_ci write_runtime_deps 14536d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 14546d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 14556d528ed9Sopenharmony_ci visibility 14566d528ed9Sopenharmony_ci Action variables: args, bridge_header, configs, data, depfile, 14576d528ed9Sopenharmony_ci framework_dirs, inputs, mnemonic, module_deps, 14586d528ed9Sopenharmony_ci module_name, outputs*, pool, response_file_contents, 14596d528ed9Sopenharmony_ci script*, sources 14606d528ed9Sopenharmony_ci * = required 14616d528ed9Sopenharmony_ci``` 14626d528ed9Sopenharmony_ci 14636d528ed9Sopenharmony_ci#### **Example** 14646d528ed9Sopenharmony_ci 14656d528ed9Sopenharmony_ci``` 14666d528ed9Sopenharmony_ci action("run_this_guy_once") { 14676d528ed9Sopenharmony_ci script = "doprocessing.py" 14686d528ed9Sopenharmony_ci sources = [ "my_configuration.txt" ] 14696d528ed9Sopenharmony_ci outputs = [ "$target_gen_dir/insightful_output.txt" ] 14706d528ed9Sopenharmony_ci 14716d528ed9Sopenharmony_ci # Our script imports this Python file so we want to rebuild if it changes. 14726d528ed9Sopenharmony_ci inputs = [ "helper_library.py" ] 14736d528ed9Sopenharmony_ci 14746d528ed9Sopenharmony_ci # Note that we have to manually pass the sources to our script if the 14756d528ed9Sopenharmony_ci # script needs them as inputs. 14766d528ed9Sopenharmony_ci args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] + 14776d528ed9Sopenharmony_ci rebase_path(sources, root_build_dir) 14786d528ed9Sopenharmony_ci } 14796d528ed9Sopenharmony_ci``` 14806d528ed9Sopenharmony_ci### <a name="func_action_foreach"></a>**action_foreach**: Declare a target that runs a script over a set of files. 14816d528ed9Sopenharmony_ci 14826d528ed9Sopenharmony_ci``` 14836d528ed9Sopenharmony_ci This target type allows you to run a script once-per-file over a set of 14846d528ed9Sopenharmony_ci sources. If you want to run a script once that takes many files as input, see 14856d528ed9Sopenharmony_ci "gn help action". 14866d528ed9Sopenharmony_ci``` 14876d528ed9Sopenharmony_ci 14886d528ed9Sopenharmony_ci#### **Inputs** 14896d528ed9Sopenharmony_ci 14906d528ed9Sopenharmony_ci``` 14916d528ed9Sopenharmony_ci The script will be run once per file in the "sources" variable. The "outputs" 14926d528ed9Sopenharmony_ci variable should specify one or more files with a source expansion pattern in 14936d528ed9Sopenharmony_ci it (see "gn help source_expansion"). The output file(s) for each script 14946d528ed9Sopenharmony_ci invocation should be unique. Normally you use "{{source_name_part}}" in each 14956d528ed9Sopenharmony_ci output file. 14966d528ed9Sopenharmony_ci 14976d528ed9Sopenharmony_ci If your script takes additional data as input, such as a shared configuration 14986d528ed9Sopenharmony_ci file or a Python module it uses, those files should be listed in the "inputs" 14996d528ed9Sopenharmony_ci variable. These files are treated as dependencies of each script invocation. 15006d528ed9Sopenharmony_ci 15016d528ed9Sopenharmony_ci If the command line length is very long, you can use response files to pass 15026d528ed9Sopenharmony_ci args to your script. See "gn help response_file_contents". 15036d528ed9Sopenharmony_ci 15046d528ed9Sopenharmony_ci You can dynamically write input dependencies (for incremental rebuilds if an 15056d528ed9Sopenharmony_ci input file changes) by writing a depfile when the script is run (see "gn help 15066d528ed9Sopenharmony_ci depfile"). This is more flexible than "inputs". 15076d528ed9Sopenharmony_ci 15086d528ed9Sopenharmony_ci The "deps" and "public_deps" for an action will always be 15096d528ed9Sopenharmony_ci completed before any part of the action is run so it can depend on 15106d528ed9Sopenharmony_ci the output of previous steps. The "data_deps" will be built if the 15116d528ed9Sopenharmony_ci action is built, but may not have completed before all steps of the 15126d528ed9Sopenharmony_ci action are started. This can give additional parallelism in the build 15136d528ed9Sopenharmony_ci for runtime-only dependencies. 15146d528ed9Sopenharmony_ci``` 15156d528ed9Sopenharmony_ci 15166d528ed9Sopenharmony_ci#### **Outputs** 15176d528ed9Sopenharmony_ci 15186d528ed9Sopenharmony_ci``` 15196d528ed9Sopenharmony_ci The script will be executed with the given arguments with the current 15206d528ed9Sopenharmony_ci directory being that of the root build directory. If you pass files 15216d528ed9Sopenharmony_ci to your script, see "gn help rebase_path" for how to convert 15226d528ed9Sopenharmony_ci file names to be relative to the build directory (file names in the 15236d528ed9Sopenharmony_ci sources, outputs, and inputs will be all treated as relative to the 15246d528ed9Sopenharmony_ci current build file and converted as needed automatically). 15256d528ed9Sopenharmony_ci 15266d528ed9Sopenharmony_ci GN sets Ninja's flag 'restat = 1` for all action commands. This means 15276d528ed9Sopenharmony_ci that Ninja will check the timestamp of the output after the action 15286d528ed9Sopenharmony_ci completes. If output timestamp is unchanged, the step will be treated 15296d528ed9Sopenharmony_ci as if it never needed to be rebuilt, potentially eliminating some 15306d528ed9Sopenharmony_ci downstream steps for incremental builds. Scripts can improve build 15316d528ed9Sopenharmony_ci performance by taking care not to change the timstamp of the output 15326d528ed9Sopenharmony_ci file(s) if the contents have not changed. 15336d528ed9Sopenharmony_ci``` 15346d528ed9Sopenharmony_ci 15356d528ed9Sopenharmony_ci#### **File name handling** 15366d528ed9Sopenharmony_ci 15376d528ed9Sopenharmony_ci``` 15386d528ed9Sopenharmony_ci All output files must be inside the output directory of the build. 15396d528ed9Sopenharmony_ci You would generally use |$target_out_dir| or |$target_gen_dir| to 15406d528ed9Sopenharmony_ci reference the output or generated intermediate file directories, 15416d528ed9Sopenharmony_ci respectively. 15426d528ed9Sopenharmony_ci``` 15436d528ed9Sopenharmony_ci 15446d528ed9Sopenharmony_ci#### **Variables** 15456d528ed9Sopenharmony_ci 15466d528ed9Sopenharmony_ci``` 15476d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 15486d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 15496d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 15506d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 15516d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 15526d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 15536d528ed9Sopenharmony_ci write_runtime_deps 15546d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 15556d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 15566d528ed9Sopenharmony_ci visibility 15576d528ed9Sopenharmony_ci Action variables: args, bridge_header, configs, data, depfile, 15586d528ed9Sopenharmony_ci framework_dirs, inputs, mnemonic, module_deps, 15596d528ed9Sopenharmony_ci module_name, outputs*, pool, response_file_contents, 15606d528ed9Sopenharmony_ci script*, sources 15616d528ed9Sopenharmony_ci * = required 15626d528ed9Sopenharmony_ci``` 15636d528ed9Sopenharmony_ci 15646d528ed9Sopenharmony_ci#### **Example** 15656d528ed9Sopenharmony_ci 15666d528ed9Sopenharmony_ci``` 15676d528ed9Sopenharmony_ci # Runs the script over each IDL file. The IDL script will generate both a .cc 15686d528ed9Sopenharmony_ci # and a .h file for each input. 15696d528ed9Sopenharmony_ci action_foreach("my_idl") { 15706d528ed9Sopenharmony_ci script = "idl_processor.py" 15716d528ed9Sopenharmony_ci sources = [ "foo.idl", "bar.idl" ] 15726d528ed9Sopenharmony_ci 15736d528ed9Sopenharmony_ci # Causes ninja to output "IDL <label>" rather than the default 15746d528ed9Sopenharmony_ci # "ACTION <label>" when building this action. 15756d528ed9Sopenharmony_ci mnemonic = "IDL" 15766d528ed9Sopenharmony_ci 15776d528ed9Sopenharmony_ci # Our script reads this file each time, so we need to list it as a 15786d528ed9Sopenharmony_ci # dependency so we can rebuild if it changes. 15796d528ed9Sopenharmony_ci inputs = [ "my_configuration.txt" ] 15806d528ed9Sopenharmony_ci 15816d528ed9Sopenharmony_ci # Transformation from source file name to output file names. 15826d528ed9Sopenharmony_ci outputs = [ "$target_gen_dir/{{source_name_part}}.h", 15836d528ed9Sopenharmony_ci "$target_gen_dir/{{source_name_part}}.cc" ] 15846d528ed9Sopenharmony_ci 15856d528ed9Sopenharmony_ci # Note that since "args" is opaque to GN, if you specify paths here, you 15866d528ed9Sopenharmony_ci # will need to convert it to be relative to the build directory using 15876d528ed9Sopenharmony_ci # rebase_path(). 15886d528ed9Sopenharmony_ci args = [ 15896d528ed9Sopenharmony_ci "{{source}}", 15906d528ed9Sopenharmony_ci "-o", 15916d528ed9Sopenharmony_ci rebase_path(target_gen_dir, root_build_dir) + 15926d528ed9Sopenharmony_ci "/{{source_name_part}}.h" ] 15936d528ed9Sopenharmony_ci } 15946d528ed9Sopenharmony_ci``` 15956d528ed9Sopenharmony_ci### <a name="func_bundle_data"></a>**bundle_data**: [iOS/macOS] Declare a target without output. 15966d528ed9Sopenharmony_ci 15976d528ed9Sopenharmony_ci``` 15986d528ed9Sopenharmony_ci This target type allows one to declare data that is required at runtime. It is 15996d528ed9Sopenharmony_ci used to inform "create_bundle" targets of the files to copy into generated 16006d528ed9Sopenharmony_ci bundle, see "gn help create_bundle" for help. 16016d528ed9Sopenharmony_ci 16026d528ed9Sopenharmony_ci The target must define a list of files as "sources" and a single "outputs". 16036d528ed9Sopenharmony_ci If there are multiple files, source expansions must be used to express the 16046d528ed9Sopenharmony_ci output. The output must reference a file inside of {{bundle_root_dir}}. 16056d528ed9Sopenharmony_ci 16066d528ed9Sopenharmony_ci This target can be used on all platforms though it is designed only to 16076d528ed9Sopenharmony_ci generate iOS/macOS bundle. In cross-platform projects, it is advised to put it 16086d528ed9Sopenharmony_ci behind iOS/macOS conditionals. 16096d528ed9Sopenharmony_ci 16106d528ed9Sopenharmony_ci If any source files in a bundle_data target match `*/*.xcassets/*` then they 16116d528ed9Sopenharmony_ci will be considered part of an assets catalog, and instead of being copied to 16126d528ed9Sopenharmony_ci the final bundle the assets catalog itself will be added to the inputs of the 16136d528ed9Sopenharmony_ci assets catalog compilation step. See "compile_xcassets" tool. 16146d528ed9Sopenharmony_ci 16156d528ed9Sopenharmony_ci See "gn help create_bundle" for more information. 16166d528ed9Sopenharmony_ci``` 16176d528ed9Sopenharmony_ci 16186d528ed9Sopenharmony_ci#### **Variables** 16196d528ed9Sopenharmony_ci 16206d528ed9Sopenharmony_ci``` 16216d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 16226d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 16236d528ed9Sopenharmony_ci write_runtime_deps 16246d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 16256d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 16266d528ed9Sopenharmony_ci visibility 16276d528ed9Sopenharmony_ci Bundle-specific: sources*, outputs* 16286d528ed9Sopenharmony_ci * = required 16296d528ed9Sopenharmony_ci``` 16306d528ed9Sopenharmony_ci 16316d528ed9Sopenharmony_ci#### **Examples** 16326d528ed9Sopenharmony_ci 16336d528ed9Sopenharmony_ci``` 16346d528ed9Sopenharmony_ci bundle_data("icudata") { 16356d528ed9Sopenharmony_ci sources = [ "sources/data/in/icudtl.dat" ] 16366d528ed9Sopenharmony_ci outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ] 16376d528ed9Sopenharmony_ci } 16386d528ed9Sopenharmony_ci 16396d528ed9Sopenharmony_ci bundle_data("base_unittests_bundle_data]") { 16406d528ed9Sopenharmony_ci sources = [ "test/data" ] 16416d528ed9Sopenharmony_ci outputs = [ 16426d528ed9Sopenharmony_ci "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" + 16436d528ed9Sopenharmony_ci "{{source_file_part}}" 16446d528ed9Sopenharmony_ci ] 16456d528ed9Sopenharmony_ci } 16466d528ed9Sopenharmony_ci 16476d528ed9Sopenharmony_ci bundle_data("material_typography_bundle_data") { 16486d528ed9Sopenharmony_ci sources = [ 16496d528ed9Sopenharmony_ci "src/MaterialTypography.bundle/Roboto-Bold.ttf", 16506d528ed9Sopenharmony_ci "src/MaterialTypography.bundle/Roboto-Italic.ttf", 16516d528ed9Sopenharmony_ci "src/MaterialTypography.bundle/Roboto-Regular.ttf", 16526d528ed9Sopenharmony_ci "src/MaterialTypography.bundle/Roboto-Thin.ttf", 16536d528ed9Sopenharmony_ci ] 16546d528ed9Sopenharmony_ci outputs = [ 16556d528ed9Sopenharmony_ci "{{bundle_resources_dir}}/MaterialTypography.bundle/" 16566d528ed9Sopenharmony_ci "{{source_file_part}}" 16576d528ed9Sopenharmony_ci ] 16586d528ed9Sopenharmony_ci } 16596d528ed9Sopenharmony_ci``` 16606d528ed9Sopenharmony_ci### <a name="func_copy"></a>**copy**: Declare a target that copies files. 16616d528ed9Sopenharmony_ci 16626d528ed9Sopenharmony_ci#### **File name handling** 16636d528ed9Sopenharmony_ci 16646d528ed9Sopenharmony_ci``` 16656d528ed9Sopenharmony_ci All output files must be inside the output directory of the build. You would 16666d528ed9Sopenharmony_ci generally use |$target_out_dir| or |$target_gen_dir| to reference the output 16676d528ed9Sopenharmony_ci or generated intermediate file directories, respectively. 16686d528ed9Sopenharmony_ci 16696d528ed9Sopenharmony_ci Both "sources" and "outputs" must be specified. Sources can include as many 16706d528ed9Sopenharmony_ci files as you want, but there can only be one item in the outputs list (plural 16716d528ed9Sopenharmony_ci is used for the name for consistency with other target types). 16726d528ed9Sopenharmony_ci 16736d528ed9Sopenharmony_ci If there is more than one source file, your output name should specify a 16746d528ed9Sopenharmony_ci mapping from each source file to an output file name using source expansion 16756d528ed9Sopenharmony_ci (see "gn help source_expansion"). The placeholders will look like 16766d528ed9Sopenharmony_ci "{{source_name_part}}", for example. 16776d528ed9Sopenharmony_ci 16786d528ed9Sopenharmony_ci If you want to copy the output of a previous build step, the target that 16796d528ed9Sopenharmony_ci generates the file to copy must be reachable from the deps or public_deps of 16806d528ed9Sopenharmony_ci the copy target. 16816d528ed9Sopenharmony_ci``` 16826d528ed9Sopenharmony_ci 16836d528ed9Sopenharmony_ci#### **Variables** 16846d528ed9Sopenharmony_ci 16856d528ed9Sopenharmony_ci``` 16866d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 16876d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 16886d528ed9Sopenharmony_ci write_runtime_deps 16896d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 16906d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 16916d528ed9Sopenharmony_ci visibility 16926d528ed9Sopenharmony_ci Copy variables: sources*, outputs* 16936d528ed9Sopenharmony_ci * = required 16946d528ed9Sopenharmony_ci``` 16956d528ed9Sopenharmony_ci 16966d528ed9Sopenharmony_ci#### **Examples** 16976d528ed9Sopenharmony_ci 16986d528ed9Sopenharmony_ci``` 16996d528ed9Sopenharmony_ci # Write a rule that copies a checked-in DLL to the output directory. 17006d528ed9Sopenharmony_ci copy("mydll") { 17016d528ed9Sopenharmony_ci sources = [ "mydll.dll" ] 17026d528ed9Sopenharmony_ci outputs = [ "$target_out_dir/mydll.dll" ] 17036d528ed9Sopenharmony_ci } 17046d528ed9Sopenharmony_ci 17056d528ed9Sopenharmony_ci # Write a rule to copy several files to the target generated files directory. 17066d528ed9Sopenharmony_ci copy("myfiles") { 17076d528ed9Sopenharmony_ci sources = [ "data1.dat", "data2.dat", "data3.dat" ] 17086d528ed9Sopenharmony_ci 17096d528ed9Sopenharmony_ci # Use source expansion to generate output files with the corresponding file 17106d528ed9Sopenharmony_ci # names in the gen dir. This will just copy each file. 17116d528ed9Sopenharmony_ci outputs = [ "$target_gen_dir/{{source_file_part}}" ] 17126d528ed9Sopenharmony_ci } 17136d528ed9Sopenharmony_ci 17146d528ed9Sopenharmony_ci # Copy the output of a generated executable. 17156d528ed9Sopenharmony_ci copy("package_melon") { 17166d528ed9Sopenharmony_ci # This example uses get_label_info() to compute the output directory of the 17176d528ed9Sopenharmony_ci # dependency. This allows the copy rule to work regardless of the toolchain. 17186d528ed9Sopenharmony_ci # 17196d528ed9Sopenharmony_ci # In some cases (particularly actions defined previously in the same file) 17206d528ed9Sopenharmony_ci # you can use get_target_outputs() to get the input file which can eliminate 17216d528ed9Sopenharmony_ci # the assumptions about the output file name of the dependency. 17226d528ed9Sopenharmony_ci 17236d528ed9Sopenharmony_ci input_dir = get_label_info("//src/tools/melon", "root_out_dir"); 17246d528ed9Sopenharmony_ci sources = [ "$input_dir/melon" ] 17256d528ed9Sopenharmony_ci 17266d528ed9Sopenharmony_ci outputs = [ "$target_gen_dir/{{source_file_part}}" ] 17276d528ed9Sopenharmony_ci 17286d528ed9Sopenharmony_ci # Depend on the target to build the file before copying. 17296d528ed9Sopenharmony_ci deps = [ "//src/tools/melon" ] 17306d528ed9Sopenharmony_ci } 17316d528ed9Sopenharmony_ci``` 17326d528ed9Sopenharmony_ci### <a name="func_create_bundle"></a>**create_bundle**: [ios/macOS] Build an iOS or macOS bundle. 17336d528ed9Sopenharmony_ci 17346d528ed9Sopenharmony_ci``` 17356d528ed9Sopenharmony_ci This target generates an iOS or macOS bundle (which is a directory with a 17366d528ed9Sopenharmony_ci well-know structure). This target does not define any sources, instead they 17376d528ed9Sopenharmony_ci are computed from all "bundle_data" target this one depends on transitively 17386d528ed9Sopenharmony_ci (the recursion stops at "create_bundle" targets). 17396d528ed9Sopenharmony_ci 17406d528ed9Sopenharmony_ci The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in 17416d528ed9Sopenharmony_ci "bundle_data" outputs. The properties are optional but must be defined if any 17426d528ed9Sopenharmony_ci of the "bundle_data" target use them. 17436d528ed9Sopenharmony_ci 17446d528ed9Sopenharmony_ci This target can be used on all platforms though it is designed only to 17456d528ed9Sopenharmony_ci generate iOS or macOS bundle. In cross-platform projects, it is advised to put 17466d528ed9Sopenharmony_ci it behind iOS/macOS conditionals. 17476d528ed9Sopenharmony_ci 17486d528ed9Sopenharmony_ci If a create_bundle is specified as a data_deps for another target, the bundle 17496d528ed9Sopenharmony_ci is considered a leaf, and its public and private dependencies will not 17506d528ed9Sopenharmony_ci contribute to any data or data_deps. Required runtime dependencies should be 17516d528ed9Sopenharmony_ci placed in the bundle. A create_bundle can declare its own explicit data and 17526d528ed9Sopenharmony_ci data_deps, however. 17536d528ed9Sopenharmony_ci``` 17546d528ed9Sopenharmony_ci 17556d528ed9Sopenharmony_ci#### **Post-processing** 17566d528ed9Sopenharmony_ci 17576d528ed9Sopenharmony_ci``` 17586d528ed9Sopenharmony_ci Some bundle needs to be post-processed as part of the build (e.g. on iOS all 17596d528ed9Sopenharmony_ci application needs to be code signed to run on a device). The post processing 17606d528ed9Sopenharmony_ci step can be configured via the post_processing_script variable. 17616d528ed9Sopenharmony_ci 17626d528ed9Sopenharmony_ci If set, `post_processing_script` is the path of a script that invoked after 17636d528ed9Sopenharmony_ci all files have been moved into the bundle. The script must not change any file 17646d528ed9Sopenharmony_ci in the bundle, but may add new files. 17656d528ed9Sopenharmony_ci 17666d528ed9Sopenharmony_ci If `post_processing_script` is defined, then `post_processing_outputs` must 17676d528ed9Sopenharmony_ci be defined and non-empty to inform when the script needs to be re-run. The 17686d528ed9Sopenharmony_ci `post_processing_args` will be passed as is to the script (so path have to be 17696d528ed9Sopenharmony_ci rebased) and additional inputs may be listed via `post_processing_sources`. 17706d528ed9Sopenharmony_ci``` 17716d528ed9Sopenharmony_ci 17726d528ed9Sopenharmony_ci#### **Migration** 17736d528ed9Sopenharmony_ci 17746d528ed9Sopenharmony_ci``` 17756d528ed9Sopenharmony_ci The post-processing step used to be limited to code-signing. The properties 17766d528ed9Sopenharmony_ci used to be named `code_signing_$name` instead of `post_processing_$name`. The 17776d528ed9Sopenharmony_ci old names are still accepted as alias to facilitate migration but a warning 17786d528ed9Sopenharmony_ci will be emitted and the alias eventually be removed. 17796d528ed9Sopenharmony_ci``` 17806d528ed9Sopenharmony_ci 17816d528ed9Sopenharmony_ci#### **Variables** 17826d528ed9Sopenharmony_ci 17836d528ed9Sopenharmony_ci``` 17846d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 17856d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 17866d528ed9Sopenharmony_ci write_runtime_deps 17876d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 17886d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 17896d528ed9Sopenharmony_ci visibility 17906d528ed9Sopenharmony_ci Bundle vars: bundle_root_dir, bundle_contents_dir, bundle_resources_dir, 17916d528ed9Sopenharmony_ci bundle_executable_dir, bundle_deps_filter, product_type, 17926d528ed9Sopenharmony_ci post_processing_args, post_processing_script, 17936d528ed9Sopenharmony_ci post_processing_sources, post_processing_outputs, 17946d528ed9Sopenharmony_ci xcode_extra_attributes, xcode_test_application_name, 17956d528ed9Sopenharmony_ci partial_info_plist 17966d528ed9Sopenharmony_ci``` 17976d528ed9Sopenharmony_ci 17986d528ed9Sopenharmony_ci#### **Example** 17996d528ed9Sopenharmony_ci 18006d528ed9Sopenharmony_ci``` 18016d528ed9Sopenharmony_ci # Defines a template to create an application. On most platform, this is just 18026d528ed9Sopenharmony_ci # an alias for an "executable" target, but on iOS/macOS, it builds an 18036d528ed9Sopenharmony_ci # application bundle. 18046d528ed9Sopenharmony_ci template("app") { 18056d528ed9Sopenharmony_ci if (!is_ios && !is_mac) { 18066d528ed9Sopenharmony_ci executable(target_name) { 18076d528ed9Sopenharmony_ci forward_variables_from(invoker, "*") 18086d528ed9Sopenharmony_ci } 18096d528ed9Sopenharmony_ci } else { 18106d528ed9Sopenharmony_ci app_name = target_name 18116d528ed9Sopenharmony_ci gen_path = target_gen_dir 18126d528ed9Sopenharmony_ci 18136d528ed9Sopenharmony_ci action("${app_name}_generate_info_plist") { 18146d528ed9Sopenharmony_ci script = [ "//build/ios/ios_gen_plist.py" ] 18156d528ed9Sopenharmony_ci sources = [ "templates/Info.plist" ] 18166d528ed9Sopenharmony_ci outputs = [ "$gen_path/Info.plist" ] 18176d528ed9Sopenharmony_ci args = rebase_path(sources, root_build_dir) + 18186d528ed9Sopenharmony_ci rebase_path(outputs, root_build_dir) 18196d528ed9Sopenharmony_ci } 18206d528ed9Sopenharmony_ci 18216d528ed9Sopenharmony_ci bundle_data("${app_name}_bundle_info_plist") { 18226d528ed9Sopenharmony_ci public_deps = [ ":${app_name}_generate_info_plist" ] 18236d528ed9Sopenharmony_ci sources = [ "$gen_path/Info.plist" ] 18246d528ed9Sopenharmony_ci outputs = [ "{{bundle_contents_dir}}/Info.plist" ] 18256d528ed9Sopenharmony_ci } 18266d528ed9Sopenharmony_ci 18276d528ed9Sopenharmony_ci executable("${app_name}_generate_executable") { 18286d528ed9Sopenharmony_ci forward_variables_from(invoker, "*", [ 18296d528ed9Sopenharmony_ci "output_name", 18306d528ed9Sopenharmony_ci "visibility", 18316d528ed9Sopenharmony_ci ]) 18326d528ed9Sopenharmony_ci output_name = 18336d528ed9Sopenharmony_ci rebase_path("$gen_path/$app_name", root_build_dir) 18346d528ed9Sopenharmony_ci } 18356d528ed9Sopenharmony_ci 18366d528ed9Sopenharmony_ci code_signing = 18376d528ed9Sopenharmony_ci defined(invoker.code_signing) && invoker.code_signing 18386d528ed9Sopenharmony_ci 18396d528ed9Sopenharmony_ci if (!is_ios || !code_signing) { 18406d528ed9Sopenharmony_ci bundle_data("${app_name}_bundle_executable") { 18416d528ed9Sopenharmony_ci public_deps = [ ":${app_name}_generate_executable" ] 18426d528ed9Sopenharmony_ci sources = [ "$gen_path/$app_name" ] 18436d528ed9Sopenharmony_ci outputs = [ "{{bundle_executable_dir}}/$app_name" ] 18446d528ed9Sopenharmony_ci } 18456d528ed9Sopenharmony_ci } 18466d528ed9Sopenharmony_ci 18476d528ed9Sopenharmony_ci create_bundle("$app_name.app") { 18486d528ed9Sopenharmony_ci product_type = "com.apple.product-type.application" 18496d528ed9Sopenharmony_ci 18506d528ed9Sopenharmony_ci if (is_ios) { 18516d528ed9Sopenharmony_ci bundle_root_dir = "$root_build_dir/$target_name" 18526d528ed9Sopenharmony_ci bundle_contents_dir = bundle_root_dir 18536d528ed9Sopenharmony_ci bundle_resources_dir = bundle_contents_dir 18546d528ed9Sopenharmony_ci bundle_executable_dir = bundle_contents_dir 18556d528ed9Sopenharmony_ci 18566d528ed9Sopenharmony_ci xcode_extra_attributes = { 18576d528ed9Sopenharmony_ci ONLY_ACTIVE_ARCH = "YES" 18586d528ed9Sopenharmony_ci DEBUG_INFORMATION_FORMAT = "dwarf" 18596d528ed9Sopenharmony_ci } 18606d528ed9Sopenharmony_ci } else { 18616d528ed9Sopenharmony_ci bundle_root_dir = "$root_build_dir/$target_name" 18626d528ed9Sopenharmony_ci bundle_contents_dir = "$bundle_root_dir/Contents" 18636d528ed9Sopenharmony_ci bundle_resources_dir = "$bundle_contents_dir/Resources" 18646d528ed9Sopenharmony_ci bundle_executable_dir = "$bundle_contents_dir/MacOS" 18656d528ed9Sopenharmony_ci } 18666d528ed9Sopenharmony_ci deps = [ ":${app_name}_bundle_info_plist" ] 18676d528ed9Sopenharmony_ci if (is_ios && code_signing) { 18686d528ed9Sopenharmony_ci deps += [ ":${app_name}_generate_executable" ] 18696d528ed9Sopenharmony_ci post_processing_script = "//build/config/ios/codesign.py" 18706d528ed9Sopenharmony_ci post_processing_sources = [ 18716d528ed9Sopenharmony_ci invoker.entitlements_path, 18726d528ed9Sopenharmony_ci "$target_gen_dir/$app_name", 18736d528ed9Sopenharmony_ci ] 18746d528ed9Sopenharmony_ci post_processing_outputs = [ 18756d528ed9Sopenharmony_ci "$bundle_root_dir/$app_name", 18766d528ed9Sopenharmony_ci "$bundle_root_dir/_CodeSignature/CodeResources", 18776d528ed9Sopenharmony_ci "$bundle_root_dir/embedded.mobileprovision", 18786d528ed9Sopenharmony_ci "$target_gen_dir/$app_name.xcent", 18796d528ed9Sopenharmony_ci ] 18806d528ed9Sopenharmony_ci post_processing_args = [ 18816d528ed9Sopenharmony_ci "-i=" + ios_post_processing_identity, 18826d528ed9Sopenharmony_ci "-b=" + rebase_path( 18836d528ed9Sopenharmony_ci "$target_gen_dir/$app_name", root_build_dir), 18846d528ed9Sopenharmony_ci "-e=" + rebase_path( 18856d528ed9Sopenharmony_ci invoker.entitlements_path, root_build_dir), 18866d528ed9Sopenharmony_ci "-e=" + rebase_path( 18876d528ed9Sopenharmony_ci "$target_gen_dir/$app_name.xcent", root_build_dir), 18886d528ed9Sopenharmony_ci rebase_path(bundle_root_dir, root_build_dir), 18896d528ed9Sopenharmony_ci ] 18906d528ed9Sopenharmony_ci } else { 18916d528ed9Sopenharmony_ci deps += [ ":${app_name}_bundle_executable" ] 18926d528ed9Sopenharmony_ci } 18936d528ed9Sopenharmony_ci } 18946d528ed9Sopenharmony_ci } 18956d528ed9Sopenharmony_ci } 18966d528ed9Sopenharmony_ci``` 18976d528ed9Sopenharmony_ci### <a name="func_executable"></a>**executable**: Declare an executable target. 18986d528ed9Sopenharmony_ci 18996d528ed9Sopenharmony_ci#### **Language and compilation** 19006d528ed9Sopenharmony_ci 19016d528ed9Sopenharmony_ci``` 19026d528ed9Sopenharmony_ci The tools and commands used to create this target type will be 19036d528ed9Sopenharmony_ci determined by the source files in its sources. Targets containing 19046d528ed9Sopenharmony_ci multiple compiler-incompatible languages are not allowed (e.g. a 19056d528ed9Sopenharmony_ci target containing both C and C++ sources is acceptable, but a 19066d528ed9Sopenharmony_ci target containing C and Rust sources is not). 19076d528ed9Sopenharmony_ci``` 19086d528ed9Sopenharmony_ci 19096d528ed9Sopenharmony_ci#### **Variables** 19106d528ed9Sopenharmony_ci 19116d528ed9Sopenharmony_ci``` 19126d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 19136d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 19146d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 19156d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 19166d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 19176d528ed9Sopenharmony_ci write_runtime_deps 19186d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 19196d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 19206d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 19216d528ed9Sopenharmony_ci visibility 19226d528ed9Sopenharmony_ci Rust variables: aliased_deps, crate_root, crate_name 19236d528ed9Sopenharmony_ci``` 19246d528ed9Sopenharmony_ci### <a name="func_generated_file"></a>**generated_file**: Declare a generated_file target. 19256d528ed9Sopenharmony_ci 19266d528ed9Sopenharmony_ci``` 19276d528ed9Sopenharmony_ci Writes data value(s) to disk on resolution. This target type mirrors some 19286d528ed9Sopenharmony_ci functionality of the write_file() function, but also provides the ability to 19296d528ed9Sopenharmony_ci collect metadata from its dependencies on resolution rather than writing out 19306d528ed9Sopenharmony_ci at parse time. 19316d528ed9Sopenharmony_ci 19326d528ed9Sopenharmony_ci The `outputs` variable is required to be a list with a single element, 19336d528ed9Sopenharmony_ci specifying the intended location of the output file. 19346d528ed9Sopenharmony_ci 19356d528ed9Sopenharmony_ci The `output_conversion` variable specified the format to write the 19366d528ed9Sopenharmony_ci value. See `gn help io_conversion`. 19376d528ed9Sopenharmony_ci 19386d528ed9Sopenharmony_ci One of `contents` or `data_keys` must be specified; use of `contents` will 19396d528ed9Sopenharmony_ci write the contents of that value to file, while use of `data_keys` will 19406d528ed9Sopenharmony_ci trigger a metadata collection walk based on the dependencies of the target and 19416d528ed9Sopenharmony_ci the optional values of the `rebase` and `walk_keys` variables. See 19426d528ed9Sopenharmony_ci `gn help metadata`. 19436d528ed9Sopenharmony_ci 19446d528ed9Sopenharmony_ci Collected metadata, if specified, will be returned in postorder of 19456d528ed9Sopenharmony_ci dependencies. See the example for details. 19466d528ed9Sopenharmony_ci``` 19476d528ed9Sopenharmony_ci 19486d528ed9Sopenharmony_ci#### **Variables** 19496d528ed9Sopenharmony_ci 19506d528ed9Sopenharmony_ci``` 19516d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 19526d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 19536d528ed9Sopenharmony_ci write_runtime_deps 19546d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 19556d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 19566d528ed9Sopenharmony_ci visibility 19576d528ed9Sopenharmony_ci Generated file: contents, data_keys, rebase, walk_keys, output_conversion 19586d528ed9Sopenharmony_ci``` 19596d528ed9Sopenharmony_ci 19606d528ed9Sopenharmony_ci#### **Example (metadata collection)** 19616d528ed9Sopenharmony_ci 19626d528ed9Sopenharmony_ci``` 19636d528ed9Sopenharmony_ci Given the following targets defined in //base/BUILD.gn, where A depends on B 19646d528ed9Sopenharmony_ci and B depends on C and D: 19656d528ed9Sopenharmony_ci 19666d528ed9Sopenharmony_ci group("a") { 19676d528ed9Sopenharmony_ci metadata = { 19686d528ed9Sopenharmony_ci doom_melon = [ "enable" ] 19696d528ed9Sopenharmony_ci my_files = [ "foo.cpp" ] 19706d528ed9Sopenharmony_ci 19716d528ed9Sopenharmony_ci # Note: this is functionally equivalent to not defining `my_barrier` 19726d528ed9Sopenharmony_ci # at all in this target's metadata. 19736d528ed9Sopenharmony_ci my_barrier = [ "" ] 19746d528ed9Sopenharmony_ci } 19756d528ed9Sopenharmony_ci 19766d528ed9Sopenharmony_ci deps = [ ":b" ] 19776d528ed9Sopenharmony_ci } 19786d528ed9Sopenharmony_ci 19796d528ed9Sopenharmony_ci group("b") { 19806d528ed9Sopenharmony_ci metadata = { 19816d528ed9Sopenharmony_ci my_files = [ "bar.cpp" ] 19826d528ed9Sopenharmony_ci my_barrier = [ ":c" ] 19836d528ed9Sopenharmony_ci } 19846d528ed9Sopenharmony_ci 19856d528ed9Sopenharmony_ci deps = [ ":c", ":d" ] 19866d528ed9Sopenharmony_ci } 19876d528ed9Sopenharmony_ci 19886d528ed9Sopenharmony_ci group("c") { 19896d528ed9Sopenharmony_ci metadata = { 19906d528ed9Sopenharmony_ci doom_melon = [ "disable" ] 19916d528ed9Sopenharmony_ci my_files = [ "baz.cpp" ] 19926d528ed9Sopenharmony_ci } 19936d528ed9Sopenharmony_ci } 19946d528ed9Sopenharmony_ci 19956d528ed9Sopenharmony_ci group("d") { 19966d528ed9Sopenharmony_ci metadata = { 19976d528ed9Sopenharmony_ci my_files = [ "missing.cpp" ] 19986d528ed9Sopenharmony_ci } 19996d528ed9Sopenharmony_ci } 20006d528ed9Sopenharmony_ci 20016d528ed9Sopenharmony_ci If the following generated_file target is defined: 20026d528ed9Sopenharmony_ci 20036d528ed9Sopenharmony_ci generated_file("my_files_metadata") { 20046d528ed9Sopenharmony_ci outputs = [ "$root_build_dir/my_files.json" ] 20056d528ed9Sopenharmony_ci data_keys = [ "my_files" ] 20066d528ed9Sopenharmony_ci 20076d528ed9Sopenharmony_ci deps = [ "//base:a" ] 20086d528ed9Sopenharmony_ci } 20096d528ed9Sopenharmony_ci 20106d528ed9Sopenharmony_ci The following will be written to "$root_build_dir/my_files.json" (less the 20116d528ed9Sopenharmony_ci comments): 20126d528ed9Sopenharmony_ci [ 20136d528ed9Sopenharmony_ci "baz.cpp", // from //base:c via //base:b 20146d528ed9Sopenharmony_ci "missing.cpp" // from //base:d via //base:b 20156d528ed9Sopenharmony_ci "bar.cpp", // from //base:b via //base:a 20166d528ed9Sopenharmony_ci "foo.cpp", // from //base:a 20176d528ed9Sopenharmony_ci ] 20186d528ed9Sopenharmony_ci 20196d528ed9Sopenharmony_ci Alternatively, as an example of using walk_keys, if the following 20206d528ed9Sopenharmony_ci generated_file target is defined: 20216d528ed9Sopenharmony_ci 20226d528ed9Sopenharmony_ci generated_file("my_files_metadata") { 20236d528ed9Sopenharmony_ci outputs = [ "$root_build_dir/my_files.json" ] 20246d528ed9Sopenharmony_ci data_keys = [ "my_files" ] 20256d528ed9Sopenharmony_ci walk_keys = [ "my_barrier" ] 20266d528ed9Sopenharmony_ci 20276d528ed9Sopenharmony_ci deps = [ "//base:a" ] 20286d528ed9Sopenharmony_ci } 20296d528ed9Sopenharmony_ci 20306d528ed9Sopenharmony_ci The following will be written to "$root_build_dir/my_files.json" (again less 20316d528ed9Sopenharmony_ci the comments): 20326d528ed9Sopenharmony_ci [ 20336d528ed9Sopenharmony_ci "baz.cpp", // from //base:c via //base:b 20346d528ed9Sopenharmony_ci "bar.cpp", // from //base:b via //base:a 20356d528ed9Sopenharmony_ci "foo.cpp", // from //base:a 20366d528ed9Sopenharmony_ci ] 20376d528ed9Sopenharmony_ci 20386d528ed9Sopenharmony_ci If `rebase` is used in the following generated_file target: 20396d528ed9Sopenharmony_ci 20406d528ed9Sopenharmony_ci generated_file("my_files_metadata") { 20416d528ed9Sopenharmony_ci outputs = [ "$root_build_dir/my_files.json" ] 20426d528ed9Sopenharmony_ci data_keys = [ "my_files" ] 20436d528ed9Sopenharmony_ci walk_keys = [ "my_barrier" ] 20446d528ed9Sopenharmony_ci rebase = root_build_dir 20456d528ed9Sopenharmony_ci 20466d528ed9Sopenharmony_ci deps = [ "//base:a" ] 20476d528ed9Sopenharmony_ci } 20486d528ed9Sopenharmony_ci 20496d528ed9Sopenharmony_ci The following will be written to "$root_build_dir/my_files.json" (again less 20506d528ed9Sopenharmony_ci the comments) (assuming root_build_dir = "//out"): 20516d528ed9Sopenharmony_ci [ 20526d528ed9Sopenharmony_ci "../base/baz.cpp", // from //base:c via //base:b 20536d528ed9Sopenharmony_ci "../base/bar.cpp", // from //base:b via //base:a 20546d528ed9Sopenharmony_ci "../base/foo.cpp", // from //base:a 20556d528ed9Sopenharmony_ci ] 20566d528ed9Sopenharmony_ci``` 20576d528ed9Sopenharmony_ci### <a name="func_group"></a>**group**: Declare a named group of targets. 20586d528ed9Sopenharmony_ci 20596d528ed9Sopenharmony_ci``` 20606d528ed9Sopenharmony_ci This target type allows you to create meta-targets that just collect a set of 20616d528ed9Sopenharmony_ci dependencies into one named target. Groups can additionally specify configs 20626d528ed9Sopenharmony_ci that apply to their dependents. 20636d528ed9Sopenharmony_ci``` 20646d528ed9Sopenharmony_ci 20656d528ed9Sopenharmony_ci#### **Variables** 20666d528ed9Sopenharmony_ci 20676d528ed9Sopenharmony_ci``` 20686d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 20696d528ed9Sopenharmony_ci write_runtime_deps 20706d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 20716d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 20726d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 20736d528ed9Sopenharmony_ci visibility 20746d528ed9Sopenharmony_ci``` 20756d528ed9Sopenharmony_ci 20766d528ed9Sopenharmony_ci#### **Example** 20776d528ed9Sopenharmony_ci 20786d528ed9Sopenharmony_ci``` 20796d528ed9Sopenharmony_ci group("all") { 20806d528ed9Sopenharmony_ci deps = [ 20816d528ed9Sopenharmony_ci "//project:runner", 20826d528ed9Sopenharmony_ci "//project:unit_tests", 20836d528ed9Sopenharmony_ci ] 20846d528ed9Sopenharmony_ci } 20856d528ed9Sopenharmony_ci``` 20866d528ed9Sopenharmony_ci### <a name="func_loadable_module"></a>**loadable_module**: Declare a loadable module target. 20876d528ed9Sopenharmony_ci 20886d528ed9Sopenharmony_ci``` 20896d528ed9Sopenharmony_ci This target type allows you to create an object file that is (and can only 20906d528ed9Sopenharmony_ci be) loaded and unloaded at runtime. 20916d528ed9Sopenharmony_ci 20926d528ed9Sopenharmony_ci A loadable module will be specified on the linker line for targets listing 20936d528ed9Sopenharmony_ci the loadable module in its "deps". If you don't want this (if you don't need 20946d528ed9Sopenharmony_ci to dynamically load the library at runtime), then you should use a 20956d528ed9Sopenharmony_ci "shared_library" target type instead. 20966d528ed9Sopenharmony_ci``` 20976d528ed9Sopenharmony_ci 20986d528ed9Sopenharmony_ci#### **Language and compilation** 20996d528ed9Sopenharmony_ci 21006d528ed9Sopenharmony_ci``` 21016d528ed9Sopenharmony_ci The tools and commands used to create this target type will be 21026d528ed9Sopenharmony_ci determined by the source files in its sources. Targets containing 21036d528ed9Sopenharmony_ci multiple compiler-incompatible languages are not allowed (e.g. a 21046d528ed9Sopenharmony_ci target containing both C and C++ sources is acceptable, but a 21056d528ed9Sopenharmony_ci target containing C and Rust sources is not). 21066d528ed9Sopenharmony_ci``` 21076d528ed9Sopenharmony_ci 21086d528ed9Sopenharmony_ci#### **Variables** 21096d528ed9Sopenharmony_ci 21106d528ed9Sopenharmony_ci``` 21116d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 21126d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 21136d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 21146d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 21156d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 21166d528ed9Sopenharmony_ci write_runtime_deps 21176d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 21186d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 21196d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 21206d528ed9Sopenharmony_ci visibility 21216d528ed9Sopenharmony_ci Rust variables: aliased_deps, crate_root, crate_name, crate_type 21226d528ed9Sopenharmony_ci``` 21236d528ed9Sopenharmony_ci### <a name="func_rust_library"></a>**rust_library**: Declare a Rust library target. 21246d528ed9Sopenharmony_ci 21256d528ed9Sopenharmony_ci``` 21266d528ed9Sopenharmony_ci A Rust library is an archive containing additional rust-c provided metadata. 21276d528ed9Sopenharmony_ci These are the files produced by the rustc compiler with the `.rlib` 21286d528ed9Sopenharmony_ci extension, and are the intermediate step for most Rust-based binaries. 21296d528ed9Sopenharmony_ci``` 21306d528ed9Sopenharmony_ci 21316d528ed9Sopenharmony_ci#### **Language and compilation** 21326d528ed9Sopenharmony_ci 21336d528ed9Sopenharmony_ci``` 21346d528ed9Sopenharmony_ci The tools and commands used to create this target type will be 21356d528ed9Sopenharmony_ci determined by the source files in its sources. Targets containing 21366d528ed9Sopenharmony_ci multiple compiler-incompatible languages are not allowed (e.g. a 21376d528ed9Sopenharmony_ci target containing both C and C++ sources is acceptable, but a 21386d528ed9Sopenharmony_ci target containing C and Rust sources is not). 21396d528ed9Sopenharmony_ci``` 21406d528ed9Sopenharmony_ci 21416d528ed9Sopenharmony_ci#### **Variables** 21426d528ed9Sopenharmony_ci 21436d528ed9Sopenharmony_ci``` 21446d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 21456d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 21466d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 21476d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 21486d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 21496d528ed9Sopenharmony_ci write_runtime_deps 21506d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 21516d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 21526d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 21536d528ed9Sopenharmony_ci visibility 21546d528ed9Sopenharmony_ci Rust variables: aliased_deps, crate_root, crate_name 21556d528ed9Sopenharmony_ci``` 21566d528ed9Sopenharmony_ci### <a name="func_rust_proc_macro"></a>**rust_proc_macro**: Declare a Rust procedural macro target. 21576d528ed9Sopenharmony_ci 21586d528ed9Sopenharmony_ci``` 21596d528ed9Sopenharmony_ci A Rust procedural macro allows creating syntax extensions as execution of a 21606d528ed9Sopenharmony_ci function. They are compiled as dynamic libraries and used by the compiler at 21616d528ed9Sopenharmony_ci runtime. 21626d528ed9Sopenharmony_ci 21636d528ed9Sopenharmony_ci Their use is the same as of other Rust libraries, but their build has some 21646d528ed9Sopenharmony_ci additional restrictions in terms of supported flags. 21656d528ed9Sopenharmony_ci``` 21666d528ed9Sopenharmony_ci 21676d528ed9Sopenharmony_ci#### **Language and compilation** 21686d528ed9Sopenharmony_ci 21696d528ed9Sopenharmony_ci``` 21706d528ed9Sopenharmony_ci The tools and commands used to create this target type will be 21716d528ed9Sopenharmony_ci determined by the source files in its sources. Targets containing 21726d528ed9Sopenharmony_ci multiple compiler-incompatible languages are not allowed (e.g. a 21736d528ed9Sopenharmony_ci target containing both C and C++ sources is acceptable, but a 21746d528ed9Sopenharmony_ci target containing C and Rust sources is not). 21756d528ed9Sopenharmony_ci``` 21766d528ed9Sopenharmony_ci 21776d528ed9Sopenharmony_ci#### **Variables** 21786d528ed9Sopenharmony_ci 21796d528ed9Sopenharmony_ci``` 21806d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 21816d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 21826d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 21836d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 21846d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 21856d528ed9Sopenharmony_ci write_runtime_deps 21866d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 21876d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 21886d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 21896d528ed9Sopenharmony_ci visibility 21906d528ed9Sopenharmony_ci Rust variables: aliased_deps, crate_root, crate_name 21916d528ed9Sopenharmony_ci``` 21926d528ed9Sopenharmony_ci### <a name="func_shared_library"></a>**shared_library**: Declare a shared library target. 21936d528ed9Sopenharmony_ci 21946d528ed9Sopenharmony_ci``` 21956d528ed9Sopenharmony_ci A shared library will be specified on the linker line for targets listing the 21966d528ed9Sopenharmony_ci shared library in its "deps". If you don't want this (say you dynamically 21976d528ed9Sopenharmony_ci load the library at runtime), then you should depend on the shared library 21986d528ed9Sopenharmony_ci via "data_deps" or, on Darwin platforms, use a "loadable_module" target type 21996d528ed9Sopenharmony_ci instead. 22006d528ed9Sopenharmony_ci``` 22016d528ed9Sopenharmony_ci 22026d528ed9Sopenharmony_ci#### **Language and compilation** 22036d528ed9Sopenharmony_ci 22046d528ed9Sopenharmony_ci``` 22056d528ed9Sopenharmony_ci The tools and commands used to create this target type will be 22066d528ed9Sopenharmony_ci determined by the source files in its sources. Targets containing 22076d528ed9Sopenharmony_ci multiple compiler-incompatible languages are not allowed (e.g. a 22086d528ed9Sopenharmony_ci target containing both C and C++ sources is acceptable, but a 22096d528ed9Sopenharmony_ci target containing C and Rust sources is not). 22106d528ed9Sopenharmony_ci``` 22116d528ed9Sopenharmony_ci 22126d528ed9Sopenharmony_ci#### **Variables** 22136d528ed9Sopenharmony_ci 22146d528ed9Sopenharmony_ci``` 22156d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 22166d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 22176d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 22186d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 22196d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 22206d528ed9Sopenharmony_ci write_runtime_deps 22216d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 22226d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 22236d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 22246d528ed9Sopenharmony_ci visibility 22256d528ed9Sopenharmony_ci Rust variables: aliased_deps, crate_root, crate_name, crate_type 22266d528ed9Sopenharmony_ci``` 22276d528ed9Sopenharmony_ci### <a name="func_source_set"></a>**source_set**: Declare a source set target. 22286d528ed9Sopenharmony_ci 22296d528ed9Sopenharmony_ci``` 22306d528ed9Sopenharmony_ci Only C-language source sets are supported at the moment. 22316d528ed9Sopenharmony_ci``` 22326d528ed9Sopenharmony_ci 22336d528ed9Sopenharmony_ci#### **C-language source_sets** 22346d528ed9Sopenharmony_ci 22356d528ed9Sopenharmony_ci``` 22366d528ed9Sopenharmony_ci A source set is a collection of sources that get compiled, but are not linked 22376d528ed9Sopenharmony_ci to produce any kind of library. Instead, the resulting object files are 22386d528ed9Sopenharmony_ci implicitly added to the linker line of all targets that depend on the source 22396d528ed9Sopenharmony_ci set. 22406d528ed9Sopenharmony_ci 22416d528ed9Sopenharmony_ci In most cases, a source set will behave like a static library, except no 22426d528ed9Sopenharmony_ci actual library file will be produced. This will make the build go a little 22436d528ed9Sopenharmony_ci faster by skipping creation of a large static library, while maintaining the 22446d528ed9Sopenharmony_ci organizational benefits of focused build targets. 22456d528ed9Sopenharmony_ci 22466d528ed9Sopenharmony_ci The main difference between a source set and a static library is around 22476d528ed9Sopenharmony_ci handling of exported symbols. Most linkers assume declaring a function 22486d528ed9Sopenharmony_ci exported means exported from the static library. The linker can then do dead 22496d528ed9Sopenharmony_ci code elimination to delete code not reachable from exported functions. 22506d528ed9Sopenharmony_ci 22516d528ed9Sopenharmony_ci A source set will not do this code elimination since there is no link step. 22526d528ed9Sopenharmony_ci This allows you to link many source sets into a shared library and have the 22536d528ed9Sopenharmony_ci "exported symbol" notation indicate "export from the final shared library and 22546d528ed9Sopenharmony_ci not from the intermediate targets." There is no way to express this concept 22556d528ed9Sopenharmony_ci when linking multiple static libraries into a shared library. 22566d528ed9Sopenharmony_ci``` 22576d528ed9Sopenharmony_ci 22586d528ed9Sopenharmony_ci#### **Variables** 22596d528ed9Sopenharmony_ci 22606d528ed9Sopenharmony_ci``` 22616d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 22626d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 22636d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 22646d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 22656d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 22666d528ed9Sopenharmony_ci write_runtime_deps 22676d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 22686d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 22696d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 22706d528ed9Sopenharmony_ci visibility 22716d528ed9Sopenharmony_ci``` 22726d528ed9Sopenharmony_ci### <a name="func_static_library"></a>**static_library**: Declare a static library target. 22736d528ed9Sopenharmony_ci 22746d528ed9Sopenharmony_ci``` 22756d528ed9Sopenharmony_ci Make a ".a" / ".lib" file. 22766d528ed9Sopenharmony_ci 22776d528ed9Sopenharmony_ci If you only need the static library for intermediate results in the build, 22786d528ed9Sopenharmony_ci you should consider a source_set instead since it will skip the (potentially 22796d528ed9Sopenharmony_ci slow) step of creating the intermediate library file. 22806d528ed9Sopenharmony_ci``` 22816d528ed9Sopenharmony_ci 22826d528ed9Sopenharmony_ci#### **Variables** 22836d528ed9Sopenharmony_ci 22846d528ed9Sopenharmony_ci``` 22856d528ed9Sopenharmony_ci complete_static_lib 22866d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 22876d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 22886d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 22896d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 22906d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 22916d528ed9Sopenharmony_ci write_runtime_deps 22926d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 22936d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 22946d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 22956d528ed9Sopenharmony_ci visibility 22966d528ed9Sopenharmony_ci Rust variables: aliased_deps, crate_root, crate_name 22976d528ed9Sopenharmony_ci 22986d528ed9Sopenharmony_ci The tools and commands used to create this target type will be 22996d528ed9Sopenharmony_ci determined by the source files in its sources. Targets containing 23006d528ed9Sopenharmony_ci multiple compiler-incompatible languages are not allowed (e.g. a 23016d528ed9Sopenharmony_ci target containing both C and C++ sources is acceptable, but a 23026d528ed9Sopenharmony_ci target containing C and Rust sources is not). 23036d528ed9Sopenharmony_ci``` 23046d528ed9Sopenharmony_ci### <a name="func_target"></a>**target**: Declare a target with the given programmatic type. 23056d528ed9Sopenharmony_ci 23066d528ed9Sopenharmony_ci``` 23076d528ed9Sopenharmony_ci target(target_type_string, target_name_string) { ... } 23086d528ed9Sopenharmony_ci 23096d528ed9Sopenharmony_ci The target() function is a way to invoke a built-in target or template with a 23106d528ed9Sopenharmony_ci type determined at runtime. This is useful for cases where the type of a 23116d528ed9Sopenharmony_ci target might not be known statically. 23126d528ed9Sopenharmony_ci 23136d528ed9Sopenharmony_ci Only templates and built-in target functions are supported for the 23146d528ed9Sopenharmony_ci target_type_string parameter. Arbitrary functions, configs, and toolchains 23156d528ed9Sopenharmony_ci are not supported. 23166d528ed9Sopenharmony_ci 23176d528ed9Sopenharmony_ci The call: 23186d528ed9Sopenharmony_ci target("source_set", "doom_melon") { 23196d528ed9Sopenharmony_ci Is equivalent to: 23206d528ed9Sopenharmony_ci source_set("doom_melon") { 23216d528ed9Sopenharmony_ci``` 23226d528ed9Sopenharmony_ci 23236d528ed9Sopenharmony_ci#### **Common target variables** 23246d528ed9Sopenharmony_ci 23256d528ed9Sopenharmony_ci``` 23266d528ed9Sopenharmony_ci Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, 23276d528ed9Sopenharmony_ci write_runtime_deps 23286d528ed9Sopenharmony_ci Dependent configs: all_dependent_configs, public_configs 23296d528ed9Sopenharmony_ci General: check_includes, configs, data, friend, inputs, metadata, 23306d528ed9Sopenharmony_ci output_extension, output_name, public, sources, testonly, 23316d528ed9Sopenharmony_ci visibility 23326d528ed9Sopenharmony_ci 23336d528ed9Sopenharmony_ci Targets will also have variables specific to that type, see "gn help <type>" 23346d528ed9Sopenharmony_ci for more. 23356d528ed9Sopenharmony_ci``` 23366d528ed9Sopenharmony_ci 23376d528ed9Sopenharmony_ci#### **Example** 23386d528ed9Sopenharmony_ci 23396d528ed9Sopenharmony_ci``` 23406d528ed9Sopenharmony_ci if (foo_build_as_shared) { 23416d528ed9Sopenharmony_ci my_type = "shared_library" 23426d528ed9Sopenharmony_ci } else { 23436d528ed9Sopenharmony_ci my_type = "source_set" 23446d528ed9Sopenharmony_ci } 23456d528ed9Sopenharmony_ci 23466d528ed9Sopenharmony_ci target(my_type, "foo") { 23476d528ed9Sopenharmony_ci ... 23486d528ed9Sopenharmony_ci } 23496d528ed9Sopenharmony_ci``` 23506d528ed9Sopenharmony_ci## <a name="functions"></a>Buildfile functions 23516d528ed9Sopenharmony_ci 23526d528ed9Sopenharmony_ci### <a name="func_assert"></a>**assert**: Assert an expression is true at generation time. 23536d528ed9Sopenharmony_ci 23546d528ed9Sopenharmony_ci``` 23556d528ed9Sopenharmony_ci assert(<condition> [, <error string>]) 23566d528ed9Sopenharmony_ci 23576d528ed9Sopenharmony_ci If the condition is false, the build will fail with an error. If the 23586d528ed9Sopenharmony_ci optional second argument is provided, that string will be printed 23596d528ed9Sopenharmony_ci with the error message. 23606d528ed9Sopenharmony_ci``` 23616d528ed9Sopenharmony_ci 23626d528ed9Sopenharmony_ci#### **Examples** 23636d528ed9Sopenharmony_ci 23646d528ed9Sopenharmony_ci``` 23656d528ed9Sopenharmony_ci assert(is_win) 23666d528ed9Sopenharmony_ci assert(defined(sources), "Sources must be defined"); 23676d528ed9Sopenharmony_ci``` 23686d528ed9Sopenharmony_ci### <a name="func_config"></a>**config**: Defines a configuration object. 23696d528ed9Sopenharmony_ci 23706d528ed9Sopenharmony_ci``` 23716d528ed9Sopenharmony_ci Configuration objects can be applied to targets and specify sets of compiler 23726d528ed9Sopenharmony_ci flags, includes, defines, etc. They provide a way to conveniently group sets 23736d528ed9Sopenharmony_ci of this configuration information. 23746d528ed9Sopenharmony_ci 23756d528ed9Sopenharmony_ci A config is referenced by its label just like a target. 23766d528ed9Sopenharmony_ci 23776d528ed9Sopenharmony_ci The values in a config are additive only. If you want to remove a flag you 23786d528ed9Sopenharmony_ci need to remove the corresponding config that sets it. The final set of flags, 23796d528ed9Sopenharmony_ci defines, etc. for a target is generated in this order: 23806d528ed9Sopenharmony_ci 23816d528ed9Sopenharmony_ci 1. The values specified directly on the target (rather than using a config). 23826d528ed9Sopenharmony_ci 2. The configs specified in the target's "configs" list, in order. 23836d528ed9Sopenharmony_ci 3. Public_configs from a breadth-first traversal of the dependency tree in 23846d528ed9Sopenharmony_ci the order that the targets appear in "deps". 23856d528ed9Sopenharmony_ci 4. All dependent configs from a breadth-first traversal of the dependency 23866d528ed9Sopenharmony_ci tree in the order that the targets appear in "deps". 23876d528ed9Sopenharmony_ci``` 23886d528ed9Sopenharmony_ci 23896d528ed9Sopenharmony_ci#### **More background** 23906d528ed9Sopenharmony_ci 23916d528ed9Sopenharmony_ci``` 23926d528ed9Sopenharmony_ci Configs solve a problem where the build system needs to have a higher-level 23936d528ed9Sopenharmony_ci understanding of various compiler settings. For example, some compiler flags 23946d528ed9Sopenharmony_ci have to appear in a certain order relative to each other, some settings like 23956d528ed9Sopenharmony_ci defines and flags logically go together, and the build system needs to 23966d528ed9Sopenharmony_ci de-duplicate flags even though raw command-line parameters can't always be 23976d528ed9Sopenharmony_ci operated on in that way. 23986d528ed9Sopenharmony_ci 23996d528ed9Sopenharmony_ci The config gives a name to a group of settings that can then be reasoned 24006d528ed9Sopenharmony_ci about by GN. GN can know that configs with the same label are the same thing 24016d528ed9Sopenharmony_ci so can be de-duplicated. It allows related settings to be grouped so they 24026d528ed9Sopenharmony_ci are added or removed as a unit. And it allows targets to refer to settings 24036d528ed9Sopenharmony_ci with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than 24046d528ed9Sopenharmony_ci having to hard-coding every compiler's flags each time they are referred to. 24056d528ed9Sopenharmony_ci``` 24066d528ed9Sopenharmony_ci 24076d528ed9Sopenharmony_ci#### **Variables valid in a config definition** 24086d528ed9Sopenharmony_ci 24096d528ed9Sopenharmony_ci``` 24106d528ed9Sopenharmony_ci Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, 24116d528ed9Sopenharmony_ci cflags_objcc, defines, include_dirs, inputs, ldflags, 24126d528ed9Sopenharmony_ci lib_dirs, libs, precompiled_header, precompiled_source, 24136d528ed9Sopenharmony_ci rustenv, rustflags, swiftflags, testonly 24146d528ed9Sopenharmony_ci Nested configs: configs 24156d528ed9Sopenharmony_ci General: visibility 24166d528ed9Sopenharmony_ci``` 24176d528ed9Sopenharmony_ci 24186d528ed9Sopenharmony_ci#### **Variables on a target used to apply configs** 24196d528ed9Sopenharmony_ci 24206d528ed9Sopenharmony_ci``` 24216d528ed9Sopenharmony_ci all_dependent_configs, configs, public_configs 24226d528ed9Sopenharmony_ci``` 24236d528ed9Sopenharmony_ci 24246d528ed9Sopenharmony_ci#### **Example** 24256d528ed9Sopenharmony_ci 24266d528ed9Sopenharmony_ci``` 24276d528ed9Sopenharmony_ci config("myconfig") { 24286d528ed9Sopenharmony_ci include_dirs = [ "include/common" ] 24296d528ed9Sopenharmony_ci defines = [ "ENABLE_DOOM_MELON" ] 24306d528ed9Sopenharmony_ci } 24316d528ed9Sopenharmony_ci 24326d528ed9Sopenharmony_ci executable("mything") { 24336d528ed9Sopenharmony_ci configs = [ ":myconfig" ] 24346d528ed9Sopenharmony_ci } 24356d528ed9Sopenharmony_ci``` 24366d528ed9Sopenharmony_ci### <a name="func_declare_args"></a>**declare_args**: Declare build arguments. 24376d528ed9Sopenharmony_ci 24386d528ed9Sopenharmony_ci``` 24396d528ed9Sopenharmony_ci Introduces the given arguments into the current scope. If they are not 24406d528ed9Sopenharmony_ci specified on the command line or in a toolchain's arguments, the default 24416d528ed9Sopenharmony_ci values given in the declare_args block will be used. However, these defaults 24426d528ed9Sopenharmony_ci will not override command-line values. 24436d528ed9Sopenharmony_ci 24446d528ed9Sopenharmony_ci See also "gn help buildargs" for an overview. 24456d528ed9Sopenharmony_ci 24466d528ed9Sopenharmony_ci The precise behavior of declare args is: 24476d528ed9Sopenharmony_ci 24486d528ed9Sopenharmony_ci 1. The declare_args() block executes. Any variable defined in the enclosing 24496d528ed9Sopenharmony_ci scope is available for reading, but any variable defined earlier in 24506d528ed9Sopenharmony_ci the current scope is not (since the overrides haven't been applied yet). 24516d528ed9Sopenharmony_ci 24526d528ed9Sopenharmony_ci 2. At the end of executing the block, any variables set within that scope 24536d528ed9Sopenharmony_ci are saved, with the values specified in the block used as the "default value" 24546d528ed9Sopenharmony_ci for that argument. Once saved, these variables are available for override 24556d528ed9Sopenharmony_ci via args.gn. 24566d528ed9Sopenharmony_ci 24576d528ed9Sopenharmony_ci 3. User-defined overrides are applied. Anything set in "gn args" now 24586d528ed9Sopenharmony_ci overrides any default values. The resulting set of variables is promoted 24596d528ed9Sopenharmony_ci to be readable from the following code in the file. 24606d528ed9Sopenharmony_ci 24616d528ed9Sopenharmony_ci This has some ramifications that may not be obvious: 24626d528ed9Sopenharmony_ci 24636d528ed9Sopenharmony_ci - You should not perform difficult work inside a declare_args block since 24646d528ed9Sopenharmony_ci this only sets a default value that may be discarded. In particular, 24656d528ed9Sopenharmony_ci don't use the result of exec_script() to set the default value. If you 24666d528ed9Sopenharmony_ci want to have a script-defined default, set some default "undefined" value 24676d528ed9Sopenharmony_ci like [], "", or -1, and after the declare_args block, call exec_script if 24686d528ed9Sopenharmony_ci the value is unset by the user. 24696d528ed9Sopenharmony_ci 24706d528ed9Sopenharmony_ci - Because you cannot read the value of a variable defined in the same 24716d528ed9Sopenharmony_ci block, if you need to make the default value of one arg depend 24726d528ed9Sopenharmony_ci on the possibly-overridden value of another, write two separate 24736d528ed9Sopenharmony_ci declare_args() blocks: 24746d528ed9Sopenharmony_ci 24756d528ed9Sopenharmony_ci declare_args() { 24766d528ed9Sopenharmony_ci enable_foo = true 24776d528ed9Sopenharmony_ci } 24786d528ed9Sopenharmony_ci declare_args() { 24796d528ed9Sopenharmony_ci # Bar defaults to same user-overridden state as foo. 24806d528ed9Sopenharmony_ci enable_bar = enable_foo 24816d528ed9Sopenharmony_ci } 24826d528ed9Sopenharmony_ci``` 24836d528ed9Sopenharmony_ci 24846d528ed9Sopenharmony_ci#### **Example** 24856d528ed9Sopenharmony_ci 24866d528ed9Sopenharmony_ci``` 24876d528ed9Sopenharmony_ci declare_args() { 24886d528ed9Sopenharmony_ci enable_teleporter = true 24896d528ed9Sopenharmony_ci enable_doom_melon = false 24906d528ed9Sopenharmony_ci } 24916d528ed9Sopenharmony_ci 24926d528ed9Sopenharmony_ci If you want to override the (default disabled) Doom Melon: 24936d528ed9Sopenharmony_ci gn --args="enable_doom_melon=true enable_teleporter=true" 24946d528ed9Sopenharmony_ci This also sets the teleporter, but it's already defaulted to on so it will 24956d528ed9Sopenharmony_ci have no effect. 24966d528ed9Sopenharmony_ci``` 24976d528ed9Sopenharmony_ci### <a name="func_defined"></a>**defined**: Returns whether an identifier is defined. 24986d528ed9Sopenharmony_ci 24996d528ed9Sopenharmony_ci``` 25006d528ed9Sopenharmony_ci Returns true if the given argument is defined. This is most useful in 25016d528ed9Sopenharmony_ci templates to assert that the caller set things up properly. 25026d528ed9Sopenharmony_ci 25036d528ed9Sopenharmony_ci You can pass an identifier: 25046d528ed9Sopenharmony_ci defined(foo) 25056d528ed9Sopenharmony_ci which will return true or false depending on whether foo is defined in the 25066d528ed9Sopenharmony_ci current scope. 25076d528ed9Sopenharmony_ci 25086d528ed9Sopenharmony_ci You can also check a named scope: 25096d528ed9Sopenharmony_ci defined(foo.bar) 25106d528ed9Sopenharmony_ci which will return true or false depending on whether bar is defined in the 25116d528ed9Sopenharmony_ci named scope foo. It will throw an error if foo is not defined or is not a 25126d528ed9Sopenharmony_ci scope. 25136d528ed9Sopenharmony_ci 25146d528ed9Sopenharmony_ci You can also check a named scope using a subscript string expression: 25156d528ed9Sopenharmony_ci defined(foo[bar + "_name"]) 25166d528ed9Sopenharmony_ci which will return true or false depending on whether the subscript 25176d528ed9Sopenharmony_ci expression expands to the name of a member of the scope foo. It will 25186d528ed9Sopenharmony_ci throw an error if foo is not defined or is not a scope, or if the 25196d528ed9Sopenharmony_ci expression does not expand to a string, or if it is an empty string. 25206d528ed9Sopenharmony_ci``` 25216d528ed9Sopenharmony_ci 25226d528ed9Sopenharmony_ci#### **Example** 25236d528ed9Sopenharmony_ci 25246d528ed9Sopenharmony_ci``` 25256d528ed9Sopenharmony_ci template("mytemplate") { 25266d528ed9Sopenharmony_ci # To help users call this template properly... 25276d528ed9Sopenharmony_ci assert(defined(invoker.sources), "Sources must be defined") 25286d528ed9Sopenharmony_ci 25296d528ed9Sopenharmony_ci # If we want to accept an optional "values" argument, we don't 25306d528ed9Sopenharmony_ci # want to dereference something that may not be defined. 25316d528ed9Sopenharmony_ci if (defined(invoker.values)) { 25326d528ed9Sopenharmony_ci values = invoker.values 25336d528ed9Sopenharmony_ci } else { 25346d528ed9Sopenharmony_ci values = "some default value" 25356d528ed9Sopenharmony_ci } 25366d528ed9Sopenharmony_ci } 25376d528ed9Sopenharmony_ci``` 25386d528ed9Sopenharmony_ci### <a name="func_exec_script"></a>**exec_script**: Synchronously run a script and return the output. 25396d528ed9Sopenharmony_ci 25406d528ed9Sopenharmony_ci``` 25416d528ed9Sopenharmony_ci exec_script(filename, 25426d528ed9Sopenharmony_ci arguments = [], 25436d528ed9Sopenharmony_ci input_conversion = "", 25446d528ed9Sopenharmony_ci file_dependencies = []) 25456d528ed9Sopenharmony_ci 25466d528ed9Sopenharmony_ci Runs the given script, returning the stdout of the script. The build 25476d528ed9Sopenharmony_ci generation will fail if the script does not exist or returns a nonzero exit 25486d528ed9Sopenharmony_ci code. 25496d528ed9Sopenharmony_ci 25506d528ed9Sopenharmony_ci The current directory when executing the script will be the root build 25516d528ed9Sopenharmony_ci directory. If you are passing file names, you will want to use the 25526d528ed9Sopenharmony_ci rebase_path() function to make file names relative to this path (see "gn help 25536d528ed9Sopenharmony_ci rebase_path"). 25546d528ed9Sopenharmony_ci 25556d528ed9Sopenharmony_ci The default script interpreter is Python ("python" on POSIX, "python.exe" or 25566d528ed9Sopenharmony_ci "python.bat" on Windows). This can be configured by the script_executable 25576d528ed9Sopenharmony_ci variable, see "gn help dotfile". 25586d528ed9Sopenharmony_ci``` 25596d528ed9Sopenharmony_ci 25606d528ed9Sopenharmony_ci#### **Arguments**: 25616d528ed9Sopenharmony_ci 25626d528ed9Sopenharmony_ci``` 25636d528ed9Sopenharmony_ci filename: 25646d528ed9Sopenharmony_ci File name of script to execute. Non-absolute names will be treated as 25656d528ed9Sopenharmony_ci relative to the current build file. 25666d528ed9Sopenharmony_ci 25676d528ed9Sopenharmony_ci arguments: 25686d528ed9Sopenharmony_ci A list of strings to be passed to the script as arguments. May be 25696d528ed9Sopenharmony_ci unspecified or the empty list which means no arguments. 25706d528ed9Sopenharmony_ci 25716d528ed9Sopenharmony_ci input_conversion: 25726d528ed9Sopenharmony_ci Controls how the file is read and parsed. See `gn help io_conversion`. 25736d528ed9Sopenharmony_ci 25746d528ed9Sopenharmony_ci If unspecified, defaults to the empty string which causes the script 25756d528ed9Sopenharmony_ci result to be discarded. exec script will return None. 25766d528ed9Sopenharmony_ci 25776d528ed9Sopenharmony_ci dependencies: 25786d528ed9Sopenharmony_ci (Optional) A list of files that this script reads or otherwise depends 25796d528ed9Sopenharmony_ci on. These dependencies will be added to the build result such that if any 25806d528ed9Sopenharmony_ci of them change, the build will be regenerated and the script will be 25816d528ed9Sopenharmony_ci re-run. 25826d528ed9Sopenharmony_ci 25836d528ed9Sopenharmony_ci The script itself will be an implicit dependency so you do not need to 25846d528ed9Sopenharmony_ci list it. 25856d528ed9Sopenharmony_ci``` 25866d528ed9Sopenharmony_ci 25876d528ed9Sopenharmony_ci#### **Example** 25886d528ed9Sopenharmony_ci 25896d528ed9Sopenharmony_ci``` 25906d528ed9Sopenharmony_ci all_lines = exec_script( 25916d528ed9Sopenharmony_ci "myscript.py", [some_input], "list lines", 25926d528ed9Sopenharmony_ci [ rebase_path("data_file.txt", root_build_dir) ]) 25936d528ed9Sopenharmony_ci 25946d528ed9Sopenharmony_ci # This example just calls the script with no arguments and discards the 25956d528ed9Sopenharmony_ci # result. 25966d528ed9Sopenharmony_ci exec_script("//foo/bar/myscript.py") 25976d528ed9Sopenharmony_ci``` 25986d528ed9Sopenharmony_ci### <a name="func_filter_exclude"></a>**filter_exclude**: Remove values that match a set of patterns. 25996d528ed9Sopenharmony_ci 26006d528ed9Sopenharmony_ci``` 26016d528ed9Sopenharmony_ci filter_exclude(values, exclude_patterns) 26026d528ed9Sopenharmony_ci 26036d528ed9Sopenharmony_ci The argument values must be a list of strings. 26046d528ed9Sopenharmony_ci 26056d528ed9Sopenharmony_ci The argument exclude_patterns must be a list of file patterns (see 26066d528ed9Sopenharmony_ci "gn help file_pattern"). Any elements in values matching at least one 26076d528ed9Sopenharmony_ci of those patterns will be excluded. 26086d528ed9Sopenharmony_ci``` 26096d528ed9Sopenharmony_ci 26106d528ed9Sopenharmony_ci#### **Examples** 26116d528ed9Sopenharmony_ci``` 26126d528ed9Sopenharmony_ci values = [ "foo.cc", "foo.h", "foo.proto" ] 26136d528ed9Sopenharmony_ci result = filter_exclude(values, [ "*.proto" ]) 26146d528ed9Sopenharmony_ci # result will be [ "foo.cc", "foo.h" ] 26156d528ed9Sopenharmony_ci``` 26166d528ed9Sopenharmony_ci### <a name="func_filter_include"></a>**filter_include**: Remove values that do not match a set of patterns. 26176d528ed9Sopenharmony_ci 26186d528ed9Sopenharmony_ci``` 26196d528ed9Sopenharmony_ci filter_include(values, include_patterns) 26206d528ed9Sopenharmony_ci 26216d528ed9Sopenharmony_ci The argument values must be a list of strings. 26226d528ed9Sopenharmony_ci 26236d528ed9Sopenharmony_ci The argument include_patterns must be a list of file patterns (see 26246d528ed9Sopenharmony_ci "gn help file_pattern"). Only elements from values matching at least 26256d528ed9Sopenharmony_ci one of the pattern will be included. 26266d528ed9Sopenharmony_ci``` 26276d528ed9Sopenharmony_ci 26286d528ed9Sopenharmony_ci#### **Examples** 26296d528ed9Sopenharmony_ci``` 26306d528ed9Sopenharmony_ci values = [ "foo.cc", "foo.h", "foo.proto" ] 26316d528ed9Sopenharmony_ci result = filter_include(values, [ "*.proto" ]) 26326d528ed9Sopenharmony_ci # result will be [ "foo.proto" ] 26336d528ed9Sopenharmony_ci``` 26346d528ed9Sopenharmony_ci### <a name="func_filter_labels_exclude"></a>**filter_labels_exclude**: Remove labels that match a set of patterns. 26356d528ed9Sopenharmony_ci 26366d528ed9Sopenharmony_ci``` 26376d528ed9Sopenharmony_ci filter_labels_exclude(labels, exclude_patterns) 26386d528ed9Sopenharmony_ci 26396d528ed9Sopenharmony_ci The argument labels must be a list of strings. 26406d528ed9Sopenharmony_ci 26416d528ed9Sopenharmony_ci The argument exclude_patterns must be a list of label patterns (see 26426d528ed9Sopenharmony_ci "gn help label_pattern"). Only elements from labels matching at least 26436d528ed9Sopenharmony_ci one of the patterns will be excluded. 26446d528ed9Sopenharmony_ci``` 26456d528ed9Sopenharmony_ci 26466d528ed9Sopenharmony_ci#### **Examples** 26476d528ed9Sopenharmony_ci``` 26486d528ed9Sopenharmony_ci labels = [ "//foo:baz", "//foo/bar:baz", "//bar:baz" ] 26496d528ed9Sopenharmony_ci result = filter_labels_exclude(labels, [ "//foo:*" ]) 26506d528ed9Sopenharmony_ci # result will be [ "//foo/bar:baz", "//bar:baz" ] 26516d528ed9Sopenharmony_ci``` 26526d528ed9Sopenharmony_ci### <a name="func_filter_labels_include"></a>**filter_labels_include**: Remove labels that do not match a set of patterns. 26536d528ed9Sopenharmony_ci 26546d528ed9Sopenharmony_ci``` 26556d528ed9Sopenharmony_ci filter_labels_include(labels, include_patterns) 26566d528ed9Sopenharmony_ci 26576d528ed9Sopenharmony_ci The argument labels must be a list of strings. 26586d528ed9Sopenharmony_ci 26596d528ed9Sopenharmony_ci The argument include_patterns must be a list of label patterns (see 26606d528ed9Sopenharmony_ci "gn help label_pattern"). Only elements from labels matching at least 26616d528ed9Sopenharmony_ci one of the patterns will be included. 26626d528ed9Sopenharmony_ci``` 26636d528ed9Sopenharmony_ci 26646d528ed9Sopenharmony_ci#### **Examples** 26656d528ed9Sopenharmony_ci``` 26666d528ed9Sopenharmony_ci labels = [ "//foo:baz", "//foo/bar:baz", "//bar:baz" ] 26676d528ed9Sopenharmony_ci result = filter_labels_include(labels, [ "//foo:*" ]) 26686d528ed9Sopenharmony_ci # result will be [ "//foo:baz" ] 26696d528ed9Sopenharmony_ci``` 26706d528ed9Sopenharmony_ci### <a name="func_foreach"></a>**foreach**: Iterate over a list. 26716d528ed9Sopenharmony_ci 26726d528ed9Sopenharmony_ci``` 26736d528ed9Sopenharmony_ci foreach(<loop_var>, <list>) { 26746d528ed9Sopenharmony_ci <loop contents> 26756d528ed9Sopenharmony_ci } 26766d528ed9Sopenharmony_ci 26776d528ed9Sopenharmony_ci Executes the loop contents block over each item in the list, assigning the 26786d528ed9Sopenharmony_ci loop_var to each item in sequence. The <loop_var> will be a copy so assigning 26796d528ed9Sopenharmony_ci to it will not mutate the list. The loop will iterate over a copy of <list> 26806d528ed9Sopenharmony_ci so mutating it inside the loop will not affect iteration. 26816d528ed9Sopenharmony_ci 26826d528ed9Sopenharmony_ci The block does not introduce a new scope, so that variable assignments inside 26836d528ed9Sopenharmony_ci the loop will be visible once the loop terminates. 26846d528ed9Sopenharmony_ci 26856d528ed9Sopenharmony_ci The loop variable will temporarily shadow any existing variables with the 26866d528ed9Sopenharmony_ci same name for the duration of the loop. After the loop terminates the loop 26876d528ed9Sopenharmony_ci variable will no longer be in scope, and the previous value (if any) will be 26886d528ed9Sopenharmony_ci restored. 26896d528ed9Sopenharmony_ci``` 26906d528ed9Sopenharmony_ci 26916d528ed9Sopenharmony_ci#### **Example** 26926d528ed9Sopenharmony_ci 26936d528ed9Sopenharmony_ci``` 26946d528ed9Sopenharmony_ci mylist = [ "a", "b", "c" ] 26956d528ed9Sopenharmony_ci foreach(i, mylist) { 26966d528ed9Sopenharmony_ci print(i) 26976d528ed9Sopenharmony_ci } 26986d528ed9Sopenharmony_ci 26996d528ed9Sopenharmony_ci Prints: 27006d528ed9Sopenharmony_ci a 27016d528ed9Sopenharmony_ci b 27026d528ed9Sopenharmony_ci c 27036d528ed9Sopenharmony_ci``` 27046d528ed9Sopenharmony_ci### <a name="func_forward_variables_from"></a>**forward_variables_from**: Copies variables from a different scope. 27056d528ed9Sopenharmony_ci 27066d528ed9Sopenharmony_ci``` 27076d528ed9Sopenharmony_ci forward_variables_from(from_scope, variable_list_or_star, 27086d528ed9Sopenharmony_ci variable_to_not_forward_list = []) 27096d528ed9Sopenharmony_ci 27106d528ed9Sopenharmony_ci Copies the given variables from the given scope to the local scope if they 27116d528ed9Sopenharmony_ci exist. This is normally used in the context of templates to use the values of 27126d528ed9Sopenharmony_ci variables defined in the template invocation to a template-defined target. 27136d528ed9Sopenharmony_ci 27146d528ed9Sopenharmony_ci The variables in the given variable_list will be copied if they exist in the 27156d528ed9Sopenharmony_ci given scope or any enclosing scope. If they do not exist, nothing will happen 27166d528ed9Sopenharmony_ci and they be left undefined in the current scope. 27176d528ed9Sopenharmony_ci 27186d528ed9Sopenharmony_ci As a special case, if the variable_list is a string with the value of "*", 27196d528ed9Sopenharmony_ci all variables from the given scope will be copied. "*" only copies variables 27206d528ed9Sopenharmony_ci set directly on the from_scope, not enclosing ones. Otherwise it would 27216d528ed9Sopenharmony_ci duplicate all global variables. 27226d528ed9Sopenharmony_ci 27236d528ed9Sopenharmony_ci When an explicit list of variables is supplied, if the variable exists in the 27246d528ed9Sopenharmony_ci current (destination) scope already, an error will be thrown. If "*" is 27256d528ed9Sopenharmony_ci specified, variables in the current scope will be clobbered (the latter is 27266d528ed9Sopenharmony_ci important because most targets have an implicit configs list, which means it 27276d528ed9Sopenharmony_ci wouldn't work at all if it didn't clobber). 27286d528ed9Sopenharmony_ci 27296d528ed9Sopenharmony_ci If variables_to_not_forward_list is non-empty, then it must contains a list 27306d528ed9Sopenharmony_ci of variable names that will not be forwarded. This is mostly useful when 27316d528ed9Sopenharmony_ci variable_list_or_star has a value of "*". 27326d528ed9Sopenharmony_ci``` 27336d528ed9Sopenharmony_ci 27346d528ed9Sopenharmony_ci#### **Examples** 27356d528ed9Sopenharmony_ci 27366d528ed9Sopenharmony_ci``` 27376d528ed9Sopenharmony_ci # forward_variables_from(invoker, ["foo"]) 27386d528ed9Sopenharmony_ci # is equivalent to: 27396d528ed9Sopenharmony_ci assert(!defined(foo)) 27406d528ed9Sopenharmony_ci if (defined(invoker.foo)) { 27416d528ed9Sopenharmony_ci foo = invoker.foo 27426d528ed9Sopenharmony_ci } 27436d528ed9Sopenharmony_ci 27446d528ed9Sopenharmony_ci # This is a common action template. It would invoke a script with some given 27456d528ed9Sopenharmony_ci # parameters, and wants to use the various types of deps and the visibility 27466d528ed9Sopenharmony_ci # from the invoker if it's defined. It also injects an additional dependency 27476d528ed9Sopenharmony_ci # to all targets. 27486d528ed9Sopenharmony_ci template("my_test") { 27496d528ed9Sopenharmony_ci action(target_name) { 27506d528ed9Sopenharmony_ci forward_variables_from(invoker, [ "data_deps", "deps", 27516d528ed9Sopenharmony_ci "public_deps", "visibility"]) 27526d528ed9Sopenharmony_ci # Add our test code to the dependencies. 27536d528ed9Sopenharmony_ci # "deps" may or may not be defined at this point. 27546d528ed9Sopenharmony_ci if (defined(deps)) { 27556d528ed9Sopenharmony_ci deps += [ "//tools/doom_melon" ] 27566d528ed9Sopenharmony_ci } else { 27576d528ed9Sopenharmony_ci deps = [ "//tools/doom_melon" ] 27586d528ed9Sopenharmony_ci } 27596d528ed9Sopenharmony_ci } 27606d528ed9Sopenharmony_ci } 27616d528ed9Sopenharmony_ci 27626d528ed9Sopenharmony_ci # This is a template around a target whose type depends on a global variable. 27636d528ed9Sopenharmony_ci # It forwards all values from the invoker. 27646d528ed9Sopenharmony_ci template("my_wrapper") { 27656d528ed9Sopenharmony_ci target(my_wrapper_target_type, target_name) { 27666d528ed9Sopenharmony_ci forward_variables_from(invoker, "*") 27676d528ed9Sopenharmony_ci } 27686d528ed9Sopenharmony_ci } 27696d528ed9Sopenharmony_ci 27706d528ed9Sopenharmony_ci # A template that wraps another. It adds behavior based on one 27716d528ed9Sopenharmony_ci # variable, and forwards all others to the nested target. 27726d528ed9Sopenharmony_ci template("my_ios_test_app") { 27736d528ed9Sopenharmony_ci ios_test_app(target_name) { 27746d528ed9Sopenharmony_ci forward_variables_from(invoker, "*", ["test_bundle_name"]) 27756d528ed9Sopenharmony_ci if (!defined(extra_substitutions)) { 27766d528ed9Sopenharmony_ci extra_substitutions = [] 27776d528ed9Sopenharmony_ci } 27786d528ed9Sopenharmony_ci extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ] 27796d528ed9Sopenharmony_ci } 27806d528ed9Sopenharmony_ci } 27816d528ed9Sopenharmony_ci``` 27826d528ed9Sopenharmony_ci### <a name="func_get_label_info"></a>**get_label_info**: Get an attribute from a target's label. 27836d528ed9Sopenharmony_ci 27846d528ed9Sopenharmony_ci``` 27856d528ed9Sopenharmony_ci get_label_info(target_label, what) 27866d528ed9Sopenharmony_ci 27876d528ed9Sopenharmony_ci Given the label of a target, returns some attribute of that target. The 27886d528ed9Sopenharmony_ci target need not have been previously defined in the same file, since none of 27896d528ed9Sopenharmony_ci the attributes depend on the actual target definition, only the label itself. 27906d528ed9Sopenharmony_ci 27916d528ed9Sopenharmony_ci See also "gn help get_target_outputs". 27926d528ed9Sopenharmony_ci``` 27936d528ed9Sopenharmony_ci 27946d528ed9Sopenharmony_ci#### **Possible values for the "what" parameter** 27956d528ed9Sopenharmony_ci 27966d528ed9Sopenharmony_ci``` 27976d528ed9Sopenharmony_ci "name" 27986d528ed9Sopenharmony_ci The short name of the target. This will match the value of the 27996d528ed9Sopenharmony_ci "target_name" variable inside that target's declaration. For the label 28006d528ed9Sopenharmony_ci "//foo/bar:baz" this will return "baz". 28016d528ed9Sopenharmony_ci 28026d528ed9Sopenharmony_ci "dir" 28036d528ed9Sopenharmony_ci The directory containing the target's definition, with no slash at the 28046d528ed9Sopenharmony_ci end. For the label "//foo/bar:baz" this will return "//foo/bar". 28056d528ed9Sopenharmony_ci 28066d528ed9Sopenharmony_ci "target_gen_dir" 28076d528ed9Sopenharmony_ci The generated file directory for the target. This will match the value of 28086d528ed9Sopenharmony_ci the "target_gen_dir" variable when inside that target's declaration. 28096d528ed9Sopenharmony_ci 28106d528ed9Sopenharmony_ci "root_gen_dir" 28116d528ed9Sopenharmony_ci The root of the generated file tree for the target. This will match the 28126d528ed9Sopenharmony_ci value of the "root_gen_dir" variable when inside that target's 28136d528ed9Sopenharmony_ci declaration. 28146d528ed9Sopenharmony_ci 28156d528ed9Sopenharmony_ci "target_out_dir 28166d528ed9Sopenharmony_ci The output directory for the target. This will match the value of the 28176d528ed9Sopenharmony_ci "target_out_dir" variable when inside that target's declaration. 28186d528ed9Sopenharmony_ci 28196d528ed9Sopenharmony_ci "root_out_dir" 28206d528ed9Sopenharmony_ci The root of the output file tree for the target. This will match the 28216d528ed9Sopenharmony_ci value of the "root_out_dir" variable when inside that target's 28226d528ed9Sopenharmony_ci declaration. 28236d528ed9Sopenharmony_ci 28246d528ed9Sopenharmony_ci "label_no_toolchain" 28256d528ed9Sopenharmony_ci The fully qualified version of this label, not including the toolchain. 28266d528ed9Sopenharmony_ci For the input ":bar" it might return "//foo:bar". 28276d528ed9Sopenharmony_ci 28286d528ed9Sopenharmony_ci "label_with_toolchain" 28296d528ed9Sopenharmony_ci The fully qualified version of this label, including the toolchain. For 28306d528ed9Sopenharmony_ci the input ":bar" it might return "//foo:bar(//toolchain:x64)". 28316d528ed9Sopenharmony_ci 28326d528ed9Sopenharmony_ci "toolchain" 28336d528ed9Sopenharmony_ci The label of the toolchain. This will match the value of the 28346d528ed9Sopenharmony_ci "current_toolchain" variable when inside that target's declaration. 28356d528ed9Sopenharmony_ci``` 28366d528ed9Sopenharmony_ci 28376d528ed9Sopenharmony_ci#### **Examples** 28386d528ed9Sopenharmony_ci 28396d528ed9Sopenharmony_ci``` 28406d528ed9Sopenharmony_ci get_label_info(":foo", "name") 28416d528ed9Sopenharmony_ci # Returns string "foo". 28426d528ed9Sopenharmony_ci 28436d528ed9Sopenharmony_ci get_label_info("//foo/bar:baz", "target_gen_dir") 28446d528ed9Sopenharmony_ci # Returns string "//out/Debug/gen/foo/bar". 28456d528ed9Sopenharmony_ci``` 28466d528ed9Sopenharmony_ci### <a name="func_get_path_info"></a>**get_path_info**: Extract parts of a file or directory name. 28476d528ed9Sopenharmony_ci 28486d528ed9Sopenharmony_ci``` 28496d528ed9Sopenharmony_ci get_path_info(input, what) 28506d528ed9Sopenharmony_ci 28516d528ed9Sopenharmony_ci The first argument is either a string representing a file or directory name, 28526d528ed9Sopenharmony_ci or a list of such strings. If the input is a list the return value will be a 28536d528ed9Sopenharmony_ci list containing the result of applying the rule to each item in the input. 28546d528ed9Sopenharmony_ci``` 28556d528ed9Sopenharmony_ci 28566d528ed9Sopenharmony_ci#### **Possible values for the "what" parameter** 28576d528ed9Sopenharmony_ci 28586d528ed9Sopenharmony_ci``` 28596d528ed9Sopenharmony_ci "file" 28606d528ed9Sopenharmony_ci The substring after the last slash in the path, including the name and 28616d528ed9Sopenharmony_ci extension. If the input ends in a slash, the empty string will be 28626d528ed9Sopenharmony_ci returned. 28636d528ed9Sopenharmony_ci "foo/bar.txt" => "bar.txt" 28646d528ed9Sopenharmony_ci "bar.txt" => "bar.txt" 28656d528ed9Sopenharmony_ci "foo/" => "" 28666d528ed9Sopenharmony_ci "" => "" 28676d528ed9Sopenharmony_ci 28686d528ed9Sopenharmony_ci "name" 28696d528ed9Sopenharmony_ci The substring of the file name not including the extension. 28706d528ed9Sopenharmony_ci "foo/bar.txt" => "bar" 28716d528ed9Sopenharmony_ci "foo/bar" => "bar" 28726d528ed9Sopenharmony_ci "foo/" => "" 28736d528ed9Sopenharmony_ci 28746d528ed9Sopenharmony_ci "extension" 28756d528ed9Sopenharmony_ci The substring following the last period following the last slash, or the 28766d528ed9Sopenharmony_ci empty string if not found. The period is not included. 28776d528ed9Sopenharmony_ci "foo/bar.txt" => "txt" 28786d528ed9Sopenharmony_ci "foo/bar" => "" 28796d528ed9Sopenharmony_ci 28806d528ed9Sopenharmony_ci "dir" 28816d528ed9Sopenharmony_ci The directory portion of the name, not including the slash. 28826d528ed9Sopenharmony_ci "foo/bar.txt" => "foo" 28836d528ed9Sopenharmony_ci "//foo/bar" => "//foo" 28846d528ed9Sopenharmony_ci "foo" => "." 28856d528ed9Sopenharmony_ci 28866d528ed9Sopenharmony_ci The result will never end in a slash, so if the resulting is empty, the 28876d528ed9Sopenharmony_ci system ("/") or source ("//") roots, a "." will be appended such that it 28886d528ed9Sopenharmony_ci is always legal to append a slash and a filename and get a valid path. 28896d528ed9Sopenharmony_ci 28906d528ed9Sopenharmony_ci "out_dir" 28916d528ed9Sopenharmony_ci The output file directory corresponding to the path of the given file, 28926d528ed9Sopenharmony_ci not including a trailing slash. 28936d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar" 28946d528ed9Sopenharmony_ci 28956d528ed9Sopenharmony_ci "gen_dir" 28966d528ed9Sopenharmony_ci The generated file directory corresponding to the path of the given file, 28976d528ed9Sopenharmony_ci not including a trailing slash. 28986d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar" 28996d528ed9Sopenharmony_ci 29006d528ed9Sopenharmony_ci "abspath" 29016d528ed9Sopenharmony_ci The full absolute path name to the file or directory. It will be resolved 29026d528ed9Sopenharmony_ci relative to the current directory, and then the source- absolute version 29036d528ed9Sopenharmony_ci will be returned. If the input is system- absolute, the same input will 29046d528ed9Sopenharmony_ci be returned. 29056d528ed9Sopenharmony_ci "foo/bar.txt" => "//mydir/foo/bar.txt" 29066d528ed9Sopenharmony_ci "foo/" => "//mydir/foo/" 29076d528ed9Sopenharmony_ci "//foo/bar" => "//foo/bar" (already absolute) 29086d528ed9Sopenharmony_ci "/usr/include" => "/usr/include" (already absolute) 29096d528ed9Sopenharmony_ci 29106d528ed9Sopenharmony_ci If you want to make the path relative to another directory, or to be 29116d528ed9Sopenharmony_ci system-absolute, see rebase_path(). 29126d528ed9Sopenharmony_ci``` 29136d528ed9Sopenharmony_ci 29146d528ed9Sopenharmony_ci#### **Examples** 29156d528ed9Sopenharmony_ci``` 29166d528ed9Sopenharmony_ci sources = [ "foo.cc", "foo.h" ] 29176d528ed9Sopenharmony_ci result = get_path_info(source, "abspath") 29186d528ed9Sopenharmony_ci # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ] 29196d528ed9Sopenharmony_ci 29206d528ed9Sopenharmony_ci result = get_path_info("//foo/bar/baz.cc", "dir") 29216d528ed9Sopenharmony_ci # result will be "//foo/bar" 29226d528ed9Sopenharmony_ci 29236d528ed9Sopenharmony_ci # Extract the source-absolute directory name, 29246d528ed9Sopenharmony_ci result = get_path_info(get_path_info(path, "dir"), "abspath") 29256d528ed9Sopenharmony_ci``` 29266d528ed9Sopenharmony_ci### <a name="func_get_target_outputs"></a>**get_target_outputs**: [file list] Get the list of outputs from a target. 29276d528ed9Sopenharmony_ci 29286d528ed9Sopenharmony_ci``` 29296d528ed9Sopenharmony_ci get_target_outputs(target_label) 29306d528ed9Sopenharmony_ci 29316d528ed9Sopenharmony_ci Returns a list of output files for the named target. The named target must 29326d528ed9Sopenharmony_ci have been previously defined in the current file before this function is 29336d528ed9Sopenharmony_ci called (it can't reference targets in other files because there isn't a 29346d528ed9Sopenharmony_ci defined execution order, and it obviously can't reference targets that are 29356d528ed9Sopenharmony_ci defined after the function call). 29366d528ed9Sopenharmony_ci 29376d528ed9Sopenharmony_ci Only copy, generated_file, and action targets are supported. The outputs from 29386d528ed9Sopenharmony_ci binary targets will depend on the toolchain definition which won't 29396d528ed9Sopenharmony_ci necessarily have been loaded by the time a given line of code has run, and 29406d528ed9Sopenharmony_ci source sets and groups have no useful output file. 29416d528ed9Sopenharmony_ci``` 29426d528ed9Sopenharmony_ci 29436d528ed9Sopenharmony_ci#### **Return value** 29446d528ed9Sopenharmony_ci 29456d528ed9Sopenharmony_ci``` 29466d528ed9Sopenharmony_ci The names in the resulting list will be absolute file paths (normally like 29476d528ed9Sopenharmony_ci "//out/Debug/bar.exe", depending on the build directory). 29486d528ed9Sopenharmony_ci 29496d528ed9Sopenharmony_ci action, copy, and generated_file targets: this will just return the files 29506d528ed9Sopenharmony_ci specified in the "outputs" variable of the target. 29516d528ed9Sopenharmony_ci 29526d528ed9Sopenharmony_ci action_foreach targets: this will return the result of applying the output 29536d528ed9Sopenharmony_ci template to the sources (see "gn help source_expansion"). This will be the 29546d528ed9Sopenharmony_ci same result (though with guaranteed absolute file paths), as 29556d528ed9Sopenharmony_ci process_file_template will return for those inputs (see "gn help 29566d528ed9Sopenharmony_ci process_file_template"). 29576d528ed9Sopenharmony_ci 29586d528ed9Sopenharmony_ci source sets and groups: this will return a list containing the path of the 29596d528ed9Sopenharmony_ci "stamp" file that Ninja will produce once all outputs are generated. This 29606d528ed9Sopenharmony_ci probably isn't very useful. 29616d528ed9Sopenharmony_ci``` 29626d528ed9Sopenharmony_ci 29636d528ed9Sopenharmony_ci#### **Example** 29646d528ed9Sopenharmony_ci 29656d528ed9Sopenharmony_ci``` 29666d528ed9Sopenharmony_ci # Say this action generates a bunch of C source files. 29676d528ed9Sopenharmony_ci action_foreach("my_action") { 29686d528ed9Sopenharmony_ci sources = [ ... ] 29696d528ed9Sopenharmony_ci outputs = [ ... ] 29706d528ed9Sopenharmony_ci } 29716d528ed9Sopenharmony_ci 29726d528ed9Sopenharmony_ci # Compile the resulting source files into a source set. 29736d528ed9Sopenharmony_ci source_set("my_lib") { 29746d528ed9Sopenharmony_ci sources = get_target_outputs(":my_action") 29756d528ed9Sopenharmony_ci } 29766d528ed9Sopenharmony_ci``` 29776d528ed9Sopenharmony_ci### <a name="func_getenv"></a>**getenv**: Get an environment variable. 29786d528ed9Sopenharmony_ci 29796d528ed9Sopenharmony_ci``` 29806d528ed9Sopenharmony_ci value = getenv(env_var_name) 29816d528ed9Sopenharmony_ci 29826d528ed9Sopenharmony_ci Returns the value of the given environment variable. If the value is not 29836d528ed9Sopenharmony_ci found, it will try to look up the variable with the "opposite" case (based on 29846d528ed9Sopenharmony_ci the case of the first letter of the variable), but is otherwise 29856d528ed9Sopenharmony_ci case-sensitive. 29866d528ed9Sopenharmony_ci 29876d528ed9Sopenharmony_ci If the environment variable is not found, the empty string will be returned. 29886d528ed9Sopenharmony_ci Note: it might be nice to extend this if we had the concept of "none" in the 29896d528ed9Sopenharmony_ci language to indicate lookup failure. 29906d528ed9Sopenharmony_ci``` 29916d528ed9Sopenharmony_ci 29926d528ed9Sopenharmony_ci#### **Example** 29936d528ed9Sopenharmony_ci 29946d528ed9Sopenharmony_ci``` 29956d528ed9Sopenharmony_ci home_dir = getenv("HOME") 29966d528ed9Sopenharmony_ci``` 29976d528ed9Sopenharmony_ci### <a name="func_import"></a>**import**: Import a file into the current scope. 29986d528ed9Sopenharmony_ci 29996d528ed9Sopenharmony_ci``` 30006d528ed9Sopenharmony_ci The import command loads the rules and variables resulting from executing the 30016d528ed9Sopenharmony_ci given file into the current scope. 30026d528ed9Sopenharmony_ci 30036d528ed9Sopenharmony_ci By convention, imported files are named with a .gni extension. 30046d528ed9Sopenharmony_ci 30056d528ed9Sopenharmony_ci An import is different than a C++ "include". The imported file is executed in 30066d528ed9Sopenharmony_ci a standalone environment from the caller of the import command. The results 30076d528ed9Sopenharmony_ci of this execution are cached for other files that import the same .gni file. 30086d528ed9Sopenharmony_ci 30096d528ed9Sopenharmony_ci Note that you can not import a BUILD.gn file that's otherwise used in the 30106d528ed9Sopenharmony_ci build. Files must either be imported or implicitly loaded as a result of deps 30116d528ed9Sopenharmony_ci rules, but not both. 30126d528ed9Sopenharmony_ci 30136d528ed9Sopenharmony_ci The imported file's scope will be merged with the scope at the point import 30146d528ed9Sopenharmony_ci was called. If there is a conflict (both the current scope and the imported 30156d528ed9Sopenharmony_ci file define some variable or rule with the same name but different value), a 30166d528ed9Sopenharmony_ci runtime error will be thrown. Therefore, it's good practice to minimize the 30176d528ed9Sopenharmony_ci stuff that an imported file defines. 30186d528ed9Sopenharmony_ci 30196d528ed9Sopenharmony_ci Variables and templates beginning with an underscore '_' are considered 30206d528ed9Sopenharmony_ci private and will not be imported. Imported files can use such variables for 30216d528ed9Sopenharmony_ci internal computation without affecting other files. 30226d528ed9Sopenharmony_ci``` 30236d528ed9Sopenharmony_ci 30246d528ed9Sopenharmony_ci#### **Examples** 30256d528ed9Sopenharmony_ci 30266d528ed9Sopenharmony_ci``` 30276d528ed9Sopenharmony_ci import("//build/rules/idl_compilation_rule.gni") 30286d528ed9Sopenharmony_ci 30296d528ed9Sopenharmony_ci # Looks in the current directory. 30306d528ed9Sopenharmony_ci import("my_vars.gni") 30316d528ed9Sopenharmony_ci``` 30326d528ed9Sopenharmony_ci### <a name="func_label_matches"></a>**label_matches**: Returns true if the label matches any of a set of patterns. 30336d528ed9Sopenharmony_ci 30346d528ed9Sopenharmony_ci``` 30356d528ed9Sopenharmony_ci label_matches(target_label, patterns) 30366d528ed9Sopenharmony_ci 30376d528ed9Sopenharmony_ci The argument patterns must be a list of label patterns (see 30386d528ed9Sopenharmony_ci "gn help label_pattern"). If the target_label matches any of the patterns, 30396d528ed9Sopenharmony_ci the function returns the value true. 30406d528ed9Sopenharmony_ci``` 30416d528ed9Sopenharmony_ci 30426d528ed9Sopenharmony_ci#### **Examples** 30436d528ed9Sopenharmony_ci``` 30446d528ed9Sopenharmony_ci result = label_matches("//baz:bar", [ "//foo/bar/*", "//baz:*" ]) 30456d528ed9Sopenharmony_ci # result will be true 30466d528ed9Sopenharmony_ci``` 30476d528ed9Sopenharmony_ci### <a name="func_not_needed"></a>**not_needed**: Mark variables from scope as not needed. 30486d528ed9Sopenharmony_ci 30496d528ed9Sopenharmony_ci``` 30506d528ed9Sopenharmony_ci not_needed(variable_list_or_star, variable_to_ignore_list = []) 30516d528ed9Sopenharmony_ci not_needed(from_scope, variable_list_or_star, 30526d528ed9Sopenharmony_ci variable_to_ignore_list = []) 30536d528ed9Sopenharmony_ci 30546d528ed9Sopenharmony_ci Mark the variables in the current or given scope as not needed, which means 30556d528ed9Sopenharmony_ci you will not get an error about unused variables for these. The 30566d528ed9Sopenharmony_ci variable_to_ignore_list allows excluding variables from "all matches" if 30576d528ed9Sopenharmony_ci variable_list_or_star is "*". 30586d528ed9Sopenharmony_ci``` 30596d528ed9Sopenharmony_ci 30606d528ed9Sopenharmony_ci#### **Example** 30616d528ed9Sopenharmony_ci 30626d528ed9Sopenharmony_ci``` 30636d528ed9Sopenharmony_ci not_needed("*", [ "config" ]) 30646d528ed9Sopenharmony_ci not_needed([ "data_deps", "deps" ]) 30656d528ed9Sopenharmony_ci not_needed(invoker, "*", [ "config" ]) 30666d528ed9Sopenharmony_ci not_needed(invoker, [ "data_deps", "deps" ]) 30676d528ed9Sopenharmony_ci``` 30686d528ed9Sopenharmony_ci### <a name="func_pool"></a>**pool**: Defines a pool object. 30696d528ed9Sopenharmony_ci 30706d528ed9Sopenharmony_ci``` 30716d528ed9Sopenharmony_ci Pool objects can be applied to a tool to limit the parallelism of the 30726d528ed9Sopenharmony_ci build. This object has a single property "depth" corresponding to 30736d528ed9Sopenharmony_ci the number of tasks that may run simultaneously. 30746d528ed9Sopenharmony_ci 30756d528ed9Sopenharmony_ci As the file containing the pool definition may be executed in the 30766d528ed9Sopenharmony_ci context of more than one toolchain it is recommended to specify an 30776d528ed9Sopenharmony_ci explicit toolchain when defining and referencing a pool. 30786d528ed9Sopenharmony_ci 30796d528ed9Sopenharmony_ci A pool named "console" defined in the root build file represents Ninja's 30806d528ed9Sopenharmony_ci console pool. Targets using this pool will have access to the console's 30816d528ed9Sopenharmony_ci stdin and stdout, and output will not be buffered. This special pool must 30826d528ed9Sopenharmony_ci have a depth of 1. Pools not defined in the root must not be named "console". 30836d528ed9Sopenharmony_ci The console pool can only be defined for the default toolchain. 30846d528ed9Sopenharmony_ci Refer to the Ninja documentation on the console pool for more info. 30856d528ed9Sopenharmony_ci 30866d528ed9Sopenharmony_ci A pool is referenced by its label just like a target. 30876d528ed9Sopenharmony_ci``` 30886d528ed9Sopenharmony_ci 30896d528ed9Sopenharmony_ci#### **Variables** 30906d528ed9Sopenharmony_ci 30916d528ed9Sopenharmony_ci``` 30926d528ed9Sopenharmony_ci depth* 30936d528ed9Sopenharmony_ci * = required 30946d528ed9Sopenharmony_ci``` 30956d528ed9Sopenharmony_ci 30966d528ed9Sopenharmony_ci#### **Example** 30976d528ed9Sopenharmony_ci 30986d528ed9Sopenharmony_ci``` 30996d528ed9Sopenharmony_ci if (current_toolchain == default_toolchain) { 31006d528ed9Sopenharmony_ci pool("link_pool") { 31016d528ed9Sopenharmony_ci depth = 1 31026d528ed9Sopenharmony_ci } 31036d528ed9Sopenharmony_ci } 31046d528ed9Sopenharmony_ci 31056d528ed9Sopenharmony_ci toolchain("toolchain") { 31066d528ed9Sopenharmony_ci tool("link") { 31076d528ed9Sopenharmony_ci command = "..." 31086d528ed9Sopenharmony_ci pool = ":link_pool($default_toolchain)" 31096d528ed9Sopenharmony_ci } 31106d528ed9Sopenharmony_ci } 31116d528ed9Sopenharmony_ci``` 31126d528ed9Sopenharmony_ci### <a name="func_print"></a>**print**: Prints to the console. 31136d528ed9Sopenharmony_ci 31146d528ed9Sopenharmony_ci``` 31156d528ed9Sopenharmony_ci Prints all arguments to the console separated by spaces. A newline is 31166d528ed9Sopenharmony_ci automatically appended to the end. 31176d528ed9Sopenharmony_ci 31186d528ed9Sopenharmony_ci This function is intended for debugging. Note that build files are run in 31196d528ed9Sopenharmony_ci parallel so you may get interleaved prints. A buildfile may also be executed 31206d528ed9Sopenharmony_ci more than once in parallel in the context of different toolchains so the 31216d528ed9Sopenharmony_ci prints from one file may be duplicated or 31226d528ed9Sopenharmony_ci interleaved with itself. 31236d528ed9Sopenharmony_ci``` 31246d528ed9Sopenharmony_ci 31256d528ed9Sopenharmony_ci#### **Examples** 31266d528ed9Sopenharmony_ci 31276d528ed9Sopenharmony_ci``` 31286d528ed9Sopenharmony_ci print("Hello world") 31296d528ed9Sopenharmony_ci 31306d528ed9Sopenharmony_ci print(sources, deps) 31316d528ed9Sopenharmony_ci``` 31326d528ed9Sopenharmony_ci### <a name="func_print_stack_trace"></a>**print_stack_trace**: Prints a stack trace. 31336d528ed9Sopenharmony_ci 31346d528ed9Sopenharmony_ci``` 31356d528ed9Sopenharmony_ci Prints the current file location, and all template invocations that led up to 31366d528ed9Sopenharmony_ci this location, to the console. 31376d528ed9Sopenharmony_ci``` 31386d528ed9Sopenharmony_ci 31396d528ed9Sopenharmony_ci#### **Examples** 31406d528ed9Sopenharmony_ci 31416d528ed9Sopenharmony_ci``` 31426d528ed9Sopenharmony_ci template("foo"){ 31436d528ed9Sopenharmony_ci print_stack_trace() 31446d528ed9Sopenharmony_ci } 31456d528ed9Sopenharmony_ci template("bar"){ 31466d528ed9Sopenharmony_ci foo(target_name + ".foo") { 31476d528ed9Sopenharmony_ci baz = invoker.baz 31486d528ed9Sopenharmony_ci } 31496d528ed9Sopenharmony_ci } 31506d528ed9Sopenharmony_ci bar("lala") { 31516d528ed9Sopenharmony_ci baz = 42 31526d528ed9Sopenharmony_ci } 31536d528ed9Sopenharmony_ci 31546d528ed9Sopenharmony_ci will print out the following: 31556d528ed9Sopenharmony_ci 31566d528ed9Sopenharmony_ci print_stack_trace() initiated at //build.gn:2 31576d528ed9Sopenharmony_ci bar("lala") //BUILD.gn:9 31586d528ed9Sopenharmony_ci foo("lala.foo") //BUILD.gn:5 31596d528ed9Sopenharmony_ci print_stack_trace() //BUILD.gn:2 31606d528ed9Sopenharmony_ci``` 31616d528ed9Sopenharmony_ci### <a name="func_process_file_template"></a>**process_file_template**: Do template expansion over a list of files. 31626d528ed9Sopenharmony_ci 31636d528ed9Sopenharmony_ci``` 31646d528ed9Sopenharmony_ci process_file_template(source_list, template) 31656d528ed9Sopenharmony_ci 31666d528ed9Sopenharmony_ci process_file_template applies a template list to a source file list, 31676d528ed9Sopenharmony_ci returning the result of applying each template to each source. This is 31686d528ed9Sopenharmony_ci typically used for computing output file names from input files. 31696d528ed9Sopenharmony_ci 31706d528ed9Sopenharmony_ci In most cases, get_target_outputs() will give the same result with shorter, 31716d528ed9Sopenharmony_ci more maintainable code. This function should only be used when that function 31726d528ed9Sopenharmony_ci can't be used (like there's no target or the target is defined in another 31736d528ed9Sopenharmony_ci build file). 31746d528ed9Sopenharmony_ci``` 31756d528ed9Sopenharmony_ci 31766d528ed9Sopenharmony_ci#### **Arguments** 31776d528ed9Sopenharmony_ci 31786d528ed9Sopenharmony_ci``` 31796d528ed9Sopenharmony_ci The source_list is a list of file names. 31806d528ed9Sopenharmony_ci 31816d528ed9Sopenharmony_ci The template can be a string or a list. If it is a list, multiple output 31826d528ed9Sopenharmony_ci strings are generated for each input. 31836d528ed9Sopenharmony_ci 31846d528ed9Sopenharmony_ci The template should contain source expansions to which each name in the 31856d528ed9Sopenharmony_ci source list is applied. See "gn help source_expansion". 31866d528ed9Sopenharmony_ci``` 31876d528ed9Sopenharmony_ci 31886d528ed9Sopenharmony_ci#### **Example** 31896d528ed9Sopenharmony_ci 31906d528ed9Sopenharmony_ci``` 31916d528ed9Sopenharmony_ci sources = [ 31926d528ed9Sopenharmony_ci "foo.idl", 31936d528ed9Sopenharmony_ci "bar.idl", 31946d528ed9Sopenharmony_ci ] 31956d528ed9Sopenharmony_ci myoutputs = process_file_template( 31966d528ed9Sopenharmony_ci sources, 31976d528ed9Sopenharmony_ci [ "$target_gen_dir/{{source_name_part}}.cc", 31986d528ed9Sopenharmony_ci "$target_gen_dir/{{source_name_part}}.h" ]) 31996d528ed9Sopenharmony_ci 32006d528ed9Sopenharmony_ci The result in this case will be: 32016d528ed9Sopenharmony_ci [ "//out/Debug/foo.cc" 32026d528ed9Sopenharmony_ci "//out/Debug/foo.h" 32036d528ed9Sopenharmony_ci "//out/Debug/bar.cc" 32046d528ed9Sopenharmony_ci "//out/Debug/bar.h" ] 32056d528ed9Sopenharmony_ci``` 32066d528ed9Sopenharmony_ci### <a name="func_read_file"></a>**read_file**: Read a file into a variable. 32076d528ed9Sopenharmony_ci 32086d528ed9Sopenharmony_ci``` 32096d528ed9Sopenharmony_ci read_file(filename, input_conversion) 32106d528ed9Sopenharmony_ci 32116d528ed9Sopenharmony_ci Whitespace will be trimmed from the end of the file. Throws an error if the 32126d528ed9Sopenharmony_ci file can not be opened. 32136d528ed9Sopenharmony_ci``` 32146d528ed9Sopenharmony_ci 32156d528ed9Sopenharmony_ci#### **Arguments** 32166d528ed9Sopenharmony_ci 32176d528ed9Sopenharmony_ci``` 32186d528ed9Sopenharmony_ci filename 32196d528ed9Sopenharmony_ci Filename to read, relative to the build file. 32206d528ed9Sopenharmony_ci 32216d528ed9Sopenharmony_ci input_conversion 32226d528ed9Sopenharmony_ci Controls how the file is read and parsed. See `gn help io_conversion`. 32236d528ed9Sopenharmony_ci``` 32246d528ed9Sopenharmony_ci 32256d528ed9Sopenharmony_ci#### **Example** 32266d528ed9Sopenharmony_ci 32276d528ed9Sopenharmony_ci``` 32286d528ed9Sopenharmony_ci lines = read_file("foo.txt", "list lines") 32296d528ed9Sopenharmony_ci``` 32306d528ed9Sopenharmony_ci### <a name="func_rebase_path"></a>**rebase_path**: Rebase a file or directory to another location. 32316d528ed9Sopenharmony_ci 32326d528ed9Sopenharmony_ci``` 32336d528ed9Sopenharmony_ci converted = rebase_path(input, 32346d528ed9Sopenharmony_ci new_base = "", 32356d528ed9Sopenharmony_ci current_base = ".") 32366d528ed9Sopenharmony_ci 32376d528ed9Sopenharmony_ci Takes a string argument representing a file name, or a list of such strings 32386d528ed9Sopenharmony_ci and converts it/them to be relative to a different base directory. 32396d528ed9Sopenharmony_ci 32406d528ed9Sopenharmony_ci When invoking the compiler or scripts, GN will automatically convert sources 32416d528ed9Sopenharmony_ci and include directories to be relative to the build directory. However, if 32426d528ed9Sopenharmony_ci you're passing files directly in the "args" array or doing other manual 32436d528ed9Sopenharmony_ci manipulations where GN doesn't know something is a file name, you will need 32446d528ed9Sopenharmony_ci to convert paths to be relative to what your tool is expecting. 32456d528ed9Sopenharmony_ci 32466d528ed9Sopenharmony_ci The common case is to use this to convert paths relative to the current 32476d528ed9Sopenharmony_ci directory to be relative to the build directory (which will be the current 32486d528ed9Sopenharmony_ci directory when executing scripts). 32496d528ed9Sopenharmony_ci 32506d528ed9Sopenharmony_ci If you want to convert a file path to be source-absolute (that is, beginning 32516d528ed9Sopenharmony_ci with a double slash like "//foo/bar"), you should use the get_path_info() 32526d528ed9Sopenharmony_ci function. This function won't work because it will always make relative 32536d528ed9Sopenharmony_ci paths, and it needs to support making paths relative to the source root, so 32546d528ed9Sopenharmony_ci it can't also generate source-absolute paths without more special-cases. 32556d528ed9Sopenharmony_ci``` 32566d528ed9Sopenharmony_ci 32576d528ed9Sopenharmony_ci#### **Arguments** 32586d528ed9Sopenharmony_ci 32596d528ed9Sopenharmony_ci``` 32606d528ed9Sopenharmony_ci input 32616d528ed9Sopenharmony_ci A string or list of strings representing file or directory names. These 32626d528ed9Sopenharmony_ci can be relative paths ("foo/bar.txt"), system absolute paths 32636d528ed9Sopenharmony_ci ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt"). 32646d528ed9Sopenharmony_ci 32656d528ed9Sopenharmony_ci new_base 32666d528ed9Sopenharmony_ci The directory to convert the paths to be relative to. This can be an 32676d528ed9Sopenharmony_ci absolute path or a relative path (which will be treated as being relative 32686d528ed9Sopenharmony_ci to the current BUILD-file's directory). 32696d528ed9Sopenharmony_ci 32706d528ed9Sopenharmony_ci As a special case, if new_base is the empty string (the default), all 32716d528ed9Sopenharmony_ci paths will be converted to system-absolute native style paths with system 32726d528ed9Sopenharmony_ci path separators. This is useful for invoking external programs. 32736d528ed9Sopenharmony_ci 32746d528ed9Sopenharmony_ci current_base 32756d528ed9Sopenharmony_ci Directory representing the base for relative paths in the input. If this 32766d528ed9Sopenharmony_ci is not an absolute path, it will be treated as being relative to the 32776d528ed9Sopenharmony_ci current build file. Use "." (the default) to convert paths from the 32786d528ed9Sopenharmony_ci current BUILD-file's directory. 32796d528ed9Sopenharmony_ci``` 32806d528ed9Sopenharmony_ci 32816d528ed9Sopenharmony_ci#### **Return value** 32826d528ed9Sopenharmony_ci 32836d528ed9Sopenharmony_ci``` 32846d528ed9Sopenharmony_ci The return value will be the same type as the input value (either a string or 32856d528ed9Sopenharmony_ci a list of strings). All relative and source-absolute file names will be 32866d528ed9Sopenharmony_ci converted to be relative to the requested output System-absolute paths will 32876d528ed9Sopenharmony_ci be unchanged. 32886d528ed9Sopenharmony_ci 32896d528ed9Sopenharmony_ci Whether an output path will end in a slash will match whether the 32906d528ed9Sopenharmony_ci corresponding input path ends in a slash. It will return "." or "./" 32916d528ed9Sopenharmony_ci (depending on whether the input ends in a slash) to avoid returning empty 32926d528ed9Sopenharmony_ci strings. This means if you want a root path ("//" or "/") not ending in a 32936d528ed9Sopenharmony_ci slash, you can add a dot ("//."). 32946d528ed9Sopenharmony_ci``` 32956d528ed9Sopenharmony_ci 32966d528ed9Sopenharmony_ci#### **Example** 32976d528ed9Sopenharmony_ci 32986d528ed9Sopenharmony_ci``` 32996d528ed9Sopenharmony_ci # Convert a file in the current directory to be relative to the build 33006d528ed9Sopenharmony_ci # directory (the current dir when executing compilers and scripts). 33016d528ed9Sopenharmony_ci foo = rebase_path("myfile.txt", root_build_dir) 33026d528ed9Sopenharmony_ci # might produce "../../project/myfile.txt". 33036d528ed9Sopenharmony_ci 33046d528ed9Sopenharmony_ci # Convert a file to be system absolute: 33056d528ed9Sopenharmony_ci foo = rebase_path("myfile.txt") 33066d528ed9Sopenharmony_ci # Might produce "D:\\source\\project\\myfile.txt" on Windows or 33076d528ed9Sopenharmony_ci # "/home/you/source/project/myfile.txt" on Linux. 33086d528ed9Sopenharmony_ci 33096d528ed9Sopenharmony_ci # Typical usage for converting to the build directory for a script. 33106d528ed9Sopenharmony_ci action("myscript") { 33116d528ed9Sopenharmony_ci # Don't convert sources, GN will automatically convert these to be relative 33126d528ed9Sopenharmony_ci # to the build directory when it constructs the command line for your 33136d528ed9Sopenharmony_ci # script. 33146d528ed9Sopenharmony_ci sources = [ "foo.txt", "bar.txt" ] 33156d528ed9Sopenharmony_ci 33166d528ed9Sopenharmony_ci # Extra file args passed manually need to be explicitly converted 33176d528ed9Sopenharmony_ci # to be relative to the build directory: 33186d528ed9Sopenharmony_ci args = [ 33196d528ed9Sopenharmony_ci "--data", 33206d528ed9Sopenharmony_ci rebase_path("//mything/data/input.dat", root_build_dir), 33216d528ed9Sopenharmony_ci "--rel", 33226d528ed9Sopenharmony_ci rebase_path("relative_path.txt", root_build_dir) 33236d528ed9Sopenharmony_ci ] + rebase_path(sources, root_build_dir) 33246d528ed9Sopenharmony_ci } 33256d528ed9Sopenharmony_ci``` 33266d528ed9Sopenharmony_ci### <a name="func_set_default_toolchain"></a>**set_default_toolchain**: Sets the default toolchain name. 33276d528ed9Sopenharmony_ci 33286d528ed9Sopenharmony_ci``` 33296d528ed9Sopenharmony_ci set_default_toolchain(toolchain_label) 33306d528ed9Sopenharmony_ci 33316d528ed9Sopenharmony_ci The given label should identify a toolchain definition (see "gn help 33326d528ed9Sopenharmony_ci toolchain"). This toolchain will be used for all targets unless otherwise 33336d528ed9Sopenharmony_ci specified. 33346d528ed9Sopenharmony_ci 33356d528ed9Sopenharmony_ci This function is only valid to call during the processing of the build 33366d528ed9Sopenharmony_ci configuration file. Since the build configuration file is processed 33376d528ed9Sopenharmony_ci separately for each toolchain, this function will be a no-op when called 33386d528ed9Sopenharmony_ci under any non-default toolchains. 33396d528ed9Sopenharmony_ci 33406d528ed9Sopenharmony_ci For example, the default toolchain should be appropriate for the current 33416d528ed9Sopenharmony_ci environment. If the current environment is 32-bit and somebody references a 33426d528ed9Sopenharmony_ci target with a 64-bit toolchain, we wouldn't want processing of the build 33436d528ed9Sopenharmony_ci config file for the 64-bit toolchain to reset the default toolchain to 33446d528ed9Sopenharmony_ci 64-bit, we want to keep it 32-bits. 33456d528ed9Sopenharmony_ci``` 33466d528ed9Sopenharmony_ci 33476d528ed9Sopenharmony_ci#### **Argument** 33486d528ed9Sopenharmony_ci 33496d528ed9Sopenharmony_ci``` 33506d528ed9Sopenharmony_ci toolchain_label 33516d528ed9Sopenharmony_ci Toolchain name. 33526d528ed9Sopenharmony_ci``` 33536d528ed9Sopenharmony_ci 33546d528ed9Sopenharmony_ci#### **Example** 33556d528ed9Sopenharmony_ci 33566d528ed9Sopenharmony_ci``` 33576d528ed9Sopenharmony_ci # Set default toolchain only has an effect when run in the context of the 33586d528ed9Sopenharmony_ci # default toolchain. Pick the right one according to the current CPU 33596d528ed9Sopenharmony_ci # architecture. 33606d528ed9Sopenharmony_ci if (target_cpu == "x64") { 33616d528ed9Sopenharmony_ci set_default_toolchain("//toolchains:64") 33626d528ed9Sopenharmony_ci } else if (target_cpu == "x86") { 33636d528ed9Sopenharmony_ci set_default_toolchain("//toolchains:32") 33646d528ed9Sopenharmony_ci } 33656d528ed9Sopenharmony_ci``` 33666d528ed9Sopenharmony_ci### <a name="func_set_defaults"></a>**set_defaults**: Set default values for a target type. 33676d528ed9Sopenharmony_ci 33686d528ed9Sopenharmony_ci``` 33696d528ed9Sopenharmony_ci set_defaults(<target_type_name>) { <values...> } 33706d528ed9Sopenharmony_ci 33716d528ed9Sopenharmony_ci Sets the default values for a given target type. Whenever target_type_name is 33726d528ed9Sopenharmony_ci seen in the future, the values specified in set_default's block will be 33736d528ed9Sopenharmony_ci copied into the current scope. 33746d528ed9Sopenharmony_ci 33756d528ed9Sopenharmony_ci When the target type is used, the variable copying is very strict. If a 33766d528ed9Sopenharmony_ci variable with that name is already in scope, the build will fail with an 33776d528ed9Sopenharmony_ci error. 33786d528ed9Sopenharmony_ci 33796d528ed9Sopenharmony_ci set_defaults can be used for built-in target types ("executable", 33806d528ed9Sopenharmony_ci "shared_library", etc.) and custom ones defined via the "template" command. 33816d528ed9Sopenharmony_ci It can be called more than once and the most recent call in any scope will 33826d528ed9Sopenharmony_ci apply, but there is no way to refer to the previous defaults and modify them 33836d528ed9Sopenharmony_ci (each call to set_defaults must supply a complete list of all defaults it 33846d528ed9Sopenharmony_ci wants). If you want to share defaults, store them in a separate variable. 33856d528ed9Sopenharmony_ci``` 33866d528ed9Sopenharmony_ci 33876d528ed9Sopenharmony_ci#### **Example** 33886d528ed9Sopenharmony_ci 33896d528ed9Sopenharmony_ci``` 33906d528ed9Sopenharmony_ci set_defaults("static_library") { 33916d528ed9Sopenharmony_ci configs = [ "//tools/mything:settings" ] 33926d528ed9Sopenharmony_ci } 33936d528ed9Sopenharmony_ci 33946d528ed9Sopenharmony_ci static_library("mylib") { 33956d528ed9Sopenharmony_ci # The configs will be auto-populated as above. You can remove it if 33966d528ed9Sopenharmony_ci # you don't want the default for a particular default: 33976d528ed9Sopenharmony_ci configs -= [ "//tools/mything:settings" ] 33986d528ed9Sopenharmony_ci } 33996d528ed9Sopenharmony_ci``` 34006d528ed9Sopenharmony_ci### <a name="func_split_list"></a>**split_list**: Splits a list into N different sub-lists. 34016d528ed9Sopenharmony_ci 34026d528ed9Sopenharmony_ci``` 34036d528ed9Sopenharmony_ci result = split_list(input, n) 34046d528ed9Sopenharmony_ci 34056d528ed9Sopenharmony_ci Given a list and a number N, splits the list into N sub-lists of 34066d528ed9Sopenharmony_ci approximately equal size. The return value is a list of the sub-lists. The 34076d528ed9Sopenharmony_ci result will always be a list of size N. If N is greater than the number of 34086d528ed9Sopenharmony_ci elements in the input, it will be padded with empty lists. 34096d528ed9Sopenharmony_ci 34106d528ed9Sopenharmony_ci The expected use is to divide source files into smaller uniform chunks. 34116d528ed9Sopenharmony_ci``` 34126d528ed9Sopenharmony_ci 34136d528ed9Sopenharmony_ci#### **Example** 34146d528ed9Sopenharmony_ci 34156d528ed9Sopenharmony_ci``` 34166d528ed9Sopenharmony_ci The code: 34176d528ed9Sopenharmony_ci mylist = [1, 2, 3, 4, 5, 6] 34186d528ed9Sopenharmony_ci print(split_list(mylist, 3)) 34196d528ed9Sopenharmony_ci 34206d528ed9Sopenharmony_ci Will print: 34216d528ed9Sopenharmony_ci [[1, 2], [3, 4], [5, 6] 34226d528ed9Sopenharmony_ci``` 34236d528ed9Sopenharmony_ci### <a name="func_string_join"></a>**string_join**: Concatenates a list of strings with a separator. 34246d528ed9Sopenharmony_ci 34256d528ed9Sopenharmony_ci``` 34266d528ed9Sopenharmony_ci result = string_join(separator, strings) 34276d528ed9Sopenharmony_ci 34286d528ed9Sopenharmony_ci Concatenate a list of strings with intervening occurrences of separator. 34296d528ed9Sopenharmony_ci``` 34306d528ed9Sopenharmony_ci 34316d528ed9Sopenharmony_ci#### **Examples** 34326d528ed9Sopenharmony_ci 34336d528ed9Sopenharmony_ci``` 34346d528ed9Sopenharmony_ci string_join("", ["a", "b", "c"]) --> "abc" 34356d528ed9Sopenharmony_ci string_join("|", ["a", "b", "c"]) --> "a|b|c" 34366d528ed9Sopenharmony_ci string_join(", ", ["a", "b", "c"]) --> "a, b, c" 34376d528ed9Sopenharmony_ci string_join("s", ["", ""]) --> "s" 34386d528ed9Sopenharmony_ci``` 34396d528ed9Sopenharmony_ci### <a name="func_string_replace"></a>**string_replace**: Replaces substring in the given string. 34406d528ed9Sopenharmony_ci 34416d528ed9Sopenharmony_ci``` 34426d528ed9Sopenharmony_ci result = string_replace(str, old, new[, max]) 34436d528ed9Sopenharmony_ci 34446d528ed9Sopenharmony_ci Returns a copy of the string str in which the occurrences of old have been 34456d528ed9Sopenharmony_ci replaced with new, optionally restricting the number of replacements. The 34466d528ed9Sopenharmony_ci replacement is performed sequentially, so if new contains old, it won't be 34476d528ed9Sopenharmony_ci replaced. 34486d528ed9Sopenharmony_ci``` 34496d528ed9Sopenharmony_ci 34506d528ed9Sopenharmony_ci#### **Example** 34516d528ed9Sopenharmony_ci 34526d528ed9Sopenharmony_ci``` 34536d528ed9Sopenharmony_ci The code: 34546d528ed9Sopenharmony_ci mystr = "Hello, world!" 34556d528ed9Sopenharmony_ci print(string_replace(mystr, "world", "GN")) 34566d528ed9Sopenharmony_ci 34576d528ed9Sopenharmony_ci Will print: 34586d528ed9Sopenharmony_ci Hello, GN! 34596d528ed9Sopenharmony_ci``` 34606d528ed9Sopenharmony_ci### <a name="func_string_split"></a>**string_split**: Split string into a list of strings. 34616d528ed9Sopenharmony_ci 34626d528ed9Sopenharmony_ci``` 34636d528ed9Sopenharmony_ci result = string_split(str[, sep]) 34646d528ed9Sopenharmony_ci 34656d528ed9Sopenharmony_ci Split string into all substrings separated by separator and returns a list 34666d528ed9Sopenharmony_ci of the substrings between those separators. 34676d528ed9Sopenharmony_ci 34686d528ed9Sopenharmony_ci If the separator argument is omitted, the split is by any whitespace, and 34696d528ed9Sopenharmony_ci any leading/trailing whitespace is ignored; similar to Python's str.split(). 34706d528ed9Sopenharmony_ci``` 34716d528ed9Sopenharmony_ci 34726d528ed9Sopenharmony_ci#### **Examples without a separator (split on whitespace)**: 34736d528ed9Sopenharmony_ci 34746d528ed9Sopenharmony_ci``` 34756d528ed9Sopenharmony_ci string_split("") --> [] 34766d528ed9Sopenharmony_ci string_split("a") --> ["a"] 34776d528ed9Sopenharmony_ci string_split(" aa bb") --> ["aa", "bb"] 34786d528ed9Sopenharmony_ci``` 34796d528ed9Sopenharmony_ci 34806d528ed9Sopenharmony_ci#### **Examples with a separator (split on separators)**: 34816d528ed9Sopenharmony_ci 34826d528ed9Sopenharmony_ci``` 34836d528ed9Sopenharmony_ci string_split("", "|") --> [""] 34846d528ed9Sopenharmony_ci string_split(" a b ", " ") --> ["", "", "a", "b", "", ""] 34856d528ed9Sopenharmony_ci string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"] 34866d528ed9Sopenharmony_ci``` 34876d528ed9Sopenharmony_ci### <a name="func_template"></a>**template**: Define a template rule. 34886d528ed9Sopenharmony_ci 34896d528ed9Sopenharmony_ci``` 34906d528ed9Sopenharmony_ci A template defines a custom name that acts like a function. It provides a way 34916d528ed9Sopenharmony_ci to add to the built-in target types. 34926d528ed9Sopenharmony_ci 34936d528ed9Sopenharmony_ci The template() function is used to declare a template. To invoke the 34946d528ed9Sopenharmony_ci template, just use the name of the template like any other target type. 34956d528ed9Sopenharmony_ci 34966d528ed9Sopenharmony_ci Often you will want to declare your template in a special file that other 34976d528ed9Sopenharmony_ci files will import (see "gn help import") so your template rule can be shared 34986d528ed9Sopenharmony_ci across build files. 34996d528ed9Sopenharmony_ci``` 35006d528ed9Sopenharmony_ci 35016d528ed9Sopenharmony_ci#### **Variables and templates**: 35026d528ed9Sopenharmony_ci 35036d528ed9Sopenharmony_ci``` 35046d528ed9Sopenharmony_ci When you call template() it creates a closure around all variables currently 35056d528ed9Sopenharmony_ci in scope with the code in the template block. When the template is invoked, 35066d528ed9Sopenharmony_ci the closure will be executed. 35076d528ed9Sopenharmony_ci 35086d528ed9Sopenharmony_ci When the template is invoked, the code in the caller is executed and passed 35096d528ed9Sopenharmony_ci to the template code as an implicit "invoker" variable. The template uses 35106d528ed9Sopenharmony_ci this to read state out of the invoking code. 35116d528ed9Sopenharmony_ci 35126d528ed9Sopenharmony_ci One thing explicitly excluded from the closure is the "current directory" 35136d528ed9Sopenharmony_ci against which relative file names are resolved. The current directory will be 35146d528ed9Sopenharmony_ci that of the invoking code, since typically that code specifies the file 35156d528ed9Sopenharmony_ci names. This means all files internal to the template should use absolute 35166d528ed9Sopenharmony_ci names. 35176d528ed9Sopenharmony_ci 35186d528ed9Sopenharmony_ci A template will typically forward some or all variables from the invoking 35196d528ed9Sopenharmony_ci scope to a target that it defines. Often, such variables might be optional. 35206d528ed9Sopenharmony_ci Use the pattern: 35216d528ed9Sopenharmony_ci 35226d528ed9Sopenharmony_ci if (defined(invoker.deps)) { 35236d528ed9Sopenharmony_ci deps = invoker.deps 35246d528ed9Sopenharmony_ci } 35256d528ed9Sopenharmony_ci 35266d528ed9Sopenharmony_ci The function forward_variables_from() provides a shortcut to forward one or 35276d528ed9Sopenharmony_ci more or possibly all variables in this manner: 35286d528ed9Sopenharmony_ci 35296d528ed9Sopenharmony_ci forward_variables_from(invoker, ["deps", "public_deps"]) 35306d528ed9Sopenharmony_ci``` 35316d528ed9Sopenharmony_ci 35326d528ed9Sopenharmony_ci#### **Target naming** 35336d528ed9Sopenharmony_ci 35346d528ed9Sopenharmony_ci``` 35356d528ed9Sopenharmony_ci Your template should almost always define a built-in target with the name the 35366d528ed9Sopenharmony_ci template invoker specified. For example, if you have an IDL template and 35376d528ed9Sopenharmony_ci somebody does: 35386d528ed9Sopenharmony_ci idl("foo") {... 35396d528ed9Sopenharmony_ci you will normally want this to expand to something defining a source_set or 35406d528ed9Sopenharmony_ci static_library named "foo" (among other things you may need). This way, when 35416d528ed9Sopenharmony_ci another target specifies a dependency on "foo", the static_library or 35426d528ed9Sopenharmony_ci source_set will be linked. 35436d528ed9Sopenharmony_ci 35446d528ed9Sopenharmony_ci It is also important that any other targets your template expands to have 35456d528ed9Sopenharmony_ci unique names, or you will get collisions. 35466d528ed9Sopenharmony_ci 35476d528ed9Sopenharmony_ci Access the invoking name in your template via the implicit "target_name" 35486d528ed9Sopenharmony_ci variable. This should also be the basis for how other targets that a template 35496d528ed9Sopenharmony_ci expands to ensure uniqueness. 35506d528ed9Sopenharmony_ci 35516d528ed9Sopenharmony_ci A typical example would be a template that defines an action to generate some 35526d528ed9Sopenharmony_ci source files, and a source_set to compile that source. Your template would 35536d528ed9Sopenharmony_ci name the source_set "target_name" because that's what you want external 35546d528ed9Sopenharmony_ci targets to depend on to link your code. And you would name the action 35556d528ed9Sopenharmony_ci something like "${target_name}_action" to make it unique. The source set 35566d528ed9Sopenharmony_ci would have a dependency on the action to make it run. 35576d528ed9Sopenharmony_ci``` 35586d528ed9Sopenharmony_ci 35596d528ed9Sopenharmony_ci#### **Overriding builtin targets** 35606d528ed9Sopenharmony_ci 35616d528ed9Sopenharmony_ci``` 35626d528ed9Sopenharmony_ci You can use template to redefine a built-in target in which case your template 35636d528ed9Sopenharmony_ci takes a precedence over the built-in one. All uses of the target from within 35646d528ed9Sopenharmony_ci the template definition will refer to the built-in target which makes it 35656d528ed9Sopenharmony_ci possible to extend the behavior of the built-in target: 35666d528ed9Sopenharmony_ci 35676d528ed9Sopenharmony_ci template("shared_library") { 35686d528ed9Sopenharmony_ci shared_library(shlib) { 35696d528ed9Sopenharmony_ci forward_variables_from(invoker, "*") 35706d528ed9Sopenharmony_ci ... 35716d528ed9Sopenharmony_ci } 35726d528ed9Sopenharmony_ci } 35736d528ed9Sopenharmony_ci``` 35746d528ed9Sopenharmony_ci 35756d528ed9Sopenharmony_ci#### **Example of defining a template** 35766d528ed9Sopenharmony_ci 35776d528ed9Sopenharmony_ci``` 35786d528ed9Sopenharmony_ci template("my_idl") { 35796d528ed9Sopenharmony_ci # Be nice and help callers debug problems by checking that the variables 35806d528ed9Sopenharmony_ci # the template requires are defined. This gives a nice message rather than 35816d528ed9Sopenharmony_ci # giving the user an error about an undefined variable in the file defining 35826d528ed9Sopenharmony_ci # the template 35836d528ed9Sopenharmony_ci # 35846d528ed9Sopenharmony_ci # You can also use defined() to give default values to variables 35856d528ed9Sopenharmony_ci # unspecified by the invoker. 35866d528ed9Sopenharmony_ci assert(defined(invoker.sources), 35876d528ed9Sopenharmony_ci "Need sources in $target_name listing the idl files.") 35886d528ed9Sopenharmony_ci 35896d528ed9Sopenharmony_ci # Name of the intermediate target that does the code gen. This must 35906d528ed9Sopenharmony_ci # incorporate the target name so it's unique across template 35916d528ed9Sopenharmony_ci # instantiations. 35926d528ed9Sopenharmony_ci code_gen_target_name = target_name + "_code_gen" 35936d528ed9Sopenharmony_ci 35946d528ed9Sopenharmony_ci # Intermediate target to convert IDL to C source. Note that the name is 35956d528ed9Sopenharmony_ci # based on the name the invoker of the template specified. This way, each 35966d528ed9Sopenharmony_ci # time the template is invoked we get a unique intermediate action name 35976d528ed9Sopenharmony_ci # (since all target names are in the global scope). 35986d528ed9Sopenharmony_ci action_foreach(code_gen_target_name) { 35996d528ed9Sopenharmony_ci # Access the scope defined by the invoker via the implicit "invoker" 36006d528ed9Sopenharmony_ci # variable. 36016d528ed9Sopenharmony_ci sources = invoker.sources 36026d528ed9Sopenharmony_ci 36036d528ed9Sopenharmony_ci # Note that we need an absolute path for our script file name. The 36046d528ed9Sopenharmony_ci # current directory when executing this code will be that of the invoker 36056d528ed9Sopenharmony_ci # (this is why we can use the "sources" directly above without having to 36066d528ed9Sopenharmony_ci # rebase all of the paths). But if we need to reference a script relative 36076d528ed9Sopenharmony_ci # to the template file, we'll need to use an absolute path instead. 36086d528ed9Sopenharmony_ci script = "//tools/idl/idl_code_generator.py" 36096d528ed9Sopenharmony_ci 36106d528ed9Sopenharmony_ci # Tell GN how to expand output names given the sources. 36116d528ed9Sopenharmony_ci # See "gn help source_expansion" for more. 36126d528ed9Sopenharmony_ci outputs = [ "$target_gen_dir/{{source_name_part}}.cc", 36136d528ed9Sopenharmony_ci "$target_gen_dir/{{source_name_part}}.h" ] 36146d528ed9Sopenharmony_ci } 36156d528ed9Sopenharmony_ci 36166d528ed9Sopenharmony_ci # Name the source set the same as the template invocation so instancing 36176d528ed9Sopenharmony_ci # this template produces something that other targets can link to in their 36186d528ed9Sopenharmony_ci # deps. 36196d528ed9Sopenharmony_ci source_set(target_name) { 36206d528ed9Sopenharmony_ci # Generates the list of sources, we get these from the action_foreach 36216d528ed9Sopenharmony_ci # above. 36226d528ed9Sopenharmony_ci sources = get_target_outputs(":$code_gen_target_name") 36236d528ed9Sopenharmony_ci 36246d528ed9Sopenharmony_ci # This target depends on the files produced by the above code gen target. 36256d528ed9Sopenharmony_ci deps = [ ":$code_gen_target_name" ] 36266d528ed9Sopenharmony_ci } 36276d528ed9Sopenharmony_ci } 36286d528ed9Sopenharmony_ci``` 36296d528ed9Sopenharmony_ci 36306d528ed9Sopenharmony_ci#### **Example of invoking the resulting template** 36316d528ed9Sopenharmony_ci 36326d528ed9Sopenharmony_ci``` 36336d528ed9Sopenharmony_ci # This calls the template code above, defining target_name to be 36346d528ed9Sopenharmony_ci # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly 36356d528ed9Sopenharmony_ci # brackets. 36366d528ed9Sopenharmony_ci my_idl("foo_idl_files") { 36376d528ed9Sopenharmony_ci # Goes into the template as "invoker.sources". 36386d528ed9Sopenharmony_ci sources = [ "foo.idl", "bar.idl" ] 36396d528ed9Sopenharmony_ci } 36406d528ed9Sopenharmony_ci 36416d528ed9Sopenharmony_ci # Here is a target that depends on our template. 36426d528ed9Sopenharmony_ci executable("my_exe") { 36436d528ed9Sopenharmony_ci # Depend on the name we gave the template call above. Internally, this will 36446d528ed9Sopenharmony_ci # produce a dependency from executable to the source_set inside the 36456d528ed9Sopenharmony_ci # template (since it has this name), which will in turn depend on the code 36466d528ed9Sopenharmony_ci # gen action. 36476d528ed9Sopenharmony_ci deps = [ ":foo_idl_files" ] 36486d528ed9Sopenharmony_ci } 36496d528ed9Sopenharmony_ci``` 36506d528ed9Sopenharmony_ci### <a name="func_tool"></a>**tool**: Specify arguments to a toolchain tool. 36516d528ed9Sopenharmony_ci 36526d528ed9Sopenharmony_ci#### **Usage** 36536d528ed9Sopenharmony_ci 36546d528ed9Sopenharmony_ci``` 36556d528ed9Sopenharmony_ci tool(<tool type>) { 36566d528ed9Sopenharmony_ci <tool variables...> 36576d528ed9Sopenharmony_ci } 36586d528ed9Sopenharmony_ci``` 36596d528ed9Sopenharmony_ci 36606d528ed9Sopenharmony_ci#### **Tool types** 36616d528ed9Sopenharmony_ci 36626d528ed9Sopenharmony_ci``` 36636d528ed9Sopenharmony_ci Compiler tools: 36646d528ed9Sopenharmony_ci "cc": C compiler 36656d528ed9Sopenharmony_ci "cxx": C++ compiler 36666d528ed9Sopenharmony_ci "cxx_module": C++ compiler used for Clang .modulemap files 36676d528ed9Sopenharmony_ci "objc": Objective C compiler 36686d528ed9Sopenharmony_ci "objcxx": Objective C++ compiler 36696d528ed9Sopenharmony_ci "rc": Resource compiler (Windows .rc files) 36706d528ed9Sopenharmony_ci "asm": Assembler 36716d528ed9Sopenharmony_ci "swift": Swift compiler driver 36726d528ed9Sopenharmony_ci 36736d528ed9Sopenharmony_ci Linker tools: 36746d528ed9Sopenharmony_ci "alink": Linker for static libraries (archives) 36756d528ed9Sopenharmony_ci "solink": Linker for shared libraries 36766d528ed9Sopenharmony_ci "link": Linker for executables 36776d528ed9Sopenharmony_ci 36786d528ed9Sopenharmony_ci Other tools: 36796d528ed9Sopenharmony_ci "stamp": Tool for creating stamp files 36806d528ed9Sopenharmony_ci "copy": Tool to copy files. 36816d528ed9Sopenharmony_ci "action": Defaults for actions 36826d528ed9Sopenharmony_ci 36836d528ed9Sopenharmony_ci Platform specific tools: 36846d528ed9Sopenharmony_ci "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle. 36856d528ed9Sopenharmony_ci "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs. 36866d528ed9Sopenharmony_ci 36876d528ed9Sopenharmony_ci Rust tools: 36886d528ed9Sopenharmony_ci "rust_bin": Tool for compiling Rust binaries 36896d528ed9Sopenharmony_ci "rust_cdylib": Tool for compiling C-compatible dynamic libraries. 36906d528ed9Sopenharmony_ci "rust_dylib": Tool for compiling Rust dynamic libraries. 36916d528ed9Sopenharmony_ci "rust_macro": Tool for compiling Rust procedural macros. 36926d528ed9Sopenharmony_ci "rust_rlib": Tool for compiling Rust libraries. 36936d528ed9Sopenharmony_ci "rust_staticlib": Tool for compiling Rust static libraries. 36946d528ed9Sopenharmony_ci``` 36956d528ed9Sopenharmony_ci 36966d528ed9Sopenharmony_ci#### **Tool variables** 36976d528ed9Sopenharmony_ci 36986d528ed9Sopenharmony_ci``` 36996d528ed9Sopenharmony_ci command [string with substitutions] 37006d528ed9Sopenharmony_ci Valid for: all tools except "action" (required) 37016d528ed9Sopenharmony_ci 37026d528ed9Sopenharmony_ci The command to run. 37036d528ed9Sopenharmony_ci 37046d528ed9Sopenharmony_ci command_launcher [string] 37056d528ed9Sopenharmony_ci Valid for: all tools except "action" (optional) 37066d528ed9Sopenharmony_ci 37076d528ed9Sopenharmony_ci The prefix with which to launch the command (e.g. the path to a Goma or 37086d528ed9Sopenharmony_ci CCache compiler launcher). 37096d528ed9Sopenharmony_ci 37106d528ed9Sopenharmony_ci Note that this prefix will not be included in the compilation database or 37116d528ed9Sopenharmony_ci IDE files generated from the build. 37126d528ed9Sopenharmony_ci 37136d528ed9Sopenharmony_ci default_output_dir [string with substitutions] 37146d528ed9Sopenharmony_ci Valid for: linker tools 37156d528ed9Sopenharmony_ci 37166d528ed9Sopenharmony_ci Default directory name for the output file relative to the 37176d528ed9Sopenharmony_ci root_build_dir. It can contain other substitution patterns. This will 37186d528ed9Sopenharmony_ci be the default value for the {{output_dir}} expansion (discussed below) 37196d528ed9Sopenharmony_ci but will be overridden by the "output_dir" variable in a target, if one 37206d528ed9Sopenharmony_ci is specified. 37216d528ed9Sopenharmony_ci 37226d528ed9Sopenharmony_ci GN doesn't do anything with this string other than pass it along, 37236d528ed9Sopenharmony_ci potentially with target-specific overrides. It is the tool's job to use 37246d528ed9Sopenharmony_ci the expansion so that the files will be in the right place. 37256d528ed9Sopenharmony_ci 37266d528ed9Sopenharmony_ci default_output_extension [string] 37276d528ed9Sopenharmony_ci Valid for: linker tools 37286d528ed9Sopenharmony_ci 37296d528ed9Sopenharmony_ci Extension for the main output of a linkable tool. It includes the 37306d528ed9Sopenharmony_ci leading dot. This will be the default value for the 37316d528ed9Sopenharmony_ci {{output_extension}} expansion (discussed below) but will be overridden 37326d528ed9Sopenharmony_ci by by the "output extension" variable in a target, if one is specified. 37336d528ed9Sopenharmony_ci Empty string means no extension. 37346d528ed9Sopenharmony_ci 37356d528ed9Sopenharmony_ci GN doesn't actually do anything with this extension other than pass it 37366d528ed9Sopenharmony_ci along, potentially with target-specific overrides. One would typically 37376d528ed9Sopenharmony_ci use the {{output_extension}} value in the "outputs" to read this value. 37386d528ed9Sopenharmony_ci 37396d528ed9Sopenharmony_ci Example: default_output_extension = ".exe" 37406d528ed9Sopenharmony_ci 37416d528ed9Sopenharmony_ci depfile [string with substitutions] 37426d528ed9Sopenharmony_ci Valid for: compiler tools (optional) 37436d528ed9Sopenharmony_ci 37446d528ed9Sopenharmony_ci If the tool can write ".d" files, this specifies the name of the 37456d528ed9Sopenharmony_ci resulting file. These files are used to list header file dependencies 37466d528ed9Sopenharmony_ci (or other implicit input dependencies) that are discovered at build 37476d528ed9Sopenharmony_ci time. See also "depsformat". 37486d528ed9Sopenharmony_ci 37496d528ed9Sopenharmony_ci Example: depfile = "{{output}}.d" 37506d528ed9Sopenharmony_ci 37516d528ed9Sopenharmony_ci depsformat [string] 37526d528ed9Sopenharmony_ci Valid for: compiler tools (when depfile is specified) 37536d528ed9Sopenharmony_ci 37546d528ed9Sopenharmony_ci Format for the deps outputs. This is either "gcc" or "msvc". See the 37556d528ed9Sopenharmony_ci ninja documentation for "deps" for more information. 37566d528ed9Sopenharmony_ci 37576d528ed9Sopenharmony_ci Example: depsformat = "gcc" 37586d528ed9Sopenharmony_ci 37596d528ed9Sopenharmony_ci description [string with substitutions, optional] 37606d528ed9Sopenharmony_ci Valid for: all tools 37616d528ed9Sopenharmony_ci 37626d528ed9Sopenharmony_ci What to print when the command is run. 37636d528ed9Sopenharmony_ci 37646d528ed9Sopenharmony_ci Example: description = "Compiling {{source}}" 37656d528ed9Sopenharmony_ci 37666d528ed9Sopenharmony_ci exe_output_extension [string, optional, rust tools only] 37676d528ed9Sopenharmony_ci rlib_output_extension [string, optional, rust tools only] 37686d528ed9Sopenharmony_ci dylib_output_extension [string, optional, rust tools only] 37696d528ed9Sopenharmony_ci cdylib_output_extension [string, optional, rust tools only] 37706d528ed9Sopenharmony_ci rust_proc_macro_output_extension [string, optional, rust tools only] 37716d528ed9Sopenharmony_ci Valid for: Rust tools 37726d528ed9Sopenharmony_ci 37736d528ed9Sopenharmony_ci These specify the default tool output for each of the crate types. 37746d528ed9Sopenharmony_ci The default is empty for executables, shared, and static libraries and 37756d528ed9Sopenharmony_ci ".rlib" for rlibs. Note that the Rust compiler complains with an error 37766d528ed9Sopenharmony_ci if external crates do not take the form `lib<name>.rlib` or 37776d528ed9Sopenharmony_ci `lib<name>.<shared_extension>`, where `<shared_extension>` is `.so`, 37786d528ed9Sopenharmony_ci `.dylib`, or `.dll` as appropriate for the platform. 37796d528ed9Sopenharmony_ci 37806d528ed9Sopenharmony_ci lib_switch [string, optional, link tools only] 37816d528ed9Sopenharmony_ci lib_dir_switch [string, optional, link tools only] 37826d528ed9Sopenharmony_ci Valid for: Linker tools except "alink" 37836d528ed9Sopenharmony_ci 37846d528ed9Sopenharmony_ci These strings will be prepended to the libraries and library search 37856d528ed9Sopenharmony_ci directories, respectively, because linkers differ on how to specify 37866d528ed9Sopenharmony_ci them. 37876d528ed9Sopenharmony_ci 37886d528ed9Sopenharmony_ci If you specified: 37896d528ed9Sopenharmony_ci lib_switch = "-l" 37906d528ed9Sopenharmony_ci lib_dir_switch = "-L" 37916d528ed9Sopenharmony_ci then the "{{libs}}" expansion for 37926d528ed9Sopenharmony_ci [ "freetype", "expat" ] 37936d528ed9Sopenharmony_ci would be 37946d528ed9Sopenharmony_ci "-lfreetype -lexpat". 37956d528ed9Sopenharmony_ci 37966d528ed9Sopenharmony_ci framework_switch [string, optional, link tools only] 37976d528ed9Sopenharmony_ci weak_framework_switch [string, optional, link tools only] 37986d528ed9Sopenharmony_ci framework_dir_switch [string, optional, link tools only] 37996d528ed9Sopenharmony_ci Valid for: Linker tools 38006d528ed9Sopenharmony_ci 38016d528ed9Sopenharmony_ci These strings will be prepended to the frameworks and framework search 38026d528ed9Sopenharmony_ci path directories, respectively, because linkers differ on how to specify 38036d528ed9Sopenharmony_ci them. 38046d528ed9Sopenharmony_ci 38056d528ed9Sopenharmony_ci If you specified: 38066d528ed9Sopenharmony_ci framework_switch = "-framework " 38076d528ed9Sopenharmony_ci weak_framework_switch = "-weak_framework " 38086d528ed9Sopenharmony_ci framework_dir_switch = "-F" 38096d528ed9Sopenharmony_ci and: 38106d528ed9Sopenharmony_ci framework_dirs = [ "$root_out_dir" ] 38116d528ed9Sopenharmony_ci frameworks = [ "UIKit.framework", "Foo.framework" ] 38126d528ed9Sopenharmony_ci weak_frameworks = [ "MediaPlayer.framework" ] 38136d528ed9Sopenharmony_ci would be: 38146d528ed9Sopenharmony_ci "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer" 38156d528ed9Sopenharmony_ci 38166d528ed9Sopenharmony_ci swiftmodule_switch [string, optional, link tools only] 38176d528ed9Sopenharmony_ci Valid for: Linker tools except "alink" 38186d528ed9Sopenharmony_ci 38196d528ed9Sopenharmony_ci The string will be prependend to the path to the .swiftmodule files 38206d528ed9Sopenharmony_ci that are embedded in the linker output. 38216d528ed9Sopenharmony_ci 38226d528ed9Sopenharmony_ci If you specified: 38236d528ed9Sopenharmony_ci swiftmodule_swift = "-Wl,-add_ast_path," 38246d528ed9Sopenharmony_ci then the "{{swiftmodules}}" expansion for 38256d528ed9Sopenharmony_ci [ "obj/foo/Foo.swiftmodule" ] 38266d528ed9Sopenharmony_ci would be 38276d528ed9Sopenharmony_ci "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule" 38286d528ed9Sopenharmony_ci 38296d528ed9Sopenharmony_ci outputs [list of strings with substitutions] 38306d528ed9Sopenharmony_ci Valid for: Linker and compiler tools (required) 38316d528ed9Sopenharmony_ci 38326d528ed9Sopenharmony_ci An array of names for the output files the tool produces. These are 38336d528ed9Sopenharmony_ci relative to the build output directory. There must always be at least 38346d528ed9Sopenharmony_ci one output file. There can be more than one output (a linker might 38356d528ed9Sopenharmony_ci produce a library and an import library, for example). 38366d528ed9Sopenharmony_ci 38376d528ed9Sopenharmony_ci This array just declares to GN what files the tool will produce. It is 38386d528ed9Sopenharmony_ci your responsibility to specify the tool command that actually produces 38396d528ed9Sopenharmony_ci these files. 38406d528ed9Sopenharmony_ci 38416d528ed9Sopenharmony_ci If you specify more than one output for shared library links, you 38426d528ed9Sopenharmony_ci should consider setting link_output, depend_output, and 38436d528ed9Sopenharmony_ci runtime_outputs. 38446d528ed9Sopenharmony_ci 38456d528ed9Sopenharmony_ci Example for a compiler tool that produces .obj files: 38466d528ed9Sopenharmony_ci outputs = [ 38476d528ed9Sopenharmony_ci "{{source_out_dir}}/{{source_name_part}}.obj" 38486d528ed9Sopenharmony_ci ] 38496d528ed9Sopenharmony_ci 38506d528ed9Sopenharmony_ci Example for a linker tool that produces a .dll and a .lib. The use of 38516d528ed9Sopenharmony_ci {{target_output_name}}, {{output_extension}} and {{output_dir}} allows 38526d528ed9Sopenharmony_ci the target to override these values. 38536d528ed9Sopenharmony_ci outputs = [ 38546d528ed9Sopenharmony_ci "{{output_dir}}/{{target_output_name}}{{output_extension}}", 38556d528ed9Sopenharmony_ci "{{output_dir}}/{{target_output_name}}.lib", 38566d528ed9Sopenharmony_ci ] 38576d528ed9Sopenharmony_ci 38586d528ed9Sopenharmony_ci partial_outputs [list of strings with substitutions] 38596d528ed9Sopenharmony_ci Valid for: "swift" only 38606d528ed9Sopenharmony_ci 38616d528ed9Sopenharmony_ci An array of names for the partial outputs the tool produces. These 38626d528ed9Sopenharmony_ci are relative to the build output directory. The expansion will be 38636d528ed9Sopenharmony_ci evaluated for each file listed in the "sources" of the target. 38646d528ed9Sopenharmony_ci 38656d528ed9Sopenharmony_ci This is used to deal with whole module optimization, allowing to 38666d528ed9Sopenharmony_ci list one object file per source file when whole module optimization 38676d528ed9Sopenharmony_ci is disabled. 38686d528ed9Sopenharmony_ci 38696d528ed9Sopenharmony_ci pool [label, optional] 38706d528ed9Sopenharmony_ci Valid for: all tools (optional) 38716d528ed9Sopenharmony_ci 38726d528ed9Sopenharmony_ci Label of the pool to use for the tool. Pools are used to limit the 38736d528ed9Sopenharmony_ci number of tasks that can execute concurrently during the build. 38746d528ed9Sopenharmony_ci 38756d528ed9Sopenharmony_ci See also "gn help pool". 38766d528ed9Sopenharmony_ci 38776d528ed9Sopenharmony_ci link_output [string with substitutions] 38786d528ed9Sopenharmony_ci depend_output [string with substitutions] 38796d528ed9Sopenharmony_ci Valid for: "solink" only (optional) 38806d528ed9Sopenharmony_ci 38816d528ed9Sopenharmony_ci These two files specify which of the outputs from the solink tool 38826d528ed9Sopenharmony_ci should be used for linking and dependency tracking. These should match 38836d528ed9Sopenharmony_ci entries in the "outputs". If unspecified, the first item in the 38846d528ed9Sopenharmony_ci "outputs" array will be used for all. See "Separate linking and 38856d528ed9Sopenharmony_ci dependencies for shared libraries" below for more. 38866d528ed9Sopenharmony_ci 38876d528ed9Sopenharmony_ci On Windows, where the tools produce a .dll shared library and a .lib 38886d528ed9Sopenharmony_ci import library, you will want the first two to be the import library 38896d528ed9Sopenharmony_ci and the third one to be the .dll file. On Linux, if you're not doing 38906d528ed9Sopenharmony_ci the separate linking/dependency optimization, all of these should be 38916d528ed9Sopenharmony_ci the .so output. 38926d528ed9Sopenharmony_ci 38936d528ed9Sopenharmony_ci output_prefix [string] 38946d528ed9Sopenharmony_ci Valid for: Linker tools (optional) 38956d528ed9Sopenharmony_ci 38966d528ed9Sopenharmony_ci Prefix to use for the output name. Defaults to empty. This prefix will 38976d528ed9Sopenharmony_ci be prepended to the name of the target (or the output_name if one is 38986d528ed9Sopenharmony_ci manually specified for it) if the prefix is not already there. The 38996d528ed9Sopenharmony_ci result will show up in the {{output_name}} substitution pattern. 39006d528ed9Sopenharmony_ci 39016d528ed9Sopenharmony_ci Individual targets can opt-out of the output prefix by setting: 39026d528ed9Sopenharmony_ci output_prefix_override = true 39036d528ed9Sopenharmony_ci (see "gn help output_prefix_override"). 39046d528ed9Sopenharmony_ci 39056d528ed9Sopenharmony_ci This is typically used to prepend "lib" to libraries on 39066d528ed9Sopenharmony_ci Posix systems: 39076d528ed9Sopenharmony_ci output_prefix = "lib" 39086d528ed9Sopenharmony_ci 39096d528ed9Sopenharmony_ci precompiled_header_type [string] 39106d528ed9Sopenharmony_ci Valid for: "cc", "cxx", "objc", "objcxx" 39116d528ed9Sopenharmony_ci 39126d528ed9Sopenharmony_ci Type of precompiled headers. If undefined or the empty string, 39136d528ed9Sopenharmony_ci precompiled headers will not be used for this tool. Otherwise use "gcc" 39146d528ed9Sopenharmony_ci or "msvc". 39156d528ed9Sopenharmony_ci 39166d528ed9Sopenharmony_ci For precompiled headers to be used for a given target, the target (or a 39176d528ed9Sopenharmony_ci config applied to it) must also specify a "precompiled_header" and, for 39186d528ed9Sopenharmony_ci "msvc"-style headers, a "precompiled_source" value. If the type is 39196d528ed9Sopenharmony_ci "gcc", then both "precompiled_header" and "precompiled_source" must 39206d528ed9Sopenharmony_ci resolve to the same file, despite the different formats required for 39216d528ed9Sopenharmony_ci each." 39226d528ed9Sopenharmony_ci 39236d528ed9Sopenharmony_ci See "gn help precompiled_header" for more. 39246d528ed9Sopenharmony_ci 39256d528ed9Sopenharmony_ci restat [boolean] 39266d528ed9Sopenharmony_ci Valid for: all tools (optional, defaults to false) 39276d528ed9Sopenharmony_ci 39286d528ed9Sopenharmony_ci Requests that Ninja check the file timestamp after this tool has run to 39296d528ed9Sopenharmony_ci determine if anything changed. Set this if your tool has the ability to 39306d528ed9Sopenharmony_ci skip writing output if the output file has not changed. 39316d528ed9Sopenharmony_ci 39326d528ed9Sopenharmony_ci Normally, Ninja will assume that when a tool runs the output be new and 39336d528ed9Sopenharmony_ci downstream dependents must be rebuild. When this is set to trye, Ninja 39346d528ed9Sopenharmony_ci can skip rebuilding downstream dependents for input changes that don't 39356d528ed9Sopenharmony_ci actually affect the output. 39366d528ed9Sopenharmony_ci 39376d528ed9Sopenharmony_ci Example: 39386d528ed9Sopenharmony_ci restat = true 39396d528ed9Sopenharmony_ci 39406d528ed9Sopenharmony_ci rspfile [string with substitutions] 39416d528ed9Sopenharmony_ci Valid for: all tools except "action" (optional) 39426d528ed9Sopenharmony_ci 39436d528ed9Sopenharmony_ci Name of the response file. If empty, no response file will be 39446d528ed9Sopenharmony_ci used. See "rspfile_content". 39456d528ed9Sopenharmony_ci 39466d528ed9Sopenharmony_ci rspfile_content [string with substitutions] 39476d528ed9Sopenharmony_ci Valid for: all tools except "action" (required when "rspfile" is used) 39486d528ed9Sopenharmony_ci 39496d528ed9Sopenharmony_ci The contents to be written to the response file. This may include all 39506d528ed9Sopenharmony_ci or part of the command to send to the tool which allows you to get 39516d528ed9Sopenharmony_ci around OS command-line length limits. 39526d528ed9Sopenharmony_ci 39536d528ed9Sopenharmony_ci This example adds the inputs and libraries to a response file, but 39546d528ed9Sopenharmony_ci passes the linker flags directly on the command line: 39556d528ed9Sopenharmony_ci tool("link") { 39566d528ed9Sopenharmony_ci command = "link -o {{output}} {{ldflags}} @{{output}}.rsp" 39576d528ed9Sopenharmony_ci rspfile = "{{output}}.rsp" 39586d528ed9Sopenharmony_ci rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}" 39596d528ed9Sopenharmony_ci } 39606d528ed9Sopenharmony_ci 39616d528ed9Sopenharmony_ci runtime_outputs [string list with substitutions] 39626d528ed9Sopenharmony_ci Valid for: linker tools 39636d528ed9Sopenharmony_ci 39646d528ed9Sopenharmony_ci If specified, this list is the subset of the outputs that should be 39656d528ed9Sopenharmony_ci added to runtime deps (see "gn help runtime_deps"). By default (if 39666d528ed9Sopenharmony_ci runtime_outputs is empty or unspecified), it will be the link_output. 39676d528ed9Sopenharmony_ci 39686d528ed9Sopenharmony_ci rust_sysroot 39696d528ed9Sopenharmony_ci Valid for: Rust tools 39706d528ed9Sopenharmony_ci 39716d528ed9Sopenharmony_ci A path relative to root_out_dir. This is not used in the build 39726d528ed9Sopenharmony_ci process, but may be used when generating metadata for rust-analyzer. 39736d528ed9Sopenharmony_ci (See --export-rust-project). It enables such metadata to include 39746d528ed9Sopenharmony_ci information about the Rust standard library. 39756d528ed9Sopenharmony_ci 39766d528ed9Sopenharmony_ci dynamic_link_switch 39776d528ed9Sopenharmony_ci Valid for: Rust tools which link 39786d528ed9Sopenharmony_ci 39796d528ed9Sopenharmony_ci A switch to be optionally inserted into linker command lines 39806d528ed9Sopenharmony_ci to indicate that subsequent items may be dynamically linked. 39816d528ed9Sopenharmony_ci For ld-like linkers, -Clink-arg=-Bdynamic may be a good choice. 39826d528ed9Sopenharmony_ci This switch is inserted by gn into rustc command lines before 39836d528ed9Sopenharmony_ci listing any non-Rust dependencies. This may be necessary because 39846d528ed9Sopenharmony_ci sometimes rustc puts the linker into a mode where it would otherwise 39856d528ed9Sopenharmony_ci link against static libraries by default. This flag will be 39866d528ed9Sopenharmony_ci inserted into the {{rustdeps}} variable at the appropriate place; 39876d528ed9Sopenharmony_ci {{ldflags}} can't be used for the same purpose because the flags 39886d528ed9Sopenharmony_ci may not be inserted at the desired place in the command line. 39896d528ed9Sopenharmony_ci``` 39906d528ed9Sopenharmony_ci 39916d528ed9Sopenharmony_ci#### **Expansions for tool variables** 39926d528ed9Sopenharmony_ci 39936d528ed9Sopenharmony_ci``` 39946d528ed9Sopenharmony_ci All paths are relative to the root build directory, which is the current 39956d528ed9Sopenharmony_ci directory for running all tools. These expansions are available to all tools: 39966d528ed9Sopenharmony_ci 39976d528ed9Sopenharmony_ci {{label}} 39986d528ed9Sopenharmony_ci The label of the current target. This is typically used in the 39996d528ed9Sopenharmony_ci "description" field for link tools. The toolchain will be omitted from 40006d528ed9Sopenharmony_ci the label for targets in the default toolchain, and will be included 40016d528ed9Sopenharmony_ci for targets in other toolchains. 40026d528ed9Sopenharmony_ci 40036d528ed9Sopenharmony_ci {{label_name}} 40046d528ed9Sopenharmony_ci The short name of the label of the target. This is the part after the 40056d528ed9Sopenharmony_ci colon. For "//foo/bar:baz" this will be "baz". Unlike 40066d528ed9Sopenharmony_ci {{target_output_name}}, this is not affected by the "output_prefix" in 40076d528ed9Sopenharmony_ci the tool or the "output_name" set on the target. 40086d528ed9Sopenharmony_ci 40096d528ed9Sopenharmony_ci {{label_no_toolchain}} 40106d528ed9Sopenharmony_ci The label of the current target, never including the toolchain 40116d528ed9Sopenharmony_ci (otherwise, this is identical to {{label}}). This is used as the module 40126d528ed9Sopenharmony_ci name when using .modulemap files. 40136d528ed9Sopenharmony_ci 40146d528ed9Sopenharmony_ci {{output}} 40156d528ed9Sopenharmony_ci The relative path and name of the output(s) of the current build step. 40166d528ed9Sopenharmony_ci If there is more than one output, this will expand to a list of all of 40176d528ed9Sopenharmony_ci them. Example: "out/base/my_file.o" 40186d528ed9Sopenharmony_ci 40196d528ed9Sopenharmony_ci {{target_gen_dir}} 40206d528ed9Sopenharmony_ci {{target_out_dir}} 40216d528ed9Sopenharmony_ci The directory of the generated file and output directories, 40226d528ed9Sopenharmony_ci respectively, for the current target. There is no trailing slash. See 40236d528ed9Sopenharmony_ci also {{output_dir}} for linker tools. Example: "out/base/test" 40246d528ed9Sopenharmony_ci 40256d528ed9Sopenharmony_ci {{target_output_name}} 40266d528ed9Sopenharmony_ci The short name of the current target with no path information, or the 40276d528ed9Sopenharmony_ci value of the "output_name" variable if one is specified in the target. 40286d528ed9Sopenharmony_ci This will include the "output_prefix" if any. See also {{label_name}}. 40296d528ed9Sopenharmony_ci 40306d528ed9Sopenharmony_ci Example: "libfoo" for the target named "foo" and an output prefix for 40316d528ed9Sopenharmony_ci the linker tool of "lib". 40326d528ed9Sopenharmony_ci 40336d528ed9Sopenharmony_ci Compiler tools have the notion of a single input and a single output, along 40346d528ed9Sopenharmony_ci with a set of compiler-specific flags. The following expansions are 40356d528ed9Sopenharmony_ci available: 40366d528ed9Sopenharmony_ci 40376d528ed9Sopenharmony_ci {{asmflags}} 40386d528ed9Sopenharmony_ci {{cflags}} 40396d528ed9Sopenharmony_ci {{cflags_c}} 40406d528ed9Sopenharmony_ci {{cflags_cc}} 40416d528ed9Sopenharmony_ci {{cflags_objc}} 40426d528ed9Sopenharmony_ci {{cflags_objcc}} 40436d528ed9Sopenharmony_ci {{defines}} 40446d528ed9Sopenharmony_ci {{include_dirs}} 40456d528ed9Sopenharmony_ci Strings correspond that to the processed flags/defines/include 40466d528ed9Sopenharmony_ci directories specified for the target. 40476d528ed9Sopenharmony_ci Example: "--enable-foo --enable-bar" 40486d528ed9Sopenharmony_ci 40496d528ed9Sopenharmony_ci Defines will be prefixed by "-D" and include directories will be 40506d528ed9Sopenharmony_ci prefixed by "-I" (these work with Posix tools as well as Microsoft 40516d528ed9Sopenharmony_ci ones). 40526d528ed9Sopenharmony_ci 40536d528ed9Sopenharmony_ci {{module_deps}} 40546d528ed9Sopenharmony_ci {{module_deps_no_self}} 40556d528ed9Sopenharmony_ci Strings that correspond to the flags necessary to depend upon the Clang 40566d528ed9Sopenharmony_ci modules referenced by the current target. The "_no_self" version doesn't 40576d528ed9Sopenharmony_ci include the module for the current target, and can be used to compile 40586d528ed9Sopenharmony_ci the pcm itself. 40596d528ed9Sopenharmony_ci 40606d528ed9Sopenharmony_ci {{source}} 40616d528ed9Sopenharmony_ci The relative path and name of the current input file. 40626d528ed9Sopenharmony_ci Example: "../../base/my_file.cc" 40636d528ed9Sopenharmony_ci 40646d528ed9Sopenharmony_ci {{source_file_part}} 40656d528ed9Sopenharmony_ci The file part of the source including the extension (with no directory 40666d528ed9Sopenharmony_ci information). 40676d528ed9Sopenharmony_ci Example: "foo.cc" 40686d528ed9Sopenharmony_ci 40696d528ed9Sopenharmony_ci {{source_name_part}} 40706d528ed9Sopenharmony_ci The filename part of the source file with no directory or extension. 40716d528ed9Sopenharmony_ci Example: "foo" 40726d528ed9Sopenharmony_ci 40736d528ed9Sopenharmony_ci {{source_gen_dir}} 40746d528ed9Sopenharmony_ci {{source_out_dir}} 40756d528ed9Sopenharmony_ci The directory in the generated file and output directories, 40766d528ed9Sopenharmony_ci respectively, for the current input file. If the source file is in the 40776d528ed9Sopenharmony_ci same directory as the target is declared in, they will will be the same 40786d528ed9Sopenharmony_ci as the "target" versions above. Example: "gen/base/test" 40796d528ed9Sopenharmony_ci 40806d528ed9Sopenharmony_ci Linker tools have multiple inputs and (potentially) multiple outputs. The 40816d528ed9Sopenharmony_ci static library tool ("alink") is not considered a linker tool. The following 40826d528ed9Sopenharmony_ci expansions are available: 40836d528ed9Sopenharmony_ci 40846d528ed9Sopenharmony_ci {{inputs}} 40856d528ed9Sopenharmony_ci {{inputs_newline}} 40866d528ed9Sopenharmony_ci Expands to the inputs to the link step. This will be a list of object 40876d528ed9Sopenharmony_ci files and static libraries. 40886d528ed9Sopenharmony_ci Example: "obj/foo.o obj/bar.o obj/somelibrary.a" 40896d528ed9Sopenharmony_ci 40906d528ed9Sopenharmony_ci The "_newline" version will separate the input files with newlines 40916d528ed9Sopenharmony_ci instead of spaces. This is useful in response files: some linkers can 40926d528ed9Sopenharmony_ci take a "-filelist" flag which expects newline separated files, and some 40936d528ed9Sopenharmony_ci Microsoft tools have a fixed-sized buffer for parsing each line of a 40946d528ed9Sopenharmony_ci response file. 40956d528ed9Sopenharmony_ci 40966d528ed9Sopenharmony_ci {{ldflags}} 40976d528ed9Sopenharmony_ci Expands to the processed set of ldflags and library search paths 40986d528ed9Sopenharmony_ci specified for the target. 40996d528ed9Sopenharmony_ci Example: "-m64 -fPIC -pthread -L/usr/local/mylib" 41006d528ed9Sopenharmony_ci 41016d528ed9Sopenharmony_ci {{libs}} 41026d528ed9Sopenharmony_ci Expands to the list of system libraries to link to. Each will be 41036d528ed9Sopenharmony_ci prefixed by the "lib_switch". 41046d528ed9Sopenharmony_ci 41056d528ed9Sopenharmony_ci Example: "-lfoo -lbar" 41066d528ed9Sopenharmony_ci 41076d528ed9Sopenharmony_ci {{output_dir}} 41086d528ed9Sopenharmony_ci The value of the "output_dir" variable in the target, or the the value 41096d528ed9Sopenharmony_ci of the "default_output_dir" value in the tool if the target does not 41106d528ed9Sopenharmony_ci override the output directory. This will be relative to the 41116d528ed9Sopenharmony_ci root_build_dir and will not end in a slash. Will be "." for output to 41126d528ed9Sopenharmony_ci the root_build_dir. 41136d528ed9Sopenharmony_ci 41146d528ed9Sopenharmony_ci This is subtly different than {{target_out_dir}} which is defined by GN 41156d528ed9Sopenharmony_ci based on the target's path and not overridable. {{output_dir}} is for 41166d528ed9Sopenharmony_ci the final output, {{target_out_dir}} is generally for object files and 41176d528ed9Sopenharmony_ci other outputs. 41186d528ed9Sopenharmony_ci 41196d528ed9Sopenharmony_ci Usually {{output_dir}} would be defined in terms of either 41206d528ed9Sopenharmony_ci {{target_out_dir}} or {{root_out_dir}} 41216d528ed9Sopenharmony_ci 41226d528ed9Sopenharmony_ci {{output_extension}} 41236d528ed9Sopenharmony_ci The value of the "output_extension" variable in the target, or the 41246d528ed9Sopenharmony_ci value of the "default_output_extension" value in the tool if the target 41256d528ed9Sopenharmony_ci does not specify an output extension. 41266d528ed9Sopenharmony_ci Example: ".so" 41276d528ed9Sopenharmony_ci 41286d528ed9Sopenharmony_ci {{solibs}} 41296d528ed9Sopenharmony_ci Extra libraries from shared library dependencies not specified in the 41306d528ed9Sopenharmony_ci {{inputs}}. This is the list of link_output files from shared libraries 41316d528ed9Sopenharmony_ci (if the solink tool specifies a "link_output" variable separate from 41326d528ed9Sopenharmony_ci the "depend_output"). 41336d528ed9Sopenharmony_ci 41346d528ed9Sopenharmony_ci These should generally be treated the same as libs by your tool. 41356d528ed9Sopenharmony_ci 41366d528ed9Sopenharmony_ci Example: "libfoo.so libbar.so" 41376d528ed9Sopenharmony_ci 41386d528ed9Sopenharmony_ci {{rlibs}} 41396d528ed9Sopenharmony_ci Any Rust .rlibs which need to be linked into a final C++ target. 41406d528ed9Sopenharmony_ci These should be treated as {{inputs}} except that sometimes 41416d528ed9Sopenharmony_ci they might have different linker directives applied. 41426d528ed9Sopenharmony_ci 41436d528ed9Sopenharmony_ci Example: "obj/foo/libfoo.rlib" 41446d528ed9Sopenharmony_ci 41456d528ed9Sopenharmony_ci {{frameworks}} 41466d528ed9Sopenharmony_ci Shared libraries packaged as framework bundle. This is principally 41476d528ed9Sopenharmony_ci used on Apple's platforms (macOS and iOS). All name must be ending 41486d528ed9Sopenharmony_ci with ".framework" suffix; the suffix will be stripped when expanding 41496d528ed9Sopenharmony_ci {{frameworks}} and each item will be preceded by "-framework" or 41506d528ed9Sopenharmony_ci "-weak_framework". 41516d528ed9Sopenharmony_ci 41526d528ed9Sopenharmony_ci {{swiftmodules}} 41536d528ed9Sopenharmony_ci Swift .swiftmodule files that needs to be embedded into the binary. 41546d528ed9Sopenharmony_ci This is necessary to correctly link with object generated by the 41556d528ed9Sopenharmony_ci Swift compiler (the .swiftmodule file cannot be embedded in object 41566d528ed9Sopenharmony_ci files directly). Those will be prefixed with "swiftmodule_switch" 41576d528ed9Sopenharmony_ci value. 41586d528ed9Sopenharmony_ci 41596d528ed9Sopenharmony_ci The static library ("alink") tool allows {{arflags}} plus the common tool 41606d528ed9Sopenharmony_ci substitutions. 41616d528ed9Sopenharmony_ci 41626d528ed9Sopenharmony_ci The copy tool allows the common compiler/linker substitutions, plus 41636d528ed9Sopenharmony_ci {{source}} which is the source of the copy. The stamp tool allows only the 41646d528ed9Sopenharmony_ci common tool substitutions. 41656d528ed9Sopenharmony_ci 41666d528ed9Sopenharmony_ci The copy_bundle_data and compile_xcassets tools only allows the common tool 41676d528ed9Sopenharmony_ci substitutions. Both tools are required to create iOS/macOS bundles and need 41686d528ed9Sopenharmony_ci only be defined on those platforms. 41696d528ed9Sopenharmony_ci 41706d528ed9Sopenharmony_ci The copy_bundle_data tool will be called with one source and needs to copy 41716d528ed9Sopenharmony_ci (optionally optimizing the data representation) to its output. It may be 41726d528ed9Sopenharmony_ci called with a directory as input and it needs to be recursively copied. 41736d528ed9Sopenharmony_ci 41746d528ed9Sopenharmony_ci The compile_xcassets tool will be called with one or more source (each an 41756d528ed9Sopenharmony_ci asset catalog) that needs to be compiled to a single output. The following 41766d528ed9Sopenharmony_ci substitutions are available: 41776d528ed9Sopenharmony_ci 41786d528ed9Sopenharmony_ci {{inputs}} 41796d528ed9Sopenharmony_ci Expands to the list of .xcassets to use as input to compile the asset 41806d528ed9Sopenharmony_ci catalog. 41816d528ed9Sopenharmony_ci 41826d528ed9Sopenharmony_ci {{bundle_product_type}} 41836d528ed9Sopenharmony_ci Expands to the product_type of the bundle that will contain the 41846d528ed9Sopenharmony_ci compiled asset catalog. Usually corresponds to the product_type 41856d528ed9Sopenharmony_ci property of the corresponding create_bundle target. 41866d528ed9Sopenharmony_ci 41876d528ed9Sopenharmony_ci {{bundle_partial_info_plist}} 41886d528ed9Sopenharmony_ci Expands to the path to the partial Info.plist generated by the 41896d528ed9Sopenharmony_ci assets catalog compiler. Usually based on the target_name of 41906d528ed9Sopenharmony_ci the create_bundle target. 41916d528ed9Sopenharmony_ci 41926d528ed9Sopenharmony_ci {{xcasset_compiler_flags}} 41936d528ed9Sopenharmony_ci Expands to the list of flags specified in corresponding 41946d528ed9Sopenharmony_ci create_bundle target. 41956d528ed9Sopenharmony_ci 41966d528ed9Sopenharmony_ci The inputs for compile_xcassets tool will be found from the bundle_data 41976d528ed9Sopenharmony_ci dependencies by looking for any file matching "*/*.xcassets/*" pattern. 41986d528ed9Sopenharmony_ci The "$assets.xcassets" directory will be added as input to the tool. 41996d528ed9Sopenharmony_ci 42006d528ed9Sopenharmony_ci The Swift tool has multiple input and outputs. It must have exactly one 42016d528ed9Sopenharmony_ci output of .swiftmodule type, but can have one or more object file outputs, 42026d528ed9Sopenharmony_ci in addition to other type of outputs. The following expansions are available: 42036d528ed9Sopenharmony_ci 42046d528ed9Sopenharmony_ci {{module_name}} 42056d528ed9Sopenharmony_ci Expands to the string representing the module name of target under 42066d528ed9Sopenharmony_ci compilation (see "module_name" variable). 42076d528ed9Sopenharmony_ci 42086d528ed9Sopenharmony_ci {{module_dirs}} 42096d528ed9Sopenharmony_ci Expands to the list of -I<path> for the target Swift module search 42106d528ed9Sopenharmony_ci path computed from target dependencies. 42116d528ed9Sopenharmony_ci 42126d528ed9Sopenharmony_ci {{swiftflags}} 42136d528ed9Sopenharmony_ci Expands to the list of strings representing Swift compiler flags. 42146d528ed9Sopenharmony_ci 42156d528ed9Sopenharmony_ci Rust tools have the notion of a single input and a single output, along 42166d528ed9Sopenharmony_ci with a set of compiler-specific flags. The following expansions are 42176d528ed9Sopenharmony_ci available: 42186d528ed9Sopenharmony_ci 42196d528ed9Sopenharmony_ci {{crate_name}} 42206d528ed9Sopenharmony_ci Expands to the string representing the crate name of target under 42216d528ed9Sopenharmony_ci compilation. 42226d528ed9Sopenharmony_ci 42236d528ed9Sopenharmony_ci {{crate_type}} 42246d528ed9Sopenharmony_ci Expands to the string representing the type of crate for the target 42256d528ed9Sopenharmony_ci under compilation. 42266d528ed9Sopenharmony_ci 42276d528ed9Sopenharmony_ci {{externs}} 42286d528ed9Sopenharmony_ci Expands to the list of --extern flags needed to include addition Rust 42296d528ed9Sopenharmony_ci libraries in this target. Includes any specified renamed dependencies. 42306d528ed9Sopenharmony_ci 42316d528ed9Sopenharmony_ci {{rustdeps}} 42326d528ed9Sopenharmony_ci Expands to the list of -Ldependency=<path> strings needed to compile 42336d528ed9Sopenharmony_ci this target. 42346d528ed9Sopenharmony_ci 42356d528ed9Sopenharmony_ci {{rustenv}} 42366d528ed9Sopenharmony_ci Expands to the list of environment variables. 42376d528ed9Sopenharmony_ci 42386d528ed9Sopenharmony_ci {{rustflags}} 42396d528ed9Sopenharmony_ci Expands to the list of strings representing Rust compiler flags. 42406d528ed9Sopenharmony_ci``` 42416d528ed9Sopenharmony_ci 42426d528ed9Sopenharmony_ci#### **Separate linking and dependencies for shared libraries** 42436d528ed9Sopenharmony_ci 42446d528ed9Sopenharmony_ci``` 42456d528ed9Sopenharmony_ci Shared libraries are special in that not all changes to them require that 42466d528ed9Sopenharmony_ci dependent targets be re-linked. If the shared library is changed but no 42476d528ed9Sopenharmony_ci imports or exports are different, dependent code needn't be relinked, which 42486d528ed9Sopenharmony_ci can speed up the build. 42496d528ed9Sopenharmony_ci 42506d528ed9Sopenharmony_ci If your link step can output a list of exports from a shared library and 42516d528ed9Sopenharmony_ci writes the file only if the new one is different, the timestamp of this file 42526d528ed9Sopenharmony_ci can be used for triggering re-links, while the actual shared library would be 42536d528ed9Sopenharmony_ci used for linking. 42546d528ed9Sopenharmony_ci 42556d528ed9Sopenharmony_ci You will need to specify 42566d528ed9Sopenharmony_ci restat = true 42576d528ed9Sopenharmony_ci in the linker tool to make this work, so Ninja will detect if the timestamp 42586d528ed9Sopenharmony_ci of the dependency file has changed after linking (otherwise it will always 42596d528ed9Sopenharmony_ci assume that running a command updates the output): 42606d528ed9Sopenharmony_ci 42616d528ed9Sopenharmony_ci tool("solink") { 42626d528ed9Sopenharmony_ci command = "..." 42636d528ed9Sopenharmony_ci outputs = [ 42646d528ed9Sopenharmony_ci "{{output_dir}}/{{target_output_name}}{{output_extension}}", 42656d528ed9Sopenharmony_ci "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC", 42666d528ed9Sopenharmony_ci ] 42676d528ed9Sopenharmony_ci link_output = 42686d528ed9Sopenharmony_ci "{{output_dir}}/{{target_output_name}}{{output_extension}}" 42696d528ed9Sopenharmony_ci depend_output = 42706d528ed9Sopenharmony_ci "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC" 42716d528ed9Sopenharmony_ci restat = true 42726d528ed9Sopenharmony_ci } 42736d528ed9Sopenharmony_ci``` 42746d528ed9Sopenharmony_ci 42756d528ed9Sopenharmony_ci#### **Example** 42766d528ed9Sopenharmony_ci 42776d528ed9Sopenharmony_ci``` 42786d528ed9Sopenharmony_ci toolchain("my_toolchain") { 42796d528ed9Sopenharmony_ci # Put these at the top to apply to all tools below. 42806d528ed9Sopenharmony_ci lib_switch = "-l" 42816d528ed9Sopenharmony_ci lib_dir_switch = "-L" 42826d528ed9Sopenharmony_ci 42836d528ed9Sopenharmony_ci tool("cc") { 42846d528ed9Sopenharmony_ci command = "gcc {{source}} -o {{output}}" 42856d528ed9Sopenharmony_ci outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ] 42866d528ed9Sopenharmony_ci description = "GCC {{source}}" 42876d528ed9Sopenharmony_ci } 42886d528ed9Sopenharmony_ci tool("cxx") { 42896d528ed9Sopenharmony_ci command = "g++ {{source}} -o {{output}}" 42906d528ed9Sopenharmony_ci outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ] 42916d528ed9Sopenharmony_ci description = "G++ {{source}}" 42926d528ed9Sopenharmony_ci } 42936d528ed9Sopenharmony_ci }; 42946d528ed9Sopenharmony_ci``` 42956d528ed9Sopenharmony_ci### <a name="func_toolchain"></a>**toolchain**: Defines a toolchain. 42966d528ed9Sopenharmony_ci 42976d528ed9Sopenharmony_ci``` 42986d528ed9Sopenharmony_ci A toolchain is a set of commands and build flags used to compile the source 42996d528ed9Sopenharmony_ci code. The toolchain() function defines these commands. 43006d528ed9Sopenharmony_ci``` 43016d528ed9Sopenharmony_ci 43026d528ed9Sopenharmony_ci#### **Toolchain overview** 43036d528ed9Sopenharmony_ci 43046d528ed9Sopenharmony_ci``` 43056d528ed9Sopenharmony_ci You can have more than one toolchain in use at once in a build and a target 43066d528ed9Sopenharmony_ci can exist simultaneously in multiple toolchains. A build file is executed 43076d528ed9Sopenharmony_ci once for each toolchain it is referenced in so the GN code can vary all 43086d528ed9Sopenharmony_ci parameters of each target (or which targets exist) on a per-toolchain basis. 43096d528ed9Sopenharmony_ci 43106d528ed9Sopenharmony_ci When you have a simple build with only one toolchain, the build config file 43116d528ed9Sopenharmony_ci is loaded only once at the beginning of the build. It must call 43126d528ed9Sopenharmony_ci set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the 43136d528ed9Sopenharmony_ci label of the toolchain definition to use. The "toolchain_args" section of the 43146d528ed9Sopenharmony_ci toolchain definition is ignored. 43156d528ed9Sopenharmony_ci 43166d528ed9Sopenharmony_ci When a target has a dependency on a target using different toolchain (see "gn 43176d528ed9Sopenharmony_ci help labels" for how to specify this), GN will start a build using that 43186d528ed9Sopenharmony_ci secondary toolchain to resolve the target. GN will load the build config file 43196d528ed9Sopenharmony_ci with the build arguments overridden as specified in the toolchain_args. 43206d528ed9Sopenharmony_ci Because the default toolchain is already known, calls to 43216d528ed9Sopenharmony_ci set_default_toolchain() are ignored. 43226d528ed9Sopenharmony_ci 43236d528ed9Sopenharmony_ci To load a file in an alternate toolchain, GN does the following: 43246d528ed9Sopenharmony_ci 43256d528ed9Sopenharmony_ci 1. Loads the file with the toolchain definition in it (as determined by the 43266d528ed9Sopenharmony_ci toolchain label). 43276d528ed9Sopenharmony_ci 2. Re-runs the master build configuration file, applying the arguments 43286d528ed9Sopenharmony_ci specified by the toolchain_args section of the toolchain definition. 43296d528ed9Sopenharmony_ci 3. Loads the destination build file in the context of the configuration file 43306d528ed9Sopenharmony_ci in the previous step. 43316d528ed9Sopenharmony_ci 43326d528ed9Sopenharmony_ci The toolchain configuration is two-way. In the default toolchain (i.e. the 43336d528ed9Sopenharmony_ci main build target) the configuration flows from the build config file to the 43346d528ed9Sopenharmony_ci toolchain. The build config file looks at the state of the build (OS type, 43356d528ed9Sopenharmony_ci CPU architecture, etc.) and decides which toolchain to use (via 43366d528ed9Sopenharmony_ci set_default_toolchain()). In secondary toolchains, the configuration flows 43376d528ed9Sopenharmony_ci from the toolchain to the build config file: the "toolchain_args" in the 43386d528ed9Sopenharmony_ci toolchain definition specifies the arguments to re-invoke the build. 43396d528ed9Sopenharmony_ci``` 43406d528ed9Sopenharmony_ci 43416d528ed9Sopenharmony_ci#### **Functions and variables** 43426d528ed9Sopenharmony_ci 43436d528ed9Sopenharmony_ci``` 43446d528ed9Sopenharmony_ci tool() 43456d528ed9Sopenharmony_ci The tool() function call specifies the commands to run for a given step. See 43466d528ed9Sopenharmony_ci "gn help tool". 43476d528ed9Sopenharmony_ci 43486d528ed9Sopenharmony_ci toolchain_args [scope] 43496d528ed9Sopenharmony_ci Overrides for build arguments to pass to the toolchain when invoking it. 43506d528ed9Sopenharmony_ci This is a variable of type "scope" where the variable names correspond to 43516d528ed9Sopenharmony_ci variables in declare_args() blocks. 43526d528ed9Sopenharmony_ci 43536d528ed9Sopenharmony_ci When you specify a target using an alternate toolchain, the master build 43546d528ed9Sopenharmony_ci configuration file is re-interpreted in the context of that toolchain. 43556d528ed9Sopenharmony_ci toolchain_args allows you to control the arguments passed into this 43566d528ed9Sopenharmony_ci alternate invocation of the build. 43576d528ed9Sopenharmony_ci 43586d528ed9Sopenharmony_ci Any default system arguments or arguments passed in via "gn args" will also 43596d528ed9Sopenharmony_ci be passed to the alternate invocation unless explicitly overridden by 43606d528ed9Sopenharmony_ci toolchain_args. 43616d528ed9Sopenharmony_ci 43626d528ed9Sopenharmony_ci The toolchain_args will be ignored when the toolchain being defined is the 43636d528ed9Sopenharmony_ci default. In this case, it's expected you want the default argument values. 43646d528ed9Sopenharmony_ci 43656d528ed9Sopenharmony_ci See also "gn help buildargs" for an overview of these arguments. 43666d528ed9Sopenharmony_ci 43676d528ed9Sopenharmony_ci propagates_configs [boolean, default=false] 43686d528ed9Sopenharmony_ci Determines whether public_configs and all_dependent_configs in this 43696d528ed9Sopenharmony_ci toolchain propagate to targets in other toolchains. 43706d528ed9Sopenharmony_ci 43716d528ed9Sopenharmony_ci When false (the default), this toolchain will not propagate any configs to 43726d528ed9Sopenharmony_ci targets in other toolchains that depend on it targets inside this 43736d528ed9Sopenharmony_ci toolchain. This matches the most common usage of toolchains where they 43746d528ed9Sopenharmony_ci represent different architectures or compilers and the settings that apply 43756d528ed9Sopenharmony_ci to one won't necessarily apply to others. 43766d528ed9Sopenharmony_ci 43776d528ed9Sopenharmony_ci When true, configs (public and all-dependent) will cross the boundary out 43786d528ed9Sopenharmony_ci of this toolchain as if the toolchain boundary wasn't there. This only 43796d528ed9Sopenharmony_ci affects one direction of dependencies: a toolchain can't control whether 43806d528ed9Sopenharmony_ci it accepts such configs, only whether it pushes them. The build is 43816d528ed9Sopenharmony_ci responsible for ensuring that any external targets depending on targets in 43826d528ed9Sopenharmony_ci this toolchain are compatible with the compiler flags, etc. that may be 43836d528ed9Sopenharmony_ci propagated. 43846d528ed9Sopenharmony_ci 43856d528ed9Sopenharmony_ci deps [string list] 43866d528ed9Sopenharmony_ci Dependencies of this toolchain. These dependencies will be resolved before 43876d528ed9Sopenharmony_ci any target in the toolchain is compiled. To avoid circular dependencies 43886d528ed9Sopenharmony_ci these must be targets defined in another toolchain. 43896d528ed9Sopenharmony_ci 43906d528ed9Sopenharmony_ci This is expressed as a list of targets, and generally these targets will 43916d528ed9Sopenharmony_ci always specify a toolchain: 43926d528ed9Sopenharmony_ci deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ] 43936d528ed9Sopenharmony_ci 43946d528ed9Sopenharmony_ci This concept is somewhat inefficient to express in Ninja (it requires a lot 43956d528ed9Sopenharmony_ci of duplicate of rules) so should only be used when absolutely necessary. 43966d528ed9Sopenharmony_ci``` 43976d528ed9Sopenharmony_ci 43986d528ed9Sopenharmony_ci#### **Example of defining a toolchain** 43996d528ed9Sopenharmony_ci 44006d528ed9Sopenharmony_ci``` 44016d528ed9Sopenharmony_ci toolchain("32") { 44026d528ed9Sopenharmony_ci tool("cc") { 44036d528ed9Sopenharmony_ci command = "gcc {{source}}" 44046d528ed9Sopenharmony_ci ... 44056d528ed9Sopenharmony_ci } 44066d528ed9Sopenharmony_ci 44076d528ed9Sopenharmony_ci toolchain_args = { 44086d528ed9Sopenharmony_ci use_doom_melon = true # Doom melon always required for 32-bit builds. 44096d528ed9Sopenharmony_ci current_cpu = "x86" 44106d528ed9Sopenharmony_ci } 44116d528ed9Sopenharmony_ci } 44126d528ed9Sopenharmony_ci 44136d528ed9Sopenharmony_ci toolchain("64") { 44146d528ed9Sopenharmony_ci tool("cc") { 44156d528ed9Sopenharmony_ci command = "gcc {{source}}" 44166d528ed9Sopenharmony_ci ... 44176d528ed9Sopenharmony_ci } 44186d528ed9Sopenharmony_ci 44196d528ed9Sopenharmony_ci toolchain_args = { 44206d528ed9Sopenharmony_ci # use_doom_melon is not overridden here, it will take the default. 44216d528ed9Sopenharmony_ci current_cpu = "x64" 44226d528ed9Sopenharmony_ci } 44236d528ed9Sopenharmony_ci } 44246d528ed9Sopenharmony_ci``` 44256d528ed9Sopenharmony_ci 44266d528ed9Sopenharmony_ci#### **Example of cross-toolchain dependencies** 44276d528ed9Sopenharmony_ci 44286d528ed9Sopenharmony_ci``` 44296d528ed9Sopenharmony_ci If a 64-bit target wants to depend on a 32-bit binary, it would specify a 44306d528ed9Sopenharmony_ci dependency using data_deps (data deps are like deps that are only needed at 44316d528ed9Sopenharmony_ci runtime and aren't linked, since you can't link a 32-bit and a 64-bit 44326d528ed9Sopenharmony_ci library). 44336d528ed9Sopenharmony_ci 44346d528ed9Sopenharmony_ci executable("my_program") { 44356d528ed9Sopenharmony_ci ... 44366d528ed9Sopenharmony_ci if (target_cpu == "x64") { 44376d528ed9Sopenharmony_ci # The 64-bit build needs this 32-bit helper. 44386d528ed9Sopenharmony_ci data_deps = [ ":helper(//toolchains:32)" ] 44396d528ed9Sopenharmony_ci } 44406d528ed9Sopenharmony_ci } 44416d528ed9Sopenharmony_ci 44426d528ed9Sopenharmony_ci if (target_cpu == "x86") { 44436d528ed9Sopenharmony_ci # Our helper library is only compiled in 32-bits. 44446d528ed9Sopenharmony_ci shared_library("helper") { 44456d528ed9Sopenharmony_ci ... 44466d528ed9Sopenharmony_ci } 44476d528ed9Sopenharmony_ci } 44486d528ed9Sopenharmony_ci``` 44496d528ed9Sopenharmony_ci### <a name="func_write_file"></a>**write_file**: Write a file to disk. 44506d528ed9Sopenharmony_ci 44516d528ed9Sopenharmony_ci``` 44526d528ed9Sopenharmony_ci write_file(filename, data, output_conversion = "") 44536d528ed9Sopenharmony_ci 44546d528ed9Sopenharmony_ci If data is a list, the list will be written one-item-per-line with no quoting 44556d528ed9Sopenharmony_ci or brackets. 44566d528ed9Sopenharmony_ci 44576d528ed9Sopenharmony_ci If the file exists and the contents are identical to that being written, the 44586d528ed9Sopenharmony_ci file will not be updated. This will prevent unnecessary rebuilds of targets 44596d528ed9Sopenharmony_ci that depend on this file. 44606d528ed9Sopenharmony_ci 44616d528ed9Sopenharmony_ci One use for write_file is to write a list of inputs to an script that might 44626d528ed9Sopenharmony_ci be too long for the command line. However, it is preferable to use response 44636d528ed9Sopenharmony_ci files for this purpose. See "gn help response_file_contents". 44646d528ed9Sopenharmony_ci``` 44656d528ed9Sopenharmony_ci 44666d528ed9Sopenharmony_ci#### **Arguments** 44676d528ed9Sopenharmony_ci 44686d528ed9Sopenharmony_ci``` 44696d528ed9Sopenharmony_ci filename 44706d528ed9Sopenharmony_ci Filename to write. This must be within the output directory. 44716d528ed9Sopenharmony_ci 44726d528ed9Sopenharmony_ci data 44736d528ed9Sopenharmony_ci The list or string to write. 44746d528ed9Sopenharmony_ci 44756d528ed9Sopenharmony_ci output_conversion 44766d528ed9Sopenharmony_ci Controls how the output is written. See `gn help io_conversion`. 44776d528ed9Sopenharmony_ci``` 44786d528ed9Sopenharmony_ci## <a name="predefined_variables"></a>Built-in predefined variables 44796d528ed9Sopenharmony_ci 44806d528ed9Sopenharmony_ci### <a name="var_current_cpu"></a>**current_cpu**: The processor architecture of the current toolchain. 44816d528ed9Sopenharmony_ci 44826d528ed9Sopenharmony_ci``` 44836d528ed9Sopenharmony_ci The build configuration usually sets this value based on the value of 44846d528ed9Sopenharmony_ci "host_cpu" (see "gn help host_cpu") and then threads this through the 44856d528ed9Sopenharmony_ci toolchain definitions to ensure that it always reflects the appropriate 44866d528ed9Sopenharmony_ci value. 44876d528ed9Sopenharmony_ci 44886d528ed9Sopenharmony_ci This value is not used internally by GN for any purpose. It is set to the 44896d528ed9Sopenharmony_ci empty string ("") by default but is declared so that it can be overridden on 44906d528ed9Sopenharmony_ci the command line if so desired. 44916d528ed9Sopenharmony_ci 44926d528ed9Sopenharmony_ci See "gn help target_cpu" for a list of common values returned. 44936d528ed9Sopenharmony_ci``` 44946d528ed9Sopenharmony_ci### <a name="var_current_os"></a>**current_os**: The operating system of the current toolchain. 44956d528ed9Sopenharmony_ci 44966d528ed9Sopenharmony_ci``` 44976d528ed9Sopenharmony_ci The build configuration usually sets this value based on the value of 44986d528ed9Sopenharmony_ci "target_os" (see "gn help target_os"), and then threads this through the 44996d528ed9Sopenharmony_ci toolchain definitions to ensure that it always reflects the appropriate 45006d528ed9Sopenharmony_ci value. 45016d528ed9Sopenharmony_ci 45026d528ed9Sopenharmony_ci This value is not used internally by GN for any purpose. It is set to the 45036d528ed9Sopenharmony_ci empty string ("") by default but is declared so that it can be overridden on 45046d528ed9Sopenharmony_ci the command line if so desired. 45056d528ed9Sopenharmony_ci 45066d528ed9Sopenharmony_ci See "gn help target_os" for a list of common values returned. 45076d528ed9Sopenharmony_ci``` 45086d528ed9Sopenharmony_ci### <a name="var_current_toolchain"></a>**current_toolchain**: Label of the current toolchain. 45096d528ed9Sopenharmony_ci 45106d528ed9Sopenharmony_ci``` 45116d528ed9Sopenharmony_ci A fully-qualified label representing the current toolchain. You can use this 45126d528ed9Sopenharmony_ci to make toolchain-related decisions in the build. See also 45136d528ed9Sopenharmony_ci "default_toolchain". 45146d528ed9Sopenharmony_ci``` 45156d528ed9Sopenharmony_ci 45166d528ed9Sopenharmony_ci#### **Example** 45176d528ed9Sopenharmony_ci 45186d528ed9Sopenharmony_ci``` 45196d528ed9Sopenharmony_ci if (current_toolchain == "//build:64_bit_toolchain") { 45206d528ed9Sopenharmony_ci executable("output_thats_64_bit_only") { 45216d528ed9Sopenharmony_ci ... 45226d528ed9Sopenharmony_ci``` 45236d528ed9Sopenharmony_ci### <a name="var_default_toolchain"></a>**default_toolchain**: [string] Label of the default toolchain. 45246d528ed9Sopenharmony_ci 45256d528ed9Sopenharmony_ci``` 45266d528ed9Sopenharmony_ci A fully-qualified label representing the default toolchain, which may not 45276d528ed9Sopenharmony_ci necessarily be the current one (see "current_toolchain"). 45286d528ed9Sopenharmony_ci``` 45296d528ed9Sopenharmony_ci### <a name="var_gn_version"></a>**gn_version**: [number] The version of gn. 45306d528ed9Sopenharmony_ci 45316d528ed9Sopenharmony_ci``` 45326d528ed9Sopenharmony_ci Corresponds to the number printed by `gn --version`. 45336d528ed9Sopenharmony_ci``` 45346d528ed9Sopenharmony_ci 45356d528ed9Sopenharmony_ci#### **Example** 45366d528ed9Sopenharmony_ci 45376d528ed9Sopenharmony_ci``` 45386d528ed9Sopenharmony_ci assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature") 45396d528ed9Sopenharmony_ci``` 45406d528ed9Sopenharmony_ci### <a name="var_host_cpu"></a>**host_cpu**: The processor architecture that GN is running on. 45416d528ed9Sopenharmony_ci 45426d528ed9Sopenharmony_ci``` 45436d528ed9Sopenharmony_ci This is value is exposed so that cross-compile toolchains can access the host 45446d528ed9Sopenharmony_ci architecture when needed. 45456d528ed9Sopenharmony_ci 45466d528ed9Sopenharmony_ci The value should generally be considered read-only, but it can be overridden 45476d528ed9Sopenharmony_ci in order to handle unusual cases where there might be multiple plausible 45486d528ed9Sopenharmony_ci values for the host architecture (e.g., if you can do either 32-bit or 64-bit 45496d528ed9Sopenharmony_ci builds). The value is not used internally by GN for any purpose. 45506d528ed9Sopenharmony_ci``` 45516d528ed9Sopenharmony_ci 45526d528ed9Sopenharmony_ci#### **Some possible values** 45536d528ed9Sopenharmony_ci 45546d528ed9Sopenharmony_ci``` 45556d528ed9Sopenharmony_ci - "x64" 45566d528ed9Sopenharmony_ci - "x86" 45576d528ed9Sopenharmony_ci``` 45586d528ed9Sopenharmony_ci### <a name="var_host_os"></a>**host_os**: [string] The operating system that GN is running on. 45596d528ed9Sopenharmony_ci 45606d528ed9Sopenharmony_ci``` 45616d528ed9Sopenharmony_ci This value is exposed so that cross-compiles can access the host build 45626d528ed9Sopenharmony_ci system's settings. 45636d528ed9Sopenharmony_ci 45646d528ed9Sopenharmony_ci This value should generally be treated as read-only. It, however, is not used 45656d528ed9Sopenharmony_ci internally by GN for any purpose. 45666d528ed9Sopenharmony_ci``` 45676d528ed9Sopenharmony_ci 45686d528ed9Sopenharmony_ci#### **Some possible values** 45696d528ed9Sopenharmony_ci 45706d528ed9Sopenharmony_ci``` 45716d528ed9Sopenharmony_ci - "linux" 45726d528ed9Sopenharmony_ci - "mac" 45736d528ed9Sopenharmony_ci - "win" 45746d528ed9Sopenharmony_ci``` 45756d528ed9Sopenharmony_ci### <a name="var_invoker"></a>**invoker**: [string] The invoking scope inside a template. 45766d528ed9Sopenharmony_ci 45776d528ed9Sopenharmony_ci``` 45786d528ed9Sopenharmony_ci Inside a template invocation, this variable refers to the scope of the 45796d528ed9Sopenharmony_ci invoker of the template. Outside of template invocations, this variable is 45806d528ed9Sopenharmony_ci undefined. 45816d528ed9Sopenharmony_ci 45826d528ed9Sopenharmony_ci All of the variables defined inside the template invocation are accessible as 45836d528ed9Sopenharmony_ci members of the "invoker" scope. This is the way that templates read values 45846d528ed9Sopenharmony_ci set by the callers. 45856d528ed9Sopenharmony_ci 45866d528ed9Sopenharmony_ci This is often used with "defined" to see if a value is set on the invoking 45876d528ed9Sopenharmony_ci scope. 45886d528ed9Sopenharmony_ci 45896d528ed9Sopenharmony_ci See "gn help template" for more examples. 45906d528ed9Sopenharmony_ci``` 45916d528ed9Sopenharmony_ci 45926d528ed9Sopenharmony_ci#### **Example** 45936d528ed9Sopenharmony_ci 45946d528ed9Sopenharmony_ci``` 45956d528ed9Sopenharmony_ci template("my_template") { 45966d528ed9Sopenharmony_ci print(invoker.sources) # Prints [ "a.cc", "b.cc" ] 45976d528ed9Sopenharmony_ci print(defined(invoker.foo)) # Prints false. 45986d528ed9Sopenharmony_ci print(defined(invoker.bar)) # Prints true. 45996d528ed9Sopenharmony_ci } 46006d528ed9Sopenharmony_ci 46016d528ed9Sopenharmony_ci my_template("doom_melon") { 46026d528ed9Sopenharmony_ci sources = [ "a.cc", "b.cc" ] 46036d528ed9Sopenharmony_ci bar = 123 46046d528ed9Sopenharmony_ci } 46056d528ed9Sopenharmony_ci``` 46066d528ed9Sopenharmony_ci### <a name="var_python_path"></a>**python_path**: Absolute path of Python. 46076d528ed9Sopenharmony_ci 46086d528ed9Sopenharmony_ci``` 46096d528ed9Sopenharmony_ci Normally used in toolchain definitions if running some command requires 46106d528ed9Sopenharmony_ci Python. You will normally not need this when invoking scripts since GN 46116d528ed9Sopenharmony_ci automatically finds it for you. 46126d528ed9Sopenharmony_ci``` 46136d528ed9Sopenharmony_ci### <a name="var_root_build_dir"></a>**root_build_dir**: [string] Directory where build commands are run. 46146d528ed9Sopenharmony_ci 46156d528ed9Sopenharmony_ci``` 46166d528ed9Sopenharmony_ci This is the root build output directory which will be the current directory 46176d528ed9Sopenharmony_ci when executing all compilers and scripts. 46186d528ed9Sopenharmony_ci 46196d528ed9Sopenharmony_ci Most often this is used with rebase_path (see "gn help rebase_path") to 46206d528ed9Sopenharmony_ci convert arguments to be relative to a script's current directory. 46216d528ed9Sopenharmony_ci``` 46226d528ed9Sopenharmony_ci### <a name="var_root_gen_dir"></a>**root_gen_dir**: Directory for the toolchain's generated files. 46236d528ed9Sopenharmony_ci 46246d528ed9Sopenharmony_ci``` 46256d528ed9Sopenharmony_ci Absolute path to the root of the generated output directory tree for the 46266d528ed9Sopenharmony_ci current toolchain. An example would be "//out/Debug/gen" for the default 46276d528ed9Sopenharmony_ci toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain. 46286d528ed9Sopenharmony_ci 46296d528ed9Sopenharmony_ci This is primarily useful for setting up include paths for generated files. If 46306d528ed9Sopenharmony_ci you are passing this to a script, you will want to pass it through 46316d528ed9Sopenharmony_ci rebase_path() (see "gn help rebase_path") to convert it to be relative to the 46326d528ed9Sopenharmony_ci build directory. 46336d528ed9Sopenharmony_ci 46346d528ed9Sopenharmony_ci See also "target_gen_dir" which is usually a better location for generated 46356d528ed9Sopenharmony_ci files. It will be inside the root generated dir. 46366d528ed9Sopenharmony_ci``` 46376d528ed9Sopenharmony_ci### <a name="var_root_out_dir"></a>**root_out_dir**: [string] Root directory for toolchain output files. 46386d528ed9Sopenharmony_ci 46396d528ed9Sopenharmony_ci``` 46406d528ed9Sopenharmony_ci Absolute path to the root of the output directory tree for the current 46416d528ed9Sopenharmony_ci toolchain. It will not have a trailing slash. 46426d528ed9Sopenharmony_ci 46436d528ed9Sopenharmony_ci For the default toolchain this will be the same as the root_build_dir. An 46446d528ed9Sopenharmony_ci example would be "//out/Debug" for the default toolchain, or 46456d528ed9Sopenharmony_ci "//out/Debug/arm" for the "arm" toolchain. 46466d528ed9Sopenharmony_ci 46476d528ed9Sopenharmony_ci This is primarily useful for setting up script calls. If you are passing this 46486d528ed9Sopenharmony_ci to a script, you will want to pass it through rebase_path() (see "gn help 46496d528ed9Sopenharmony_ci rebase_path") to convert it to be relative to the build directory. 46506d528ed9Sopenharmony_ci 46516d528ed9Sopenharmony_ci See also "target_out_dir" which is usually a better location for output 46526d528ed9Sopenharmony_ci files. It will be inside the root output dir. 46536d528ed9Sopenharmony_ci``` 46546d528ed9Sopenharmony_ci 46556d528ed9Sopenharmony_ci#### **Example** 46566d528ed9Sopenharmony_ci 46576d528ed9Sopenharmony_ci``` 46586d528ed9Sopenharmony_ci action("myscript") { 46596d528ed9Sopenharmony_ci # Pass the output dir to the script. 46606d528ed9Sopenharmony_ci args = [ "-o", rebase_path(root_out_dir, root_build_dir) ] 46616d528ed9Sopenharmony_ci } 46626d528ed9Sopenharmony_ci``` 46636d528ed9Sopenharmony_ci### <a name="var_target_cpu"></a>**target_cpu**: The desired cpu architecture for the build. 46646d528ed9Sopenharmony_ci 46656d528ed9Sopenharmony_ci``` 46666d528ed9Sopenharmony_ci This value should be used to indicate the desired architecture for the 46676d528ed9Sopenharmony_ci primary objects of the build. It will match the cpu architecture of the 46686d528ed9Sopenharmony_ci default toolchain, but not necessarily the current toolchain. 46696d528ed9Sopenharmony_ci 46706d528ed9Sopenharmony_ci In many cases, this is the same as "host_cpu", but in the case of 46716d528ed9Sopenharmony_ci cross-compiles, this can be set to something different. This value is 46726d528ed9Sopenharmony_ci different from "current_cpu" in that it does not change based on the current 46736d528ed9Sopenharmony_ci toolchain. When writing rules, "current_cpu" should be used rather than 46746d528ed9Sopenharmony_ci "target_cpu" most of the time. 46756d528ed9Sopenharmony_ci 46766d528ed9Sopenharmony_ci This value is not used internally by GN for any purpose, so it may be set to 46776d528ed9Sopenharmony_ci whatever value is needed for the build. GN defaults this value to the empty 46786d528ed9Sopenharmony_ci string ("") and the configuration files should set it to an appropriate value 46796d528ed9Sopenharmony_ci (e.g., setting it to the value of "host_cpu") if it is not overridden on the 46806d528ed9Sopenharmony_ci command line or in the args.gn file. 46816d528ed9Sopenharmony_ci``` 46826d528ed9Sopenharmony_ci 46836d528ed9Sopenharmony_ci#### **Possible values** 46846d528ed9Sopenharmony_ci 46856d528ed9Sopenharmony_ci``` 46866d528ed9Sopenharmony_ci - "x86" 46876d528ed9Sopenharmony_ci - "x64" 46886d528ed9Sopenharmony_ci - "arm" 46896d528ed9Sopenharmony_ci - "arm64" 46906d528ed9Sopenharmony_ci - "mipsel" 46916d528ed9Sopenharmony_ci - "mips64el" 46926d528ed9Sopenharmony_ci - "s390x" 46936d528ed9Sopenharmony_ci - "ppc64" 46946d528ed9Sopenharmony_ci - "riscv32" 46956d528ed9Sopenharmony_ci - "riscv64" 46966d528ed9Sopenharmony_ci - "e2k" 46976d528ed9Sopenharmony_ci - "loong64" 46986d528ed9Sopenharmony_ci``` 46996d528ed9Sopenharmony_ci### <a name="var_target_gen_dir"></a>**target_gen_dir**: Directory for a target's generated files. 47006d528ed9Sopenharmony_ci 47016d528ed9Sopenharmony_ci``` 47026d528ed9Sopenharmony_ci Absolute path to the target's generated file directory. This will be the 47036d528ed9Sopenharmony_ci "root_gen_dir" followed by the relative path to the current build file. If 47046d528ed9Sopenharmony_ci your file is in "//tools/doom_melon" then target_gen_dir would be 47056d528ed9Sopenharmony_ci "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash. 47066d528ed9Sopenharmony_ci 47076d528ed9Sopenharmony_ci This is primarily useful for setting up include paths for generated files. If 47086d528ed9Sopenharmony_ci you are passing this to a script, you will want to pass it through 47096d528ed9Sopenharmony_ci rebase_path() (see "gn help rebase_path") to convert it to be relative to the 47106d528ed9Sopenharmony_ci build directory. 47116d528ed9Sopenharmony_ci 47126d528ed9Sopenharmony_ci See also "gn help root_gen_dir". 47136d528ed9Sopenharmony_ci``` 47146d528ed9Sopenharmony_ci 47156d528ed9Sopenharmony_ci#### **Example** 47166d528ed9Sopenharmony_ci 47176d528ed9Sopenharmony_ci``` 47186d528ed9Sopenharmony_ci action("myscript") { 47196d528ed9Sopenharmony_ci # Pass the generated output dir to the script. 47206d528ed9Sopenharmony_ci args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ] 47216d528ed9Sopenharmony_ci } 47226d528ed9Sopenharmony_ci``` 47236d528ed9Sopenharmony_ci### <a name="var_target_name"></a>**target_name**: [string] The name of the current target. 47246d528ed9Sopenharmony_ci 47256d528ed9Sopenharmony_ci``` 47266d528ed9Sopenharmony_ci Inside a target or template invocation, this variable refers to the name 47276d528ed9Sopenharmony_ci given to the target or template invocation. Outside of these, this variable 47286d528ed9Sopenharmony_ci is undefined. 47296d528ed9Sopenharmony_ci 47306d528ed9Sopenharmony_ci This is most often used in template definitions to name targets defined in 47316d528ed9Sopenharmony_ci the template based on the name of the invocation. This is necessary both to 47326d528ed9Sopenharmony_ci ensure generated targets have unique names and to generate a target with the 47336d528ed9Sopenharmony_ci exact name of the invocation that other targets can depend on. 47346d528ed9Sopenharmony_ci 47356d528ed9Sopenharmony_ci Be aware that this value will always reflect the innermost scope. So when 47366d528ed9Sopenharmony_ci defining a target inside a template, target_name will refer to the target 47376d528ed9Sopenharmony_ci rather than the template invocation. To get the name of the template 47386d528ed9Sopenharmony_ci invocation in this case, you should save target_name to a temporary variable 47396d528ed9Sopenharmony_ci outside of any target definitions. 47406d528ed9Sopenharmony_ci 47416d528ed9Sopenharmony_ci See "gn help template" for more examples. 47426d528ed9Sopenharmony_ci``` 47436d528ed9Sopenharmony_ci 47446d528ed9Sopenharmony_ci#### **Example** 47456d528ed9Sopenharmony_ci 47466d528ed9Sopenharmony_ci``` 47476d528ed9Sopenharmony_ci executable("doom_melon") { 47486d528ed9Sopenharmony_ci print(target_name) # Prints "doom_melon". 47496d528ed9Sopenharmony_ci } 47506d528ed9Sopenharmony_ci 47516d528ed9Sopenharmony_ci template("my_template") { 47526d528ed9Sopenharmony_ci print(target_name) # Prints "space_ray" when invoked below. 47536d528ed9Sopenharmony_ci 47546d528ed9Sopenharmony_ci executable(target_name + "_impl") { 47556d528ed9Sopenharmony_ci print(target_name) # Prints "space_ray_impl". 47566d528ed9Sopenharmony_ci } 47576d528ed9Sopenharmony_ci } 47586d528ed9Sopenharmony_ci 47596d528ed9Sopenharmony_ci my_template("space_ray") { 47606d528ed9Sopenharmony_ci } 47616d528ed9Sopenharmony_ci``` 47626d528ed9Sopenharmony_ci### <a name="var_target_os"></a>**target_os**: The desired operating system for the build. 47636d528ed9Sopenharmony_ci 47646d528ed9Sopenharmony_ci``` 47656d528ed9Sopenharmony_ci This value should be used to indicate the desired operating system for the 47666d528ed9Sopenharmony_ci primary object(s) of the build. It will match the OS of the default 47676d528ed9Sopenharmony_ci toolchain. 47686d528ed9Sopenharmony_ci 47696d528ed9Sopenharmony_ci In many cases, this is the same as "host_os", but in the case of 47706d528ed9Sopenharmony_ci cross-compiles, it may be different. This variable differs from "current_os" 47716d528ed9Sopenharmony_ci in that it can be referenced from inside any toolchain and will always return 47726d528ed9Sopenharmony_ci the initial value. 47736d528ed9Sopenharmony_ci 47746d528ed9Sopenharmony_ci This should be set to the most specific value possible. So, "android" or 47756d528ed9Sopenharmony_ci "chromeos" should be used instead of "linux" where applicable, even though 47766d528ed9Sopenharmony_ci Android and ChromeOS are both Linux variants. This can mean that one needs to 47776d528ed9Sopenharmony_ci write 47786d528ed9Sopenharmony_ci 47796d528ed9Sopenharmony_ci if (target_os == "android" || target_os == "linux") { 47806d528ed9Sopenharmony_ci # ... 47816d528ed9Sopenharmony_ci } 47826d528ed9Sopenharmony_ci 47836d528ed9Sopenharmony_ci and so forth. 47846d528ed9Sopenharmony_ci 47856d528ed9Sopenharmony_ci This value is not used internally by GN for any purpose, so it may be set to 47866d528ed9Sopenharmony_ci whatever value is needed for the build. GN defaults this value to the empty 47876d528ed9Sopenharmony_ci string ("") and the configuration files should set it to an appropriate value 47886d528ed9Sopenharmony_ci (e.g., setting it to the value of "host_os") if it is not set via the command 47896d528ed9Sopenharmony_ci line or in the args.gn file. 47906d528ed9Sopenharmony_ci``` 47916d528ed9Sopenharmony_ci 47926d528ed9Sopenharmony_ci#### **Possible values** 47936d528ed9Sopenharmony_ci 47946d528ed9Sopenharmony_ci``` 47956d528ed9Sopenharmony_ci - "android" 47966d528ed9Sopenharmony_ci - "chromeos" 47976d528ed9Sopenharmony_ci - "ios" 47986d528ed9Sopenharmony_ci - "linux" 47996d528ed9Sopenharmony_ci - "nacl" 48006d528ed9Sopenharmony_ci - "mac" 48016d528ed9Sopenharmony_ci - "win" 48026d528ed9Sopenharmony_ci``` 48036d528ed9Sopenharmony_ci### <a name="var_target_out_dir"></a>**target_out_dir**: [string] Directory for target output files. 48046d528ed9Sopenharmony_ci 48056d528ed9Sopenharmony_ci``` 48066d528ed9Sopenharmony_ci Absolute path to the target's generated file directory. If your current 48076d528ed9Sopenharmony_ci target is in "//tools/doom_melon" then this value might be 48086d528ed9Sopenharmony_ci "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash. 48096d528ed9Sopenharmony_ci 48106d528ed9Sopenharmony_ci This is primarily useful for setting up arguments for calling scripts. If you 48116d528ed9Sopenharmony_ci are passing this to a script, you will want to pass it through rebase_path() 48126d528ed9Sopenharmony_ci (see "gn help rebase_path") to convert it to be relative to the build 48136d528ed9Sopenharmony_ci directory. 48146d528ed9Sopenharmony_ci 48156d528ed9Sopenharmony_ci See also "gn help root_out_dir". 48166d528ed9Sopenharmony_ci``` 48176d528ed9Sopenharmony_ci 48186d528ed9Sopenharmony_ci#### **Example** 48196d528ed9Sopenharmony_ci 48206d528ed9Sopenharmony_ci``` 48216d528ed9Sopenharmony_ci action("myscript") { 48226d528ed9Sopenharmony_ci # Pass the output dir to the script. 48236d528ed9Sopenharmony_ci args = [ "-o", rebase_path(target_out_dir, root_build_dir) ] 48246d528ed9Sopenharmony_ci } 48256d528ed9Sopenharmony_ci``` 48266d528ed9Sopenharmony_ci## <a name="target_variables"></a>Variables you set in targets 48276d528ed9Sopenharmony_ci 48286d528ed9Sopenharmony_ci### <a name="var_aliased_deps"></a>**aliased_deps**: [scope] Set of crate-dependency pairs. 48296d528ed9Sopenharmony_ci 48306d528ed9Sopenharmony_ci``` 48316d528ed9Sopenharmony_ci Valid for `rust_library` targets and `executable`, `static_library`, and 48326d528ed9Sopenharmony_ci `shared_library` targets that contain Rust sources. 48336d528ed9Sopenharmony_ci 48346d528ed9Sopenharmony_ci A scope, each key indicating the renamed crate and the corresponding value 48356d528ed9Sopenharmony_ci specifying the label of the dependency producing the relevant binary. 48366d528ed9Sopenharmony_ci 48376d528ed9Sopenharmony_ci All dependencies listed in this field *must* be listed as deps of the target. 48386d528ed9Sopenharmony_ci 48396d528ed9Sopenharmony_ci executable("foo") { 48406d528ed9Sopenharmony_ci sources = [ "main.rs" ] 48416d528ed9Sopenharmony_ci deps = [ "//bar" ] 48426d528ed9Sopenharmony_ci } 48436d528ed9Sopenharmony_ci 48446d528ed9Sopenharmony_ci This target would compile the `foo` crate with the following `extern` flag: 48456d528ed9Sopenharmony_ci `rustc ...command... --extern bar=<build_out_dir>/obj/bar` 48466d528ed9Sopenharmony_ci 48476d528ed9Sopenharmony_ci executable("foo") { 48486d528ed9Sopenharmony_ci sources = [ "main.rs" ] 48496d528ed9Sopenharmony_ci deps = [ ":bar" ] 48506d528ed9Sopenharmony_ci aliased_deps = { 48516d528ed9Sopenharmony_ci bar_renamed = ":bar" 48526d528ed9Sopenharmony_ci } 48536d528ed9Sopenharmony_ci } 48546d528ed9Sopenharmony_ci 48556d528ed9Sopenharmony_ci With the addition of `aliased_deps`, above target would instead compile with: 48566d528ed9Sopenharmony_ci `rustc ...command... --extern bar_renamed=<build_out_dir>/obj/bar` 48576d528ed9Sopenharmony_ci``` 48586d528ed9Sopenharmony_ci### <a name="var_all_dependent_configs"></a>**all_dependent_configs**: Configs to be forced on dependents. 48596d528ed9Sopenharmony_ci 48606d528ed9Sopenharmony_ci``` 48616d528ed9Sopenharmony_ci A list of config labels. 48626d528ed9Sopenharmony_ci 48636d528ed9Sopenharmony_ci All targets depending on this one, and recursively, all targets depending on 48646d528ed9Sopenharmony_ci those, will have the configs listed in this variable added to them. These 48656d528ed9Sopenharmony_ci configs will also apply to the current target. 48666d528ed9Sopenharmony_ci 48676d528ed9Sopenharmony_ci This addition happens in a second phase once a target and all of its 48686d528ed9Sopenharmony_ci dependencies have been resolved. Therefore, a target will not see these 48696d528ed9Sopenharmony_ci force-added configs in their "configs" variable while the script is running, 48706d528ed9Sopenharmony_ci and they can not be removed. As a result, this capability should generally 48716d528ed9Sopenharmony_ci only be used to add defines and include directories necessary to compile a 48726d528ed9Sopenharmony_ci target's headers. 48736d528ed9Sopenharmony_ci 48746d528ed9Sopenharmony_ci See also "public_configs". 48756d528ed9Sopenharmony_ci``` 48766d528ed9Sopenharmony_ci 48776d528ed9Sopenharmony_ci#### **Ordering of flags and values** 48786d528ed9Sopenharmony_ci 48796d528ed9Sopenharmony_ci``` 48806d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 48816d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 48826d528ed9Sopenharmony_ci configs appear in the list. 48836d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 48846d528ed9Sopenharmony_ci that the configs appear in the list. 48856d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 48866d528ed9Sopenharmony_ci those configs appear in the list. 48876d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 48886d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 48896d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 48906d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 48916d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 48926d528ed9Sopenharmony_ci recursively. 48936d528ed9Sopenharmony_ci``` 48946d528ed9Sopenharmony_ci### <a name="var_allow_circular_includes_from"></a>**allow_circular_includes_from**: Permit includes from deps. 48956d528ed9Sopenharmony_ci 48966d528ed9Sopenharmony_ci``` 48976d528ed9Sopenharmony_ci A list of target labels. Must be a subset of the target's "deps". These 48986d528ed9Sopenharmony_ci targets will be permitted to include headers from the current target despite 48996d528ed9Sopenharmony_ci the dependency going in the opposite direction. 49006d528ed9Sopenharmony_ci 49016d528ed9Sopenharmony_ci When you use this, both targets must be included in a final binary for it to 49026d528ed9Sopenharmony_ci link. To keep linker errors from happening, it is good practice to have all 49036d528ed9Sopenharmony_ci external dependencies depend only on one of the two targets, and to set the 49046d528ed9Sopenharmony_ci visibility on the other to enforce this. Thus the targets will always be 49056d528ed9Sopenharmony_ci linked together in any output. 49066d528ed9Sopenharmony_ci``` 49076d528ed9Sopenharmony_ci 49086d528ed9Sopenharmony_ci#### **Details** 49096d528ed9Sopenharmony_ci 49106d528ed9Sopenharmony_ci``` 49116d528ed9Sopenharmony_ci Normally, for a file in target A to include a file from target B, A must list 49126d528ed9Sopenharmony_ci B as a dependency. This invariant is enforced by the "gn check" command (and 49136d528ed9Sopenharmony_ci the --check flag to "gn gen" -- see "gn help check"). 49146d528ed9Sopenharmony_ci 49156d528ed9Sopenharmony_ci Sometimes, two targets might be the same unit for linking purposes (two 49166d528ed9Sopenharmony_ci source sets or static libraries that would always be linked together in a 49176d528ed9Sopenharmony_ci final executable or shared library) and they each include headers from the 49186d528ed9Sopenharmony_ci other: you want A to be able to include B's headers, and B to include A's 49196d528ed9Sopenharmony_ci headers. This is not an ideal situation but is sometimes unavoidable. 49206d528ed9Sopenharmony_ci 49216d528ed9Sopenharmony_ci This list, if specified, lists which of the dependencies of the current 49226d528ed9Sopenharmony_ci target can include header files from the current target. That is, if A 49236d528ed9Sopenharmony_ci depends on B, B can only include headers from A if it is in A's 49246d528ed9Sopenharmony_ci allow_circular_includes_from list. Normally includes must follow the 49256d528ed9Sopenharmony_ci direction of dependencies, this flag allows them to go in the opposite 49266d528ed9Sopenharmony_ci direction. 49276d528ed9Sopenharmony_ci``` 49286d528ed9Sopenharmony_ci 49296d528ed9Sopenharmony_ci#### **Danger** 49306d528ed9Sopenharmony_ci 49316d528ed9Sopenharmony_ci``` 49326d528ed9Sopenharmony_ci In the above example, A's headers are likely to include headers from A's 49336d528ed9Sopenharmony_ci dependencies. Those dependencies may have public_configs that apply flags, 49346d528ed9Sopenharmony_ci defines, and include paths that make those headers work properly. 49356d528ed9Sopenharmony_ci 49366d528ed9Sopenharmony_ci With allow_circular_includes_from, B can include A's headers, and 49376d528ed9Sopenharmony_ci transitively from A's dependencies, without having the dependencies that 49386d528ed9Sopenharmony_ci would bring in the public_configs those headers need. The result may be 49396d528ed9Sopenharmony_ci errors or inconsistent builds. 49406d528ed9Sopenharmony_ci 49416d528ed9Sopenharmony_ci So when you use allow_circular_includes_from, make sure that any compiler 49426d528ed9Sopenharmony_ci settings, flags, and include directories are the same between both targets 49436d528ed9Sopenharmony_ci (consider putting such things in a shared config they can both reference). 49446d528ed9Sopenharmony_ci Make sure the dependencies are also the same (you might consider a group to 49456d528ed9Sopenharmony_ci collect such dependencies they both depend on). 49466d528ed9Sopenharmony_ci``` 49476d528ed9Sopenharmony_ci 49486d528ed9Sopenharmony_ci#### **Example** 49496d528ed9Sopenharmony_ci 49506d528ed9Sopenharmony_ci``` 49516d528ed9Sopenharmony_ci source_set("a") { 49526d528ed9Sopenharmony_ci deps = [ ":b", ":a_b_shared_deps" ] 49536d528ed9Sopenharmony_ci allow_circular_includes_from = [ ":b" ] 49546d528ed9Sopenharmony_ci ... 49556d528ed9Sopenharmony_ci } 49566d528ed9Sopenharmony_ci 49576d528ed9Sopenharmony_ci source_set("b") { 49586d528ed9Sopenharmony_ci deps = [ ":a_b_shared_deps" ] 49596d528ed9Sopenharmony_ci # Sources here can include headers from a despite lack of deps. 49606d528ed9Sopenharmony_ci ... 49616d528ed9Sopenharmony_ci } 49626d528ed9Sopenharmony_ci 49636d528ed9Sopenharmony_ci group("a_b_shared_deps") { 49646d528ed9Sopenharmony_ci public_deps = [ ":c" ] 49656d528ed9Sopenharmony_ci } 49666d528ed9Sopenharmony_ci``` 49676d528ed9Sopenharmony_ci### <a name="var_arflags"></a>**arflags**: Arguments passed to static_library archiver. 49686d528ed9Sopenharmony_ci 49696d528ed9Sopenharmony_ci``` 49706d528ed9Sopenharmony_ci A list of flags passed to the archive/lib command that creates static 49716d528ed9Sopenharmony_ci libraries. 49726d528ed9Sopenharmony_ci 49736d528ed9Sopenharmony_ci arflags are NOT pushed to dependents, so applying arflags to source sets or 49746d528ed9Sopenharmony_ci any other target type will be a no-op. As with ldflags, you could put the 49756d528ed9Sopenharmony_ci arflags in a config and set that as a public or "all dependent" config, but 49766d528ed9Sopenharmony_ci that will likely not be what you want. If you have a chain of static 49776d528ed9Sopenharmony_ci libraries dependent on each other, this can cause the flags to propagate up 49786d528ed9Sopenharmony_ci to other static libraries. Due to the nature of how arflags are typically 49796d528ed9Sopenharmony_ci used, you will normally want to apply them directly on static_library targets 49806d528ed9Sopenharmony_ci themselves. 49816d528ed9Sopenharmony_ci``` 49826d528ed9Sopenharmony_ci 49836d528ed9Sopenharmony_ci#### **Ordering of flags and values** 49846d528ed9Sopenharmony_ci 49856d528ed9Sopenharmony_ci``` 49866d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 49876d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 49886d528ed9Sopenharmony_ci configs appear in the list. 49896d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 49906d528ed9Sopenharmony_ci that the configs appear in the list. 49916d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 49926d528ed9Sopenharmony_ci those configs appear in the list. 49936d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 49946d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 49956d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 49966d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 49976d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 49986d528ed9Sopenharmony_ci recursively. 49996d528ed9Sopenharmony_ci``` 50006d528ed9Sopenharmony_ci### <a name="var_args"></a>**args**: (target variable) Arguments passed to an action. 50016d528ed9Sopenharmony_ci 50026d528ed9Sopenharmony_ci``` 50036d528ed9Sopenharmony_ci For action and action_foreach targets, args is the list of arguments to pass 50046d528ed9Sopenharmony_ci to the script. Typically you would use source expansion (see "gn help 50056d528ed9Sopenharmony_ci source_expansion") to insert the source file names. 50066d528ed9Sopenharmony_ci 50076d528ed9Sopenharmony_ci Args can also expand the substitution patterns corresponding to config 50086d528ed9Sopenharmony_ci variables in the same way that compiler tools (see "gn help tool") do. These 50096d528ed9Sopenharmony_ci allow actions that run compiler or compiler-like tools to access the results 50106d528ed9Sopenharmony_ci of propagating configs through the build graph. For example: 50116d528ed9Sopenharmony_ci 50126d528ed9Sopenharmony_ci args = [ "{{defines}}", "{{include_dirs}}", "{{rustenv}}", "--input-file", 50136d528ed9Sopenharmony_ci "{{source}}" ] 50146d528ed9Sopenharmony_ci 50156d528ed9Sopenharmony_ci See also "gn help action" and "gn help action_foreach". 50166d528ed9Sopenharmony_ci``` 50176d528ed9Sopenharmony_ci### <a name="var_asmflags"></a>**asmflags**: Flags passed to the assembler. 50186d528ed9Sopenharmony_ci 50196d528ed9Sopenharmony_ci``` 50206d528ed9Sopenharmony_ci A list of strings. 50216d528ed9Sopenharmony_ci 50226d528ed9Sopenharmony_ci "asmflags" are passed to any invocation of a tool that takes an .asm or .S 50236d528ed9Sopenharmony_ci file as input. 50246d528ed9Sopenharmony_ci``` 50256d528ed9Sopenharmony_ci 50266d528ed9Sopenharmony_ci#### **Ordering of flags and values** 50276d528ed9Sopenharmony_ci 50286d528ed9Sopenharmony_ci``` 50296d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 50306d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 50316d528ed9Sopenharmony_ci configs appear in the list. 50326d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 50336d528ed9Sopenharmony_ci that the configs appear in the list. 50346d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 50356d528ed9Sopenharmony_ci those configs appear in the list. 50366d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 50376d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 50386d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 50396d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 50406d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 50416d528ed9Sopenharmony_ci recursively. 50426d528ed9Sopenharmony_ci``` 50436d528ed9Sopenharmony_ci### <a name="var_assert_no_deps"></a>**assert_no_deps**: Ensure no deps on these targets. 50446d528ed9Sopenharmony_ci 50456d528ed9Sopenharmony_ci``` 50466d528ed9Sopenharmony_ci A list of label patterns. 50476d528ed9Sopenharmony_ci 50486d528ed9Sopenharmony_ci This list is a list of patterns that must not match any of the transitive 50496d528ed9Sopenharmony_ci dependencies of the target. These include all public, private, and data 50506d528ed9Sopenharmony_ci dependencies, and cross shared library boundaries. This allows you to express 50516d528ed9Sopenharmony_ci that undesirable code isn't accidentally added to downstream dependencies in 50526d528ed9Sopenharmony_ci a way that might otherwise be difficult to notice. 50536d528ed9Sopenharmony_ci 50546d528ed9Sopenharmony_ci Checking does not cross executable boundaries. If a target depends on an 50556d528ed9Sopenharmony_ci executable, it's assumed that the executable is a tool that is producing part 50566d528ed9Sopenharmony_ci of the build rather than something that is linked and distributed. This 50576d528ed9Sopenharmony_ci allows assert_no_deps to express what is distributed in the final target 50586d528ed9Sopenharmony_ci rather than depend on the internal build steps (which may include 50596d528ed9Sopenharmony_ci non-distributable code). 50606d528ed9Sopenharmony_ci 50616d528ed9Sopenharmony_ci See "gn help label_pattern" for the format of the entries in the list. These 50626d528ed9Sopenharmony_ci patterns allow blacklisting individual targets or whole directory 50636d528ed9Sopenharmony_ci hierarchies. 50646d528ed9Sopenharmony_ci 50656d528ed9Sopenharmony_ci Sometimes it is desirable to enforce that many targets have no dependencies 50666d528ed9Sopenharmony_ci on a target or set of targets. One efficient way to express this is to create 50676d528ed9Sopenharmony_ci a group with the assert_no_deps rule on it, and make that group depend on all 50686d528ed9Sopenharmony_ci targets you want to apply that assertion to. 50696d528ed9Sopenharmony_ci``` 50706d528ed9Sopenharmony_ci 50716d528ed9Sopenharmony_ci#### **Example** 50726d528ed9Sopenharmony_ci 50736d528ed9Sopenharmony_ci``` 50746d528ed9Sopenharmony_ci executable("doom_melon") { 50756d528ed9Sopenharmony_ci deps = [ "//foo:bar" ] 50766d528ed9Sopenharmony_ci ... 50776d528ed9Sopenharmony_ci assert_no_deps = [ 50786d528ed9Sopenharmony_ci "//evil/*", # Don't link any code from the evil directory. 50796d528ed9Sopenharmony_ci "//foo:test_support", # This target is also disallowed. 50806d528ed9Sopenharmony_ci ] 50816d528ed9Sopenharmony_ci } 50826d528ed9Sopenharmony_ci``` 50836d528ed9Sopenharmony_ci### <a name="var_bridge_header"></a>**bridge_header**: [string] Path to C/Objective-C compatibility header. 50846d528ed9Sopenharmony_ci 50856d528ed9Sopenharmony_ci``` 50866d528ed9Sopenharmony_ci Valid for binary targets that contain Swift sources. 50876d528ed9Sopenharmony_ci 50886d528ed9Sopenharmony_ci Path to an header that includes C/Objective-C functions and types that 50896d528ed9Sopenharmony_ci needs to be made available to the Swift module. 50906d528ed9Sopenharmony_ci``` 50916d528ed9Sopenharmony_ci### <a name="var_bundle_contents_dir"></a>**bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in 50926d528ed9Sopenharmony_ci``` 50936d528ed9Sopenharmony_ci create_bundle. 50946d528ed9Sopenharmony_ci 50956d528ed9Sopenharmony_ci A string corresponding to a path in $root_build_dir. 50966d528ed9Sopenharmony_ci 50976d528ed9Sopenharmony_ci This string is used by the "create_bundle" target to expand the 50986d528ed9Sopenharmony_ci {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must 50996d528ed9Sopenharmony_ci correspond to a path under "bundle_root_dir". 51006d528ed9Sopenharmony_ci 51016d528ed9Sopenharmony_ci See "gn help bundle_root_dir" for examples. 51026d528ed9Sopenharmony_ci``` 51036d528ed9Sopenharmony_ci### <a name="var_bundle_deps_filter"></a>**bundle_deps_filter**: [label list] A list of labels that are filtered out. 51046d528ed9Sopenharmony_ci 51056d528ed9Sopenharmony_ci``` 51066d528ed9Sopenharmony_ci A list of target labels. 51076d528ed9Sopenharmony_ci 51086d528ed9Sopenharmony_ci This list contains target label patterns that should be filtered out when 51096d528ed9Sopenharmony_ci creating the bundle. Any target matching one of those label will be removed 51106d528ed9Sopenharmony_ci from the dependencies of the create_bundle target. 51116d528ed9Sopenharmony_ci 51126d528ed9Sopenharmony_ci This is mostly useful when creating application extension bundle as the 51136d528ed9Sopenharmony_ci application extension has access to runtime resources from the application 51146d528ed9Sopenharmony_ci bundle and thus do not require a second copy. 51156d528ed9Sopenharmony_ci 51166d528ed9Sopenharmony_ci See "gn help create_bundle" for more information. 51176d528ed9Sopenharmony_ci``` 51186d528ed9Sopenharmony_ci 51196d528ed9Sopenharmony_ci#### **Example** 51206d528ed9Sopenharmony_ci 51216d528ed9Sopenharmony_ci``` 51226d528ed9Sopenharmony_ci create_bundle("today_extension") { 51236d528ed9Sopenharmony_ci deps = [ 51246d528ed9Sopenharmony_ci "//base" 51256d528ed9Sopenharmony_ci ] 51266d528ed9Sopenharmony_ci bundle_root_dir = "$root_out_dir/today_extension.appex" 51276d528ed9Sopenharmony_ci bundle_deps_filter = [ 51286d528ed9Sopenharmony_ci # The extension uses //base but does not use any function calling into 51296d528ed9Sopenharmony_ci # third_party/icu and thus does not need the icudtl.dat file. 51306d528ed9Sopenharmony_ci "//third_party/icu:icudata", 51316d528ed9Sopenharmony_ci ] 51326d528ed9Sopenharmony_ci } 51336d528ed9Sopenharmony_ci``` 51346d528ed9Sopenharmony_ci### <a name="var_bundle_executable_dir"></a>**bundle_executable_dir** 51356d528ed9Sopenharmony_ci 51366d528ed9Sopenharmony_ci``` 51376d528ed9Sopenharmony_ci bundle_executable_dir: Expansion of {{bundle_executable_dir}} in 51386d528ed9Sopenharmony_ci create_bundle. 51396d528ed9Sopenharmony_ci 51406d528ed9Sopenharmony_ci A string corresponding to a path in $root_build_dir. 51416d528ed9Sopenharmony_ci 51426d528ed9Sopenharmony_ci This string is used by the "create_bundle" target to expand the 51436d528ed9Sopenharmony_ci {{bundle_executable_dir}} of the "bundle_data" target it depends on. This 51446d528ed9Sopenharmony_ci must correspond to a path under "bundle_root_dir". 51456d528ed9Sopenharmony_ci 51466d528ed9Sopenharmony_ci See "gn help bundle_root_dir" for examples. 51476d528ed9Sopenharmony_ci``` 51486d528ed9Sopenharmony_ci### <a name="var_bundle_resources_dir"></a>**bundle_resources_dir** 51496d528ed9Sopenharmony_ci 51506d528ed9Sopenharmony_ci``` 51516d528ed9Sopenharmony_ci bundle_resources_dir: Expansion of {{bundle_resources_dir}} in 51526d528ed9Sopenharmony_ci create_bundle. 51536d528ed9Sopenharmony_ci 51546d528ed9Sopenharmony_ci A string corresponding to a path in $root_build_dir. 51556d528ed9Sopenharmony_ci 51566d528ed9Sopenharmony_ci This string is used by the "create_bundle" target to expand the 51576d528ed9Sopenharmony_ci {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must 51586d528ed9Sopenharmony_ci correspond to a path under "bundle_root_dir". 51596d528ed9Sopenharmony_ci 51606d528ed9Sopenharmony_ci See "gn help bundle_root_dir" for examples. 51616d528ed9Sopenharmony_ci``` 51626d528ed9Sopenharmony_ci### <a name="var_bundle_root_dir"></a>**bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle. 51636d528ed9Sopenharmony_ci 51646d528ed9Sopenharmony_ci``` 51656d528ed9Sopenharmony_ci A string corresponding to a path in root_build_dir. 51666d528ed9Sopenharmony_ci 51676d528ed9Sopenharmony_ci This string is used by the "create_bundle" target to expand the 51686d528ed9Sopenharmony_ci {{bundle_root_dir}} of the "bundle_data" target it depends on. This must 51696d528ed9Sopenharmony_ci correspond to a path under root_build_dir. 51706d528ed9Sopenharmony_ci``` 51716d528ed9Sopenharmony_ci 51726d528ed9Sopenharmony_ci#### **Example** 51736d528ed9Sopenharmony_ci 51746d528ed9Sopenharmony_ci``` 51756d528ed9Sopenharmony_ci bundle_data("info_plist") { 51766d528ed9Sopenharmony_ci sources = [ "Info.plist" ] 51776d528ed9Sopenharmony_ci outputs = [ "{{bundle_contents_dir}}/Info.plist" ] 51786d528ed9Sopenharmony_ci } 51796d528ed9Sopenharmony_ci 51806d528ed9Sopenharmony_ci create_bundle("doom_melon.app") { 51816d528ed9Sopenharmony_ci deps = [ ":info_plist" ] 51826d528ed9Sopenharmony_ci bundle_root_dir = "${root_build_dir}/doom_melon.app" 51836d528ed9Sopenharmony_ci bundle_contents_dir = "${bundle_root_dir}/Contents" 51846d528ed9Sopenharmony_ci bundle_resources_dir = "${bundle_contents_dir}/Resources" 51856d528ed9Sopenharmony_ci bundle_executable_dir = "${bundle_contents_dir}/MacOS" 51866d528ed9Sopenharmony_ci } 51876d528ed9Sopenharmony_ci``` 51886d528ed9Sopenharmony_ci### <a name="var_cflags"></a>**cflags***: Flags passed to the C compiler. 51896d528ed9Sopenharmony_ci 51906d528ed9Sopenharmony_ci``` 51916d528ed9Sopenharmony_ci A list of strings. 51926d528ed9Sopenharmony_ci 51936d528ed9Sopenharmony_ci "cflags" are passed to all invocations of the C, C++, Objective C, and 51946d528ed9Sopenharmony_ci Objective C++ compilers. 51956d528ed9Sopenharmony_ci 51966d528ed9Sopenharmony_ci To target one of these variants individually, use "cflags_c", "cflags_cc", 51976d528ed9Sopenharmony_ci "cflags_objc", and "cflags_objcc", respectively. These variant-specific 51986d528ed9Sopenharmony_ci versions of cflags* will be appended on the compiler command line after 51996d528ed9Sopenharmony_ci "cflags". 52006d528ed9Sopenharmony_ci 52016d528ed9Sopenharmony_ci See also "asmflags" for flags for assembly-language files, "swiftflags" for 52026d528ed9Sopenharmony_ci swift files, and "rustflags" for Rust files. 52036d528ed9Sopenharmony_ci``` 52046d528ed9Sopenharmony_ci 52056d528ed9Sopenharmony_ci#### **Ordering of flags and values** 52066d528ed9Sopenharmony_ci 52076d528ed9Sopenharmony_ci``` 52086d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 52096d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 52106d528ed9Sopenharmony_ci configs appear in the list. 52116d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 52126d528ed9Sopenharmony_ci that the configs appear in the list. 52136d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 52146d528ed9Sopenharmony_ci those configs appear in the list. 52156d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 52166d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 52176d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 52186d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 52196d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 52206d528ed9Sopenharmony_ci recursively. 52216d528ed9Sopenharmony_ci``` 52226d528ed9Sopenharmony_ci### <a name="var_cflags_c"></a>**cflags***: Flags passed to the C compiler. 52236d528ed9Sopenharmony_ci 52246d528ed9Sopenharmony_ci``` 52256d528ed9Sopenharmony_ci A list of strings. 52266d528ed9Sopenharmony_ci 52276d528ed9Sopenharmony_ci "cflags" are passed to all invocations of the C, C++, Objective C, and 52286d528ed9Sopenharmony_ci Objective C++ compilers. 52296d528ed9Sopenharmony_ci 52306d528ed9Sopenharmony_ci To target one of these variants individually, use "cflags_c", "cflags_cc", 52316d528ed9Sopenharmony_ci "cflags_objc", and "cflags_objcc", respectively. These variant-specific 52326d528ed9Sopenharmony_ci versions of cflags* will be appended on the compiler command line after 52336d528ed9Sopenharmony_ci "cflags". 52346d528ed9Sopenharmony_ci 52356d528ed9Sopenharmony_ci See also "asmflags" for flags for assembly-language files, "swiftflags" for 52366d528ed9Sopenharmony_ci swift files, and "rustflags" for Rust files. 52376d528ed9Sopenharmony_ci``` 52386d528ed9Sopenharmony_ci 52396d528ed9Sopenharmony_ci#### **Ordering of flags and values** 52406d528ed9Sopenharmony_ci 52416d528ed9Sopenharmony_ci``` 52426d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 52436d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 52446d528ed9Sopenharmony_ci configs appear in the list. 52456d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 52466d528ed9Sopenharmony_ci that the configs appear in the list. 52476d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 52486d528ed9Sopenharmony_ci those configs appear in the list. 52496d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 52506d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 52516d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 52526d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 52536d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 52546d528ed9Sopenharmony_ci recursively. 52556d528ed9Sopenharmony_ci``` 52566d528ed9Sopenharmony_ci### <a name="var_cflags_cc"></a>**cflags***: Flags passed to the C compiler. 52576d528ed9Sopenharmony_ci 52586d528ed9Sopenharmony_ci``` 52596d528ed9Sopenharmony_ci A list of strings. 52606d528ed9Sopenharmony_ci 52616d528ed9Sopenharmony_ci "cflags" are passed to all invocations of the C, C++, Objective C, and 52626d528ed9Sopenharmony_ci Objective C++ compilers. 52636d528ed9Sopenharmony_ci 52646d528ed9Sopenharmony_ci To target one of these variants individually, use "cflags_c", "cflags_cc", 52656d528ed9Sopenharmony_ci "cflags_objc", and "cflags_objcc", respectively. These variant-specific 52666d528ed9Sopenharmony_ci versions of cflags* will be appended on the compiler command line after 52676d528ed9Sopenharmony_ci "cflags". 52686d528ed9Sopenharmony_ci 52696d528ed9Sopenharmony_ci See also "asmflags" for flags for assembly-language files, "swiftflags" for 52706d528ed9Sopenharmony_ci swift files, and "rustflags" for Rust files. 52716d528ed9Sopenharmony_ci``` 52726d528ed9Sopenharmony_ci 52736d528ed9Sopenharmony_ci#### **Ordering of flags and values** 52746d528ed9Sopenharmony_ci 52756d528ed9Sopenharmony_ci``` 52766d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 52776d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 52786d528ed9Sopenharmony_ci configs appear in the list. 52796d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 52806d528ed9Sopenharmony_ci that the configs appear in the list. 52816d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 52826d528ed9Sopenharmony_ci those configs appear in the list. 52836d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 52846d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 52856d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 52866d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 52876d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 52886d528ed9Sopenharmony_ci recursively. 52896d528ed9Sopenharmony_ci``` 52906d528ed9Sopenharmony_ci### <a name="var_cflags_objc"></a>**cflags***: Flags passed to the C compiler. 52916d528ed9Sopenharmony_ci 52926d528ed9Sopenharmony_ci``` 52936d528ed9Sopenharmony_ci A list of strings. 52946d528ed9Sopenharmony_ci 52956d528ed9Sopenharmony_ci "cflags" are passed to all invocations of the C, C++, Objective C, and 52966d528ed9Sopenharmony_ci Objective C++ compilers. 52976d528ed9Sopenharmony_ci 52986d528ed9Sopenharmony_ci To target one of these variants individually, use "cflags_c", "cflags_cc", 52996d528ed9Sopenharmony_ci "cflags_objc", and "cflags_objcc", respectively. These variant-specific 53006d528ed9Sopenharmony_ci versions of cflags* will be appended on the compiler command line after 53016d528ed9Sopenharmony_ci "cflags". 53026d528ed9Sopenharmony_ci 53036d528ed9Sopenharmony_ci See also "asmflags" for flags for assembly-language files, "swiftflags" for 53046d528ed9Sopenharmony_ci swift files, and "rustflags" for Rust files. 53056d528ed9Sopenharmony_ci``` 53066d528ed9Sopenharmony_ci 53076d528ed9Sopenharmony_ci#### **Ordering of flags and values** 53086d528ed9Sopenharmony_ci 53096d528ed9Sopenharmony_ci``` 53106d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 53116d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 53126d528ed9Sopenharmony_ci configs appear in the list. 53136d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 53146d528ed9Sopenharmony_ci that the configs appear in the list. 53156d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 53166d528ed9Sopenharmony_ci those configs appear in the list. 53176d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 53186d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 53196d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 53206d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 53216d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 53226d528ed9Sopenharmony_ci recursively. 53236d528ed9Sopenharmony_ci``` 53246d528ed9Sopenharmony_ci### <a name="var_cflags_objcc"></a>**cflags***: Flags passed to the C compiler. 53256d528ed9Sopenharmony_ci 53266d528ed9Sopenharmony_ci``` 53276d528ed9Sopenharmony_ci A list of strings. 53286d528ed9Sopenharmony_ci 53296d528ed9Sopenharmony_ci "cflags" are passed to all invocations of the C, C++, Objective C, and 53306d528ed9Sopenharmony_ci Objective C++ compilers. 53316d528ed9Sopenharmony_ci 53326d528ed9Sopenharmony_ci To target one of these variants individually, use "cflags_c", "cflags_cc", 53336d528ed9Sopenharmony_ci "cflags_objc", and "cflags_objcc", respectively. These variant-specific 53346d528ed9Sopenharmony_ci versions of cflags* will be appended on the compiler command line after 53356d528ed9Sopenharmony_ci "cflags". 53366d528ed9Sopenharmony_ci 53376d528ed9Sopenharmony_ci See also "asmflags" for flags for assembly-language files, "swiftflags" for 53386d528ed9Sopenharmony_ci swift files, and "rustflags" for Rust files. 53396d528ed9Sopenharmony_ci``` 53406d528ed9Sopenharmony_ci 53416d528ed9Sopenharmony_ci#### **Ordering of flags and values** 53426d528ed9Sopenharmony_ci 53436d528ed9Sopenharmony_ci``` 53446d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 53456d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 53466d528ed9Sopenharmony_ci configs appear in the list. 53476d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 53486d528ed9Sopenharmony_ci that the configs appear in the list. 53496d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 53506d528ed9Sopenharmony_ci those configs appear in the list. 53516d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 53526d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 53536d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 53546d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 53556d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 53566d528ed9Sopenharmony_ci recursively. 53576d528ed9Sopenharmony_ci``` 53586d528ed9Sopenharmony_ci### <a name="var_check_includes"></a>**check_includes**: [boolean] Controls whether a target's files are checked. 53596d528ed9Sopenharmony_ci 53606d528ed9Sopenharmony_ci``` 53616d528ed9Sopenharmony_ci When true (the default), the "gn check" command (as well as "gn gen" with the 53626d528ed9Sopenharmony_ci --check flag) will check this target's sources and headers for proper 53636d528ed9Sopenharmony_ci dependencies. 53646d528ed9Sopenharmony_ci 53656d528ed9Sopenharmony_ci When false, the files in this target will be skipped by default. This does 53666d528ed9Sopenharmony_ci not affect other targets that depend on the current target, it just skips 53676d528ed9Sopenharmony_ci checking the includes of the current target's files. 53686d528ed9Sopenharmony_ci 53696d528ed9Sopenharmony_ci If there are a few conditionally included headers that trip up checking, you 53706d528ed9Sopenharmony_ci can exclude headers individually by annotating them with "nogncheck" (see "gn 53716d528ed9Sopenharmony_ci help nogncheck"). 53726d528ed9Sopenharmony_ci 53736d528ed9Sopenharmony_ci The topic "gn help check" has general information on how checking works and 53746d528ed9Sopenharmony_ci advice on how to pass a check in problematic cases. 53756d528ed9Sopenharmony_ci``` 53766d528ed9Sopenharmony_ci 53776d528ed9Sopenharmony_ci#### **Example** 53786d528ed9Sopenharmony_ci 53796d528ed9Sopenharmony_ci``` 53806d528ed9Sopenharmony_ci source_set("busted_includes") { 53816d528ed9Sopenharmony_ci # This target's includes are messed up, exclude it from checking. 53826d528ed9Sopenharmony_ci check_includes = false 53836d528ed9Sopenharmony_ci ... 53846d528ed9Sopenharmony_ci } 53856d528ed9Sopenharmony_ci``` 53866d528ed9Sopenharmony_ci### <a name="var_code_signing_args"></a>**code_signing_args**: [string list] [deprecated] Args for the post-processing script. 53876d528ed9Sopenharmony_ci 53886d528ed9Sopenharmony_ci``` 53896d528ed9Sopenharmony_ci For create_bundle targets, post_processing_args is the list of arguments to 53906d528ed9Sopenharmony_ci pass to the post-processing script. Typically you would use source expansion 53916d528ed9Sopenharmony_ci (see "gn help source_expansion") to insert the source file names. 53926d528ed9Sopenharmony_ci 53936d528ed9Sopenharmony_ci Deprecated: this is an old name for the "post_processing_args" property of 53946d528ed9Sopenharmony_ci the "create_bundle" target. It is still supported to avoid breaking existing 53956d528ed9Sopenharmony_ci build rules, but a warning will be emitted when it is used. 53966d528ed9Sopenharmony_ci 53976d528ed9Sopenharmony_ci See also "gn help create_bundle" and "gn help post_processing_args". 53986d528ed9Sopenharmony_ci``` 53996d528ed9Sopenharmony_ci### <a name="var_code_signing_outputs"></a>**code_signing_outputs**: [file list] [deprecated] Outputs of the post-processing step. 54006d528ed9Sopenharmony_ci 54016d528ed9Sopenharmony_ci``` 54026d528ed9Sopenharmony_ci Outputs from the post-processing step of a create_bundle target. Must refer to 54036d528ed9Sopenharmony_ci files in the build directory. 54046d528ed9Sopenharmony_ci 54056d528ed9Sopenharmony_ci Deprecated: this is an old name for the "post_processing_outputs" property of 54066d528ed9Sopenharmony_ci the "create_bundle" target. It is still supported to avoid breaking existing 54076d528ed9Sopenharmony_ci build rules, but a warning will be emitted when it is used. 54086d528ed9Sopenharmony_ci 54096d528ed9Sopenharmony_ci See also "gn help create_bundle" and "gn help post_processing_args". 54106d528ed9Sopenharmony_ci``` 54116d528ed9Sopenharmony_ci### <a name="var_code_signing_script"></a>**code_signing_script**: [file name] [deprecated] Script for the post-processing step." 54126d528ed9Sopenharmony_ci 54136d528ed9Sopenharmony_ci``` 54146d528ed9Sopenharmony_ci An absolute or buildfile-relative file name of a Python script to run for a 54156d528ed9Sopenharmony_ci create_bundle target to perform the post-processing step. 54166d528ed9Sopenharmony_ci 54176d528ed9Sopenharmony_ci Deprecated: this is an old name for the "post_processing_script" property of 54186d528ed9Sopenharmony_ci the "create_bundle" target. It is still supported to avoid breaking existing 54196d528ed9Sopenharmony_ci build rules, but a warning will be emitted when it is used. 54206d528ed9Sopenharmony_ci 54216d528ed9Sopenharmony_ci See also "gn help create_bundle" and "gn help post_processing_args". 54226d528ed9Sopenharmony_ci``` 54236d528ed9Sopenharmony_ci### <a name="var_code_signing_sources"></a>**code_signing_sources**: [file list] [deprecated] Sources for the post-processing step. 54246d528ed9Sopenharmony_ci 54256d528ed9Sopenharmony_ci``` 54266d528ed9Sopenharmony_ci A list of files used as input for the post-processing step of a create_bundle 54276d528ed9Sopenharmony_ci target. Non-absolute paths will be resolved relative to the current build 54286d528ed9Sopenharmony_ci file. 54296d528ed9Sopenharmony_ci 54306d528ed9Sopenharmony_ci Deprecated: this is an old name for the "post_processing_sources" property of 54316d528ed9Sopenharmony_ci the "create_bundle" target. It is still supported to avoid breaking existing 54326d528ed9Sopenharmony_ci build rules, but a warning will be emitted when it is used. 54336d528ed9Sopenharmony_ci 54346d528ed9Sopenharmony_ci See also "gn help create_bundle" and "gn help post_processing_args". 54356d528ed9Sopenharmony_ci``` 54366d528ed9Sopenharmony_ci### <a name="var_complete_static_lib"></a>**complete_static_lib**: [boolean] Links all deps into a static library. 54376d528ed9Sopenharmony_ci 54386d528ed9Sopenharmony_ci``` 54396d528ed9Sopenharmony_ci A static library normally doesn't include code from dependencies, but instead 54406d528ed9Sopenharmony_ci forwards the static libraries and source sets in its deps up the dependency 54416d528ed9Sopenharmony_ci chain until a linkable target (an executable or shared library) is reached. 54426d528ed9Sopenharmony_ci The final linkable target only links each static library once, even if it 54436d528ed9Sopenharmony_ci appears more than once in its dependency graph. 54446d528ed9Sopenharmony_ci 54456d528ed9Sopenharmony_ci In some cases the static library might be the final desired output. For 54466d528ed9Sopenharmony_ci example, you may be producing a static library for distribution to third 54476d528ed9Sopenharmony_ci parties. In this case, the static library should include code for all 54486d528ed9Sopenharmony_ci dependencies in one complete package. However, complete static libraries 54496d528ed9Sopenharmony_ci themselves are never linked into other complete static libraries. All 54506d528ed9Sopenharmony_ci complete static libraries are for distribution and linking them in would 54516d528ed9Sopenharmony_ci cause code duplication in this case. If the static library is not for 54526d528ed9Sopenharmony_ci distribution, it should not be complete. 54536d528ed9Sopenharmony_ci 54546d528ed9Sopenharmony_ci GN treats non-complete static libraries as source sets when they are linked 54556d528ed9Sopenharmony_ci into complete static libraries. This is done because some tools like AR do 54566d528ed9Sopenharmony_ci not handle dependent static libraries properly. This makes it easier to write 54576d528ed9Sopenharmony_ci "alink" rules. 54586d528ed9Sopenharmony_ci 54596d528ed9Sopenharmony_ci In rare cases it makes sense to list a header in more than one target if it 54606d528ed9Sopenharmony_ci could be considered conceptually a member of both. libraries. 54616d528ed9Sopenharmony_ci``` 54626d528ed9Sopenharmony_ci 54636d528ed9Sopenharmony_ci#### **Example** 54646d528ed9Sopenharmony_ci 54656d528ed9Sopenharmony_ci``` 54666d528ed9Sopenharmony_ci static_library("foo") { 54676d528ed9Sopenharmony_ci complete_static_lib = true 54686d528ed9Sopenharmony_ci deps = [ "bar" ] 54696d528ed9Sopenharmony_ci } 54706d528ed9Sopenharmony_ci``` 54716d528ed9Sopenharmony_ci### <a name="var_configs"></a>**configs**: Configs applying to this target or config. 54726d528ed9Sopenharmony_ci 54736d528ed9Sopenharmony_ci``` 54746d528ed9Sopenharmony_ci A list of config labels. 54756d528ed9Sopenharmony_ci``` 54766d528ed9Sopenharmony_ci 54776d528ed9Sopenharmony_ci#### **Configs on a target** 54786d528ed9Sopenharmony_ci 54796d528ed9Sopenharmony_ci``` 54806d528ed9Sopenharmony_ci When used on a target, the include_dirs, defines, etc. in each config are 54816d528ed9Sopenharmony_ci appended in the order they appear to the compile command for each file in the 54826d528ed9Sopenharmony_ci target. They will appear after the include_dirs, defines, etc. that the 54836d528ed9Sopenharmony_ci target sets directly. 54846d528ed9Sopenharmony_ci 54856d528ed9Sopenharmony_ci Since configs apply after the values set on a target, directly setting a 54866d528ed9Sopenharmony_ci compiler flag will prepend it to the command line. If you want to append a 54876d528ed9Sopenharmony_ci flag instead, you can put that flag in a one-off config and append that 54886d528ed9Sopenharmony_ci config to the target's configs list. 54896d528ed9Sopenharmony_ci 54906d528ed9Sopenharmony_ci The build configuration script will generally set up the default configs 54916d528ed9Sopenharmony_ci applying to a given target type (see "set_defaults"). When a target is being 54926d528ed9Sopenharmony_ci defined, it can add to or remove from this list. 54936d528ed9Sopenharmony_ci``` 54946d528ed9Sopenharmony_ci 54956d528ed9Sopenharmony_ci#### **Configs on a config** 54966d528ed9Sopenharmony_ci 54976d528ed9Sopenharmony_ci``` 54986d528ed9Sopenharmony_ci It is possible to create composite configs by specifying configs on a config. 54996d528ed9Sopenharmony_ci One might do this to forward values, or to factor out blocks of settings from 55006d528ed9Sopenharmony_ci very large configs into more manageable named chunks. 55016d528ed9Sopenharmony_ci 55026d528ed9Sopenharmony_ci In this case, the composite config is expanded to be the concatenation of its 55036d528ed9Sopenharmony_ci own values, and in order, the values from its sub-configs *before* anything 55046d528ed9Sopenharmony_ci else happens. This has some ramifications: 55056d528ed9Sopenharmony_ci 55066d528ed9Sopenharmony_ci - A target has no visibility into a config's sub-configs. Target code only 55076d528ed9Sopenharmony_ci sees the name of the composite config. It can't remove sub-configs or opt 55086d528ed9Sopenharmony_ci in to only parts of it. The composite config may not even be defined 55096d528ed9Sopenharmony_ci before the target is. 55106d528ed9Sopenharmony_ci 55116d528ed9Sopenharmony_ci - You can get duplication of values if a config is listed twice, say, on a 55126d528ed9Sopenharmony_ci target and in a sub-config that also applies. In other cases, the configs 55136d528ed9Sopenharmony_ci applying to a target are de-duped. It's expected that if a config is 55146d528ed9Sopenharmony_ci listed as a sub-config that it is only used in that context. (Note that 55156d528ed9Sopenharmony_ci it's possible to fix this and de-dupe, but it's not normally relevant and 55166d528ed9Sopenharmony_ci complicates the implementation.) 55176d528ed9Sopenharmony_ci``` 55186d528ed9Sopenharmony_ci 55196d528ed9Sopenharmony_ci#### **Ordering of flags and values** 55206d528ed9Sopenharmony_ci 55216d528ed9Sopenharmony_ci``` 55226d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 55236d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 55246d528ed9Sopenharmony_ci configs appear in the list. 55256d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 55266d528ed9Sopenharmony_ci that the configs appear in the list. 55276d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 55286d528ed9Sopenharmony_ci those configs appear in the list. 55296d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 55306d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 55316d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 55326d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 55336d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 55346d528ed9Sopenharmony_ci recursively. 55356d528ed9Sopenharmony_ci``` 55366d528ed9Sopenharmony_ci 55376d528ed9Sopenharmony_ci#### **Example** 55386d528ed9Sopenharmony_ci 55396d528ed9Sopenharmony_ci``` 55406d528ed9Sopenharmony_ci # Configs on a target. 55416d528ed9Sopenharmony_ci source_set("foo") { 55426d528ed9Sopenharmony_ci # Don't use the default RTTI config that BUILDCONFIG applied to us. 55436d528ed9Sopenharmony_ci configs -= [ "//build:no_rtti" ] 55446d528ed9Sopenharmony_ci 55456d528ed9Sopenharmony_ci # Add some of our own settings. 55466d528ed9Sopenharmony_ci configs += [ ":mysettings" ] 55476d528ed9Sopenharmony_ci } 55486d528ed9Sopenharmony_ci 55496d528ed9Sopenharmony_ci # Create a default_optimization config that forwards to one of a set of more 55506d528ed9Sopenharmony_ci # specialized configs depending on build flags. This pattern is useful 55516d528ed9Sopenharmony_ci # because it allows a target to opt in to either a default set, or a more 55526d528ed9Sopenharmony_ci # specific set, while avoid duplicating the settings in two places. 55536d528ed9Sopenharmony_ci config("super_optimization") { 55546d528ed9Sopenharmony_ci cflags = [ ... ] 55556d528ed9Sopenharmony_ci } 55566d528ed9Sopenharmony_ci config("default_optimization") { 55576d528ed9Sopenharmony_ci if (optimize_everything) { 55586d528ed9Sopenharmony_ci configs = [ ":super_optimization" ] 55596d528ed9Sopenharmony_ci } else { 55606d528ed9Sopenharmony_ci configs = [ ":no_optimization" ] 55616d528ed9Sopenharmony_ci } 55626d528ed9Sopenharmony_ci } 55636d528ed9Sopenharmony_ci``` 55646d528ed9Sopenharmony_ci### <a name="var_contents"></a>**contents**: Contents to write to file. 55656d528ed9Sopenharmony_ci 55666d528ed9Sopenharmony_ci``` 55676d528ed9Sopenharmony_ci The contents of the file for a generated_file target. 55686d528ed9Sopenharmony_ci See "gn help generated_file". 55696d528ed9Sopenharmony_ci``` 55706d528ed9Sopenharmony_ci### <a name="var_crate_name"></a>**crate_name**: [string] The name for the compiled crate. 55716d528ed9Sopenharmony_ci 55726d528ed9Sopenharmony_ci``` 55736d528ed9Sopenharmony_ci Valid for `rust_library` targets and `executable`, `static_library`, 55746d528ed9Sopenharmony_ci `shared_library`, and `source_set` targets that contain Rust sources. 55756d528ed9Sopenharmony_ci 55766d528ed9Sopenharmony_ci If crate_name is not set, then this rule will use the target name. 55776d528ed9Sopenharmony_ci``` 55786d528ed9Sopenharmony_ci### <a name="var_crate_root"></a>**crate_root**: [string] The root source file for a binary or library. 55796d528ed9Sopenharmony_ci 55806d528ed9Sopenharmony_ci``` 55816d528ed9Sopenharmony_ci Valid for `rust_library` targets and `executable`, `static_library`, 55826d528ed9Sopenharmony_ci `shared_library`, and `source_set` targets that contain Rust sources. 55836d528ed9Sopenharmony_ci 55846d528ed9Sopenharmony_ci This file is usually the `main.rs` or `lib.rs` for binaries and libraries, 55856d528ed9Sopenharmony_ci respectively. 55866d528ed9Sopenharmony_ci 55876d528ed9Sopenharmony_ci If crate_root is not set, then this rule will look for a lib.rs file (or 55886d528ed9Sopenharmony_ci main.rs for executable) or a single file in sources, if sources contains 55896d528ed9Sopenharmony_ci only one file. 55906d528ed9Sopenharmony_ci``` 55916d528ed9Sopenharmony_ci### <a name="var_crate_type"></a>**crate_type**: [string] The type of linkage to use on a shared_library. 55926d528ed9Sopenharmony_ci 55936d528ed9Sopenharmony_ci``` 55946d528ed9Sopenharmony_ci Valid for `rust_library` targets and `executable`, `static_library`, 55956d528ed9Sopenharmony_ci `shared_library`, and `source_set` targets that contain Rust sources. 55966d528ed9Sopenharmony_ci 55976d528ed9Sopenharmony_ci Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib". 55986d528ed9Sopenharmony_ci This field sets the `crate-type` attribute for the `rustc` tool on static 55996d528ed9Sopenharmony_ci libraries, as well as the appropriate output extension in the 56006d528ed9Sopenharmony_ci `rust_output_extension` attribute. Since outputs must be explicit, the `lib` 56016d528ed9Sopenharmony_ci crate type (where the Rust compiler produces what it thinks is the 56026d528ed9Sopenharmony_ci appropriate library type) is not supported. 56036d528ed9Sopenharmony_ci 56046d528ed9Sopenharmony_ci It should be noted that the "dylib" crate type in Rust is unstable in the set 56056d528ed9Sopenharmony_ci of symbols it exposes, and most usages today are potentially wrong and will 56066d528ed9Sopenharmony_ci be broken in the future. 56076d528ed9Sopenharmony_ci 56086d528ed9Sopenharmony_ci Static libraries, rust libraries, and executables have this field set 56096d528ed9Sopenharmony_ci automatically. 56106d528ed9Sopenharmony_ci``` 56116d528ed9Sopenharmony_ci### <a name="var_data"></a>**data**: Runtime data file dependencies. 56126d528ed9Sopenharmony_ci 56136d528ed9Sopenharmony_ci``` 56146d528ed9Sopenharmony_ci Lists files or directories required to run the given target. These are 56156d528ed9Sopenharmony_ci typically data files or directories of data files. The paths are interpreted 56166d528ed9Sopenharmony_ci as being relative to the current build file. Since these are runtime 56176d528ed9Sopenharmony_ci dependencies, they do not affect which targets are built or when. To declare 56186d528ed9Sopenharmony_ci input files to a script, use "inputs". 56196d528ed9Sopenharmony_ci 56206d528ed9Sopenharmony_ci Appearing in the "data" section does not imply any special handling such as 56216d528ed9Sopenharmony_ci copying them to the output directory. This is just used for declaring runtime 56226d528ed9Sopenharmony_ci dependencies. Runtime dependencies can be queried using the "runtime_deps" 56236d528ed9Sopenharmony_ci category of "gn desc" or written during build generation via 56246d528ed9Sopenharmony_ci "--runtime-deps-list-file". 56256d528ed9Sopenharmony_ci 56266d528ed9Sopenharmony_ci GN doesn't require data files to exist at build-time. So actions that produce 56276d528ed9Sopenharmony_ci files that are in turn runtime dependencies can list those generated files 56286d528ed9Sopenharmony_ci both in the "outputs" list as well as the "data" list. 56296d528ed9Sopenharmony_ci 56306d528ed9Sopenharmony_ci By convention, directories are listed with a trailing slash: 56316d528ed9Sopenharmony_ci data = [ "test/data/" ] 56326d528ed9Sopenharmony_ci However, no verification is done on these so GN doesn't enforce this. The 56336d528ed9Sopenharmony_ci paths are just rebased and passed along when requested. 56346d528ed9Sopenharmony_ci 56356d528ed9Sopenharmony_ci Note: On iOS and macOS, create_bundle targets will not be recursed into when 56366d528ed9Sopenharmony_ci gathering data. See "gn help create_bundle" for details. 56376d528ed9Sopenharmony_ci 56386d528ed9Sopenharmony_ci See "gn help runtime_deps" for how these are used. 56396d528ed9Sopenharmony_ci``` 56406d528ed9Sopenharmony_ci### <a name="var_data_deps"></a>**data_deps**: Non-linked dependencies. 56416d528ed9Sopenharmony_ci 56426d528ed9Sopenharmony_ci``` 56436d528ed9Sopenharmony_ci A list of target labels. 56446d528ed9Sopenharmony_ci 56456d528ed9Sopenharmony_ci Specifies dependencies of a target that are not actually linked into the 56466d528ed9Sopenharmony_ci current target. Such dependencies will be built and will be available at 56476d528ed9Sopenharmony_ci runtime. 56486d528ed9Sopenharmony_ci 56496d528ed9Sopenharmony_ci This is normally used for things like plugins or helper programs that a 56506d528ed9Sopenharmony_ci target needs at runtime. 56516d528ed9Sopenharmony_ci 56526d528ed9Sopenharmony_ci Note: On iOS and macOS, create_bundle targets will not be recursed into when 56536d528ed9Sopenharmony_ci gathering data_deps. See "gn help create_bundle" for details. 56546d528ed9Sopenharmony_ci 56556d528ed9Sopenharmony_ci See also "gn help deps" and "gn help data". 56566d528ed9Sopenharmony_ci``` 56576d528ed9Sopenharmony_ci 56586d528ed9Sopenharmony_ci#### **Example** 56596d528ed9Sopenharmony_ci 56606d528ed9Sopenharmony_ci``` 56616d528ed9Sopenharmony_ci executable("foo") { 56626d528ed9Sopenharmony_ci deps = [ "//base" ] 56636d528ed9Sopenharmony_ci data_deps = [ "//plugins:my_runtime_plugin" ] 56646d528ed9Sopenharmony_ci } 56656d528ed9Sopenharmony_ci``` 56666d528ed9Sopenharmony_ci### <a name="var_data_keys"></a>**data_keys**: Keys from which to collect metadata. 56676d528ed9Sopenharmony_ci 56686d528ed9Sopenharmony_ci``` 56696d528ed9Sopenharmony_ci These keys are used to identify metadata to collect. If a walked target 56706d528ed9Sopenharmony_ci defines this key in its metadata, its value will be appended to the resulting 56716d528ed9Sopenharmony_ci collection. 56726d528ed9Sopenharmony_ci 56736d528ed9Sopenharmony_ci See "gn help generated_file". 56746d528ed9Sopenharmony_ci``` 56756d528ed9Sopenharmony_ci### <a name="var_defines"></a>**defines**: C preprocessor defines. 56766d528ed9Sopenharmony_ci 56776d528ed9Sopenharmony_ci``` 56786d528ed9Sopenharmony_ci A list of strings 56796d528ed9Sopenharmony_ci 56806d528ed9Sopenharmony_ci These strings will be passed to the C/C++ compiler as #defines. The strings 56816d528ed9Sopenharmony_ci may or may not include an "=" to assign a value. 56826d528ed9Sopenharmony_ci``` 56836d528ed9Sopenharmony_ci 56846d528ed9Sopenharmony_ci#### **Ordering of flags and values** 56856d528ed9Sopenharmony_ci 56866d528ed9Sopenharmony_ci``` 56876d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 56886d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 56896d528ed9Sopenharmony_ci configs appear in the list. 56906d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 56916d528ed9Sopenharmony_ci that the configs appear in the list. 56926d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 56936d528ed9Sopenharmony_ci those configs appear in the list. 56946d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 56956d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 56966d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 56976d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 56986d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 56996d528ed9Sopenharmony_ci recursively. 57006d528ed9Sopenharmony_ci``` 57016d528ed9Sopenharmony_ci 57026d528ed9Sopenharmony_ci#### **Example** 57036d528ed9Sopenharmony_ci 57046d528ed9Sopenharmony_ci``` 57056d528ed9Sopenharmony_ci defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ] 57066d528ed9Sopenharmony_ci``` 57076d528ed9Sopenharmony_ci### <a name="var_depfile"></a>**depfile**: [string] File name for input dependencies for actions. 57086d528ed9Sopenharmony_ci 57096d528ed9Sopenharmony_ci``` 57106d528ed9Sopenharmony_ci If nonempty, this string specifies that the current action or action_foreach 57116d528ed9Sopenharmony_ci target will generate the given ".d" file containing the dependencies of the 57126d528ed9Sopenharmony_ci input. Empty or unset means that the script doesn't generate the files. 57136d528ed9Sopenharmony_ci 57146d528ed9Sopenharmony_ci A depfile should be used only when a target depends on files that are not 57156d528ed9Sopenharmony_ci already specified by a target's inputs and sources. Likewise, depfiles should 57166d528ed9Sopenharmony_ci specify only those dependencies not already included in sources or inputs. 57176d528ed9Sopenharmony_ci 57186d528ed9Sopenharmony_ci The .d file should go in the target output directory. If you have more than 57196d528ed9Sopenharmony_ci one source file that the script is being run over, you can use the output 57206d528ed9Sopenharmony_ci file expansions described in "gn help action_foreach" to name the .d file 57216d528ed9Sopenharmony_ci according to the input. 57226d528ed9Sopenharmony_ci 57236d528ed9Sopenharmony_ci The format is that of a Makefile and all paths must be relative to the root 57246d528ed9Sopenharmony_ci build directory. Only one output may be listed and it must match the first 57256d528ed9Sopenharmony_ci output of the action. 57266d528ed9Sopenharmony_ci 57276d528ed9Sopenharmony_ci Although depfiles are created by an action, they should not be listed in the 57286d528ed9Sopenharmony_ci action's "outputs" unless another target will use the file as an input. 57296d528ed9Sopenharmony_ci``` 57306d528ed9Sopenharmony_ci 57316d528ed9Sopenharmony_ci#### **Example** 57326d528ed9Sopenharmony_ci 57336d528ed9Sopenharmony_ci``` 57346d528ed9Sopenharmony_ci action_foreach("myscript_target") { 57356d528ed9Sopenharmony_ci script = "myscript.py" 57366d528ed9Sopenharmony_ci sources = [ ... ] 57376d528ed9Sopenharmony_ci 57386d528ed9Sopenharmony_ci # Locate the depfile in the output directory named like the 57396d528ed9Sopenharmony_ci # inputs but with a ".d" appended. 57406d528ed9Sopenharmony_ci depfile = "$target_gen_dir/{{source_name_part}}.d" 57416d528ed9Sopenharmony_ci 57426d528ed9Sopenharmony_ci # Say our script uses "-o <d file>" to indicate the depfile. 57436d528ed9Sopenharmony_ci args = [ "{{source}}", "-o", rebase_path(depfile, root_build_dir)] 57446d528ed9Sopenharmony_ci } 57456d528ed9Sopenharmony_ci``` 57466d528ed9Sopenharmony_ci### <a name="var_deps"></a>**deps**: Private linked dependencies. 57476d528ed9Sopenharmony_ci 57486d528ed9Sopenharmony_ci``` 57496d528ed9Sopenharmony_ci A list of target labels. 57506d528ed9Sopenharmony_ci 57516d528ed9Sopenharmony_ci Specifies private dependencies of a target. Private dependencies are 57526d528ed9Sopenharmony_ci propagated up the dependency tree and linked to dependent targets, but do not 57536d528ed9Sopenharmony_ci grant the ability to include headers from the dependency. Public configs are 57546d528ed9Sopenharmony_ci not forwarded. 57556d528ed9Sopenharmony_ci``` 57566d528ed9Sopenharmony_ci 57576d528ed9Sopenharmony_ci#### **Details of dependency propagation** 57586d528ed9Sopenharmony_ci 57596d528ed9Sopenharmony_ci``` 57606d528ed9Sopenharmony_ci Source sets, shared libraries, and non-complete static libraries will be 57616d528ed9Sopenharmony_ci propagated up the dependency tree across groups, non-complete static 57626d528ed9Sopenharmony_ci libraries and source sets. 57636d528ed9Sopenharmony_ci 57646d528ed9Sopenharmony_ci Executables, shared libraries, and complete static libraries will link all 57656d528ed9Sopenharmony_ci propagated targets and stop propagation. Actions and copy steps also stop 57666d528ed9Sopenharmony_ci propagation, allowing them to take a library as an input but not force 57676d528ed9Sopenharmony_ci dependents to link to it. 57686d528ed9Sopenharmony_ci 57696d528ed9Sopenharmony_ci Propagation of all_dependent_configs and public_configs happens independently 57706d528ed9Sopenharmony_ci of target type. all_dependent_configs are always propagated across all types 57716d528ed9Sopenharmony_ci of targets, and public_configs are always propagated across public deps of 57726d528ed9Sopenharmony_ci all types of targets. 57736d528ed9Sopenharmony_ci 57746d528ed9Sopenharmony_ci For Rust targets, deps ensures that Rust code can refer to the dependency 57756d528ed9Sopenharmony_ci target. If the dependency is a C/C++ target, the path to that target will 57766d528ed9Sopenharmony_ci be made available to Rust for `#[link]` directives. 57776d528ed9Sopenharmony_ci 57786d528ed9Sopenharmony_ci Data dependencies are propagated differently. See "gn help data_deps" and 57796d528ed9Sopenharmony_ci "gn help runtime_deps". 57806d528ed9Sopenharmony_ci 57816d528ed9Sopenharmony_ci See also "public_deps". 57826d528ed9Sopenharmony_ci``` 57836d528ed9Sopenharmony_ci### <a name="var_externs"></a>**externs**: [scope] Set of Rust crate-dependency pairs. 57846d528ed9Sopenharmony_ci 57856d528ed9Sopenharmony_ci``` 57866d528ed9Sopenharmony_ci A list, each value being a scope indicating a pair of crate name and the path 57876d528ed9Sopenharmony_ci to the Rust library. 57886d528ed9Sopenharmony_ci 57896d528ed9Sopenharmony_ci These libraries will be passed as `--extern crate_name=path` to compiler 57906d528ed9Sopenharmony_ci invocation containing the current target. 57916d528ed9Sopenharmony_ci``` 57926d528ed9Sopenharmony_ci 57936d528ed9Sopenharmony_ci#### **Examples** 57946d528ed9Sopenharmony_ci 57956d528ed9Sopenharmony_ci``` 57966d528ed9Sopenharmony_ci executable("foo") { 57976d528ed9Sopenharmony_ci sources = [ "main.rs" ] 57986d528ed9Sopenharmony_ci externs = [{ 57996d528ed9Sopenharmony_ci crate_name = "bar", 58006d528ed9Sopenharmony_ci path = "path/to/bar.rlib" 58016d528ed9Sopenharmony_ci }] 58026d528ed9Sopenharmony_ci } 58036d528ed9Sopenharmony_ci 58046d528ed9Sopenharmony_ci This target would compile the `foo` crate with the following `extern` flag: 58056d528ed9Sopenharmony_ci `--extern bar=path/to/bar.rlib`. 58066d528ed9Sopenharmony_ci``` 58076d528ed9Sopenharmony_ci### <a name="var_framework_dirs"></a>**framework_dirs**: [directory list] Additional framework search directories. 58086d528ed9Sopenharmony_ci 58096d528ed9Sopenharmony_ci``` 58106d528ed9Sopenharmony_ci A list of source directories. 58116d528ed9Sopenharmony_ci 58126d528ed9Sopenharmony_ci The directories in this list will be added to the framework search path for 58136d528ed9Sopenharmony_ci the files in the affected target. 58146d528ed9Sopenharmony_ci``` 58156d528ed9Sopenharmony_ci 58166d528ed9Sopenharmony_ci#### **Ordering of flags and values** 58176d528ed9Sopenharmony_ci 58186d528ed9Sopenharmony_ci``` 58196d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 58206d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 58216d528ed9Sopenharmony_ci configs appear in the list. 58226d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 58236d528ed9Sopenharmony_ci that the configs appear in the list. 58246d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 58256d528ed9Sopenharmony_ci those configs appear in the list. 58266d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 58276d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 58286d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 58296d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 58306d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 58316d528ed9Sopenharmony_ci recursively. 58326d528ed9Sopenharmony_ci``` 58336d528ed9Sopenharmony_ci 58346d528ed9Sopenharmony_ci#### **Example** 58356d528ed9Sopenharmony_ci 58366d528ed9Sopenharmony_ci``` 58376d528ed9Sopenharmony_ci framework_dirs = [ "src/include", "//third_party/foo" ] 58386d528ed9Sopenharmony_ci``` 58396d528ed9Sopenharmony_ci### <a name="var_frameworks"></a>**frameworks**: [name list] Name of frameworks that must be linked. 58406d528ed9Sopenharmony_ci 58416d528ed9Sopenharmony_ci``` 58426d528ed9Sopenharmony_ci A list of framework names. 58436d528ed9Sopenharmony_ci 58446d528ed9Sopenharmony_ci The frameworks named in that list will be linked with any dynamic link 58456d528ed9Sopenharmony_ci type target. 58466d528ed9Sopenharmony_ci``` 58476d528ed9Sopenharmony_ci 58486d528ed9Sopenharmony_ci#### **Ordering of flags and values** 58496d528ed9Sopenharmony_ci 58506d528ed9Sopenharmony_ci``` 58516d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 58526d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 58536d528ed9Sopenharmony_ci configs appear in the list. 58546d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 58556d528ed9Sopenharmony_ci that the configs appear in the list. 58566d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 58576d528ed9Sopenharmony_ci those configs appear in the list. 58586d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 58596d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 58606d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 58616d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 58626d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 58636d528ed9Sopenharmony_ci recursively. 58646d528ed9Sopenharmony_ci``` 58656d528ed9Sopenharmony_ci 58666d528ed9Sopenharmony_ci#### **Example** 58676d528ed9Sopenharmony_ci 58686d528ed9Sopenharmony_ci``` 58696d528ed9Sopenharmony_ci frameworks = [ "Foundation.framework", "Foo.framework" ] 58706d528ed9Sopenharmony_ci``` 58716d528ed9Sopenharmony_ci### <a name="var_friend"></a>**friend**: Allow targets to include private headers. 58726d528ed9Sopenharmony_ci 58736d528ed9Sopenharmony_ci``` 58746d528ed9Sopenharmony_ci A list of label patterns (see "gn help label_pattern") that allow dependent 58756d528ed9Sopenharmony_ci targets to include private headers. Applies to all binary targets. 58766d528ed9Sopenharmony_ci 58776d528ed9Sopenharmony_ci Normally if a target lists headers in the "public" list (see "gn help 58786d528ed9Sopenharmony_ci public"), other headers are implicitly marked as private. Private headers 58796d528ed9Sopenharmony_ci can not be included by other targets, even with a public dependency path. 58806d528ed9Sopenharmony_ci The "gn check" function performs this validation. 58816d528ed9Sopenharmony_ci 58826d528ed9Sopenharmony_ci A friend declaration allows one or more targets to include private headers. 58836d528ed9Sopenharmony_ci This is useful for things like unit tests that are closely associated with a 58846d528ed9Sopenharmony_ci target and require internal knowledge without opening up all headers to be 58856d528ed9Sopenharmony_ci included by all dependents. 58866d528ed9Sopenharmony_ci 58876d528ed9Sopenharmony_ci A friend target does not allow that target to include headers when no 58886d528ed9Sopenharmony_ci dependency exists. A public dependency path must still exist between two 58896d528ed9Sopenharmony_ci targets to include any headers from a destination target. The friend 58906d528ed9Sopenharmony_ci annotation merely allows the use of headers that would otherwise be 58916d528ed9Sopenharmony_ci prohibited because they are private. 58926d528ed9Sopenharmony_ci 58936d528ed9Sopenharmony_ci The friend annotation is matched only against the target containing the file 58946d528ed9Sopenharmony_ci with the include directive. Friend annotations are not propagated across 58956d528ed9Sopenharmony_ci public or private dependencies. Friend annotations do not affect visibility. 58966d528ed9Sopenharmony_ci``` 58976d528ed9Sopenharmony_ci 58986d528ed9Sopenharmony_ci#### **Example** 58996d528ed9Sopenharmony_ci 59006d528ed9Sopenharmony_ci``` 59016d528ed9Sopenharmony_ci static_library("lib") { 59026d528ed9Sopenharmony_ci # This target can include our private headers. 59036d528ed9Sopenharmony_ci friend = [ ":unit_tests" ] 59046d528ed9Sopenharmony_ci 59056d528ed9Sopenharmony_ci public = [ 59066d528ed9Sopenharmony_ci "public_api.h", # Normal public API for dependent targets. 59076d528ed9Sopenharmony_ci ] 59086d528ed9Sopenharmony_ci 59096d528ed9Sopenharmony_ci # Private API and sources. 59106d528ed9Sopenharmony_ci sources = [ 59116d528ed9Sopenharmony_ci "a_source_file.cc", 59126d528ed9Sopenharmony_ci 59136d528ed9Sopenharmony_ci # Normal targets that depend on this one won't be able to include this 59146d528ed9Sopenharmony_ci # because this target defines a list of "public" headers. Without the 59156d528ed9Sopenharmony_ci # "public" list, all headers are implicitly public. 59166d528ed9Sopenharmony_ci "private_api.h", 59176d528ed9Sopenharmony_ci ] 59186d528ed9Sopenharmony_ci } 59196d528ed9Sopenharmony_ci 59206d528ed9Sopenharmony_ci executable("unit_tests") { 59216d528ed9Sopenharmony_ci sources = [ 59226d528ed9Sopenharmony_ci # This can include "private_api.h" from the :lib target because it 59236d528ed9Sopenharmony_ci # depends on that target and because of the friend annotation. 59246d528ed9Sopenharmony_ci "my_test.cc", 59256d528ed9Sopenharmony_ci ] 59266d528ed9Sopenharmony_ci 59276d528ed9Sopenharmony_ci deps = [ 59286d528ed9Sopenharmony_ci ":lib", # Required for the include to be allowed. 59296d528ed9Sopenharmony_ci ] 59306d528ed9Sopenharmony_ci } 59316d528ed9Sopenharmony_ci``` 59326d528ed9Sopenharmony_ci### <a name="var_gen_deps"></a>**gen_deps**: Declares targets that should generate when this one does. 59336d528ed9Sopenharmony_ci 59346d528ed9Sopenharmony_ci``` 59356d528ed9Sopenharmony_ci A list of target labels. 59366d528ed9Sopenharmony_ci 59376d528ed9Sopenharmony_ci Not all GN targets that get evaluated are actually turned into ninja targets 59386d528ed9Sopenharmony_ci (see "gn help execution"). If this target is generated, then any targets in 59396d528ed9Sopenharmony_ci the "gen_deps" list will also be generated, regardless of the usual criteria. 59406d528ed9Sopenharmony_ci 59416d528ed9Sopenharmony_ci Since "gen_deps" are not build time dependencies, there can be cycles between 59426d528ed9Sopenharmony_ci "deps" and "gen_deps" or within "gen_deps" itself. 59436d528ed9Sopenharmony_ci``` 59446d528ed9Sopenharmony_ci### <a name="var_include_dirs"></a>**include_dirs**: Additional include directories. 59456d528ed9Sopenharmony_ci 59466d528ed9Sopenharmony_ci``` 59476d528ed9Sopenharmony_ci A list of source directories. 59486d528ed9Sopenharmony_ci 59496d528ed9Sopenharmony_ci The directories in this list will be added to the include path for the files 59506d528ed9Sopenharmony_ci in the affected target. 59516d528ed9Sopenharmony_ci``` 59526d528ed9Sopenharmony_ci 59536d528ed9Sopenharmony_ci#### **Ordering of flags and values** 59546d528ed9Sopenharmony_ci 59556d528ed9Sopenharmony_ci``` 59566d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 59576d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 59586d528ed9Sopenharmony_ci configs appear in the list. 59596d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 59606d528ed9Sopenharmony_ci that the configs appear in the list. 59616d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 59626d528ed9Sopenharmony_ci those configs appear in the list. 59636d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 59646d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 59656d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 59666d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 59676d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 59686d528ed9Sopenharmony_ci recursively. 59696d528ed9Sopenharmony_ci``` 59706d528ed9Sopenharmony_ci 59716d528ed9Sopenharmony_ci#### **Example** 59726d528ed9Sopenharmony_ci 59736d528ed9Sopenharmony_ci``` 59746d528ed9Sopenharmony_ci include_dirs = [ "src/include", "//third_party/foo" ] 59756d528ed9Sopenharmony_ci``` 59766d528ed9Sopenharmony_ci### <a name="var_inputs"></a>**inputs**: Additional compile-time dependencies. 59776d528ed9Sopenharmony_ci 59786d528ed9Sopenharmony_ci``` 59796d528ed9Sopenharmony_ci Inputs are compile-time dependencies of the current target. This means that 59806d528ed9Sopenharmony_ci all inputs must be available before compiling any of the sources or executing 59816d528ed9Sopenharmony_ci any actions. 59826d528ed9Sopenharmony_ci 59836d528ed9Sopenharmony_ci Inputs are typically only used for action and action_foreach targets. 59846d528ed9Sopenharmony_ci``` 59856d528ed9Sopenharmony_ci 59866d528ed9Sopenharmony_ci#### **Inputs for actions** 59876d528ed9Sopenharmony_ci 59886d528ed9Sopenharmony_ci``` 59896d528ed9Sopenharmony_ci For action and action_foreach targets, inputs should be the inputs to script 59906d528ed9Sopenharmony_ci that don't vary. These should be all .py files that the script uses via 59916d528ed9Sopenharmony_ci imports (the main script itself will be an implicit dependency of the action 59926d528ed9Sopenharmony_ci so need not be listed). 59936d528ed9Sopenharmony_ci 59946d528ed9Sopenharmony_ci For action targets, inputs and sources are treated the same, but from a style 59956d528ed9Sopenharmony_ci perspective, it's recommended to follow the same rule as action_foreach and 59966d528ed9Sopenharmony_ci put helper files in the inputs, and the data used by the script (if any) in 59976d528ed9Sopenharmony_ci sources. 59986d528ed9Sopenharmony_ci 59996d528ed9Sopenharmony_ci Note that another way to declare input dependencies from an action is to have 60006d528ed9Sopenharmony_ci the action write a depfile (see "gn help depfile"). This allows the script to 60016d528ed9Sopenharmony_ci dynamically write input dependencies, that might not be known until actually 60026d528ed9Sopenharmony_ci executing the script. This is more efficient than doing processing while 60036d528ed9Sopenharmony_ci running GN to determine the inputs, and is easier to keep in-sync than 60046d528ed9Sopenharmony_ci hardcoding the list. 60056d528ed9Sopenharmony_ci``` 60066d528ed9Sopenharmony_ci 60076d528ed9Sopenharmony_ci#### **Script input gotchas** 60086d528ed9Sopenharmony_ci 60096d528ed9Sopenharmony_ci``` 60106d528ed9Sopenharmony_ci It may be tempting to write a script that enumerates all files in a directory 60116d528ed9Sopenharmony_ci as inputs. Don't do this! Even if you specify all the files in the inputs or 60126d528ed9Sopenharmony_ci sources in the GN target (or worse, enumerate the files in an exec_script 60136d528ed9Sopenharmony_ci call when running GN, which will be slow), the dependencies will be broken. 60146d528ed9Sopenharmony_ci 60156d528ed9Sopenharmony_ci The problem happens if a file is ever removed because the inputs are not 60166d528ed9Sopenharmony_ci listed on the command line to the script. Because the script hasn't changed 60176d528ed9Sopenharmony_ci and all inputs are up to date, the script will not re-run and you will get a 60186d528ed9Sopenharmony_ci stale build. Instead, either list all inputs on the command line to the 60196d528ed9Sopenharmony_ci script, or if there are many, create a separate list file that the script 60206d528ed9Sopenharmony_ci reads. As long as this file is listed in the inputs, the build will detect 60216d528ed9Sopenharmony_ci when it has changed in any way and the action will re-run. 60226d528ed9Sopenharmony_ci``` 60236d528ed9Sopenharmony_ci 60246d528ed9Sopenharmony_ci#### **Inputs for binary targets** 60256d528ed9Sopenharmony_ci 60266d528ed9Sopenharmony_ci``` 60276d528ed9Sopenharmony_ci Any input dependencies will be resolved before compiling any sources or 60286d528ed9Sopenharmony_ci linking the target. Normally, all actions that a target depends on will be run 60296d528ed9Sopenharmony_ci before any files in a target are compiled. So if you depend on generated 60306d528ed9Sopenharmony_ci headers, you do not typically need to list them in the inputs section. 60316d528ed9Sopenharmony_ci 60326d528ed9Sopenharmony_ci Inputs for binary targets will be treated as implicit dependencies, meaning 60336d528ed9Sopenharmony_ci that changes in any of the inputs will force all sources in the target to be 60346d528ed9Sopenharmony_ci recompiled. If an input only applies to a subset of source files, you may 60356d528ed9Sopenharmony_ci want to split those into a separate target to avoid unnecessary recompiles. 60366d528ed9Sopenharmony_ci``` 60376d528ed9Sopenharmony_ci 60386d528ed9Sopenharmony_ci#### **Example** 60396d528ed9Sopenharmony_ci 60406d528ed9Sopenharmony_ci``` 60416d528ed9Sopenharmony_ci action("myscript") { 60426d528ed9Sopenharmony_ci script = "domything.py" 60436d528ed9Sopenharmony_ci inputs = [ "input.data" ] 60446d528ed9Sopenharmony_ci } 60456d528ed9Sopenharmony_ci``` 60466d528ed9Sopenharmony_ci### <a name="var_ldflags"></a>**ldflags**: Flags passed to the linker. 60476d528ed9Sopenharmony_ci 60486d528ed9Sopenharmony_ci``` 60496d528ed9Sopenharmony_ci A list of strings. 60506d528ed9Sopenharmony_ci 60516d528ed9Sopenharmony_ci These flags are passed on the command-line to the linker and generally 60526d528ed9Sopenharmony_ci specify various linking options. Most targets will not need these and will 60536d528ed9Sopenharmony_ci use "libs" and "lib_dirs" instead. 60546d528ed9Sopenharmony_ci 60556d528ed9Sopenharmony_ci ldflags are NOT pushed to dependents, so applying ldflags to source sets or 60566d528ed9Sopenharmony_ci static libraries will be a no-op. If you want to apply ldflags to dependent 60576d528ed9Sopenharmony_ci targets, put them in a config and set it in the all_dependent_configs or 60586d528ed9Sopenharmony_ci public_configs. 60596d528ed9Sopenharmony_ci``` 60606d528ed9Sopenharmony_ci 60616d528ed9Sopenharmony_ci#### **Ordering of flags and values** 60626d528ed9Sopenharmony_ci 60636d528ed9Sopenharmony_ci``` 60646d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 60656d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 60666d528ed9Sopenharmony_ci configs appear in the list. 60676d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 60686d528ed9Sopenharmony_ci that the configs appear in the list. 60696d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 60706d528ed9Sopenharmony_ci those configs appear in the list. 60716d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 60726d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 60736d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 60746d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 60756d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 60766d528ed9Sopenharmony_ci recursively. 60776d528ed9Sopenharmony_ci``` 60786d528ed9Sopenharmony_ci### <a name="var_lib_dirs"></a>**lib_dirs**: Additional library directories. 60796d528ed9Sopenharmony_ci 60806d528ed9Sopenharmony_ci``` 60816d528ed9Sopenharmony_ci A list of directories. 60826d528ed9Sopenharmony_ci 60836d528ed9Sopenharmony_ci Specifies additional directories passed to the linker for searching for the 60846d528ed9Sopenharmony_ci required libraries. If an item is not an absolute path, it will be treated as 60856d528ed9Sopenharmony_ci being relative to the current build file. 60866d528ed9Sopenharmony_ci 60876d528ed9Sopenharmony_ci libs and lib_dirs work differently than other flags in two respects. 60886d528ed9Sopenharmony_ci First, they are inherited across static library boundaries until a 60896d528ed9Sopenharmony_ci shared library or executable target is reached. Second, they are 60906d528ed9Sopenharmony_ci uniquified so each one is only passed once (the first instance of it 60916d528ed9Sopenharmony_ci will be the one used). 60926d528ed9Sopenharmony_ci``` 60936d528ed9Sopenharmony_ci 60946d528ed9Sopenharmony_ci#### **Ordering of flags and values** 60956d528ed9Sopenharmony_ci 60966d528ed9Sopenharmony_ci``` 60976d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 60986d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 60996d528ed9Sopenharmony_ci configs appear in the list. 61006d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 61016d528ed9Sopenharmony_ci that the configs appear in the list. 61026d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 61036d528ed9Sopenharmony_ci those configs appear in the list. 61046d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 61056d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 61066d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 61076d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 61086d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 61096d528ed9Sopenharmony_ci recursively. 61106d528ed9Sopenharmony_ci 61116d528ed9Sopenharmony_ci For "libs" and "lib_dirs" only, the values propagated from 61126d528ed9Sopenharmony_ci dependencies (as described above) are applied last assuming they 61136d528ed9Sopenharmony_ci are not already in the list. 61146d528ed9Sopenharmony_ci``` 61156d528ed9Sopenharmony_ci 61166d528ed9Sopenharmony_ci#### **Example** 61176d528ed9Sopenharmony_ci 61186d528ed9Sopenharmony_ci``` 61196d528ed9Sopenharmony_ci lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ] 61206d528ed9Sopenharmony_ci``` 61216d528ed9Sopenharmony_ci### <a name="var_libs"></a>**libs**: Additional libraries to link. 61226d528ed9Sopenharmony_ci 61236d528ed9Sopenharmony_ci``` 61246d528ed9Sopenharmony_ci A list of library names or library paths. 61256d528ed9Sopenharmony_ci 61266d528ed9Sopenharmony_ci These libraries will be linked into the final binary (executable or shared 61276d528ed9Sopenharmony_ci library) containing the current target. 61286d528ed9Sopenharmony_ci 61296d528ed9Sopenharmony_ci libs and lib_dirs work differently than other flags in two respects. 61306d528ed9Sopenharmony_ci First, they are inherited across static library boundaries until a 61316d528ed9Sopenharmony_ci shared library or executable target is reached. Second, they are 61326d528ed9Sopenharmony_ci uniquified so each one is only passed once (the first instance of it 61336d528ed9Sopenharmony_ci will be the one used). 61346d528ed9Sopenharmony_ci``` 61356d528ed9Sopenharmony_ci 61366d528ed9Sopenharmony_ci#### **Types of libs** 61376d528ed9Sopenharmony_ci 61386d528ed9Sopenharmony_ci``` 61396d528ed9Sopenharmony_ci There are several different things that can be expressed in libs: 61406d528ed9Sopenharmony_ci 61416d528ed9Sopenharmony_ci File paths 61426d528ed9Sopenharmony_ci Values containing '/' will be treated as references to files in the 61436d528ed9Sopenharmony_ci checkout. They will be rebased to be relative to the build directory and 61446d528ed9Sopenharmony_ci specified in the "libs" for linker tools. This facility should be used 61456d528ed9Sopenharmony_ci for libraries that are checked in to the version control. For libraries 61466d528ed9Sopenharmony_ci that are generated by the build, use normal GN deps to link them. 61476d528ed9Sopenharmony_ci 61486d528ed9Sopenharmony_ci System libraries 61496d528ed9Sopenharmony_ci Values not containing '/' will be treated as system library names. These 61506d528ed9Sopenharmony_ci will be passed unmodified to the linker and prefixed with the 61516d528ed9Sopenharmony_ci "lib_switch" attribute of the linker tool. Generally you would set the 61526d528ed9Sopenharmony_ci "lib_dirs" so the given library is found. Your BUILD.gn file should not 61536d528ed9Sopenharmony_ci specify the switch (like "-l"): this will be encoded in the "lib_switch" 61546d528ed9Sopenharmony_ci of the tool. 61556d528ed9Sopenharmony_ci``` 61566d528ed9Sopenharmony_ci 61576d528ed9Sopenharmony_ci#### **Ordering of flags and values** 61586d528ed9Sopenharmony_ci 61596d528ed9Sopenharmony_ci``` 61606d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 61616d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 61626d528ed9Sopenharmony_ci configs appear in the list. 61636d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 61646d528ed9Sopenharmony_ci that the configs appear in the list. 61656d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 61666d528ed9Sopenharmony_ci those configs appear in the list. 61676d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 61686d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 61696d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 61706d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 61716d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 61726d528ed9Sopenharmony_ci recursively. 61736d528ed9Sopenharmony_ci 61746d528ed9Sopenharmony_ci For "libs" and "lib_dirs" only, the values propagated from 61756d528ed9Sopenharmony_ci dependencies (as described above) are applied last assuming they 61766d528ed9Sopenharmony_ci are not already in the list. 61776d528ed9Sopenharmony_ci``` 61786d528ed9Sopenharmony_ci 61796d528ed9Sopenharmony_ci#### **Examples** 61806d528ed9Sopenharmony_ci 61816d528ed9Sopenharmony_ci``` 61826d528ed9Sopenharmony_ci On Windows: 61836d528ed9Sopenharmony_ci libs = [ "ctl3d.lib" ] 61846d528ed9Sopenharmony_ci 61856d528ed9Sopenharmony_ci On Linux: 61866d528ed9Sopenharmony_ci libs = [ "ld" ] 61876d528ed9Sopenharmony_ci``` 61886d528ed9Sopenharmony_ci### <a name="var_metadata"></a>**metadata**: Metadata of this target. 61896d528ed9Sopenharmony_ci 61906d528ed9Sopenharmony_ci``` 61916d528ed9Sopenharmony_ci Metadata is a collection of keys and values relating to a particular target. 61926d528ed9Sopenharmony_ci Values must be lists, allowing for sane and predictable collection behavior. 61936d528ed9Sopenharmony_ci Generally, these keys will include three types of lists: lists of ordinary 61946d528ed9Sopenharmony_ci strings, lists of filenames intended to be rebased according to their 61956d528ed9Sopenharmony_ci particular source directory, and lists of target labels intended to be used 61966d528ed9Sopenharmony_ci as barriers to the walk. Verification of these categories occurs at walk time, 61976d528ed9Sopenharmony_ci not creation time (since it is not clear until the walk which values are 61986d528ed9Sopenharmony_ci intended for which purpose). 61996d528ed9Sopenharmony_ci``` 62006d528ed9Sopenharmony_ci 62016d528ed9Sopenharmony_ci#### **Example** 62026d528ed9Sopenharmony_ci 62036d528ed9Sopenharmony_ci``` 62046d528ed9Sopenharmony_ci group("doom_melon") { 62056d528ed9Sopenharmony_ci metadata = { 62066d528ed9Sopenharmony_ci # These keys are not built in to GN but are interpreted when consuming 62076d528ed9Sopenharmony_ci # metadata. 62086d528ed9Sopenharmony_ci my_barrier = [] 62096d528ed9Sopenharmony_ci my_files = [ "a.txt", "b.txt" ] 62106d528ed9Sopenharmony_ci } 62116d528ed9Sopenharmony_ci } 62126d528ed9Sopenharmony_ci``` 62136d528ed9Sopenharmony_ci### <a name="var_mnemonic"></a>**mnemonic**: [string] Prefix displayed when ninja runs this action. 62146d528ed9Sopenharmony_ci 62156d528ed9Sopenharmony_ci``` 62166d528ed9Sopenharmony_ci Tools in GN can set their ninja "description" which is displayed when 62176d528ed9Sopenharmony_ci building a target. These are commonly set with the format "CXX $output" 62186d528ed9Sopenharmony_ci or "LINK $label". By default, all GN actions will have the description 62196d528ed9Sopenharmony_ci "ACTION $label". Setting a mnemonic will override the "ACTION" prefix 62206d528ed9Sopenharmony_ci with another string, but the label will still be unconditionally displayed. 62216d528ed9Sopenharmony_ci 62226d528ed9Sopenharmony_ci Whitespace is not allowed within a mnemonic. 62236d528ed9Sopenharmony_ci``` 62246d528ed9Sopenharmony_ci### <a name="var_module_name"></a>**module_name**: [string] The name for the compiled module. 62256d528ed9Sopenharmony_ci 62266d528ed9Sopenharmony_ci``` 62276d528ed9Sopenharmony_ci Valid for binary targets that contain Swift sources. 62286d528ed9Sopenharmony_ci 62296d528ed9Sopenharmony_ci If module_name is not set, then this rule will use the target name. 62306d528ed9Sopenharmony_ci``` 62316d528ed9Sopenharmony_ci### <a name="var_output_conversion"></a>**output_conversion**: Data format for generated_file targets. 62326d528ed9Sopenharmony_ci 62336d528ed9Sopenharmony_ci``` 62346d528ed9Sopenharmony_ci Controls how the "contents" of a generated_file target is formatted. 62356d528ed9Sopenharmony_ci See `gn help io_conversion`. 62366d528ed9Sopenharmony_ci``` 62376d528ed9Sopenharmony_ci### <a name="var_output_dir"></a>**output_dir**: [directory] Directory to put output file in. 62386d528ed9Sopenharmony_ci 62396d528ed9Sopenharmony_ci``` 62406d528ed9Sopenharmony_ci For library and executable targets, overrides the directory for the final 62416d528ed9Sopenharmony_ci output. This must be in the root_build_dir or a child thereof. 62426d528ed9Sopenharmony_ci 62436d528ed9Sopenharmony_ci This should generally be in the root_out_dir or a subdirectory thereof (the 62446d528ed9Sopenharmony_ci root_out_dir will be the same as the root_build_dir for the default 62456d528ed9Sopenharmony_ci toolchain, and will be a subdirectory for other toolchains). Not putting the 62466d528ed9Sopenharmony_ci output in a subdirectory of root_out_dir can result in collisions between 62476d528ed9Sopenharmony_ci different toolchains, so you will need to take steps to ensure that your 62486d528ed9Sopenharmony_ci target is only present in one toolchain. 62496d528ed9Sopenharmony_ci 62506d528ed9Sopenharmony_ci Normally the toolchain specifies the output directory for libraries and 62516d528ed9Sopenharmony_ci executables (see "gn help tool"). You will have to consult that for the 62526d528ed9Sopenharmony_ci default location. The default location will be used if output_dir is 62536d528ed9Sopenharmony_ci undefined or empty. 62546d528ed9Sopenharmony_ci``` 62556d528ed9Sopenharmony_ci 62566d528ed9Sopenharmony_ci#### **Example** 62576d528ed9Sopenharmony_ci 62586d528ed9Sopenharmony_ci``` 62596d528ed9Sopenharmony_ci shared_library("doom_melon") { 62606d528ed9Sopenharmony_ci output_dir = "$root_out_dir/plugin_libs" 62616d528ed9Sopenharmony_ci ... 62626d528ed9Sopenharmony_ci } 62636d528ed9Sopenharmony_ci``` 62646d528ed9Sopenharmony_ci### <a name="var_output_extension"></a>**output_extension**: Value to use for the output's file extension. 62656d528ed9Sopenharmony_ci 62666d528ed9Sopenharmony_ci``` 62676d528ed9Sopenharmony_ci Normally the file extension for a target is based on the target type and the 62686d528ed9Sopenharmony_ci operating system, but in rare cases you will need to override the name (for 62696d528ed9Sopenharmony_ci example to use "libfreetype.so.6" instead of libfreetype.so on Linux). 62706d528ed9Sopenharmony_ci 62716d528ed9Sopenharmony_ci This value should not include a leading dot. If undefined, the default 62726d528ed9Sopenharmony_ci specified on the tool will be used. If set to the empty string, no output 62736d528ed9Sopenharmony_ci extension will be used. 62746d528ed9Sopenharmony_ci 62756d528ed9Sopenharmony_ci The output_extension will be used to set the "{{output_extension}}" expansion 62766d528ed9Sopenharmony_ci which the linker tool will generally use to specify the output file name. See 62776d528ed9Sopenharmony_ci "gn help tool". 62786d528ed9Sopenharmony_ci``` 62796d528ed9Sopenharmony_ci 62806d528ed9Sopenharmony_ci#### **Example** 62816d528ed9Sopenharmony_ci 62826d528ed9Sopenharmony_ci``` 62836d528ed9Sopenharmony_ci shared_library("freetype") { 62846d528ed9Sopenharmony_ci if (is_linux) { 62856d528ed9Sopenharmony_ci # Call the output "libfreetype.so.6" 62866d528ed9Sopenharmony_ci output_extension = "so.6" 62876d528ed9Sopenharmony_ci } 62886d528ed9Sopenharmony_ci ... 62896d528ed9Sopenharmony_ci } 62906d528ed9Sopenharmony_ci 62916d528ed9Sopenharmony_ci # On Windows, generate a "mysettings.cpl" control panel applet. Control panel 62926d528ed9Sopenharmony_ci # applets are actually special shared libraries. 62936d528ed9Sopenharmony_ci if (is_win) { 62946d528ed9Sopenharmony_ci shared_library("mysettings") { 62956d528ed9Sopenharmony_ci output_extension = "cpl" 62966d528ed9Sopenharmony_ci ... 62976d528ed9Sopenharmony_ci } 62986d528ed9Sopenharmony_ci } 62996d528ed9Sopenharmony_ci``` 63006d528ed9Sopenharmony_ci### <a name="var_output_name"></a>**output_name**: Define a name for the output file other than the default. 63016d528ed9Sopenharmony_ci 63026d528ed9Sopenharmony_ci``` 63036d528ed9Sopenharmony_ci Normally the output name of a target will be based on the target name, so the 63046d528ed9Sopenharmony_ci target "//foo/bar:bar_unittests" will generate an output file such as 63056d528ed9Sopenharmony_ci "bar_unittests.exe" (using Windows as an example). 63066d528ed9Sopenharmony_ci 63076d528ed9Sopenharmony_ci Sometimes you will want an alternate name to avoid collisions or if the 63086d528ed9Sopenharmony_ci internal name isn't appropriate for public distribution. 63096d528ed9Sopenharmony_ci 63106d528ed9Sopenharmony_ci The output name should have no extension or prefixes, these will be added 63116d528ed9Sopenharmony_ci using the default system rules. For example, on Linux an output name of "foo" 63126d528ed9Sopenharmony_ci will produce a shared library "libfoo.so". There is no way to override the 63136d528ed9Sopenharmony_ci output prefix of a linker tool on a per- target basis. If you need more 63146d528ed9Sopenharmony_ci flexibility, create a copy target to produce the file you want. 63156d528ed9Sopenharmony_ci 63166d528ed9Sopenharmony_ci This variable is valid for all binary output target types. 63176d528ed9Sopenharmony_ci``` 63186d528ed9Sopenharmony_ci 63196d528ed9Sopenharmony_ci#### **Example** 63206d528ed9Sopenharmony_ci 63216d528ed9Sopenharmony_ci``` 63226d528ed9Sopenharmony_ci static_library("doom_melon") { 63236d528ed9Sopenharmony_ci output_name = "fluffy_bunny" 63246d528ed9Sopenharmony_ci } 63256d528ed9Sopenharmony_ci``` 63266d528ed9Sopenharmony_ci### <a name="var_output_prefix_override"></a>**output_prefix_override**: Don't use prefix for output name. 63276d528ed9Sopenharmony_ci 63286d528ed9Sopenharmony_ci``` 63296d528ed9Sopenharmony_ci A boolean that overrides the output prefix for a target. Defaults to false. 63306d528ed9Sopenharmony_ci 63316d528ed9Sopenharmony_ci Some systems use prefixes for the names of the final target output file. The 63326d528ed9Sopenharmony_ci normal example is "libfoo.so" on Linux for a target named "foo". 63336d528ed9Sopenharmony_ci 63346d528ed9Sopenharmony_ci The output prefix for a given target type is specified on the linker tool 63356d528ed9Sopenharmony_ci (see "gn help tool"). Sometimes this prefix is undesired. 63366d528ed9Sopenharmony_ci 63376d528ed9Sopenharmony_ci See also "gn help output_extension". 63386d528ed9Sopenharmony_ci``` 63396d528ed9Sopenharmony_ci 63406d528ed9Sopenharmony_ci#### **Example** 63416d528ed9Sopenharmony_ci 63426d528ed9Sopenharmony_ci``` 63436d528ed9Sopenharmony_ci shared_library("doom_melon") { 63446d528ed9Sopenharmony_ci # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag 63456d528ed9Sopenharmony_ci # will produce "doom_melon.so". 63466d528ed9Sopenharmony_ci output_prefix_override = true 63476d528ed9Sopenharmony_ci ... 63486d528ed9Sopenharmony_ci } 63496d528ed9Sopenharmony_ci``` 63506d528ed9Sopenharmony_ci### <a name="var_outputs"></a>**outputs**: Output files for actions and copy targets. 63516d528ed9Sopenharmony_ci 63526d528ed9Sopenharmony_ci``` 63536d528ed9Sopenharmony_ci Outputs is valid for "copy", "action", and "action_foreach" target types and 63546d528ed9Sopenharmony_ci indicates the resulting files. Outputs must always refer to files in the 63556d528ed9Sopenharmony_ci build directory. 63566d528ed9Sopenharmony_ci 63576d528ed9Sopenharmony_ci copy 63586d528ed9Sopenharmony_ci Copy targets should have exactly one entry in the outputs list. If there is 63596d528ed9Sopenharmony_ci exactly one source, this can be a literal file name or a source expansion. 63606d528ed9Sopenharmony_ci If there is more than one source, this must contain a source expansion to 63616d528ed9Sopenharmony_ci map a single input name to a single output name. See "gn help copy". 63626d528ed9Sopenharmony_ci 63636d528ed9Sopenharmony_ci action_foreach 63646d528ed9Sopenharmony_ci Action_foreach targets must always use source expansions to map input files 63656d528ed9Sopenharmony_ci to output files. There can be more than one output, which means that each 63666d528ed9Sopenharmony_ci invocation of the script will produce a set of files (presumably based on 63676d528ed9Sopenharmony_ci the name of the input file). See "gn help action_foreach". 63686d528ed9Sopenharmony_ci 63696d528ed9Sopenharmony_ci action 63706d528ed9Sopenharmony_ci Action targets (excluding action_foreach) must list literal output file(s) 63716d528ed9Sopenharmony_ci with no source expansions. See "gn help action". 63726d528ed9Sopenharmony_ci``` 63736d528ed9Sopenharmony_ci### <a name="var_partial_info_plist"></a>**partial_info_plist**: [filename] Path plist from asset catalog compiler. 63746d528ed9Sopenharmony_ci 63756d528ed9Sopenharmony_ci``` 63766d528ed9Sopenharmony_ci Valid for create_bundle target, corresponds to the path for the partial 63776d528ed9Sopenharmony_ci Info.plist created by the asset catalog compiler that needs to be merged 63786d528ed9Sopenharmony_ci with the application Info.plist (usually done by the post-processing script). 63796d528ed9Sopenharmony_ci 63806d528ed9Sopenharmony_ci The file will be generated regardless of whether the asset compiler has 63816d528ed9Sopenharmony_ci been invoked or not. See "gn help create_bundle". 63826d528ed9Sopenharmony_ci``` 63836d528ed9Sopenharmony_ci### <a name="var_pool"></a>**pool**: Label of the pool used by binary targets actions. 63846d528ed9Sopenharmony_ci 63856d528ed9Sopenharmony_ci``` 63866d528ed9Sopenharmony_ci A fully-qualified label representing the pool that will be used for binary 63876d528ed9Sopenharmony_ci targets and actions. Pools are defined using the pool() {...} declaration. 63886d528ed9Sopenharmony_ci``` 63896d528ed9Sopenharmony_ci 63906d528ed9Sopenharmony_ci#### **Example** 63916d528ed9Sopenharmony_ci 63926d528ed9Sopenharmony_ci``` 63936d528ed9Sopenharmony_ci executable("binary") { 63946d528ed9Sopenharmony_ci pool = "//build:custom_pool" 63956d528ed9Sopenharmony_ci ... 63966d528ed9Sopenharmony_ci } 63976d528ed9Sopenharmony_ci 63986d528ed9Sopenharmony_ci action("action") { 63996d528ed9Sopenharmony_ci pool = "//build:custom_pool" 64006d528ed9Sopenharmony_ci ... 64016d528ed9Sopenharmony_ci } 64026d528ed9Sopenharmony_ci``` 64036d528ed9Sopenharmony_ci### <a name="var_post_processing_args"></a>**post_processing_args**: [string list] Args for the post-processing script. 64046d528ed9Sopenharmony_ci 64056d528ed9Sopenharmony_ci``` 64066d528ed9Sopenharmony_ci For create_bundle targets, post_processing_args is the list of arguments to 64076d528ed9Sopenharmony_ci pass to the post-processing script. Typically you would use source expansion 64086d528ed9Sopenharmony_ci (see "gn help source_expansion") to insert the source file names. 64096d528ed9Sopenharmony_ci 64106d528ed9Sopenharmony_ci See also "gn help create_bundle". 64116d528ed9Sopenharmony_ci``` 64126d528ed9Sopenharmony_ci### <a name="var_post_processing_outputs"></a>**post_processing_outputs**: [file list] Outputs of the post-processing step. 64136d528ed9Sopenharmony_ci 64146d528ed9Sopenharmony_ci``` 64156d528ed9Sopenharmony_ci Outputs from the post-processing step of a create_bundle target. Must refer to 64166d528ed9Sopenharmony_ci files in the build directory. 64176d528ed9Sopenharmony_ci 64186d528ed9Sopenharmony_ci See also "gn help create_bundle". 64196d528ed9Sopenharmony_ci``` 64206d528ed9Sopenharmony_ci### <a name="var_post_processing_script"></a>**post_processing_script**: [file name] Script for the post-processing step." 64216d528ed9Sopenharmony_ci 64226d528ed9Sopenharmony_ci``` 64236d528ed9Sopenharmony_ci An absolute or buildfile-relative file name of a Python script to run for a 64246d528ed9Sopenharmony_ci create_bundle target to perform the post-processing step. 64256d528ed9Sopenharmony_ci 64266d528ed9Sopenharmony_ci See also "gn help create_bundle". 64276d528ed9Sopenharmony_ci``` 64286d528ed9Sopenharmony_ci### <a name="var_post_processing_sources"></a>**post_processing_sources**: [file list] Sources for the post-processing step. 64296d528ed9Sopenharmony_ci 64306d528ed9Sopenharmony_ci``` 64316d528ed9Sopenharmony_ci A list of files used as input for the post-processing step of a create_bundle 64326d528ed9Sopenharmony_ci target. Non-absolute paths will be resolved relative to the current build 64336d528ed9Sopenharmony_ci file. 64346d528ed9Sopenharmony_ci 64356d528ed9Sopenharmony_ci See also "gn help create_bundle". 64366d528ed9Sopenharmony_ci``` 64376d528ed9Sopenharmony_ci### <a name="var_precompiled_header"></a>**precompiled_header**: [string] Header file to precompile. 64386d528ed9Sopenharmony_ci 64396d528ed9Sopenharmony_ci``` 64406d528ed9Sopenharmony_ci Precompiled headers will be used when a target specifies this value, or a 64416d528ed9Sopenharmony_ci config applying to this target specifies this value. In addition, the tool 64426d528ed9Sopenharmony_ci corresponding to the source files must also specify precompiled headers (see 64436d528ed9Sopenharmony_ci "gn help tool"). The tool will also specify what type of precompiled headers 64446d528ed9Sopenharmony_ci to use, by setting precompiled_header_type to either "gcc" or "msvc". 64456d528ed9Sopenharmony_ci 64466d528ed9Sopenharmony_ci The precompiled header/source variables can be specified on a target or a 64476d528ed9Sopenharmony_ci config, but must be the same for all configs applying to a given target since 64486d528ed9Sopenharmony_ci a target can only have one precompiled header. 64496d528ed9Sopenharmony_ci 64506d528ed9Sopenharmony_ci If you use both C and C++ sources, the precompiled header and source file 64516d528ed9Sopenharmony_ci will be compiled once per language. You will want to make sure to wrap C++ 64526d528ed9Sopenharmony_ci includes in __cplusplus #ifdefs so the file will compile in C mode. 64536d528ed9Sopenharmony_ci``` 64546d528ed9Sopenharmony_ci 64556d528ed9Sopenharmony_ci#### **GCC precompiled headers** 64566d528ed9Sopenharmony_ci 64576d528ed9Sopenharmony_ci``` 64586d528ed9Sopenharmony_ci When using GCC-style precompiled headers, "precompiled_source" contains the 64596d528ed9Sopenharmony_ci path of a .h file that is precompiled and then included by all source files 64606d528ed9Sopenharmony_ci in targets that set "precompiled_source". 64616d528ed9Sopenharmony_ci 64626d528ed9Sopenharmony_ci The value of "precompiled_header" is not used with GCC-style precompiled 64636d528ed9Sopenharmony_ci headers. 64646d528ed9Sopenharmony_ci``` 64656d528ed9Sopenharmony_ci 64666d528ed9Sopenharmony_ci#### **MSVC precompiled headers** 64676d528ed9Sopenharmony_ci 64686d528ed9Sopenharmony_ci``` 64696d528ed9Sopenharmony_ci When using MSVC-style precompiled headers, the "precompiled_header" value is 64706d528ed9Sopenharmony_ci a string corresponding to the header. This is NOT a path to a file that GN 64716d528ed9Sopenharmony_ci recognises, but rather the exact string that appears in quotes after 64726d528ed9Sopenharmony_ci an #include line in source code. The compiler will match this string against 64736d528ed9Sopenharmony_ci includes or forced includes (/FI). 64746d528ed9Sopenharmony_ci 64756d528ed9Sopenharmony_ci MSVC also requires a source file to compile the header with. This must be 64766d528ed9Sopenharmony_ci specified by the "precompiled_source" value. In contrast to the header value, 64776d528ed9Sopenharmony_ci this IS a GN-style file name, and tells GN which source file to compile to 64786d528ed9Sopenharmony_ci make the .pch file used for subsequent compiles. 64796d528ed9Sopenharmony_ci 64806d528ed9Sopenharmony_ci For example, if the toolchain specifies MSVC headers: 64816d528ed9Sopenharmony_ci 64826d528ed9Sopenharmony_ci toolchain("vc_x64") { 64836d528ed9Sopenharmony_ci ... 64846d528ed9Sopenharmony_ci tool("cxx") { 64856d528ed9Sopenharmony_ci precompiled_header_type = "msvc" 64866d528ed9Sopenharmony_ci ... 64876d528ed9Sopenharmony_ci 64886d528ed9Sopenharmony_ci You might make a config like this: 64896d528ed9Sopenharmony_ci 64906d528ed9Sopenharmony_ci config("use_precompiled_headers") { 64916d528ed9Sopenharmony_ci precompiled_header = "build/precompile.h" 64926d528ed9Sopenharmony_ci precompiled_source = "//build/precompile.cc" 64936d528ed9Sopenharmony_ci 64946d528ed9Sopenharmony_ci # Either your source files should #include "build/precompile.h" 64956d528ed9Sopenharmony_ci # first, or you can do this to force-include the header. 64966d528ed9Sopenharmony_ci cflags = [ "/FI$precompiled_header" ] 64976d528ed9Sopenharmony_ci } 64986d528ed9Sopenharmony_ci 64996d528ed9Sopenharmony_ci And then define a target that uses the config: 65006d528ed9Sopenharmony_ci 65016d528ed9Sopenharmony_ci executable("doom_melon") { 65026d528ed9Sopenharmony_ci configs += [ ":use_precompiled_headers" ] 65036d528ed9Sopenharmony_ci ... 65046d528ed9Sopenharmony_ci``` 65056d528ed9Sopenharmony_ci### <a name="var_precompiled_header_type"></a>**precompiled_header_type**: [string] "gcc" or "msvc". 65066d528ed9Sopenharmony_ci 65076d528ed9Sopenharmony_ci``` 65086d528ed9Sopenharmony_ci See "gn help precompiled_header". 65096d528ed9Sopenharmony_ci``` 65106d528ed9Sopenharmony_ci### <a name="var_precompiled_source"></a>**precompiled_source**: [file name] Source file to precompile. 65116d528ed9Sopenharmony_ci 65126d528ed9Sopenharmony_ci``` 65136d528ed9Sopenharmony_ci The source file that goes along with the precompiled_header when using 65146d528ed9Sopenharmony_ci "msvc"-style precompiled headers. It will be implicitly added to the sources 65156d528ed9Sopenharmony_ci of the target. See "gn help precompiled_header". 65166d528ed9Sopenharmony_ci``` 65176d528ed9Sopenharmony_ci### <a name="var_product_type"></a>**product_type**: [string] Product type for the bundle. 65186d528ed9Sopenharmony_ci 65196d528ed9Sopenharmony_ci``` 65206d528ed9Sopenharmony_ci Valid for "create_bundle" and "bundle_data" targets. 65216d528ed9Sopenharmony_ci 65226d528ed9Sopenharmony_ci Correspond to the type of the bundle. Used by transparent "create_bundle" 65236d528ed9Sopenharmony_ci target to control whether a "bundle_data" needs to be propagated or not. 65246d528ed9Sopenharmony_ci 65256d528ed9Sopenharmony_ci When generating Xcode project, the product_type is propagated and only 65266d528ed9Sopenharmony_ci "create_bundle" with a non-empty product_type will have a corresponding 65276d528ed9Sopenharmony_ci target in the project. 65286d528ed9Sopenharmony_ci``` 65296d528ed9Sopenharmony_ci### <a name="var_public"></a>**public**: Declare public header files for a target. 65306d528ed9Sopenharmony_ci 65316d528ed9Sopenharmony_ci``` 65326d528ed9Sopenharmony_ci A list of files that other targets can include. These permissions are checked 65336d528ed9Sopenharmony_ci via the "check" command (see "gn help check"). 65346d528ed9Sopenharmony_ci 65356d528ed9Sopenharmony_ci If no public files are declared, other targets (assuming they have visibility 65366d528ed9Sopenharmony_ci to depend on this target) can include any file in the sources list. If this 65376d528ed9Sopenharmony_ci variable is defined on a target, dependent targets may only include files on 65386d528ed9Sopenharmony_ci this whitelist unless that target is marked as a friend (see "gn help 65396d528ed9Sopenharmony_ci friend"). 65406d528ed9Sopenharmony_ci 65416d528ed9Sopenharmony_ci Header file permissions are also subject to visibility. A target must be 65426d528ed9Sopenharmony_ci visible to another target to include any files from it at all and the public 65436d528ed9Sopenharmony_ci headers indicate which subset of those files are permitted. See "gn help 65446d528ed9Sopenharmony_ci visibility" for more. 65456d528ed9Sopenharmony_ci 65466d528ed9Sopenharmony_ci Public files are inherited through the dependency tree. So if there is a 65476d528ed9Sopenharmony_ci dependency A -> B -> C, then A can include C's public headers. However, the 65486d528ed9Sopenharmony_ci same is NOT true of visibility, so unless A is in C's visibility list, the 65496d528ed9Sopenharmony_ci include will be rejected. 65506d528ed9Sopenharmony_ci 65516d528ed9Sopenharmony_ci GN only knows about files declared in the "sources" and "public" sections of 65526d528ed9Sopenharmony_ci targets. If a file is included that is not known to the build, it will be 65536d528ed9Sopenharmony_ci allowed. 65546d528ed9Sopenharmony_ci 65556d528ed9Sopenharmony_ci It is common for test targets to need to include private headers for their 65566d528ed9Sopenharmony_ci associated code. In this case, list the test target in the "friend" list of 65576d528ed9Sopenharmony_ci the target that owns the private header to allow the inclusion. See 65586d528ed9Sopenharmony_ci "gn help friend" for more. 65596d528ed9Sopenharmony_ci 65606d528ed9Sopenharmony_ci When a binary target has no explicit or implicit public headers (a "public" 65616d528ed9Sopenharmony_ci list is defined but is empty), GN assumes that the target can not propagate 65626d528ed9Sopenharmony_ci any compile-time dependencies up the dependency tree. In this case, the build 65636d528ed9Sopenharmony_ci can be parallelized more efficiently. 65646d528ed9Sopenharmony_ci Say there are dependencies: 65656d528ed9Sopenharmony_ci A (shared library) -> B (shared library) -> C (action). 65666d528ed9Sopenharmony_ci Normally C must complete before any source files in A can compile (because 65676d528ed9Sopenharmony_ci there might be generated includes). But when B explicitly declares no public 65686d528ed9Sopenharmony_ci headers, C can execute in parallel with A's compile steps. C must still be 65696d528ed9Sopenharmony_ci complete before any dependents link. 65706d528ed9Sopenharmony_ci``` 65716d528ed9Sopenharmony_ci 65726d528ed9Sopenharmony_ci#### **Examples** 65736d528ed9Sopenharmony_ci 65746d528ed9Sopenharmony_ci``` 65756d528ed9Sopenharmony_ci These exact files are public: 65766d528ed9Sopenharmony_ci public = [ "foo.h", "bar.h" ] 65776d528ed9Sopenharmony_ci 65786d528ed9Sopenharmony_ci No files are public (no targets may include headers from this one): 65796d528ed9Sopenharmony_ci # This allows starting compilation in dependent targets earlier. 65806d528ed9Sopenharmony_ci public = [] 65816d528ed9Sopenharmony_ci``` 65826d528ed9Sopenharmony_ci### <a name="var_public_configs"></a>**public_configs**: Configs to be applied on dependents. 65836d528ed9Sopenharmony_ci 65846d528ed9Sopenharmony_ci``` 65856d528ed9Sopenharmony_ci A list of config labels. 65866d528ed9Sopenharmony_ci 65876d528ed9Sopenharmony_ci Targets directly depending on this one will have the configs listed in this 65886d528ed9Sopenharmony_ci variable added to them. These configs will also apply to the current target. 65896d528ed9Sopenharmony_ci Generally, public configs are used to apply defines and include directories 65906d528ed9Sopenharmony_ci necessary to compile this target's header files. 65916d528ed9Sopenharmony_ci 65926d528ed9Sopenharmony_ci See also "gn help all_dependent_configs". 65936d528ed9Sopenharmony_ci``` 65946d528ed9Sopenharmony_ci 65956d528ed9Sopenharmony_ci#### **Propagation of public configs** 65966d528ed9Sopenharmony_ci 65976d528ed9Sopenharmony_ci``` 65986d528ed9Sopenharmony_ci Public configs are applied to all targets that depend directly on this one. 65996d528ed9Sopenharmony_ci These dependent targets can further push this target's public configs 66006d528ed9Sopenharmony_ci higher in the dependency tree by depending on it via public_deps (see "gn 66016d528ed9Sopenharmony_ci help public_deps"). 66026d528ed9Sopenharmony_ci 66036d528ed9Sopenharmony_ci static_library("toplevel") { 66046d528ed9Sopenharmony_ci # This target will get "my_config" applied to it. However, since this 66056d528ed9Sopenharmony_ci # target uses "deps" and not "public_deps", targets that depend on this 66066d528ed9Sopenharmony_ci # one won't get it. 66076d528ed9Sopenharmony_ci deps = [ ":intermediate" ] 66086d528ed9Sopenharmony_ci } 66096d528ed9Sopenharmony_ci 66106d528ed9Sopenharmony_ci static_library("intermediate") { 66116d528ed9Sopenharmony_ci # Depending on "lower" in any way will apply "my_config" to this target. 66126d528ed9Sopenharmony_ci # Additionall, since this target depends on "lower" via public_deps, 66136d528ed9Sopenharmony_ci # targets that depend on this one will also get "my_config". 66146d528ed9Sopenharmony_ci public_deps = [ ":lower" ] 66156d528ed9Sopenharmony_ci } 66166d528ed9Sopenharmony_ci 66176d528ed9Sopenharmony_ci static_library("lower") { 66186d528ed9Sopenharmony_ci # This will get applied to all targets that depend on this one. 66196d528ed9Sopenharmony_ci public_configs = [ ":my_config" ] 66206d528ed9Sopenharmony_ci } 66216d528ed9Sopenharmony_ci 66226d528ed9Sopenharmony_ci Public config propagation happens in a second phase once a target and all of 66236d528ed9Sopenharmony_ci its dependencies have been resolved. Therefore, a target will not see these 66246d528ed9Sopenharmony_ci force-added configs in their "configs" variable while the script is running, 66256d528ed9Sopenharmony_ci and they can not be removed. As a result, this capability should generally 66266d528ed9Sopenharmony_ci only be used to add defines and include directories rather than setting 66276d528ed9Sopenharmony_ci complicated flags that some targets may not want. 66286d528ed9Sopenharmony_ci 66296d528ed9Sopenharmony_ci Public configs may or may not be propagated across toolchain boundaries 66306d528ed9Sopenharmony_ci depending on the value of the propagates_configs flag (see "gn help 66316d528ed9Sopenharmony_ci toolchain") on the toolchain of the target declaring the public_config. 66326d528ed9Sopenharmony_ci``` 66336d528ed9Sopenharmony_ci 66346d528ed9Sopenharmony_ci#### **Avoiding applying public configs to this target** 66356d528ed9Sopenharmony_ci 66366d528ed9Sopenharmony_ci``` 66376d528ed9Sopenharmony_ci If you want the config to apply to targets that depend on this one, but NOT 66386d528ed9Sopenharmony_ci this one, define an extra layer of indirection using a group: 66396d528ed9Sopenharmony_ci 66406d528ed9Sopenharmony_ci # External targets depend on this group. 66416d528ed9Sopenharmony_ci group("my_target") { 66426d528ed9Sopenharmony_ci # Config to apply to all targets that depend on this one. 66436d528ed9Sopenharmony_ci public_configs = [ ":external_settings" ] 66446d528ed9Sopenharmony_ci deps = [ ":internal_target" ] 66456d528ed9Sopenharmony_ci } 66466d528ed9Sopenharmony_ci 66476d528ed9Sopenharmony_ci # Internal target to actually compile the sources. 66486d528ed9Sopenharmony_ci static_library("internal_target") { 66496d528ed9Sopenharmony_ci # Force all external targets to depend on the group instead of directly 66506d528ed9Sopenharmony_ci # on this so the "external_settings" config will get applied. 66516d528ed9Sopenharmony_ci visibility = [ ":my_target" ] 66526d528ed9Sopenharmony_ci ... 66536d528ed9Sopenharmony_ci } 66546d528ed9Sopenharmony_ci``` 66556d528ed9Sopenharmony_ci 66566d528ed9Sopenharmony_ci#### **Ordering of flags and values** 66576d528ed9Sopenharmony_ci 66586d528ed9Sopenharmony_ci``` 66596d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 66606d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 66616d528ed9Sopenharmony_ci configs appear in the list. 66626d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 66636d528ed9Sopenharmony_ci that the configs appear in the list. 66646d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 66656d528ed9Sopenharmony_ci those configs appear in the list. 66666d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 66676d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 66686d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 66696d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 66706d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 66716d528ed9Sopenharmony_ci recursively. 66726d528ed9Sopenharmony_ci``` 66736d528ed9Sopenharmony_ci### <a name="var_public_deps"></a>**public_deps**: Declare public dependencies. 66746d528ed9Sopenharmony_ci 66756d528ed9Sopenharmony_ci``` 66766d528ed9Sopenharmony_ci Public dependencies are like private dependencies (see "gn help deps") but 66776d528ed9Sopenharmony_ci additionally express that the current target exposes the listed deps as part 66786d528ed9Sopenharmony_ci of its public API. 66796d528ed9Sopenharmony_ci 66806d528ed9Sopenharmony_ci This has several ramifications: 66816d528ed9Sopenharmony_ci 66826d528ed9Sopenharmony_ci - public_configs that are part of the dependency are forwarded to direct 66836d528ed9Sopenharmony_ci dependents. 66846d528ed9Sopenharmony_ci 66856d528ed9Sopenharmony_ci - Public headers in the dependency are usable by dependents (includes do 66866d528ed9Sopenharmony_ci not require a direct dependency or visibility). 66876d528ed9Sopenharmony_ci 66886d528ed9Sopenharmony_ci - If the current target is a shared library, other shared libraries that it 66896d528ed9Sopenharmony_ci publicly depends on (directly or indirectly) are propagated up the 66906d528ed9Sopenharmony_ci dependency tree to dependents for linking. 66916d528ed9Sopenharmony_ci 66926d528ed9Sopenharmony_ci See also "gn help public_configs". 66936d528ed9Sopenharmony_ci``` 66946d528ed9Sopenharmony_ci 66956d528ed9Sopenharmony_ci#### **Discussion** 66966d528ed9Sopenharmony_ci 66976d528ed9Sopenharmony_ci``` 66986d528ed9Sopenharmony_ci Say you have three targets: A -> B -> C. C's visibility may allow B to depend 66996d528ed9Sopenharmony_ci on it but not A. Normally, this would prevent A from including any headers 67006d528ed9Sopenharmony_ci from C, and C's public_configs would apply only to B. 67016d528ed9Sopenharmony_ci 67026d528ed9Sopenharmony_ci If B lists C in its public_deps instead of regular deps, A will now inherit 67036d528ed9Sopenharmony_ci C's public_configs and the ability to include C's public headers. 67046d528ed9Sopenharmony_ci 67056d528ed9Sopenharmony_ci Generally if you are writing a target B and you include C's headers as part 67066d528ed9Sopenharmony_ci of B's public headers, or targets depending on B should consider B and C to 67076d528ed9Sopenharmony_ci be part of a unit, you should use public_deps instead of deps. 67086d528ed9Sopenharmony_ci``` 67096d528ed9Sopenharmony_ci 67106d528ed9Sopenharmony_ci#### **Example** 67116d528ed9Sopenharmony_ci 67126d528ed9Sopenharmony_ci``` 67136d528ed9Sopenharmony_ci # This target can include files from "c" but not from 67146d528ed9Sopenharmony_ci # "super_secret_implementation_details". 67156d528ed9Sopenharmony_ci executable("a") { 67166d528ed9Sopenharmony_ci deps = [ ":b" ] 67176d528ed9Sopenharmony_ci } 67186d528ed9Sopenharmony_ci 67196d528ed9Sopenharmony_ci shared_library("b") { 67206d528ed9Sopenharmony_ci deps = [ ":super_secret_implementation_details" ] 67216d528ed9Sopenharmony_ci public_deps = [ ":c" ] 67226d528ed9Sopenharmony_ci } 67236d528ed9Sopenharmony_ci``` 67246d528ed9Sopenharmony_ci### <a name="var_rebase"></a>**rebase**: Rebase collected metadata as files. 67256d528ed9Sopenharmony_ci 67266d528ed9Sopenharmony_ci``` 67276d528ed9Sopenharmony_ci A boolean that triggers a rebase of collected metadata strings based on their 67286d528ed9Sopenharmony_ci declared file. Defaults to false. 67296d528ed9Sopenharmony_ci 67306d528ed9Sopenharmony_ci Metadata generally declares files as strings relative to the local build file. 67316d528ed9Sopenharmony_ci However, this data is often used in other contexts, and so setting this flag 67326d528ed9Sopenharmony_ci will force the metadata collection to be rebased according to the local build 67336d528ed9Sopenharmony_ci file's location and thus allow the filename to be used anywhere. 67346d528ed9Sopenharmony_ci 67356d528ed9Sopenharmony_ci Setting this flag will raise an error if any target's specified metadata is 67366d528ed9Sopenharmony_ci not a string value. 67376d528ed9Sopenharmony_ci 67386d528ed9Sopenharmony_ci See also "gn help generated_file". 67396d528ed9Sopenharmony_ci``` 67406d528ed9Sopenharmony_ci### <a name="var_response_file_contents"></a>**response_file_contents**: Contents of a response file for actions. 67416d528ed9Sopenharmony_ci 67426d528ed9Sopenharmony_ci``` 67436d528ed9Sopenharmony_ci Sometimes the arguments passed to a script can be too long for the system's 67446d528ed9Sopenharmony_ci command-line capabilities. This is especially the case on Windows where the 67456d528ed9Sopenharmony_ci maximum command-line length is less than 8K. A response file allows you to 67466d528ed9Sopenharmony_ci pass an unlimited amount of data to a script in a temporary file for an 67476d528ed9Sopenharmony_ci action or action_foreach target. 67486d528ed9Sopenharmony_ci 67496d528ed9Sopenharmony_ci If the response_file_contents variable is defined and non-empty, the list 67506d528ed9Sopenharmony_ci will be treated as script args (including possibly substitution patterns) 67516d528ed9Sopenharmony_ci that will be written to a temporary file at build time. The name of the 67526d528ed9Sopenharmony_ci temporary file will be substituted for "{{response_file_name}}" in the script 67536d528ed9Sopenharmony_ci args. 67546d528ed9Sopenharmony_ci 67556d528ed9Sopenharmony_ci The response file contents will always be quoted and escaped according to 67566d528ed9Sopenharmony_ci Unix shell rules. To parse the response file, the Python script should use 67576d528ed9Sopenharmony_ci "shlex.split(file_contents)". 67586d528ed9Sopenharmony_ci``` 67596d528ed9Sopenharmony_ci 67606d528ed9Sopenharmony_ci#### **Example** 67616d528ed9Sopenharmony_ci 67626d528ed9Sopenharmony_ci``` 67636d528ed9Sopenharmony_ci action("process_lots_of_files") { 67646d528ed9Sopenharmony_ci script = "process.py", 67656d528ed9Sopenharmony_ci inputs = [ ... huge list of files ... ] 67666d528ed9Sopenharmony_ci 67676d528ed9Sopenharmony_ci # Write all the inputs to a response file for the script. Also, 67686d528ed9Sopenharmony_ci # make the paths relative to the script working directory. 67696d528ed9Sopenharmony_ci response_file_contents = rebase_path(inputs, root_build_dir) 67706d528ed9Sopenharmony_ci 67716d528ed9Sopenharmony_ci # The script expects the name of the response file in --file-list. 67726d528ed9Sopenharmony_ci args = [ 67736d528ed9Sopenharmony_ci "--enable-foo", 67746d528ed9Sopenharmony_ci "--file-list={{response_file_name}}", 67756d528ed9Sopenharmony_ci ] 67766d528ed9Sopenharmony_ci } 67776d528ed9Sopenharmony_ci``` 67786d528ed9Sopenharmony_ci### <a name="var_rustflags"></a>**rustflags**: Flags passed to the Rust compiler. 67796d528ed9Sopenharmony_ci 67806d528ed9Sopenharmony_ci``` 67816d528ed9Sopenharmony_ci A list of strings. 67826d528ed9Sopenharmony_ci 67836d528ed9Sopenharmony_ci "rustflags" are passed to all invocations of the Rust compiler. 67846d528ed9Sopenharmony_ci``` 67856d528ed9Sopenharmony_ci### <a name="var_script"></a>**script**: Script file for actions. 67866d528ed9Sopenharmony_ci 67876d528ed9Sopenharmony_ci``` 67886d528ed9Sopenharmony_ci An absolute or buildfile-relative file name of a Python script to run for a 67896d528ed9Sopenharmony_ci action and action_foreach targets (see "gn help action" and "gn help 67906d528ed9Sopenharmony_ci action_foreach"). 67916d528ed9Sopenharmony_ci``` 67926d528ed9Sopenharmony_ci### <a name="var_sources"></a>**sources**: Source files for a target 67936d528ed9Sopenharmony_ci 67946d528ed9Sopenharmony_ci``` 67956d528ed9Sopenharmony_ci A list of files. Non-absolute paths will be resolved relative to the current 67966d528ed9Sopenharmony_ci build file. 67976d528ed9Sopenharmony_ci``` 67986d528ed9Sopenharmony_ci 67996d528ed9Sopenharmony_ci#### **Sources for binary targets** 68006d528ed9Sopenharmony_ci 68016d528ed9Sopenharmony_ci``` 68026d528ed9Sopenharmony_ci For binary targets (source sets, executables, and libraries), the known file 68036d528ed9Sopenharmony_ci types will be compiled with the associated tools. Unknown file types and 68046d528ed9Sopenharmony_ci headers will be skipped. However, you should still list all C/C+ header files 68056d528ed9Sopenharmony_ci so GN knows about the existence of those files for the purposes of include 68066d528ed9Sopenharmony_ci checking. 68076d528ed9Sopenharmony_ci 68086d528ed9Sopenharmony_ci As a special case, a file ending in ".def" will be treated as a Windows 68096d528ed9Sopenharmony_ci module definition file. It will be appended to the link line with a 68106d528ed9Sopenharmony_ci preceding "/DEF:" string. There must be at most one .def file in a target 68116d528ed9Sopenharmony_ci and they do not cross dependency boundaries (so specifying a .def file in a 68126d528ed9Sopenharmony_ci static library or source set will have no effect on the executable or shared 68136d528ed9Sopenharmony_ci library they're linked into). 68146d528ed9Sopenharmony_ci 68156d528ed9Sopenharmony_ci For Rust targets that do not specify a crate_root, then the crate_root will 68166d528ed9Sopenharmony_ci look for a lib.rs file (or main.rs for executable) or a single file in 68176d528ed9Sopenharmony_ci sources, if sources contains only one file. 68186d528ed9Sopenharmony_ci``` 68196d528ed9Sopenharmony_ci 68206d528ed9Sopenharmony_ci#### **Sources for non-binary targets** 68216d528ed9Sopenharmony_ci 68226d528ed9Sopenharmony_ci``` 68236d528ed9Sopenharmony_ci action_foreach 68246d528ed9Sopenharmony_ci The sources are the set of files that the script will be executed over. The 68256d528ed9Sopenharmony_ci script will run once per file. 68266d528ed9Sopenharmony_ci 68276d528ed9Sopenharmony_ci action 68286d528ed9Sopenharmony_ci The sources will be treated the same as inputs. See "gn help inputs" for 68296d528ed9Sopenharmony_ci more information and usage advice. 68306d528ed9Sopenharmony_ci 68316d528ed9Sopenharmony_ci copy 68326d528ed9Sopenharmony_ci The source are the source files to copy. 68336d528ed9Sopenharmony_ci``` 68346d528ed9Sopenharmony_ci### <a name="var_swiftflags"></a>**swiftflags**: Flags passed to the swift compiler. 68356d528ed9Sopenharmony_ci 68366d528ed9Sopenharmony_ci``` 68376d528ed9Sopenharmony_ci A list of strings. 68386d528ed9Sopenharmony_ci 68396d528ed9Sopenharmony_ci "swiftflags" are passed to any invocation of a tool that takes an .swift 68406d528ed9Sopenharmony_ci file as input. 68416d528ed9Sopenharmony_ci``` 68426d528ed9Sopenharmony_ci 68436d528ed9Sopenharmony_ci#### **Ordering of flags and values** 68446d528ed9Sopenharmony_ci 68456d528ed9Sopenharmony_ci``` 68466d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 68476d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 68486d528ed9Sopenharmony_ci configs appear in the list. 68496d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 68506d528ed9Sopenharmony_ci that the configs appear in the list. 68516d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 68526d528ed9Sopenharmony_ci those configs appear in the list. 68536d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 68546d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 68556d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 68566d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 68576d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 68586d528ed9Sopenharmony_ci recursively. 68596d528ed9Sopenharmony_ci``` 68606d528ed9Sopenharmony_ci### <a name="var_testonly"></a>**testonly**: Declares a target must only be used for testing. 68616d528ed9Sopenharmony_ci 68626d528ed9Sopenharmony_ci``` 68636d528ed9Sopenharmony_ci Boolean. Defaults to false. 68646d528ed9Sopenharmony_ci 68656d528ed9Sopenharmony_ci When a target is marked "testonly = true", it must only be depended on by 68666d528ed9Sopenharmony_ci other test-only targets. Otherwise, GN will issue an error that the 68676d528ed9Sopenharmony_ci depenedency is not allowed. 68686d528ed9Sopenharmony_ci 68696d528ed9Sopenharmony_ci This feature is intended to prevent accidentally shipping test code in a 68706d528ed9Sopenharmony_ci final product. 68716d528ed9Sopenharmony_ci``` 68726d528ed9Sopenharmony_ci 68736d528ed9Sopenharmony_ci#### **Example** 68746d528ed9Sopenharmony_ci 68756d528ed9Sopenharmony_ci``` 68766d528ed9Sopenharmony_ci source_set("test_support") { 68776d528ed9Sopenharmony_ci testonly = true 68786d528ed9Sopenharmony_ci ... 68796d528ed9Sopenharmony_ci } 68806d528ed9Sopenharmony_ci``` 68816d528ed9Sopenharmony_ci### <a name="var_transparent"></a>**transparent**: [bool] True if the bundle is transparent. 68826d528ed9Sopenharmony_ci 68836d528ed9Sopenharmony_ci``` 68846d528ed9Sopenharmony_ci A boolean. 68856d528ed9Sopenharmony_ci 68866d528ed9Sopenharmony_ci Valid for "create_bundle" target. If true, the "create_bundle" target will 68876d528ed9Sopenharmony_ci not package the "bundle_data" deps but will forward them to all targets that 68886d528ed9Sopenharmony_ci depends on it (unless the "bundle_data" target sets "product_type" to the 68896d528ed9Sopenharmony_ci same value as the "create_bundle" target). 68906d528ed9Sopenharmony_ci``` 68916d528ed9Sopenharmony_ci### <a name="var_visibility"></a>**visibility**: A list of labels that can depend on a target. 68926d528ed9Sopenharmony_ci 68936d528ed9Sopenharmony_ci``` 68946d528ed9Sopenharmony_ci A list of labels and label patterns that define which targets can depend on 68956d528ed9Sopenharmony_ci the current one. These permissions are checked via the "check" command (see 68966d528ed9Sopenharmony_ci "gn help check"). 68976d528ed9Sopenharmony_ci 68986d528ed9Sopenharmony_ci If visibility is not defined, it defaults to public ("*"). 68996d528ed9Sopenharmony_ci 69006d528ed9Sopenharmony_ci If visibility is defined, only the targets with labels that match it can 69016d528ed9Sopenharmony_ci depend on the current target. The empty list means no targets can depend on 69026d528ed9Sopenharmony_ci the current target. 69036d528ed9Sopenharmony_ci 69046d528ed9Sopenharmony_ci Tip: Often you will want the same visibility for all targets in a BUILD file. 69056d528ed9Sopenharmony_ci In this case you can just put the definition at the top, outside of any 69066d528ed9Sopenharmony_ci target, and the targets will inherit that scope and see the definition. 69076d528ed9Sopenharmony_ci``` 69086d528ed9Sopenharmony_ci 69096d528ed9Sopenharmony_ci#### **Patterns** 69106d528ed9Sopenharmony_ci 69116d528ed9Sopenharmony_ci``` 69126d528ed9Sopenharmony_ci See "gn help label_pattern" for more details on what types of patterns are 69136d528ed9Sopenharmony_ci supported. If a toolchain is specified, only targets in that toolchain will 69146d528ed9Sopenharmony_ci be matched. If a toolchain is not specified on a pattern, targets in all 69156d528ed9Sopenharmony_ci toolchains will be matched. 69166d528ed9Sopenharmony_ci``` 69176d528ed9Sopenharmony_ci 69186d528ed9Sopenharmony_ci#### **Examples** 69196d528ed9Sopenharmony_ci 69206d528ed9Sopenharmony_ci``` 69216d528ed9Sopenharmony_ci Only targets in the current buildfile ("private"): 69226d528ed9Sopenharmony_ci visibility = [ ":*" ] 69236d528ed9Sopenharmony_ci 69246d528ed9Sopenharmony_ci No targets (used for targets that should be leaf nodes): 69256d528ed9Sopenharmony_ci visibility = [] 69266d528ed9Sopenharmony_ci 69276d528ed9Sopenharmony_ci Any target ("public", the default): 69286d528ed9Sopenharmony_ci visibility = [ "*" ] 69296d528ed9Sopenharmony_ci 69306d528ed9Sopenharmony_ci All targets in the current directory and any subdirectory: 69316d528ed9Sopenharmony_ci visibility = [ "./*" ] 69326d528ed9Sopenharmony_ci 69336d528ed9Sopenharmony_ci Any target in "//bar/BUILD.gn": 69346d528ed9Sopenharmony_ci visibility = [ "//bar:*" ] 69356d528ed9Sopenharmony_ci 69366d528ed9Sopenharmony_ci Any target in "//bar/" or any subdirectory thereof: 69376d528ed9Sopenharmony_ci visibility = [ "//bar/*" ] 69386d528ed9Sopenharmony_ci 69396d528ed9Sopenharmony_ci Just these specific targets: 69406d528ed9Sopenharmony_ci visibility = [ ":mything", "//foo:something_else" ] 69416d528ed9Sopenharmony_ci 69426d528ed9Sopenharmony_ci Any target in the current directory and any subdirectory thereof, plus 69436d528ed9Sopenharmony_ci any targets in "//bar/" and any subdirectory thereof. 69446d528ed9Sopenharmony_ci visibility = [ "./*", "//bar/*" ] 69456d528ed9Sopenharmony_ci``` 69466d528ed9Sopenharmony_ci### <a name="var_walk_keys"></a>**walk_keys**: Key(s) for managing the metadata collection walk. 69476d528ed9Sopenharmony_ci 69486d528ed9Sopenharmony_ci``` 69496d528ed9Sopenharmony_ci Defaults to [""]. 69506d528ed9Sopenharmony_ci 69516d528ed9Sopenharmony_ci These keys are used to control the next step in a collection walk, acting as 69526d528ed9Sopenharmony_ci barriers. If a specified key is defined in a target's metadata, the walk will 69536d528ed9Sopenharmony_ci use the targets listed in that value to determine which targets are walked. 69546d528ed9Sopenharmony_ci 69556d528ed9Sopenharmony_ci If no walk_keys are specified for a generated_file target (i.e. "[""]"), the 69566d528ed9Sopenharmony_ci walk will touch all deps and data_deps of the specified target recursively. 69576d528ed9Sopenharmony_ci 69586d528ed9Sopenharmony_ci See "gn help generated_file". 69596d528ed9Sopenharmony_ci``` 69606d528ed9Sopenharmony_ci### <a name="var_weak_frameworks"></a>**weak_frameworks**: [name list] Name of frameworks that must be weak linked. 69616d528ed9Sopenharmony_ci 69626d528ed9Sopenharmony_ci``` 69636d528ed9Sopenharmony_ci A list of framework names. 69646d528ed9Sopenharmony_ci 69656d528ed9Sopenharmony_ci The frameworks named in that list will be weak linked with any dynamic link 69666d528ed9Sopenharmony_ci type target. Weak linking instructs the dynamic loader to attempt to load 69676d528ed9Sopenharmony_ci the framework, but if it is not able to do so, it leaves any imported symbols 69686d528ed9Sopenharmony_ci unresolved. This is typically used when a framework is present in a new 69696d528ed9Sopenharmony_ci version of an SDK but not on older versions of the OS that the software runs 69706d528ed9Sopenharmony_ci on. 69716d528ed9Sopenharmony_ci``` 69726d528ed9Sopenharmony_ci 69736d528ed9Sopenharmony_ci#### **Ordering of flags and values** 69746d528ed9Sopenharmony_ci 69756d528ed9Sopenharmony_ci``` 69766d528ed9Sopenharmony_ci 1. Those set on the current target (not in a config). 69776d528ed9Sopenharmony_ci 2. Those set on the "configs" on the target in order that the 69786d528ed9Sopenharmony_ci configs appear in the list. 69796d528ed9Sopenharmony_ci 3. Those set on the "all_dependent_configs" on the target in order 69806d528ed9Sopenharmony_ci that the configs appear in the list. 69816d528ed9Sopenharmony_ci 4. Those set on the "public_configs" on the target in order that 69826d528ed9Sopenharmony_ci those configs appear in the list. 69836d528ed9Sopenharmony_ci 5. all_dependent_configs pulled from dependencies, in the order of 69846d528ed9Sopenharmony_ci the "deps" list. This is done recursively. If a config appears 69856d528ed9Sopenharmony_ci more than once, only the first occurrence will be used. 69866d528ed9Sopenharmony_ci 6. public_configs pulled from dependencies, in the order of the 69876d528ed9Sopenharmony_ci "deps" list. If a dependency is public, they will be applied 69886d528ed9Sopenharmony_ci recursively. 69896d528ed9Sopenharmony_ci``` 69906d528ed9Sopenharmony_ci 69916d528ed9Sopenharmony_ci#### **Example** 69926d528ed9Sopenharmony_ci 69936d528ed9Sopenharmony_ci``` 69946d528ed9Sopenharmony_ci weak_frameworks = [ "OnlyOnNewerOSes.framework" ] 69956d528ed9Sopenharmony_ci``` 69966d528ed9Sopenharmony_ci### <a name="var_write_runtime_deps"></a>**write_runtime_deps**: Writes the target's runtime_deps to the given path. 69976d528ed9Sopenharmony_ci 69986d528ed9Sopenharmony_ci``` 69996d528ed9Sopenharmony_ci Does not synchronously write the file, but rather schedules it to be written 70006d528ed9Sopenharmony_ci at the end of generation. 70016d528ed9Sopenharmony_ci 70026d528ed9Sopenharmony_ci If the file exists and the contents are identical to that being written, the 70036d528ed9Sopenharmony_ci file will not be updated. This will prevent unnecessary rebuilds of targets 70046d528ed9Sopenharmony_ci that depend on this file. 70056d528ed9Sopenharmony_ci 70066d528ed9Sopenharmony_ci Path must be within the output directory. 70076d528ed9Sopenharmony_ci 70086d528ed9Sopenharmony_ci See "gn help runtime_deps" for how the runtime dependencies are computed. 70096d528ed9Sopenharmony_ci 70106d528ed9Sopenharmony_ci The format of this file will list one file per line with no escaping. The 70116d528ed9Sopenharmony_ci files will be relative to the root_build_dir. The first line of the file will 70126d528ed9Sopenharmony_ci be the main output file of the target itself. The file contents will be the 70136d528ed9Sopenharmony_ci same as requesting the runtime deps be written on the command line (see "gn 70146d528ed9Sopenharmony_ci help --runtime-deps-list-file"). 70156d528ed9Sopenharmony_ci``` 70166d528ed9Sopenharmony_ci### <a name="var_xcasset_compiler_flags"></a>**xcasset_compiler_flags**: Flags passed to xcassets compiler. 70176d528ed9Sopenharmony_ci 70186d528ed9Sopenharmony_ci``` 70196d528ed9Sopenharmony_ci A list of strings. 70206d528ed9Sopenharmony_ci 70216d528ed9Sopenharmony_ci Valid for create_bundle target. Those flags are directly passed to 70226d528ed9Sopenharmony_ci xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution 70236d528ed9Sopenharmony_ci in compile_xcassets tool. 70246d528ed9Sopenharmony_ci``` 70256d528ed9Sopenharmony_ci### <a name="var_xcode_extra_attributes"></a>**xcode_extra_attributes**: [scope] Extra attributes for Xcode projects. 70266d528ed9Sopenharmony_ci 70276d528ed9Sopenharmony_ci``` 70286d528ed9Sopenharmony_ci The value defined in this scope will be copied to the EXTRA_ATTRIBUTES 70296d528ed9Sopenharmony_ci property of the generated Xcode project. They are only meaningful when 70306d528ed9Sopenharmony_ci generating with --ide=xcode. 70316d528ed9Sopenharmony_ci 70326d528ed9Sopenharmony_ci See "gn help create_bundle" for more information. 70336d528ed9Sopenharmony_ci``` 70346d528ed9Sopenharmony_ci### <a name="var_xcode_test_application_name"></a>**xcode_test_application_name**: Name for Xcode test target. 70356d528ed9Sopenharmony_ci 70366d528ed9Sopenharmony_ci``` 70376d528ed9Sopenharmony_ci Each unit and ui test target must have a test application target, and this 70386d528ed9Sopenharmony_ci value is used to specify the relationship. Only meaningful to Xcode (used as 70396d528ed9Sopenharmony_ci part of the Xcode project generation). 70406d528ed9Sopenharmony_ci 70416d528ed9Sopenharmony_ci See "gn help create_bundle" for more information. 70426d528ed9Sopenharmony_ci``` 70436d528ed9Sopenharmony_ci 70446d528ed9Sopenharmony_ci#### **Example** 70456d528ed9Sopenharmony_ci 70466d528ed9Sopenharmony_ci``` 70476d528ed9Sopenharmony_ci create_bundle("chrome_xctest") { 70486d528ed9Sopenharmony_ci test_application_name = "chrome" 70496d528ed9Sopenharmony_ci ... 70506d528ed9Sopenharmony_ci } 70516d528ed9Sopenharmony_ci``` 70526d528ed9Sopenharmony_ci## <a name="other"></a>Other help topics 70536d528ed9Sopenharmony_ci 70546d528ed9Sopenharmony_ci### <a name="buildargs"></a>**Build Arguments Overview** 70556d528ed9Sopenharmony_ci 70566d528ed9Sopenharmony_ci``` 70576d528ed9Sopenharmony_ci Build arguments are variables passed in from outside of the build that build 70586d528ed9Sopenharmony_ci files can query to determine how the build works. 70596d528ed9Sopenharmony_ci``` 70606d528ed9Sopenharmony_ci 70616d528ed9Sopenharmony_ci#### **How build arguments are set** 70626d528ed9Sopenharmony_ci 70636d528ed9Sopenharmony_ci``` 70646d528ed9Sopenharmony_ci First, system default arguments are set based on the current system. The 70656d528ed9Sopenharmony_ci built-in arguments are: 70666d528ed9Sopenharmony_ci - host_cpu 70676d528ed9Sopenharmony_ci - host_os 70686d528ed9Sopenharmony_ci - current_cpu 70696d528ed9Sopenharmony_ci - current_os 70706d528ed9Sopenharmony_ci - target_cpu 70716d528ed9Sopenharmony_ci - target_os 70726d528ed9Sopenharmony_ci 70736d528ed9Sopenharmony_ci Next, project-specific overrides are applied. These are specified inside 70746d528ed9Sopenharmony_ci the default_args variable of //.gn. See "gn help dotfile" for more. 70756d528ed9Sopenharmony_ci 70766d528ed9Sopenharmony_ci If specified, arguments from the --args command line flag are used. If that 70776d528ed9Sopenharmony_ci flag is not specified, args from previous builds in the build directory will 70786d528ed9Sopenharmony_ci be used (this is in the file args.gn in the build directory). 70796d528ed9Sopenharmony_ci 70806d528ed9Sopenharmony_ci Last, for targets being compiled with a non-default toolchain, the toolchain 70816d528ed9Sopenharmony_ci overrides are applied. These are specified in the toolchain_args section of a 70826d528ed9Sopenharmony_ci toolchain definition. The use-case for this is that a toolchain may be 70836d528ed9Sopenharmony_ci building code for a different platform, and that it may want to always 70846d528ed9Sopenharmony_ci specify Posix, for example. See "gn help toolchain" for more. 70856d528ed9Sopenharmony_ci 70866d528ed9Sopenharmony_ci If you specify an override for a build argument that never appears in a 70876d528ed9Sopenharmony_ci "declare_args" call, a nonfatal error will be displayed. 70886d528ed9Sopenharmony_ci``` 70896d528ed9Sopenharmony_ci 70906d528ed9Sopenharmony_ci#### **Examples** 70916d528ed9Sopenharmony_ci 70926d528ed9Sopenharmony_ci``` 70936d528ed9Sopenharmony_ci gn args out/FooBar 70946d528ed9Sopenharmony_ci Create the directory out/FooBar and open an editor. You would type 70956d528ed9Sopenharmony_ci something like this into that file: 70966d528ed9Sopenharmony_ci enable_doom_melon=false 70976d528ed9Sopenharmony_ci os="android" 70986d528ed9Sopenharmony_ci 70996d528ed9Sopenharmony_ci gn gen out/FooBar --args="enable_doom_melon=true os=\"android\"" 71006d528ed9Sopenharmony_ci This will overwrite the build directory with the given arguments. (Note 71016d528ed9Sopenharmony_ci that the quotes inside the args command will usually need to be escaped 71026d528ed9Sopenharmony_ci for your shell to pass through strings values.) 71036d528ed9Sopenharmony_ci``` 71046d528ed9Sopenharmony_ci 71056d528ed9Sopenharmony_ci#### **How build arguments are used** 71066d528ed9Sopenharmony_ci 71076d528ed9Sopenharmony_ci``` 71086d528ed9Sopenharmony_ci If you want to use an argument, you use declare_args() and specify default 71096d528ed9Sopenharmony_ci values. These default values will apply if none of the steps listed in the 71106d528ed9Sopenharmony_ci "How build arguments are set" section above apply to the given argument, but 71116d528ed9Sopenharmony_ci the defaults will not override any of these. 71126d528ed9Sopenharmony_ci 71136d528ed9Sopenharmony_ci Often, the root build config file will declare global arguments that will be 71146d528ed9Sopenharmony_ci passed to all buildfiles. Individual build files can also specify arguments 71156d528ed9Sopenharmony_ci that apply only to those files. It is also useful to specify build args in an 71166d528ed9Sopenharmony_ci "import"-ed file if you want such arguments to apply to multiple buildfiles. 71176d528ed9Sopenharmony_ci``` 71186d528ed9Sopenharmony_ci### <a name="dotfile"></a>**.gn file** 71196d528ed9Sopenharmony_ci 71206d528ed9Sopenharmony_ci``` 71216d528ed9Sopenharmony_ci When gn starts, it will search the current directory and parent directories 71226d528ed9Sopenharmony_ci for a file called ".gn". This indicates the source root. You can override 71236d528ed9Sopenharmony_ci this detection by using the --root command-line argument 71246d528ed9Sopenharmony_ci 71256d528ed9Sopenharmony_ci The .gn file in the source root will be executed. The syntax is the same as a 71266d528ed9Sopenharmony_ci buildfile, but with very limited build setup-specific meaning. 71276d528ed9Sopenharmony_ci 71286d528ed9Sopenharmony_ci If you specify --root, by default GN will look for the file .gn in that 71296d528ed9Sopenharmony_ci directory. If you want to specify a different file, you can additionally pass 71306d528ed9Sopenharmony_ci --dotfile: 71316d528ed9Sopenharmony_ci 71326d528ed9Sopenharmony_ci gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn 71336d528ed9Sopenharmony_ci``` 71346d528ed9Sopenharmony_ci 71356d528ed9Sopenharmony_ci#### **Variables** 71366d528ed9Sopenharmony_ci 71376d528ed9Sopenharmony_ci``` 71386d528ed9Sopenharmony_ci arg_file_template [optional] 71396d528ed9Sopenharmony_ci Path to a file containing the text that should be used as the default 71406d528ed9Sopenharmony_ci args.gn content when you run `gn args`. 71416d528ed9Sopenharmony_ci 71426d528ed9Sopenharmony_ci buildconfig [required] 71436d528ed9Sopenharmony_ci Path to the build config file. This file will be used to set up the 71446d528ed9Sopenharmony_ci build file execution environment for each toolchain. 71456d528ed9Sopenharmony_ci 71466d528ed9Sopenharmony_ci check_targets [optional] 71476d528ed9Sopenharmony_ci A list of labels and label patterns that should be checked when running 71486d528ed9Sopenharmony_ci "gn check" or "gn gen --check". If neither check_targets or 71496d528ed9Sopenharmony_ci no_check_targets (see below) is specified, all targets will be checked. 71506d528ed9Sopenharmony_ci It is an error to specify both check_targets and no_check_targets. If it 71516d528ed9Sopenharmony_ci is the empty list, no targets will be checked. To bypass this list, 71526d528ed9Sopenharmony_ci request an explicit check of targets, like "//*". 71536d528ed9Sopenharmony_ci 71546d528ed9Sopenharmony_ci The format of this list is identical to that of "visibility" so see "gn 71556d528ed9Sopenharmony_ci help visibility" for examples. 71566d528ed9Sopenharmony_ci 71576d528ed9Sopenharmony_ci no_check_targets [optional] 71586d528ed9Sopenharmony_ci A list of labels and label patterns that should *not* be checked when 71596d528ed9Sopenharmony_ci running "gn check" or "gn gen --check". All other targets will be checked. 71606d528ed9Sopenharmony_ci If neither check_targets (see above) or no_check_targets is specified, all 71616d528ed9Sopenharmony_ci targets will be checked. It is an error to specify both check_targets and 71626d528ed9Sopenharmony_ci no_check_targets. 71636d528ed9Sopenharmony_ci 71646d528ed9Sopenharmony_ci The format of this list is identical to that of "visibility" so see "gn 71656d528ed9Sopenharmony_ci help visibility" for examples. 71666d528ed9Sopenharmony_ci 71676d528ed9Sopenharmony_ci check_system_includes [optional] 71686d528ed9Sopenharmony_ci Boolean to control whether system style includes are checked by default 71696d528ed9Sopenharmony_ci when running "gn check" or "gn gen --check". System style includes are 71706d528ed9Sopenharmony_ci includes that use angle brackets <> instead of double quotes "". If this 71716d528ed9Sopenharmony_ci setting is omitted or set to false, these includes will be ignored by 71726d528ed9Sopenharmony_ci default. They can be checked explicitly by running 71736d528ed9Sopenharmony_ci "gn check --check-system" or "gn gen --check=system" 71746d528ed9Sopenharmony_ci 71756d528ed9Sopenharmony_ci exec_script_whitelist [optional] 71766d528ed9Sopenharmony_ci A list of .gn/.gni files (not labels) that have permission to call the 71776d528ed9Sopenharmony_ci exec_script function. If this list is defined, calls to exec_script will 71786d528ed9Sopenharmony_ci be checked against this list and GN will fail if the current file isn't 71796d528ed9Sopenharmony_ci in the list. 71806d528ed9Sopenharmony_ci 71816d528ed9Sopenharmony_ci This is to allow the use of exec_script to be restricted since is easy to 71826d528ed9Sopenharmony_ci use inappropriately. Wildcards are not supported. Files in the 71836d528ed9Sopenharmony_ci secondary_source tree (if defined) should be referenced by ignoring the 71846d528ed9Sopenharmony_ci secondary tree and naming them as if they are in the main tree. 71856d528ed9Sopenharmony_ci 71866d528ed9Sopenharmony_ci If unspecified, the ability to call exec_script is unrestricted. 71876d528ed9Sopenharmony_ci 71886d528ed9Sopenharmony_ci Example: 71896d528ed9Sopenharmony_ci exec_script_whitelist = [ 71906d528ed9Sopenharmony_ci "//base/BUILD.gn", 71916d528ed9Sopenharmony_ci "//build/my_config.gni", 71926d528ed9Sopenharmony_ci ] 71936d528ed9Sopenharmony_ci 71946d528ed9Sopenharmony_ci export_compile_commands [optional] 71956d528ed9Sopenharmony_ci A list of label patterns for which to generate a Clang compilation 71966d528ed9Sopenharmony_ci database (see "gn help label_pattern" for the string format). 71976d528ed9Sopenharmony_ci 71986d528ed9Sopenharmony_ci When specified, GN will generate a compile_commands.json file in the root 71996d528ed9Sopenharmony_ci of the build directory containing information on how to compile each 72006d528ed9Sopenharmony_ci source file reachable from any label matching any pattern in the list. 72016d528ed9Sopenharmony_ci This is used for Clang-based tooling and some editor integration. See 72026d528ed9Sopenharmony_ci https://clang.llvm.org/docs/JSONCompilationDatabase.html 72036d528ed9Sopenharmony_ci 72046d528ed9Sopenharmony_ci The switch --add-export-compile-commands to "gn gen" (see "gn help gen") 72056d528ed9Sopenharmony_ci appends to this value which provides a per-user way to customize it. 72066d528ed9Sopenharmony_ci 72076d528ed9Sopenharmony_ci The deprecated switch --export-compile-commands to "gn gen" (see "gn help 72086d528ed9Sopenharmony_ci gen") adds to the export target list using a different format. 72096d528ed9Sopenharmony_ci 72106d528ed9Sopenharmony_ci Example: 72116d528ed9Sopenharmony_ci export_compile_commands = [ 72126d528ed9Sopenharmony_ci "//base/*", 72136d528ed9Sopenharmony_ci "//tools:doom_melon", 72146d528ed9Sopenharmony_ci ] 72156d528ed9Sopenharmony_ci 72166d528ed9Sopenharmony_ci root [optional] 72176d528ed9Sopenharmony_ci Label of the root build target. The GN build will start by loading the 72186d528ed9Sopenharmony_ci build file containing this target name. This defaults to "//:" which will 72196d528ed9Sopenharmony_ci cause the file //BUILD.gn to be loaded. Note that build_file_extension 72206d528ed9Sopenharmony_ci applies to the default case as well. 72216d528ed9Sopenharmony_ci 72226d528ed9Sopenharmony_ci The command-line switch --root-target will override this value (see "gn 72236d528ed9Sopenharmony_ci help --root-target"). 72246d528ed9Sopenharmony_ci 72256d528ed9Sopenharmony_ci root_patterns [optional] 72266d528ed9Sopenharmony_ci A list of label pattern strings. When not defined or empty, the GN build 72276d528ed9Sopenharmony_ci graph will contain all targets from any BUILD.gn evaluated in the default 72286d528ed9Sopenharmony_ci toolchain context, and their transitive dependencies. 72296d528ed9Sopenharmony_ci 72306d528ed9Sopenharmony_ci When set to a non empty list, only the targets in the default toolchain 72316d528ed9Sopenharmony_ci matching these patterns, and their transitive dependencies, will be defined 72326d528ed9Sopenharmony_ci instead. 72336d528ed9Sopenharmony_ci 72346d528ed9Sopenharmony_ci The command-line switch --root-pattern will override this value (see 72356d528ed9Sopenharmony_ci "gn help --root-pattern") 72366d528ed9Sopenharmony_ci 72376d528ed9Sopenharmony_ci script_executable [optional] 72386d528ed9Sopenharmony_ci By default, GN runs the scripts used in action targets and exec_script 72396d528ed9Sopenharmony_ci calls using the Python interpreter found in PATH. This value specifies the 72406d528ed9Sopenharmony_ci Python executable or other interpreter to use instead. 72416d528ed9Sopenharmony_ci 72426d528ed9Sopenharmony_ci If set to the empty string, the scripts will be executed directly. 72436d528ed9Sopenharmony_ci 72446d528ed9Sopenharmony_ci The command-line switch --script-executable will override this value (see 72456d528ed9Sopenharmony_ci "gn help --script-executable") 72466d528ed9Sopenharmony_ci 72476d528ed9Sopenharmony_ci secondary_source [optional] 72486d528ed9Sopenharmony_ci Label of an alternate directory tree to find input files. When searching 72496d528ed9Sopenharmony_ci for a BUILD.gn file (or the build config file discussed above), the file 72506d528ed9Sopenharmony_ci will first be looked for in the source root. If it's not found, the 72516d528ed9Sopenharmony_ci secondary source root will be checked (which would contain a parallel 72526d528ed9Sopenharmony_ci directory hierarchy). 72536d528ed9Sopenharmony_ci 72546d528ed9Sopenharmony_ci This behavior is intended to be used when BUILD.gn files can't be checked 72556d528ed9Sopenharmony_ci in to certain source directories for whatever reason. 72566d528ed9Sopenharmony_ci 72576d528ed9Sopenharmony_ci The secondary source root must be inside the main source tree. 72586d528ed9Sopenharmony_ci 72596d528ed9Sopenharmony_ci default_args [optional] 72606d528ed9Sopenharmony_ci Scope containing the default overrides for declared arguments. These 72616d528ed9Sopenharmony_ci overrides take precedence over the default values specified in the 72626d528ed9Sopenharmony_ci declare_args() block, but can be overridden using --args or the 72636d528ed9Sopenharmony_ci args.gn file. 72646d528ed9Sopenharmony_ci 72656d528ed9Sopenharmony_ci This is intended to be used when subprojects declare arguments with 72666d528ed9Sopenharmony_ci default values that need to be changed for whatever reason. 72676d528ed9Sopenharmony_ci 72686d528ed9Sopenharmony_ci build_file_extension [optional] 72696d528ed9Sopenharmony_ci If set to a non-empty string, this is added to the name of all build files 72706d528ed9Sopenharmony_ci to load. 72716d528ed9Sopenharmony_ci GN will look for build files named "BUILD.$build_file_extension.gn". 72726d528ed9Sopenharmony_ci This is intended to be used during migrations or other situations where 72736d528ed9Sopenharmony_ci there are two independent GN builds in the same directories. 72746d528ed9Sopenharmony_ci 72756d528ed9Sopenharmony_ci ninja_required_version [optional] 72766d528ed9Sopenharmony_ci When set specifies the minimum required version of Ninja. The default 72776d528ed9Sopenharmony_ci required version is 1.7.2. Specifying a higher version might enable the 72786d528ed9Sopenharmony_ci use of some of newer features that can make the build more efficient. 72796d528ed9Sopenharmony_ci``` 72806d528ed9Sopenharmony_ci 72816d528ed9Sopenharmony_ci#### **Example .gn file contents** 72826d528ed9Sopenharmony_ci 72836d528ed9Sopenharmony_ci``` 72846d528ed9Sopenharmony_ci buildconfig = "//build/config/BUILDCONFIG.gn" 72856d528ed9Sopenharmony_ci 72866d528ed9Sopenharmony_ci check_targets = [ 72876d528ed9Sopenharmony_ci "//doom_melon/*", # Check everything in this subtree. 72886d528ed9Sopenharmony_ci "//tools:mind_controlling_ant", # Check this specific target. 72896d528ed9Sopenharmony_ci ] 72906d528ed9Sopenharmony_ci 72916d528ed9Sopenharmony_ci root = "//:root" 72926d528ed9Sopenharmony_ci 72936d528ed9Sopenharmony_ci secondary_source = "//build/config/temporary_buildfiles/" 72946d528ed9Sopenharmony_ci 72956d528ed9Sopenharmony_ci default_args = { 72966d528ed9Sopenharmony_ci # Default to release builds for this project. 72976d528ed9Sopenharmony_ci is_debug = false 72986d528ed9Sopenharmony_ci is_component_build = false 72996d528ed9Sopenharmony_ci } 73006d528ed9Sopenharmony_ci``` 73016d528ed9Sopenharmony_ci### <a name="execution"></a>**Build graph and execution overview** 73026d528ed9Sopenharmony_ci 73036d528ed9Sopenharmony_ci#### **Overall build flow** 73046d528ed9Sopenharmony_ci 73056d528ed9Sopenharmony_ci``` 73066d528ed9Sopenharmony_ci 1. Look for ".gn" file (see "gn help dotfile") in the current directory and 73076d528ed9Sopenharmony_ci walk up the directory tree until one is found. Set this directory to be 73086d528ed9Sopenharmony_ci the "source root" and interpret this file to find the name of the build 73096d528ed9Sopenharmony_ci config file. 73106d528ed9Sopenharmony_ci 73116d528ed9Sopenharmony_ci 2. Execute the build config file identified by .gn to set up the global 73126d528ed9Sopenharmony_ci variables and default toolchain name. Any arguments, variables, defaults, 73136d528ed9Sopenharmony_ci etc. set up in this file will be visible to all files in the build. 73146d528ed9Sopenharmony_ci 73156d528ed9Sopenharmony_ci 3. Load the //BUILD.gn (in the source root directory). 73166d528ed9Sopenharmony_ci 73176d528ed9Sopenharmony_ci 4. Recursively evaluate rules and load BUILD.gn in other directories as 73186d528ed9Sopenharmony_ci necessary to resolve dependencies. If a BUILD file isn't found in the 73196d528ed9Sopenharmony_ci specified location, GN will look in the corresponding location inside 73206d528ed9Sopenharmony_ci the secondary_source defined in the dotfile (see "gn help dotfile"). 73216d528ed9Sopenharmony_ci 73226d528ed9Sopenharmony_ci 5. When a target's dependencies are resolved, write out the `.ninja` 73236d528ed9Sopenharmony_ci file to disk. 73246d528ed9Sopenharmony_ci 73256d528ed9Sopenharmony_ci 6. When all targets are resolved, write out the root build.ninja file. 73266d528ed9Sopenharmony_ci 73276d528ed9Sopenharmony_ci Note that the BUILD.gn file name may be modulated by .gn arguments such as 73286d528ed9Sopenharmony_ci build_file_extension. 73296d528ed9Sopenharmony_ci``` 73306d528ed9Sopenharmony_ci 73316d528ed9Sopenharmony_ci#### **Executing target definitions and templates** 73326d528ed9Sopenharmony_ci 73336d528ed9Sopenharmony_ci``` 73346d528ed9Sopenharmony_ci Build files are loaded in parallel. This means it is impossible to 73356d528ed9Sopenharmony_ci interrogate a target from GN code for any information not derivable from its 73366d528ed9Sopenharmony_ci label (see "gn help label"). The exception is the get_target_outputs() 73376d528ed9Sopenharmony_ci function which requires the target being interrogated to have been defined 73386d528ed9Sopenharmony_ci previously in the same file. 73396d528ed9Sopenharmony_ci 73406d528ed9Sopenharmony_ci Targets are declared by their type and given a name: 73416d528ed9Sopenharmony_ci 73426d528ed9Sopenharmony_ci static_library("my_static_library") { 73436d528ed9Sopenharmony_ci ... target parameter definitions ... 73446d528ed9Sopenharmony_ci } 73456d528ed9Sopenharmony_ci 73466d528ed9Sopenharmony_ci There is also a generic "target" function for programmatically defined types 73476d528ed9Sopenharmony_ci (see "gn help target"). You can define new types using templates (see "gn 73486d528ed9Sopenharmony_ci help template"). A template defines some custom code that expands to one or 73496d528ed9Sopenharmony_ci more other targets. 73506d528ed9Sopenharmony_ci 73516d528ed9Sopenharmony_ci Before executing the code inside the target's { }, the target defaults are 73526d528ed9Sopenharmony_ci applied (see "gn help set_defaults"). It will inject implicit variable 73536d528ed9Sopenharmony_ci definitions that can be overridden by the target code as necessary. Typically 73546d528ed9Sopenharmony_ci this mechanism is used to inject a default set of configs that define the 73556d528ed9Sopenharmony_ci global compiler and linker flags. 73566d528ed9Sopenharmony_ci``` 73576d528ed9Sopenharmony_ci 73586d528ed9Sopenharmony_ci#### **Which targets are built** 73596d528ed9Sopenharmony_ci 73606d528ed9Sopenharmony_ci``` 73616d528ed9Sopenharmony_ci All targets encountered in the default toolchain (see "gn help toolchain") 73626d528ed9Sopenharmony_ci will have build rules generated for them, even if no other targets reference 73636d528ed9Sopenharmony_ci them. Their dependencies must resolve and they will be added to the implicit 73646d528ed9Sopenharmony_ci "all" rule (see "gn help ninja_rules"). 73656d528ed9Sopenharmony_ci 73666d528ed9Sopenharmony_ci Targets in non-default toolchains will only be generated when they are 73676d528ed9Sopenharmony_ci required (directly or transitively) to build a target in the default 73686d528ed9Sopenharmony_ci toolchain. 73696d528ed9Sopenharmony_ci 73706d528ed9Sopenharmony_ci Some targets might be associated but without a formal build dependency (for 73716d528ed9Sopenharmony_ci example, related tools or optional variants). A target that is marked as 73726d528ed9Sopenharmony_ci "generated" can propagate its generated state to an associated target using 73736d528ed9Sopenharmony_ci "gen_deps". This will make the referenced dependency have Ninja rules 73746d528ed9Sopenharmony_ci generated in the same cases the source target has but without a build-time 73756d528ed9Sopenharmony_ci dependency and even in non-default toolchains. 73766d528ed9Sopenharmony_ci 73776d528ed9Sopenharmony_ci See also "gn help ninja_rules". 73786d528ed9Sopenharmony_ci``` 73796d528ed9Sopenharmony_ci 73806d528ed9Sopenharmony_ci#### **Dependencies** 73816d528ed9Sopenharmony_ci 73826d528ed9Sopenharmony_ci``` 73836d528ed9Sopenharmony_ci The only difference between "public_deps" and "deps" except for pushing 73846d528ed9Sopenharmony_ci configs around the build tree and allowing includes for the purposes of "gn 73856d528ed9Sopenharmony_ci check". 73866d528ed9Sopenharmony_ci 73876d528ed9Sopenharmony_ci A target's "data_deps" are guaranteed to be built whenever the target is 73886d528ed9Sopenharmony_ci built, but the ordering is not defined. The meaning of this is dependencies 73896d528ed9Sopenharmony_ci required at runtime. Currently data deps will be complete before the target 73906d528ed9Sopenharmony_ci is linked, but this is not semantically guaranteed and this is undesirable 73916d528ed9Sopenharmony_ci from a build performance perspective. Since we hope to change this in the 73926d528ed9Sopenharmony_ci future, do not rely on this behavior. 73936d528ed9Sopenharmony_ci``` 73946d528ed9Sopenharmony_ci### <a name="grammar"></a>**Language and grammar for GN build files** 73956d528ed9Sopenharmony_ci 73966d528ed9Sopenharmony_ci#### **Tokens** 73976d528ed9Sopenharmony_ci 73986d528ed9Sopenharmony_ci``` 73996d528ed9Sopenharmony_ci GN build files are read as sequences of tokens. While splitting the file 74006d528ed9Sopenharmony_ci into tokens, the next token is the longest sequence of characters that form a 74016d528ed9Sopenharmony_ci valid token. 74026d528ed9Sopenharmony_ci``` 74036d528ed9Sopenharmony_ci 74046d528ed9Sopenharmony_ci#### **White space and comments** 74056d528ed9Sopenharmony_ci 74066d528ed9Sopenharmony_ci``` 74076d528ed9Sopenharmony_ci White space is comprised of spaces (U+0020), horizontal tabs (U+0009), 74086d528ed9Sopenharmony_ci carriage returns (U+000D), and newlines (U+000A). 74096d528ed9Sopenharmony_ci 74106d528ed9Sopenharmony_ci Comments start at the character "#" and stop at the next newline. 74116d528ed9Sopenharmony_ci 74126d528ed9Sopenharmony_ci White space and comments are ignored except that they may separate tokens 74136d528ed9Sopenharmony_ci that would otherwise combine into a single token. 74146d528ed9Sopenharmony_ci``` 74156d528ed9Sopenharmony_ci 74166d528ed9Sopenharmony_ci#### **Identifiers** 74176d528ed9Sopenharmony_ci 74186d528ed9Sopenharmony_ci``` 74196d528ed9Sopenharmony_ci Identifiers name variables and functions. 74206d528ed9Sopenharmony_ci 74216d528ed9Sopenharmony_ci identifier = letter { letter | digit } . 74226d528ed9Sopenharmony_ci letter = "A" ... "Z" | "a" ... "z" | "_" . 74236d528ed9Sopenharmony_ci digit = "0" ... "9" . 74246d528ed9Sopenharmony_ci``` 74256d528ed9Sopenharmony_ci 74266d528ed9Sopenharmony_ci#### **Keywords** 74276d528ed9Sopenharmony_ci 74286d528ed9Sopenharmony_ci``` 74296d528ed9Sopenharmony_ci The following keywords are reserved and may not be used as identifiers: 74306d528ed9Sopenharmony_ci 74316d528ed9Sopenharmony_ci else false if true 74326d528ed9Sopenharmony_ci``` 74336d528ed9Sopenharmony_ci 74346d528ed9Sopenharmony_ci#### **Integer literals** 74356d528ed9Sopenharmony_ci 74366d528ed9Sopenharmony_ci``` 74376d528ed9Sopenharmony_ci An integer literal represents a decimal integer value. 74386d528ed9Sopenharmony_ci 74396d528ed9Sopenharmony_ci integer = [ "-" ] digit { digit } . 74406d528ed9Sopenharmony_ci 74416d528ed9Sopenharmony_ci Leading zeros and negative zero are disallowed. 74426d528ed9Sopenharmony_ci``` 74436d528ed9Sopenharmony_ci 74446d528ed9Sopenharmony_ci#### **String literals** 74456d528ed9Sopenharmony_ci 74466d528ed9Sopenharmony_ci``` 74476d528ed9Sopenharmony_ci A string literal represents a string value consisting of the quoted 74486d528ed9Sopenharmony_ci characters with possible escape sequences and variable expansions. 74496d528ed9Sopenharmony_ci 74506d528ed9Sopenharmony_ci string = `"` { char | escape | expansion } `"` . 74516d528ed9Sopenharmony_ci escape = `\` ( "$" | `"` | char ) . 74526d528ed9Sopenharmony_ci BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" . 74536d528ed9Sopenharmony_ci Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f] 74546d528ed9Sopenharmony_ci expansion = "$" ( identifier | BracketExpansion | Hex ) . 74556d528ed9Sopenharmony_ci char = /* any character except "$", `"`, or newline */ . 74566d528ed9Sopenharmony_ci 74576d528ed9Sopenharmony_ci After a backslash, certain sequences represent special characters: 74586d528ed9Sopenharmony_ci 74596d528ed9Sopenharmony_ci \" U+0022 quotation mark 74606d528ed9Sopenharmony_ci \$ U+0024 dollar sign 74616d528ed9Sopenharmony_ci \\ U+005C backslash 74626d528ed9Sopenharmony_ci 74636d528ed9Sopenharmony_ci All other backslashes represent themselves. 74646d528ed9Sopenharmony_ci 74656d528ed9Sopenharmony_ci To insert an arbitrary byte value, use $0xFF. For example, to insert a 74666d528ed9Sopenharmony_ci newline character: "Line one$0x0ALine two". 74676d528ed9Sopenharmony_ci 74686d528ed9Sopenharmony_ci An expansion will evaluate the variable following the '$' and insert a 74696d528ed9Sopenharmony_ci stringified version of it into the result. For example, to concat two path 74706d528ed9Sopenharmony_ci components with a slash separating them: 74716d528ed9Sopenharmony_ci "$var_one/$var_two" 74726d528ed9Sopenharmony_ci Use the "${var_one}" format to be explicitly deliniate the variable for 74736d528ed9Sopenharmony_ci otherwise-ambiguous cases. 74746d528ed9Sopenharmony_ci``` 74756d528ed9Sopenharmony_ci 74766d528ed9Sopenharmony_ci#### **Punctuation** 74776d528ed9Sopenharmony_ci 74786d528ed9Sopenharmony_ci``` 74796d528ed9Sopenharmony_ci The following character sequences represent punctuation: 74806d528ed9Sopenharmony_ci 74816d528ed9Sopenharmony_ci + += == != ( ) 74826d528ed9Sopenharmony_ci - -= < <= [ ] 74836d528ed9Sopenharmony_ci ! = > >= { } 74846d528ed9Sopenharmony_ci && || . , 74856d528ed9Sopenharmony_ci``` 74866d528ed9Sopenharmony_ci 74876d528ed9Sopenharmony_ci#### **Grammar** 74886d528ed9Sopenharmony_ci 74896d528ed9Sopenharmony_ci``` 74906d528ed9Sopenharmony_ci The input tokens form a syntax tree following a context-free grammar: 74916d528ed9Sopenharmony_ci 74926d528ed9Sopenharmony_ci File = StatementList . 74936d528ed9Sopenharmony_ci 74946d528ed9Sopenharmony_ci Statement = Assignment | Call | Condition . 74956d528ed9Sopenharmony_ci LValue = identifier | ArrayAccess | ScopeAccess . 74966d528ed9Sopenharmony_ci Assignment = LValue AssignOp Expr . 74976d528ed9Sopenharmony_ci Call = identifier "(" [ ExprList ] ")" [ Block ] . 74986d528ed9Sopenharmony_ci Condition = "if" "(" Expr ")" Block 74996d528ed9Sopenharmony_ci [ "else" ( Condition | Block ) ] . 75006d528ed9Sopenharmony_ci Block = "{" StatementList "}" . 75016d528ed9Sopenharmony_ci StatementList = { Statement } . 75026d528ed9Sopenharmony_ci 75036d528ed9Sopenharmony_ci ArrayAccess = identifier "[" Expr "]" . 75046d528ed9Sopenharmony_ci ScopeAccess = identifier "." identifier . 75056d528ed9Sopenharmony_ci Expr = UnaryExpr | Expr BinaryOp Expr . 75066d528ed9Sopenharmony_ci UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr . 75076d528ed9Sopenharmony_ci PrimaryExpr = identifier | integer | string | Call 75086d528ed9Sopenharmony_ci | ArrayAccess | ScopeAccess | Block 75096d528ed9Sopenharmony_ci | "(" Expr ")" 75106d528ed9Sopenharmony_ci | "[" [ ExprList [ "," ] ] "]" . 75116d528ed9Sopenharmony_ci ExprList = Expr { "," Expr } . 75126d528ed9Sopenharmony_ci 75136d528ed9Sopenharmony_ci AssignOp = "=" | "+=" | "-=" . 75146d528ed9Sopenharmony_ci UnaryOp = "!" . 75156d528ed9Sopenharmony_ci BinaryOp = "+" | "-" // highest priority 75166d528ed9Sopenharmony_ci | "<" | "<=" | ">" | ">=" 75176d528ed9Sopenharmony_ci | "==" | "!=" 75186d528ed9Sopenharmony_ci | "&&" 75196d528ed9Sopenharmony_ci | "||" . // lowest priority 75206d528ed9Sopenharmony_ci 75216d528ed9Sopenharmony_ci All binary operators are left-associative. 75226d528ed9Sopenharmony_ci``` 75236d528ed9Sopenharmony_ci 75246d528ed9Sopenharmony_ci#### **Types** 75256d528ed9Sopenharmony_ci 75266d528ed9Sopenharmony_ci``` 75276d528ed9Sopenharmony_ci The GN language is dynamically typed. The following types are used: 75286d528ed9Sopenharmony_ci 75296d528ed9Sopenharmony_ci - Boolean: Uses the keywords "true" and "false". There is no implicit 75306d528ed9Sopenharmony_ci conversion between booleans and integers. 75316d528ed9Sopenharmony_ci 75326d528ed9Sopenharmony_ci - Integers: All numbers in GN are signed 64-bit integers. 75336d528ed9Sopenharmony_ci 75346d528ed9Sopenharmony_ci - Strings: Strings are 8-bit with no enforced encoding. When a string is 75356d528ed9Sopenharmony_ci used to interact with other systems with particular encodings (like the 75366d528ed9Sopenharmony_ci Windows and Mac filesystems) it is assumed to be UTF-8. See "String 75376d528ed9Sopenharmony_ci literals" above for more. 75386d528ed9Sopenharmony_ci 75396d528ed9Sopenharmony_ci - Lists: Lists are arbitrary-length ordered lists of values. See "Lists" 75406d528ed9Sopenharmony_ci below for more. 75416d528ed9Sopenharmony_ci 75426d528ed9Sopenharmony_ci - Scopes: Scopes are like dictionaries that use variable names for keys. See 75436d528ed9Sopenharmony_ci "Scopes" below for more. 75446d528ed9Sopenharmony_ci``` 75456d528ed9Sopenharmony_ci 75466d528ed9Sopenharmony_ci#### **Lists** 75476d528ed9Sopenharmony_ci 75486d528ed9Sopenharmony_ci``` 75496d528ed9Sopenharmony_ci Lists are created with [] and using commas to separate items: 75506d528ed9Sopenharmony_ci 75516d528ed9Sopenharmony_ci mylist = [ 0, 1, 2, "some string" ] 75526d528ed9Sopenharmony_ci 75536d528ed9Sopenharmony_ci A comma after the last item is optional. Lists are dereferenced using 0-based 75546d528ed9Sopenharmony_ci indexing: 75556d528ed9Sopenharmony_ci 75566d528ed9Sopenharmony_ci mylist[0] += 1 75576d528ed9Sopenharmony_ci var = mylist[2] 75586d528ed9Sopenharmony_ci 75596d528ed9Sopenharmony_ci Lists can be concatenated using the '+' and '+=' operators. Bare values can 75606d528ed9Sopenharmony_ci not be concatenated with lists, to add a single item, it must be put into a 75616d528ed9Sopenharmony_ci list of length one. 75626d528ed9Sopenharmony_ci 75636d528ed9Sopenharmony_ci Items can be removed from lists using the '-' and '-=' operators. This will 75646d528ed9Sopenharmony_ci remove all occurrences of every item in the right-hand list from the 75656d528ed9Sopenharmony_ci left-hand list. It is an error to remove an item not in the list. This is to 75666d528ed9Sopenharmony_ci prevent common typos and to detect dead code that is removing things that no 75676d528ed9Sopenharmony_ci longer apply. 75686d528ed9Sopenharmony_ci 75696d528ed9Sopenharmony_ci It is an error to use '=' to replace a nonempty list with another nonempty 75706d528ed9Sopenharmony_ci list. This is to prevent accidentally overwriting data when in most cases 75716d528ed9Sopenharmony_ci '+=' was intended. To overwrite a list on purpose, first assign it to the 75726d528ed9Sopenharmony_ci empty list: 75736d528ed9Sopenharmony_ci 75746d528ed9Sopenharmony_ci mylist = [] 75756d528ed9Sopenharmony_ci mylist = otherlist 75766d528ed9Sopenharmony_ci``` 75776d528ed9Sopenharmony_ci 75786d528ed9Sopenharmony_ci#### **Scopes** 75796d528ed9Sopenharmony_ci 75806d528ed9Sopenharmony_ci``` 75816d528ed9Sopenharmony_ci All execution happens in the context of a scope which holds the current state 75826d528ed9Sopenharmony_ci (like variables). With the exception of loops and conditions, '{' introduces 75836d528ed9Sopenharmony_ci a new scope. 75846d528ed9Sopenharmony_ci 75856d528ed9Sopenharmony_ci Most scopes have a parent reference to the old scope. Variable reads 75866d528ed9Sopenharmony_ci recursively search all parent scopes until the variable is found or there are 75876d528ed9Sopenharmony_ci no more scopes. Variable writes always go into the current scope. This means 75886d528ed9Sopenharmony_ci that after the closing '}' (again excepting loops and conditions), all local 75896d528ed9Sopenharmony_ci variables will be restored to the previous values. This also means that "foo 75906d528ed9Sopenharmony_ci = foo" can do useful work by copying a variable into the current scope that 75916d528ed9Sopenharmony_ci was defined in a containing scope. 75926d528ed9Sopenharmony_ci 75936d528ed9Sopenharmony_ci Scopes can be assigned to variables. Examples of such scopes are the 75946d528ed9Sopenharmony_ci implicitly-created "invoker" when invoking a template (which refers to the 75956d528ed9Sopenharmony_ci variables set by the invoking code), scopes created by functions like 75966d528ed9Sopenharmony_ci exec_script, and scopes explicitly created like 75976d528ed9Sopenharmony_ci 75986d528ed9Sopenharmony_ci empty_scope = {} 75996d528ed9Sopenharmony_ci myvalues = { 76006d528ed9Sopenharmony_ci foo = 21 76016d528ed9Sopenharmony_ci bar = "something" 76026d528ed9Sopenharmony_ci } 76036d528ed9Sopenharmony_ci 76046d528ed9Sopenharmony_ci In the case of explicitly created scopes and scopes created by functions like 76056d528ed9Sopenharmony_ci exec_script, there is no reference to the parent scope. Such scopes are fully 76066d528ed9Sopenharmony_ci self-contained and do not "inherit" values from their defining scope. 76076d528ed9Sopenharmony_ci 76086d528ed9Sopenharmony_ci Inside an explicit scope definition can be any GN code including conditionals 76096d528ed9Sopenharmony_ci and function calls. After the close of the scope, it will contain all 76106d528ed9Sopenharmony_ci variables explicitly set by the code contained inside it. After this, the 76116d528ed9Sopenharmony_ci values can be read, modified, or added to: 76126d528ed9Sopenharmony_ci 76136d528ed9Sopenharmony_ci myvalues.foo += 2 76146d528ed9Sopenharmony_ci empty_scope.new_thing = [ 1, 2, 3 ] 76156d528ed9Sopenharmony_ci 76166d528ed9Sopenharmony_ci Scope equality is defined as single-level scopes identical within the current 76176d528ed9Sopenharmony_ci scope. That is, all values in the first scope must be present and identical 76186d528ed9Sopenharmony_ci within the second, and vice versa. Note that this means inherited scopes are 76196d528ed9Sopenharmony_ci always unequal by definition. 76206d528ed9Sopenharmony_ci``` 76216d528ed9Sopenharmony_ci### <a name="io_conversion"></a>**Input and output conversion** 76226d528ed9Sopenharmony_ci 76236d528ed9Sopenharmony_ci``` 76246d528ed9Sopenharmony_ci Input and output conversions are arguments to file and process functions 76256d528ed9Sopenharmony_ci that specify how to convert data to or from external formats. The possible 76266d528ed9Sopenharmony_ci values for parameters specifying conversions are: 76276d528ed9Sopenharmony_ci 76286d528ed9Sopenharmony_ci "" (the default) 76296d528ed9Sopenharmony_ci input: Discard the result and return None. 76306d528ed9Sopenharmony_ci 76316d528ed9Sopenharmony_ci output: If value is a list, then "list lines"; otherwise "value". 76326d528ed9Sopenharmony_ci 76336d528ed9Sopenharmony_ci "list lines" 76346d528ed9Sopenharmony_ci input: 76356d528ed9Sopenharmony_ci Return the file contents as a list, with a string for each line. The 76366d528ed9Sopenharmony_ci newlines will not be present in the result. The last line may or may 76376d528ed9Sopenharmony_ci not end in a newline. 76386d528ed9Sopenharmony_ci 76396d528ed9Sopenharmony_ci After splitting, each individual line will be trimmed of whitespace on 76406d528ed9Sopenharmony_ci both ends. 76416d528ed9Sopenharmony_ci 76426d528ed9Sopenharmony_ci output: 76436d528ed9Sopenharmony_ci Renders the value contents as a list, with a string for each line. The 76446d528ed9Sopenharmony_ci newlines will not be present in the result. The last line will end in 76456d528ed9Sopenharmony_ci with a newline. 76466d528ed9Sopenharmony_ci 76476d528ed9Sopenharmony_ci "scope" 76486d528ed9Sopenharmony_ci input: 76496d528ed9Sopenharmony_ci Execute the block as GN code and return a scope with the resulting 76506d528ed9Sopenharmony_ci values in it. If the input was: 76516d528ed9Sopenharmony_ci a = [ "hello.cc", "world.cc" ] 76526d528ed9Sopenharmony_ci b = 26 76536d528ed9Sopenharmony_ci and you read the result into a variable named "val", then you could 76546d528ed9Sopenharmony_ci access contents the "." operator on "val": 76556d528ed9Sopenharmony_ci sources = val.a 76566d528ed9Sopenharmony_ci some_count = val.b 76576d528ed9Sopenharmony_ci 76586d528ed9Sopenharmony_ci output: 76596d528ed9Sopenharmony_ci Renders the value contents as a GN code block, reversing the input 76606d528ed9Sopenharmony_ci result above. 76616d528ed9Sopenharmony_ci 76626d528ed9Sopenharmony_ci "string" 76636d528ed9Sopenharmony_ci input: Return the file contents into a single string. 76646d528ed9Sopenharmony_ci 76656d528ed9Sopenharmony_ci output: 76666d528ed9Sopenharmony_ci Render the value contents into a single string. The output is: 76676d528ed9Sopenharmony_ci a string renders with quotes, e.g. "str" 76686d528ed9Sopenharmony_ci an integer renders as a stringified integer, e.g. "6" 76696d528ed9Sopenharmony_ci a boolean renders as the associated string, e.g. "true" 76706d528ed9Sopenharmony_ci a list renders as a representation of its contents, e.g. "[\"str\", 6]" 76716d528ed9Sopenharmony_ci a scope renders as a GN code block of its values. If the Value was: 76726d528ed9Sopenharmony_ci Value val; 76736d528ed9Sopenharmony_ci val.a = [ "hello.cc", "world.cc" ]; 76746d528ed9Sopenharmony_ci val.b = 26 76756d528ed9Sopenharmony_ci the resulting output would be: 76766d528ed9Sopenharmony_ci "{ 76776d528ed9Sopenharmony_ci a = [ \"hello.cc\", \"world.cc\" ] 76786d528ed9Sopenharmony_ci b = 26 76796d528ed9Sopenharmony_ci }" 76806d528ed9Sopenharmony_ci 76816d528ed9Sopenharmony_ci "value" 76826d528ed9Sopenharmony_ci input: 76836d528ed9Sopenharmony_ci Parse the input as if it was a literal rvalue in a buildfile. Examples of 76846d528ed9Sopenharmony_ci typical program output using this mode: 76856d528ed9Sopenharmony_ci [ "foo", "bar" ] (result will be a list) 76866d528ed9Sopenharmony_ci or 76876d528ed9Sopenharmony_ci "foo bar" (result will be a string) 76886d528ed9Sopenharmony_ci or 76896d528ed9Sopenharmony_ci 5 (result will be an integer) 76906d528ed9Sopenharmony_ci 76916d528ed9Sopenharmony_ci Note that if the input is empty, the result will be a null value which 76926d528ed9Sopenharmony_ci will produce an error if assigned to a variable. 76936d528ed9Sopenharmony_ci 76946d528ed9Sopenharmony_ci output: 76956d528ed9Sopenharmony_ci Render the value contents as a literal rvalue. Strings render with 76966d528ed9Sopenharmony_ci escaped quotes. 76976d528ed9Sopenharmony_ci 76986d528ed9Sopenharmony_ci "json" 76996d528ed9Sopenharmony_ci input: Parse the input as a JSON and convert it to equivalent GN rvalue. 77006d528ed9Sopenharmony_ci 77016d528ed9Sopenharmony_ci output: Convert the Value to equivalent JSON value. 77026d528ed9Sopenharmony_ci 77036d528ed9Sopenharmony_ci The data type mapping is: 77046d528ed9Sopenharmony_ci a string in JSON maps to string in GN 77056d528ed9Sopenharmony_ci an integer in JSON maps to integer in GN 77066d528ed9Sopenharmony_ci a float in JSON is unsupported and will result in an error 77076d528ed9Sopenharmony_ci an object in JSON maps to scope in GN 77086d528ed9Sopenharmony_ci an array in JSON maps to list in GN 77096d528ed9Sopenharmony_ci a boolean in JSON maps to boolean in GN 77106d528ed9Sopenharmony_ci a null in JSON is unsupported and will result in an error 77116d528ed9Sopenharmony_ci 77126d528ed9Sopenharmony_ci Nota that the input dictionary keys have to be valid GN identifiers 77136d528ed9Sopenharmony_ci otherwise they will produce an error. 77146d528ed9Sopenharmony_ci 77156d528ed9Sopenharmony_ci "trim ..." (input only) 77166d528ed9Sopenharmony_ci Prefixing any of the other transformations with the word "trim" will 77176d528ed9Sopenharmony_ci result in whitespace being trimmed from the beginning and end of the 77186d528ed9Sopenharmony_ci result before processing. 77196d528ed9Sopenharmony_ci 77206d528ed9Sopenharmony_ci Examples: "trim string" or "trim list lines" 77216d528ed9Sopenharmony_ci 77226d528ed9Sopenharmony_ci Note that "trim value" is useless because the value parser skips 77236d528ed9Sopenharmony_ci whitespace anyway. 77246d528ed9Sopenharmony_ci``` 77256d528ed9Sopenharmony_ci### <a name="file_pattern"></a>**File patterns** 77266d528ed9Sopenharmony_ci 77276d528ed9Sopenharmony_ci``` 77286d528ed9Sopenharmony_ci File patterns are VERY limited regular expressions. They must match the 77296d528ed9Sopenharmony_ci entire input string to be counted as a match. In regular expression parlance, 77306d528ed9Sopenharmony_ci there is an implicit "^...$" surrounding your input. If you want to match a 77316d528ed9Sopenharmony_ci substring, you need to use wildcards at the beginning and end. 77326d528ed9Sopenharmony_ci 77336d528ed9Sopenharmony_ci There are only two special tokens understood by the pattern matcher. 77346d528ed9Sopenharmony_ci Everything else is a literal. 77356d528ed9Sopenharmony_ci 77366d528ed9Sopenharmony_ci - "*" Matches zero or more of any character. It does not depend on the 77376d528ed9Sopenharmony_ci preceding character (in regular expression parlance it is equivalent to 77386d528ed9Sopenharmony_ci ".*"). 77396d528ed9Sopenharmony_ci 77406d528ed9Sopenharmony_ci - "\b" Matches a path boundary. This will match the beginning or end of a 77416d528ed9Sopenharmony_ci string, or a slash. 77426d528ed9Sopenharmony_ci``` 77436d528ed9Sopenharmony_ci 77446d528ed9Sopenharmony_ci#### **Pattern examples** 77456d528ed9Sopenharmony_ci 77466d528ed9Sopenharmony_ci``` 77476d528ed9Sopenharmony_ci "*asdf*" 77486d528ed9Sopenharmony_ci Matches a string containing "asdf" anywhere. 77496d528ed9Sopenharmony_ci 77506d528ed9Sopenharmony_ci "asdf" 77516d528ed9Sopenharmony_ci Matches only the exact string "asdf". 77526d528ed9Sopenharmony_ci 77536d528ed9Sopenharmony_ci "*.cc" 77546d528ed9Sopenharmony_ci Matches strings ending in the literal ".cc". 77556d528ed9Sopenharmony_ci 77566d528ed9Sopenharmony_ci "\bwin/*" 77576d528ed9Sopenharmony_ci Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo". 77586d528ed9Sopenharmony_ci``` 77596d528ed9Sopenharmony_ci### <a name="label_pattern"></a>**Label patterns** 77606d528ed9Sopenharmony_ci 77616d528ed9Sopenharmony_ci``` 77626d528ed9Sopenharmony_ci A label pattern is a way of expressing one or more labels in a portion of the 77636d528ed9Sopenharmony_ci source tree. They are not general regular expressions. 77646d528ed9Sopenharmony_ci 77656d528ed9Sopenharmony_ci They can take the following forms only: 77666d528ed9Sopenharmony_ci 77676d528ed9Sopenharmony_ci - Explicit (no wildcard): 77686d528ed9Sopenharmony_ci "//foo/bar:baz" 77696d528ed9Sopenharmony_ci ":baz" 77706d528ed9Sopenharmony_ci 77716d528ed9Sopenharmony_ci - Wildcard target names: 77726d528ed9Sopenharmony_ci "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file) 77736d528ed9Sopenharmony_ci ":*" (all targets in the current build file) 77746d528ed9Sopenharmony_ci 77756d528ed9Sopenharmony_ci - Wildcard directory names ("*" is only supported at the end) 77766d528ed9Sopenharmony_ci "*" (all targets) 77776d528ed9Sopenharmony_ci "//foo/bar/*" (all targets in any subdir of //foo/bar) 77786d528ed9Sopenharmony_ci "./*" (all targets in the current build file or sub dirs) 77796d528ed9Sopenharmony_ci 77806d528ed9Sopenharmony_ci Any of the above forms can additionally take an explicit toolchain 77816d528ed9Sopenharmony_ci in parenthesis at the end of the label pattern. In this case, the 77826d528ed9Sopenharmony_ci toolchain must be fully qualified (no wildcards are supported in the 77836d528ed9Sopenharmony_ci toolchain name). 77846d528ed9Sopenharmony_ci 77856d528ed9Sopenharmony_ci "//foo:bar(//build/toolchain:mac)" 77866d528ed9Sopenharmony_ci An explicit target in an explicit toolchain. 77876d528ed9Sopenharmony_ci 77886d528ed9Sopenharmony_ci ":*(//build/toolchain/linux:32bit)" 77896d528ed9Sopenharmony_ci All targets in the current build file using the 32-bit Linux toolchain. 77906d528ed9Sopenharmony_ci 77916d528ed9Sopenharmony_ci "//foo/*(//build/toolchain:win)" 77926d528ed9Sopenharmony_ci All targets in //foo and any subdirectory using the Windows 77936d528ed9Sopenharmony_ci toolchain. 77946d528ed9Sopenharmony_ci``` 77956d528ed9Sopenharmony_ci### <a name="labels"></a>**About labels** 77966d528ed9Sopenharmony_ci 77976d528ed9Sopenharmony_ci``` 77986d528ed9Sopenharmony_ci Everything that can participate in the dependency graph (targets, configs, 77996d528ed9Sopenharmony_ci and toolchains) are identified by labels. A common label looks like: 78006d528ed9Sopenharmony_ci 78016d528ed9Sopenharmony_ci //base/test:test_support 78026d528ed9Sopenharmony_ci 78036d528ed9Sopenharmony_ci This consists of a source-root-absolute path, a colon, and a name. This means 78046d528ed9Sopenharmony_ci to look for the thing named "test_support" in "base/test/BUILD.gn". 78056d528ed9Sopenharmony_ci 78066d528ed9Sopenharmony_ci You can also specify system absolute paths if necessary. Typically such 78076d528ed9Sopenharmony_ci paths would be specified via a build arg so the developer can specify where 78086d528ed9Sopenharmony_ci the component is on their system. 78096d528ed9Sopenharmony_ci 78106d528ed9Sopenharmony_ci /usr/local/foo:bar (Posix) 78116d528ed9Sopenharmony_ci /C:/Program Files/MyLibs:bar (Windows) 78126d528ed9Sopenharmony_ci``` 78136d528ed9Sopenharmony_ci 78146d528ed9Sopenharmony_ci#### **Toolchains** 78156d528ed9Sopenharmony_ci 78166d528ed9Sopenharmony_ci``` 78176d528ed9Sopenharmony_ci A canonical label includes the label of the toolchain being used. Normally, 78186d528ed9Sopenharmony_ci the toolchain label is implicitly inherited from the current execution 78196d528ed9Sopenharmony_ci context, but you can override this to specify cross-toolchain dependencies: 78206d528ed9Sopenharmony_ci 78216d528ed9Sopenharmony_ci //base/test:test_support(//build/toolchain/win:msvc) 78226d528ed9Sopenharmony_ci 78236d528ed9Sopenharmony_ci Here GN will look for the toolchain definition called "msvc" in the file 78246d528ed9Sopenharmony_ci "//build/toolchain/win" to know how to compile this target. 78256d528ed9Sopenharmony_ci``` 78266d528ed9Sopenharmony_ci 78276d528ed9Sopenharmony_ci#### **Relative labels** 78286d528ed9Sopenharmony_ci 78296d528ed9Sopenharmony_ci``` 78306d528ed9Sopenharmony_ci If you want to refer to something in the same buildfile, you can omit 78316d528ed9Sopenharmony_ci the path name and just start with a colon. This format is recommended for 78326d528ed9Sopenharmony_ci all same-file references. 78336d528ed9Sopenharmony_ci 78346d528ed9Sopenharmony_ci :base 78356d528ed9Sopenharmony_ci 78366d528ed9Sopenharmony_ci Labels can be specified as being relative to the current directory. 78376d528ed9Sopenharmony_ci Stylistically, we prefer to use absolute paths for all non-file-local 78386d528ed9Sopenharmony_ci references unless a build file needs to be run in different contexts (like a 78396d528ed9Sopenharmony_ci project needs to be both standalone and pulled into other projects in 78406d528ed9Sopenharmony_ci difference places in the directory hierarchy). 78416d528ed9Sopenharmony_ci 78426d528ed9Sopenharmony_ci source/plugin:myplugin 78436d528ed9Sopenharmony_ci ../net:url_request 78446d528ed9Sopenharmony_ci``` 78456d528ed9Sopenharmony_ci 78466d528ed9Sopenharmony_ci#### **Implicit names** 78476d528ed9Sopenharmony_ci 78486d528ed9Sopenharmony_ci``` 78496d528ed9Sopenharmony_ci If a name is unspecified, it will inherit the directory name. Stylistically, 78506d528ed9Sopenharmony_ci we prefer to omit the colon and name when possible: 78516d528ed9Sopenharmony_ci 78526d528ed9Sopenharmony_ci //net -> //net:net 78536d528ed9Sopenharmony_ci //tools/gn -> //tools/gn:gn 78546d528ed9Sopenharmony_ci``` 78556d528ed9Sopenharmony_ci### <a name="metadata_collection"></a>**Metadata Collection** 78566d528ed9Sopenharmony_ci 78576d528ed9Sopenharmony_ci``` 78586d528ed9Sopenharmony_ci Metadata is information attached to targets throughout the dependency tree. GN 78596d528ed9Sopenharmony_ci allows for the collection of this data into files written during the generation 78606d528ed9Sopenharmony_ci step, enabling users to expose and aggregate this data based on the dependency 78616d528ed9Sopenharmony_ci tree. 78626d528ed9Sopenharmony_ci``` 78636d528ed9Sopenharmony_ci 78646d528ed9Sopenharmony_ci#### **generated_file targets** 78656d528ed9Sopenharmony_ci 78666d528ed9Sopenharmony_ci``` 78676d528ed9Sopenharmony_ci Similar to the write_file() function, the generated_file target type 78686d528ed9Sopenharmony_ci creates a file in the specified location with the specified content. The 78696d528ed9Sopenharmony_ci primary difference between write_file() and this target type is that the 78706d528ed9Sopenharmony_ci write_file function does the file write at parse time, while the 78716d528ed9Sopenharmony_ci generated_file target type writes at target resolution time. See 78726d528ed9Sopenharmony_ci "gn help generated_file" for more detail. 78736d528ed9Sopenharmony_ci 78746d528ed9Sopenharmony_ci When written at target resolution time, generated_file enables GN to 78756d528ed9Sopenharmony_ci collect and write aggregated metadata from dependents. 78766d528ed9Sopenharmony_ci 78776d528ed9Sopenharmony_ci A generated_file target can declare either 'contents' to write statically 78786d528ed9Sopenharmony_ci known contents to a file or 'data_keys' to aggregate metadata and write the 78796d528ed9Sopenharmony_ci result to a file. It can also specify 'walk_keys' (to restrict the metadata 78806d528ed9Sopenharmony_ci collection), 'output_conversion', and 'rebase'. 78816d528ed9Sopenharmony_ci``` 78826d528ed9Sopenharmony_ci 78836d528ed9Sopenharmony_ci#### **Collection and Aggregation** 78846d528ed9Sopenharmony_ci 78856d528ed9Sopenharmony_ci``` 78866d528ed9Sopenharmony_ci Targets can declare a 'metadata' variable containing a scope, and this 78876d528ed9Sopenharmony_ci metadata may be collected and written out to a file specified by 78886d528ed9Sopenharmony_ci generated_file aggregation targets. The 'metadata' scope must contain 78896d528ed9Sopenharmony_ci only list values since the aggregation step collects a list of these values. 78906d528ed9Sopenharmony_ci 78916d528ed9Sopenharmony_ci During the target resolution, generated_file targets will walk their 78926d528ed9Sopenharmony_ci dependencies recursively, collecting metadata based on the specified 78936d528ed9Sopenharmony_ci 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk 78946d528ed9Sopenharmony_ci to identify which variables in dependencies' 'metadata' scopes to collect. 78956d528ed9Sopenharmony_ci 78966d528ed9Sopenharmony_ci The walk begins with the listed dependencies of the 'generated_file' target. 78976d528ed9Sopenharmony_ci The 'metadata' scope for each dependency is inspected for matching elements 78986d528ed9Sopenharmony_ci of the 'generated_file' target's 'data_keys' list. If a match is found, the 78996d528ed9Sopenharmony_ci data from the dependent's matching key list is appended to the aggregate walk 79006d528ed9Sopenharmony_ci list. Note that this means that if more than one walk key is specified, the 79016d528ed9Sopenharmony_ci data in all of them will be aggregated into one list. From there, the walk 79026d528ed9Sopenharmony_ci will then recurse into the dependencies of each target it encounters, 79036d528ed9Sopenharmony_ci collecting the specified metadata for each. 79046d528ed9Sopenharmony_ci 79056d528ed9Sopenharmony_ci For example: 79066d528ed9Sopenharmony_ci 79076d528ed9Sopenharmony_ci group("a") { 79086d528ed9Sopenharmony_ci metadata = { 79096d528ed9Sopenharmony_ci doom_melon = [ "enable" ] 79106d528ed9Sopenharmony_ci my_files = [ "foo.cpp" ] 79116d528ed9Sopenharmony_ci my_extra_files = [ "bar.cpp" ] 79126d528ed9Sopenharmony_ci } 79136d528ed9Sopenharmony_ci 79146d528ed9Sopenharmony_ci deps = [ ":b" ] 79156d528ed9Sopenharmony_ci } 79166d528ed9Sopenharmony_ci 79176d528ed9Sopenharmony_ci group("b") { 79186d528ed9Sopenharmony_ci metadata = { 79196d528ed9Sopenharmony_ci my_files = [ "baz.cpp" ] 79206d528ed9Sopenharmony_ci } 79216d528ed9Sopenharmony_ci } 79226d528ed9Sopenharmony_ci 79236d528ed9Sopenharmony_ci generated_file("metadata") { 79246d528ed9Sopenharmony_ci outputs = [ "$root_build_dir/my_files.json" ] 79256d528ed9Sopenharmony_ci data_keys = [ "my_files", "my_extra_files" ] 79266d528ed9Sopenharmony_ci 79276d528ed9Sopenharmony_ci deps = [ ":a" ] 79286d528ed9Sopenharmony_ci } 79296d528ed9Sopenharmony_ci 79306d528ed9Sopenharmony_ci The above will produce the following file data: 79316d528ed9Sopenharmony_ci 79326d528ed9Sopenharmony_ci foo.cpp 79336d528ed9Sopenharmony_ci bar.cpp 79346d528ed9Sopenharmony_ci baz.cpp 79356d528ed9Sopenharmony_ci 79366d528ed9Sopenharmony_ci The dependency walk can be limited by using the 'walk_keys'. This is a list of 79376d528ed9Sopenharmony_ci labels that should be included in the walk. All labels specified here should 79386d528ed9Sopenharmony_ci also be in one of the deps lists. These keys act as barriers, where the walk 79396d528ed9Sopenharmony_ci will only recurse into the targets listed. An empty list in all specified 79406d528ed9Sopenharmony_ci barriers will end that portion of the walk. 79416d528ed9Sopenharmony_ci 79426d528ed9Sopenharmony_ci group("a") { 79436d528ed9Sopenharmony_ci metadata = { 79446d528ed9Sopenharmony_ci my_files = [ "foo.cpp" ] 79456d528ed9Sopenharmony_ci my_files_barrier = [ ":b" ] 79466d528ed9Sopenharmony_ci } 79476d528ed9Sopenharmony_ci 79486d528ed9Sopenharmony_ci deps = [ ":b", ":c" ] 79496d528ed9Sopenharmony_ci } 79506d528ed9Sopenharmony_ci 79516d528ed9Sopenharmony_ci group("b") { 79526d528ed9Sopenharmony_ci metadata = { 79536d528ed9Sopenharmony_ci my_files = [ "bar.cpp" ] 79546d528ed9Sopenharmony_ci } 79556d528ed9Sopenharmony_ci } 79566d528ed9Sopenharmony_ci 79576d528ed9Sopenharmony_ci group("c") { 79586d528ed9Sopenharmony_ci metadata = { 79596d528ed9Sopenharmony_ci my_files = [ "doom_melon.cpp" ] 79606d528ed9Sopenharmony_ci } 79616d528ed9Sopenharmony_ci } 79626d528ed9Sopenharmony_ci 79636d528ed9Sopenharmony_ci generated_file("metadata") { 79646d528ed9Sopenharmony_ci outputs = [ "$root_build_dir/my_files.json" ] 79656d528ed9Sopenharmony_ci data_keys = [ "my_files" ] 79666d528ed9Sopenharmony_ci walk_keys = [ "my_files_barrier" ] 79676d528ed9Sopenharmony_ci 79686d528ed9Sopenharmony_ci deps = [ ":a" ] 79696d528ed9Sopenharmony_ci } 79706d528ed9Sopenharmony_ci 79716d528ed9Sopenharmony_ci The above will produce the following file data (note that `doom_melon.cpp` is 79726d528ed9Sopenharmony_ci not included): 79736d528ed9Sopenharmony_ci 79746d528ed9Sopenharmony_ci foo.cpp 79756d528ed9Sopenharmony_ci bar.cpp 79766d528ed9Sopenharmony_ci 79776d528ed9Sopenharmony_ci A common example of this sort of barrier is in builds that have host tools 79786d528ed9Sopenharmony_ci built as part of the tree, but do not want the metadata from those host tools 79796d528ed9Sopenharmony_ci to be collected with the target-side code. 79806d528ed9Sopenharmony_ci``` 79816d528ed9Sopenharmony_ci 79826d528ed9Sopenharmony_ci#### **Common Uses** 79836d528ed9Sopenharmony_ci 79846d528ed9Sopenharmony_ci``` 79856d528ed9Sopenharmony_ci Metadata can be used to collect information about the different targets in the 79866d528ed9Sopenharmony_ci build, and so a common use is to provide post-build tooling with a set of data 79876d528ed9Sopenharmony_ci necessary to do aggregation tasks. For example, if each test target specifies 79886d528ed9Sopenharmony_ci the output location of its binary to run in a metadata field, that can be 79896d528ed9Sopenharmony_ci collected into a single file listing the locations of all tests in the 79906d528ed9Sopenharmony_ci dependency tree. A local build tool (or continuous integration infrastructure) 79916d528ed9Sopenharmony_ci can then use that file to know which tests exist, and where, and run them 79926d528ed9Sopenharmony_ci accordingly. 79936d528ed9Sopenharmony_ci 79946d528ed9Sopenharmony_ci Another use is in image creation, where a post-build image tool needs to know 79956d528ed9Sopenharmony_ci various pieces of information about the components it should include in order 79966d528ed9Sopenharmony_ci to put together the correct image. 79976d528ed9Sopenharmony_ci``` 79986d528ed9Sopenharmony_ci### <a name="ninja_rules"></a>**Ninja build rules** 79996d528ed9Sopenharmony_ci 80006d528ed9Sopenharmony_ci#### **The "all" and "default" rules** 80016d528ed9Sopenharmony_ci 80026d528ed9Sopenharmony_ci``` 80036d528ed9Sopenharmony_ci All generated targets (see "gn help execution") will be added to an implicit 80046d528ed9Sopenharmony_ci build rule called "all" so "ninja all" will always compile everything. The 80056d528ed9Sopenharmony_ci default rule will be used by Ninja if no specific target is specified (just 80066d528ed9Sopenharmony_ci typing "ninja"). If there is a target named "default" in the root build file, 80076d528ed9Sopenharmony_ci it will be the default build rule, otherwise the implicit "all" rule will be 80086d528ed9Sopenharmony_ci used. 80096d528ed9Sopenharmony_ci``` 80106d528ed9Sopenharmony_ci 80116d528ed9Sopenharmony_ci#### **Phony rules** 80126d528ed9Sopenharmony_ci 80136d528ed9Sopenharmony_ci``` 80146d528ed9Sopenharmony_ci GN generates Ninja "phony" rules for targets in the default toolchain. The 80156d528ed9Sopenharmony_ci phony rules can collide with each other and with the names of generated files 80166d528ed9Sopenharmony_ci so are generated with the following priority: 80176d528ed9Sopenharmony_ci 80186d528ed9Sopenharmony_ci 1. Actual files generated by the build always take precedence. 80196d528ed9Sopenharmony_ci 80206d528ed9Sopenharmony_ci 2. Targets in the toplevel //BUILD.gn file. 80216d528ed9Sopenharmony_ci 80226d528ed9Sopenharmony_ci 3. Targets in toplevel directories matching the names of the directories. 80236d528ed9Sopenharmony_ci So "ninja foo" can be used to compile "//foo:foo". This only applies to 80246d528ed9Sopenharmony_ci the first level of directories since usually these are the most 80256d528ed9Sopenharmony_ci important (so this won't apply to "//foo/bar:bar"). 80266d528ed9Sopenharmony_ci 80276d528ed9Sopenharmony_ci 4. The short names of executables if there is only one executable with that 80286d528ed9Sopenharmony_ci short name. Use "ninja doom_melon" to compile the 80296d528ed9Sopenharmony_ci "//tools/fruit:doom_melon" executable. 80306d528ed9Sopenharmony_ci 80316d528ed9Sopenharmony_ci Note that for Apple platforms, create_bundle targets with a product_type 80326d528ed9Sopenharmony_ci of "com.apple.product-type.application" are considered as executable 80336d528ed9Sopenharmony_ci for this rule (as they define application bundles). 80346d528ed9Sopenharmony_ci 80356d528ed9Sopenharmony_ci 5. The short names of all targets if there is only one target with that 80366d528ed9Sopenharmony_ci short name. 80376d528ed9Sopenharmony_ci 80386d528ed9Sopenharmony_ci 6. Full label name with no leading slashes. So you can use 80396d528ed9Sopenharmony_ci "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon". 80406d528ed9Sopenharmony_ci 80416d528ed9Sopenharmony_ci 7. Labels with an implicit name part (when the short names match the 80426d528ed9Sopenharmony_ci directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar". 80436d528ed9Sopenharmony_ci 80446d528ed9Sopenharmony_ci These "phony" rules are provided only for running Ninja since this matches 80456d528ed9Sopenharmony_ci people's historical expectations for building. For consistency with the rest 80466d528ed9Sopenharmony_ci of the program, GN introspection commands accept explicit labels. 80476d528ed9Sopenharmony_ci 80486d528ed9Sopenharmony_ci To explicitly compile a target in a non-default toolchain, you must give 80496d528ed9Sopenharmony_ci Ninja the exact name of the output file relative to the build directory. 80506d528ed9Sopenharmony_ci``` 80516d528ed9Sopenharmony_ci### <a name="nogncheck"></a>**nogncheck**: Skip an include line from checking. 80526d528ed9Sopenharmony_ci 80536d528ed9Sopenharmony_ci``` 80546d528ed9Sopenharmony_ci GN's header checker helps validate that the includes match the build 80556d528ed9Sopenharmony_ci dependency graph. Sometimes an include might be conditional or otherwise 80566d528ed9Sopenharmony_ci problematic, but you want to specifically allow it. In this case, it can be 80576d528ed9Sopenharmony_ci whitelisted. 80586d528ed9Sopenharmony_ci 80596d528ed9Sopenharmony_ci Include lines containing the substring "nogncheck" will be excluded from 80606d528ed9Sopenharmony_ci header checking. The most common case is a conditional include: 80616d528ed9Sopenharmony_ci 80626d528ed9Sopenharmony_ci #if defined(ENABLE_DOOM_MELON) 80636d528ed9Sopenharmony_ci #include "tools/doom_melon/doom_melon.h" // nogncheck 80646d528ed9Sopenharmony_ci #endif 80656d528ed9Sopenharmony_ci 80666d528ed9Sopenharmony_ci If the build file has a conditional dependency on the corresponding target 80676d528ed9Sopenharmony_ci that matches the conditional include, everything will always link correctly: 80686d528ed9Sopenharmony_ci 80696d528ed9Sopenharmony_ci source_set("mytarget") { 80706d528ed9Sopenharmony_ci ... 80716d528ed9Sopenharmony_ci if (enable_doom_melon) { 80726d528ed9Sopenharmony_ci defines = [ "ENABLE_DOOM_MELON" ] 80736d528ed9Sopenharmony_ci deps += [ "//tools/doom_melon" ] 80746d528ed9Sopenharmony_ci } 80756d528ed9Sopenharmony_ci 80766d528ed9Sopenharmony_ci But GN's header checker does not understand preprocessor directives, won't 80776d528ed9Sopenharmony_ci know it matches the build dependencies, and will flag this include as 80786d528ed9Sopenharmony_ci incorrect when the condition is false. 80796d528ed9Sopenharmony_ci``` 80806d528ed9Sopenharmony_ci 80816d528ed9Sopenharmony_ci#### **More information** 80826d528ed9Sopenharmony_ci 80836d528ed9Sopenharmony_ci``` 80846d528ed9Sopenharmony_ci The topic "gn help check" has general information on how checking works and 80856d528ed9Sopenharmony_ci advice on fixing problems. Targets can also opt-out of checking, see 80866d528ed9Sopenharmony_ci "gn help check_includes". 80876d528ed9Sopenharmony_ci``` 80886d528ed9Sopenharmony_ci### <a name="runtime_deps"></a>**Runtime dependencies** 80896d528ed9Sopenharmony_ci 80906d528ed9Sopenharmony_ci``` 80916d528ed9Sopenharmony_ci Runtime dependencies of a target are exposed via the "runtime_deps" category 80926d528ed9Sopenharmony_ci of "gn desc" (see "gn help desc") or they can be written at build generation 80936d528ed9Sopenharmony_ci time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help 80946d528ed9Sopenharmony_ci --runtime-deps-list-file"). 80956d528ed9Sopenharmony_ci 80966d528ed9Sopenharmony_ci To a first approximation, the runtime dependencies of a target are the set of 80976d528ed9Sopenharmony_ci "data" files, data directories, and the shared libraries from all transitive 80986d528ed9Sopenharmony_ci dependencies. Executables, shared libraries, and loadable modules are 80996d528ed9Sopenharmony_ci considered runtime dependencies of themselves. 81006d528ed9Sopenharmony_ci``` 81016d528ed9Sopenharmony_ci 81026d528ed9Sopenharmony_ci#### **Executables** 81036d528ed9Sopenharmony_ci 81046d528ed9Sopenharmony_ci``` 81056d528ed9Sopenharmony_ci Executable targets and those executable targets' transitive dependencies are 81066d528ed9Sopenharmony_ci not considered unless that executable is listed in "data_deps". Otherwise, GN 81076d528ed9Sopenharmony_ci assumes that the executable (and everything it requires) is a build-time 81086d528ed9Sopenharmony_ci dependency only. 81096d528ed9Sopenharmony_ci``` 81106d528ed9Sopenharmony_ci 81116d528ed9Sopenharmony_ci#### **Actions and copies** 81126d528ed9Sopenharmony_ci 81136d528ed9Sopenharmony_ci``` 81146d528ed9Sopenharmony_ci Action and copy targets that are listed as "data_deps" will have all of their 81156d528ed9Sopenharmony_ci outputs and data files considered as runtime dependencies. Action and copy 81166d528ed9Sopenharmony_ci targets that are "deps" or "public_deps" will have only their data files 81176d528ed9Sopenharmony_ci considered as runtime dependencies. These targets can list an output file in 81186d528ed9Sopenharmony_ci both the "outputs" and "data" lists to force an output file as a runtime 81196d528ed9Sopenharmony_ci dependency in all cases. 81206d528ed9Sopenharmony_ci 81216d528ed9Sopenharmony_ci The different rules for deps and data_deps are to express build-time (deps) 81226d528ed9Sopenharmony_ci vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as 81236d528ed9Sopenharmony_ci data dependencies, there would be a lot of extra stuff, and if GN counted all 81246d528ed9Sopenharmony_ci run-time dependencies as regular deps, the build's parallelism would be 81256d528ed9Sopenharmony_ci unnecessarily constrained. 81266d528ed9Sopenharmony_ci 81276d528ed9Sopenharmony_ci This rule can sometimes lead to unintuitive results. For example, given the 81286d528ed9Sopenharmony_ci three targets: 81296d528ed9Sopenharmony_ci A --[data_deps]--> B --[deps]--> ACTION 81306d528ed9Sopenharmony_ci GN would say that A does not have runtime deps on the result of the ACTION, 81316d528ed9Sopenharmony_ci which is often correct. But the purpose of the B target might be to collect 81326d528ed9Sopenharmony_ci many actions into one logic unit, and the "data"-ness of A's dependency is 81336d528ed9Sopenharmony_ci lost. Solutions: 81346d528ed9Sopenharmony_ci 81356d528ed9Sopenharmony_ci - List the outputs of the action in its data section (if the results of 81366d528ed9Sopenharmony_ci that action are always runtime files). 81376d528ed9Sopenharmony_ci - Have B list the action in data_deps (if the outputs of the actions are 81386d528ed9Sopenharmony_ci always runtime files). 81396d528ed9Sopenharmony_ci - Have B list the action in both deps and data deps (if the outputs might be 81406d528ed9Sopenharmony_ci used in both contexts and you don't care about unnecessary entries in the 81416d528ed9Sopenharmony_ci list of files required at runtime). 81426d528ed9Sopenharmony_ci - Split B into run-time and build-time versions with the appropriate "deps" 81436d528ed9Sopenharmony_ci for each. 81446d528ed9Sopenharmony_ci``` 81456d528ed9Sopenharmony_ci 81466d528ed9Sopenharmony_ci#### **Static libraries and source sets** 81476d528ed9Sopenharmony_ci 81486d528ed9Sopenharmony_ci``` 81496d528ed9Sopenharmony_ci The results of static_library or source_set targets are not considered 81506d528ed9Sopenharmony_ci runtime dependencies since these are assumed to be intermediate targets only. 81516d528ed9Sopenharmony_ci If you need to list a static library as a runtime dependency, you can 81526d528ed9Sopenharmony_ci manually compute the .a/.lib file name for the current platform and list it 81536d528ed9Sopenharmony_ci in the "data" list of a target (possibly on the static library target 81546d528ed9Sopenharmony_ci itself). 81556d528ed9Sopenharmony_ci``` 81566d528ed9Sopenharmony_ci 81576d528ed9Sopenharmony_ci#### **Multiple outputs** 81586d528ed9Sopenharmony_ci 81596d528ed9Sopenharmony_ci``` 81606d528ed9Sopenharmony_ci Linker tools can specify which of their outputs should be considered when 81616d528ed9Sopenharmony_ci computing the runtime deps by setting runtime_outputs. If this is unset on 81626d528ed9Sopenharmony_ci the tool, the default will be the first output only. 81636d528ed9Sopenharmony_ci``` 81646d528ed9Sopenharmony_ci### <a name="source_expansion"></a>**How Source Expansion Works** 81656d528ed9Sopenharmony_ci 81666d528ed9Sopenharmony_ci``` 81676d528ed9Sopenharmony_ci Source expansion is used for the action_foreach and copy target types to map 81686d528ed9Sopenharmony_ci source file names to output file names or arguments. 81696d528ed9Sopenharmony_ci 81706d528ed9Sopenharmony_ci To perform source expansion in the outputs, GN maps every entry in the 81716d528ed9Sopenharmony_ci sources to every entry in the outputs list, producing the cross product of 81726d528ed9Sopenharmony_ci all combinations, expanding placeholders (see below). 81736d528ed9Sopenharmony_ci 81746d528ed9Sopenharmony_ci Source expansion in the args works similarly, but performing the placeholder 81756d528ed9Sopenharmony_ci substitution produces a different set of arguments for each invocation of the 81766d528ed9Sopenharmony_ci script. 81776d528ed9Sopenharmony_ci 81786d528ed9Sopenharmony_ci If no placeholders are found, the outputs or args list will be treated as a 81796d528ed9Sopenharmony_ci static list of literal file names that do not depend on the sources. 81806d528ed9Sopenharmony_ci 81816d528ed9Sopenharmony_ci See "gn help copy" and "gn help action_foreach" for more on how this is 81826d528ed9Sopenharmony_ci applied. 81836d528ed9Sopenharmony_ci``` 81846d528ed9Sopenharmony_ci 81856d528ed9Sopenharmony_ci#### **Placeholders** 81866d528ed9Sopenharmony_ci 81876d528ed9Sopenharmony_ci``` 81886d528ed9Sopenharmony_ci This section discusses only placeholders for actions. There are other 81896d528ed9Sopenharmony_ci placeholders used in the definition of tools. See "gn help tool" for those. 81906d528ed9Sopenharmony_ci 81916d528ed9Sopenharmony_ci {{source}} 81926d528ed9Sopenharmony_ci The name of the source file including directory (*). This will generally 81936d528ed9Sopenharmony_ci be used for specifying inputs to a script in the "args" variable. 81946d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "../../foo/bar/baz.txt" 81956d528ed9Sopenharmony_ci 81966d528ed9Sopenharmony_ci {{source_file_part}} 81976d528ed9Sopenharmony_ci The file part of the source including the extension. 81986d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "baz.txt" 81996d528ed9Sopenharmony_ci 82006d528ed9Sopenharmony_ci {{source_name_part}} 82016d528ed9Sopenharmony_ci The filename part of the source file with no directory or extension. This 82026d528ed9Sopenharmony_ci will generally be used for specifying a transformation from a source file 82036d528ed9Sopenharmony_ci to a destination file with the same name but different extension. 82046d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "baz" 82056d528ed9Sopenharmony_ci 82066d528ed9Sopenharmony_ci {{source_dir}} 82076d528ed9Sopenharmony_ci The directory (*) containing the source file with no trailing slash. 82086d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "../../foo/bar" 82096d528ed9Sopenharmony_ci 82106d528ed9Sopenharmony_ci {{source_root_relative_dir}} 82116d528ed9Sopenharmony_ci The path to the source file's directory relative to the source root, with 82126d528ed9Sopenharmony_ci no leading "//" or trailing slashes. If the path is system-absolute, 82136d528ed9Sopenharmony_ci (beginning in a single slash) this will just return the path with no 82146d528ed9Sopenharmony_ci trailing slash. This value will always be the same, regardless of whether 82156d528ed9Sopenharmony_ci it appears in the "outputs" or "args" section. 82166d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "foo/bar" 82176d528ed9Sopenharmony_ci 82186d528ed9Sopenharmony_ci {{source_gen_dir}} 82196d528ed9Sopenharmony_ci The generated file directory (*) corresponding to the source file's path. 82206d528ed9Sopenharmony_ci This will be different than the target's generated file directory if the 82216d528ed9Sopenharmony_ci source file is in a different directory than the BUILD.gn file. 82226d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "gen/foo/bar" 82236d528ed9Sopenharmony_ci 82246d528ed9Sopenharmony_ci {{source_out_dir}} 82256d528ed9Sopenharmony_ci The object file directory (*) corresponding to the source file's path, 82266d528ed9Sopenharmony_ci relative to the build directory. this us be different than the target's 82276d528ed9Sopenharmony_ci out directory if the source file is in a different directory than the 82286d528ed9Sopenharmony_ci build.gn file. 82296d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "obj/foo/bar" 82306d528ed9Sopenharmony_ci 82316d528ed9Sopenharmony_ci {{source_target_relative}} 82326d528ed9Sopenharmony_ci The path to the source file relative to the target's directory. This will 82336d528ed9Sopenharmony_ci generally be used for replicating the source directory layout in the 82346d528ed9Sopenharmony_ci output directory. This can only be used in actions and bundle_data 82356d528ed9Sopenharmony_ci targets. It is an error to use in process_file_template where there is no 82366d528ed9Sopenharmony_ci "target". 82376d528ed9Sopenharmony_ci "//foo/bar/baz.txt" => "baz.txt" 82386d528ed9Sopenharmony_ci``` 82396d528ed9Sopenharmony_ci 82406d528ed9Sopenharmony_ci#### **(*) Note on directories** 82416d528ed9Sopenharmony_ci 82426d528ed9Sopenharmony_ci``` 82436d528ed9Sopenharmony_ci Paths containing directories (except the source_root_relative_dir) will be 82446d528ed9Sopenharmony_ci different depending on what context the expansion is evaluated in. Generally 82456d528ed9Sopenharmony_ci it should "just work" but it means you can't concatenate strings containing 82466d528ed9Sopenharmony_ci these values with reasonable results. 82476d528ed9Sopenharmony_ci 82486d528ed9Sopenharmony_ci Details: source expansions can be used in the "outputs" variable, the "args" 82496d528ed9Sopenharmony_ci variable, and in calls to "process_file_template". The "args" are passed to a 82506d528ed9Sopenharmony_ci script which is run from the build directory, so these directories will 82516d528ed9Sopenharmony_ci relative to the build directory for the script to find. In the other cases, 82526d528ed9Sopenharmony_ci the directories will be source- absolute (begin with a "//") because the 82536d528ed9Sopenharmony_ci results of those expansions will be handled by GN internally. 82546d528ed9Sopenharmony_ci``` 82556d528ed9Sopenharmony_ci 82566d528ed9Sopenharmony_ci#### **Examples** 82576d528ed9Sopenharmony_ci 82586d528ed9Sopenharmony_ci``` 82596d528ed9Sopenharmony_ci Non-varying outputs: 82606d528ed9Sopenharmony_ci action("hardcoded_outputs") { 82616d528ed9Sopenharmony_ci sources = [ "input1.idl", "input2.idl" ] 82626d528ed9Sopenharmony_ci outputs = [ "$target_out_dir/output1.dat", 82636d528ed9Sopenharmony_ci "$target_out_dir/output2.dat" ] 82646d528ed9Sopenharmony_ci } 82656d528ed9Sopenharmony_ci The outputs in this case will be the two literal files given. 82666d528ed9Sopenharmony_ci 82676d528ed9Sopenharmony_ci Varying outputs: 82686d528ed9Sopenharmony_ci action_foreach("varying_outputs") { 82696d528ed9Sopenharmony_ci sources = [ "input1.idl", "input2.idl" ] 82706d528ed9Sopenharmony_ci outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h", 82716d528ed9Sopenharmony_ci "{{source_gen_dir}}/{{source_name_part}}.cc" ] 82726d528ed9Sopenharmony_ci } 82736d528ed9Sopenharmony_ci Performing source expansion will result in the following output names: 82746d528ed9Sopenharmony_ci //out/Debug/obj/mydirectory/input1.h 82756d528ed9Sopenharmony_ci //out/Debug/obj/mydirectory/input1.cc 82766d528ed9Sopenharmony_ci //out/Debug/obj/mydirectory/input2.h 82776d528ed9Sopenharmony_ci //out/Debug/obj/mydirectory/input2.cc 82786d528ed9Sopenharmony_ci``` 82796d528ed9Sopenharmony_ci### <a name="switch_list"></a>**Available global switches** 82806d528ed9Sopenharmony_ci 82816d528ed9Sopenharmony_ci``` 82826d528ed9Sopenharmony_ci Do "gn help --the_switch_you_want_help_on" for more. Individual commands may 82836d528ed9Sopenharmony_ci take command-specific switches not listed here. See the help on your specific 82846d528ed9Sopenharmony_ci command for more. 82856d528ed9Sopenharmony_ci``` 82866d528ed9Sopenharmony_ci``` 82876d528ed9Sopenharmony_ci * --args: Specifies build arguments overrides. 82886d528ed9Sopenharmony_ci * --color: Force colored output. 82896d528ed9Sopenharmony_ci * --dotfile: Override the name of the ".gn" file. 82906d528ed9Sopenharmony_ci * --fail-on-unused-args: Treat unused build args as fatal errors. 82916d528ed9Sopenharmony_ci * --markdown: Write help output in the Markdown format. 82926d528ed9Sopenharmony_ci * --ninja-executable: Set the Ninja executable. 82936d528ed9Sopenharmony_ci * --nocolor: Force non-colored output. 82946d528ed9Sopenharmony_ci * -q: Quiet mode. Don't print output on success. 82956d528ed9Sopenharmony_ci * --root: Explicitly specify source root. 82966d528ed9Sopenharmony_ci * --root-target: Override the root target. 82976d528ed9Sopenharmony_ci * --runtime-deps-list-file: Save runtime dependencies for targets in file. 82986d528ed9Sopenharmony_ci * --script-executable: Set the executable used to execute scripts. 82996d528ed9Sopenharmony_ci * --threads: Specify number of worker threads. 83006d528ed9Sopenharmony_ci * --time: Outputs a summary of how long everything took. 83016d528ed9Sopenharmony_ci * --tracelog: Writes a Chrome-compatible trace log to the given file. 83026d528ed9Sopenharmony_ci * -v: Verbose logging. 83036d528ed9Sopenharmony_ci * --version: Prints the GN version number and exits. 83046d528ed9Sopenharmony_ci``` 83056d528ed9Sopenharmony_ci 8306