16d528ed9Sopenharmony_ci# GN Frequently Asked Questions
26d528ed9Sopenharmony_ci
36d528ed9Sopenharmony_ci[TOC]
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci## Where is the GN documentation?
66d528ed9Sopenharmony_ci
76d528ed9Sopenharmony_ciGN has extensive built-in help, so you can run `gn help`, but you can also see
86d528ed9Sopenharmony_ciall of the help on [the reference page](reference.md). See also the [quick
96d528ed9Sopenharmony_cistart](quick_start.md) guide and the [language and operation
106d528ed9Sopenharmony_cidetails](language.md).
116d528ed9Sopenharmony_ci
126d528ed9Sopenharmony_ci## Can I generate XCode or Visual Studio projects?
136d528ed9Sopenharmony_ci
146d528ed9Sopenharmony_ciYou can generate skeleton (or wrapper) projects for Xcode, Visual Studio,
156d528ed9Sopenharmony_ciQTCreator, and Eclipse that will list the files and targets in the build, but
166d528ed9Sopenharmony_ciuse Ninja to do the actual build. You cannot generate "real" projects that look
176d528ed9Sopenharmony_ciand compile like native ones.
186d528ed9Sopenharmony_ci
196d528ed9Sopenharmony_ciRun `gn help gen` for more details.
206d528ed9Sopenharmony_ci
216d528ed9Sopenharmony_ci## How do I do cross-compiles?
226d528ed9Sopenharmony_ci
236d528ed9Sopenharmony_ciGN has robust support for doing cross compiles and building things for
246d528ed9Sopenharmony_cimultiple architectures in a single build.
256d528ed9Sopenharmony_ci
266d528ed9Sopenharmony_ciSee [GNCrossCompiles](cross_compiles.md) for more info.
276d528ed9Sopenharmony_ci
286d528ed9Sopenharmony_ci## Can I control what targets are built by default?
296d528ed9Sopenharmony_ci
306d528ed9Sopenharmony_ciYes! If you create a group target called "default" in the top-level (root) build
316d528ed9Sopenharmony_cifile, i.e., "//:default", GN will tell Ninja to build that by default, rather
326d528ed9Sopenharmony_cithan building everything.
336d528ed9Sopenharmony_ci
346d528ed9Sopenharmony_ci## Are there any public presentations on GN?
356d528ed9Sopenharmony_ci
366d528ed9Sopenharmony_ci[There's at least one](https://docs.google.com/presentation/d/15Zwb53JcncHfEwHpnG_PoIbbzQ3GQi_cpujYwbpcbZo/edit?usp=sharing), from 2015. There
376d528ed9Sopenharmony_cihaven't been big changes since then apart from moving it to a standalone
386d528ed9Sopenharmony_cirepo, so it should still be relevant.
396d528ed9Sopenharmony_ci
406d528ed9Sopenharmony_ci## What is the order of flags and values given to the compiler?
416d528ed9Sopenharmony_ci
426d528ed9Sopenharmony_ciThe final values of compiler flags, linker flags, defines, and include
436d528ed9Sopenharmony_cidirectories are collected from various sources by GN. The ordering is defined
446d528ed9Sopenharmony_cias:
456d528ed9Sopenharmony_ci
466d528ed9Sopenharmony_ci1. Those set directly on the current target (not in a config).
476d528ed9Sopenharmony_ci2. Those set on the `configs` on the target in order that the configs appear in the list.
486d528ed9Sopenharmony_ci3. Those set on the `all_dependent_configs` on the target in order that the configs appear in the list.
496d528ed9Sopenharmony_ci4. Those set on the `public_configs` on the target in order that those configs appear in the list.
506d528ed9Sopenharmony_ci5. `all_dependent_configs` pulled from dependencies, in the order of the `deps` list. This is done recursively. If a config appears more than once, only the first occurrence will be used.
516d528ed9Sopenharmony_ci6. `public_configs` pulled from dependencies, in the order of the `deps` list. If a dependency is public, they will be applied recursively.
526d528ed9Sopenharmony_ci
536d528ed9Sopenharmony_ciIf you need a specific relative ordering of values you may need to put those
546d528ed9Sopenharmony_ciflags in a config and prepend or append that config in a way that produces the
556d528ed9Sopenharmony_cidesired result.
566d528ed9Sopenharmony_ci
576d528ed9Sopenharmony_ci## How can a target affect those that depend on it?
586d528ed9Sopenharmony_ci
596d528ed9Sopenharmony_ciThe main way that information flows up the dependency graph is via
606d528ed9Sopenharmony_ci`public_configs`. This allows a target to add preprocessor defines, compiler
616d528ed9Sopenharmony_ciflags, and linker flags to targets that depend on it. A typical example is a
626d528ed9Sopenharmony_cilibrary that requires `include_dirs` and `defines` to be set in a certain way
636d528ed9Sopenharmony_cifor its headers to work. It would put its values in a config:
646d528ed9Sopenharmony_ci
656d528ed9Sopenharmony_ci```
666d528ed9Sopenharmony_ciconfig("icu_config") {
676d528ed9Sopenharmony_ci  include_dirs = [ "//third_party/icu" ]
686d528ed9Sopenharmony_ci  defines = [ "U_USING_ICU_NAMESPACE=0" ]
696d528ed9Sopenharmony_ci}
706d528ed9Sopenharmony_ci```
716d528ed9Sopenharmony_ci
726d528ed9Sopenharmony_ciThe library would then reference that as a `public_config` which will apply it
736d528ed9Sopenharmony_cito any target that directly depends on the `icu` target:
746d528ed9Sopenharmony_ci
756d528ed9Sopenharmony_ci```
766d528ed9Sopenharmony_cishared_library("icu") {
776d528ed9Sopenharmony_ci  sources = [ ... ]
786d528ed9Sopenharmony_ci  deps = [ ... ]
796d528ed9Sopenharmony_ci
806d528ed9Sopenharmony_ci  public_configs = [ ":icu_config" ]  # Label of config defined above.
816d528ed9Sopenharmony_ci}
826d528ed9Sopenharmony_ci```
836d528ed9Sopenharmony_ci
846d528ed9Sopenharmony_ciA `public_config` applies only to direct dependencies of the target. If a target
856d528ed9Sopenharmony_ciwants to "republish" the `public_configs` from its dependencies, it would list
866d528ed9Sopenharmony_cithose dependencies in its `public_deps`. In this example, a "browser" library
876d528ed9Sopenharmony_cimight use ICU headers in its own headers, so anything that depends on it also
886d528ed9Sopenharmony_cineeds to get the ICU configuration:
896d528ed9Sopenharmony_ci
906d528ed9Sopenharmony_ci```
916d528ed9Sopenharmony_cishared_library("browser") {
926d528ed9Sopenharmony_ci  ...
936d528ed9Sopenharmony_ci
946d528ed9Sopenharmony_ci  # Anything that depends on this "browser" library will also get ICU's settings.
956d528ed9Sopenharmony_ci  public_deps = [ "//third_party/icu" ]
966d528ed9Sopenharmony_ci}
976d528ed9Sopenharmony_ci```
986d528ed9Sopenharmony_ci
996d528ed9Sopenharmony_ciAnother way apply settings up the dependency graph is with
1006d528ed9Sopenharmony_ci`all_dependent_configs` which works like `public_configs` except that it is
1016d528ed9Sopenharmony_ciapplied to all dependent targets regardless of `deps`/`public_deps`. Use of this
1026d528ed9Sopenharmony_cifeature is discouraged because it is easy to accumulate lots of unnecessary
1036d528ed9Sopenharmony_cisettings in a large project. Ideally all targets can define which information
1046d528ed9Sopenharmony_citheir dependencies need and can control this explicitly with `public_deps`.
1056d528ed9Sopenharmony_ci
1066d528ed9Sopenharmony_ciThe last way that information can be collected across the dependency graph is
1076d528ed9Sopenharmony_ciwith the metadata feature. This allows data (see `gn help metadata`) to be
1086d528ed9Sopenharmony_cicollected from targets to be written to disk (see `gn help generated_file`) for
1096d528ed9Sopenharmony_cithe build to use. Computed metadata values are written after all BUILD.gn files
1106d528ed9Sopenharmony_ciare processed and are not available to the GN script.
1116d528ed9Sopenharmony_ci
1126d528ed9Sopenharmony_ciSometimes people want to write conditional GN code based on values of a
1136d528ed9Sopenharmony_cidependency. This is not possible: GN has no defined order for loading BUILD.gn
1146d528ed9Sopenharmony_cifiles (this allows pararellism) so GN may not have even loaded the file
1156d528ed9Sopenharmony_cicontaining a dependency when you might want information about it. The only way
1166d528ed9Sopenharmony_ciinformation flows around the dependency graph is if GN itself is propagating
1176d528ed9Sopenharmony_cithat data after the targets are defined.
1186d528ed9Sopenharmony_ci
1196d528ed9Sopenharmony_ci## How can a target affect its dependencies?
1206d528ed9Sopenharmony_ci
1216d528ed9Sopenharmony_ciSometimes you might have a dependency graph **A � Z** or a longer chain **A � B
1226d528ed9Sopenharmony_ci� C � Z** and want to control some aspect of **Z** when used from **A**. This is
1236d528ed9Sopenharmony_cinot possible in GN: information only flows up the dependency chain.
1246d528ed9Sopenharmony_ci
1256d528ed9Sopenharmony_ciEvery label in GN is compiled once per _toolchain_. This means that every target
1266d528ed9Sopenharmony_cithat depends on **B** gets the same version of **B**. If you need different
1276d528ed9Sopenharmony_civariants of **B** there are only two options:
1286d528ed9Sopenharmony_ci
1296d528ed9Sopenharmony_ci1. Explicitly define two similar but differently named targets encoding the
1306d528ed9Sopenharmony_civariations you need. This can be done without specifying everything twice using
1316d528ed9Sopenharmony_cia template or by writing things like the sources to a variable and using that
1326d528ed9Sopenharmony_civariable in each version of the target.
1336d528ed9Sopenharmony_ci
1346d528ed9Sopenharmony_ci2. Use different toolchains. This is commonly used to encode "host" versus
1356d528ed9Sopenharmony_ci"target" differences or to compile parts of a project with something like ASAN.
1366d528ed9Sopenharmony_ciIt is possible to use toolchains to encode any variation you might desire but
1376d528ed9Sopenharmony_cithis can be difficult to manage and might be impossible or discoraged depending
1386d528ed9Sopenharmony_cion how your project is set up (Chrome and Fuchsia use toolchains for specific
1396d528ed9Sopenharmony_cipurposes only).
1406d528ed9Sopenharmony_ci
1416d528ed9Sopenharmony_ci## How can I recursively copy a directory as a build step?
1426d528ed9Sopenharmony_ci
1436d528ed9Sopenharmony_ciSometimes people want to write a build action that expresses copying all files
1446d528ed9Sopenharmony_ci(possibly recursively, possily not) from a source directory without specifying
1456d528ed9Sopenharmony_ciall files in that directory in a BUILD file. This is not possible to express:
1466d528ed9Sopenharmony_cicorrect builds must list all inputs. Most approaches people try to work around
1476d528ed9Sopenharmony_cithis break in some way for incremental builds, either the build step is run
1486d528ed9Sopenharmony_cievery time (the build is always "dirty"), file modifications will be missed, or
1496d528ed9Sopenharmony_cifile additions will be missed.
1506d528ed9Sopenharmony_ci
1516d528ed9Sopenharmony_ciOne thing people try is to write an action that declares an input directory and
1526d528ed9Sopenharmony_cian output directory and have it copy all files from the source to the
1536d528ed9Sopenharmony_cidestination. But incremental builds are likely going to be incorrect if you do
1546d528ed9Sopenharmony_cithis. Ninja determines if an output is in need of rebuilding by comparing the
1556d528ed9Sopenharmony_cilast modified date of the source to the last modified date of the destination.
1566d528ed9Sopenharmony_ciSince almost no filesystems propagate the last modified date of files to their
1576d528ed9Sopenharmony_cidirectory, modifications to files in the source will not trigger an incremental
1586d528ed9Sopenharmony_cirebuild.
1596d528ed9Sopenharmony_ci
1606d528ed9Sopenharmony_ciBeware when testing this: most filesystems update the last modified date of the
1616d528ed9Sopenharmony_ciparent directory (but not recursive parents) when adding to or removing a file
1626d528ed9Sopenharmony_cifrom that directory so this will appear to work in many cases. But no modern
1636d528ed9Sopenharmony_ciproduction filesystems propagate modification times of the contents of the files
1646d528ed9Sopenharmony_cito any directories because it would be very slow. The result will be that
1656d528ed9Sopenharmony_cimodifications to the source files will not be reflected in the output when doing
1666d528ed9Sopenharmony_ciincremental builds.
1676d528ed9Sopenharmony_ci
1686d528ed9Sopenharmony_ciAnother thing people try is to write all of the source files to a "depfile" (see
1696d528ed9Sopenharmony_ci`gn help depfile`) and to write a single stamp file that tracks the modified
1706d528ed9Sopenharmony_cidate of the output. This approach also may appear to work but is subtly wrong:
1716d528ed9Sopenharmony_cithe additions of new files to the source directory will not trigger the build
1726d528ed9Sopenharmony_cistep and that addition will not be reflected in an incremental build.
1736d528ed9Sopenharmony_ci
1746d528ed9Sopenharmony_ci## How can I reference all files in a directory (glob)?
1756d528ed9Sopenharmony_ci
1766d528ed9Sopenharmony_ciSometimes people want to automatically refer to all files in a directory,
1776d528ed9Sopenharmony_citypically something like `"*.cc"` for the sources. This is called a "glob."
1786d528ed9Sopenharmony_ci
1796d528ed9Sopenharmony_ciGlobs are not supported in GN. In order for Ninja to know when to re-run
1806d528ed9Sopenharmony_ciGN, it would need to check the directory modification times of any
1816d528ed9Sopenharmony_cidirectories being globbed. Directory modification times that reflect
1826d528ed9Sopenharmony_ciadditions and removals of files are not as reliably implemented across
1836d528ed9Sopenharmony_ciplatforms and filesystems as file modification times (for example, it is
1846d528ed9Sopenharmony_cinot supported on Windows FAT32 drives).
1856d528ed9Sopenharmony_ci
1866d528ed9Sopenharmony_ciEven if directory modification times work properly on your build systems,
1876d528ed9Sopenharmony_ciGN's philosophy prefers a very explicit build specification style that
1886d528ed9Sopenharmony_ciis contrary to globs.
1896d528ed9Sopenharmony_ci
1906d528ed9Sopenharmony_ci## Why does "gn check" complain about conditionally included headers?
1916d528ed9Sopenharmony_ci
1926d528ed9Sopenharmony_ciThe "gn check" feature (see `gn help check`) validates that the source code's
1936d528ed9Sopenharmony_ciuse of header files follows the requirements set up in the build. It can be a
1946d528ed9Sopenharmony_civery useful tool for ensuring build correctness.
1956d528ed9Sopenharmony_ci
1966d528ed9Sopenharmony_ciGN scans the source code for `#include` directives and checks that the included
1976d528ed9Sopenharmony_cifiles are allowed given the specification of the build. But it is relatively
1986d528ed9Sopenharmony_cisimplistic and does not understand the preprocessor. This means that some
1996d528ed9Sopenharmony_ciheaders that are correctly included for a different build variant might be
2006d528ed9Sopenharmony_ciflagged by GN. To disable checking of an include, append a "nogncheck"
2016d528ed9Sopenharmony_ciannotation to the include line:
2026d528ed9Sopenharmony_ci
2036d528ed9Sopenharmony_ci```
2046d528ed9Sopenharmony_ci#if defined(OS_ANDROID)
2056d528ed9Sopenharmony_ci#include "src/android/foo/bar.h"  // nogncheck
2066d528ed9Sopenharmony_ci#endif
2076d528ed9Sopenharmony_ci```
2086d528ed9Sopenharmony_ci
2096d528ed9Sopenharmony_ciCorrectly handling these cases requires a full preprocessor implementation
2106d528ed9Sopenharmony_cibecause many preprocessor conditions depend on values set by other headers.
2116d528ed9Sopenharmony_ciImplementing this would require new code and complexity to define the toolchain
2126d528ed9Sopenharmony_ciand run the preprocessor, and also means that a full build be done before doing
2136d528ed9Sopenharmony_cithe check (since some headers will be generated at build-time). So far, the
2146d528ed9Sopenharmony_cicomplexity and disadvantages have outweighed the advantages of a perfectly
2156d528ed9Sopenharmony_cicorrect "gn check" implementation.
2166d528ed9Sopenharmony_ci
2176d528ed9Sopenharmony_ci## Why does "gn check" miss my header?
2186d528ed9Sopenharmony_ci
2196d528ed9Sopenharmony_ciThe "gn check" feature (see previous question) only checks for headers that have
2206d528ed9Sopenharmony_cibeen declared in the current toolchain. So if your header never appears in a
2216d528ed9Sopenharmony_ci`sources` or `public` list, any file will be able to include it without "gn
2226d528ed9Sopenharmony_cicheck" failing. As a result, targets should always list all headers they contain
2236d528ed9Sopenharmony_cieven though listing them does not affect the build.
2246d528ed9Sopenharmony_ci
2256d528ed9Sopenharmony_ciSometimes a feature request is made to flag unknown headers so that people will
2266d528ed9Sopenharmony_ciknow they should be added to the build. But the silent omission of headers
2276d528ed9Sopenharmony_cioutside of the current toolchain is an important feature that limits the
2286d528ed9Sopenharmony_cinecessity of "nogncheck" annotations (see previous question).
2296d528ed9Sopenharmony_ci
2306d528ed9Sopenharmony_ciIn a large project like Chrome, many platform-specific headers will only be
2316d528ed9Sopenharmony_cidefined in that platform's build (for example, Android-specific headers would
2326d528ed9Sopenharmony_cionly be listed in the build when compiling for Android). Because the checking
2336d528ed9Sopenharmony_cidoesn't understand the preprocessor, checking unknown files would flag uses of
2346d528ed9Sopenharmony_cithese headers even if they were properly guarded by platform conditionals. By
2356d528ed9Sopenharmony_ciignoring headers outside of the current toolchain, the "nogncheck" annotations
2366d528ed9Sopenharmony_cican be omitted for most platform-specific files.
237