| OLD | NEW |
| 1 GN has several different ways to check dependencies. Many of them are checked by
the `gn check` command. Running checks involve opening and scanning all source
files so this isn't run every time a build is updated. To run check on an existi
ng build: | 1 # GN Check |
| 2 ``` | |
| 3 gn check out/mybuild | |
| 4 ``` | |
| 5 | 2 |
| 6 To run the check as part of the "gen" command to update the build (this is what
the bots do): | 3 GN has several different ways to check dependencies. Many of them are checked by |
| 7 ``` | 4 the `gn check` command. Running checks involve opening and scanning all source |
| 8 gn gen out/mybuild --check | 5 files so this isn't run every time a build is updated. To run check on an |
| 9 ``` | 6 existing build: |
| 10 | 7 |
| 11 # Concepts | 8 gn check out/mybuild |
| 12 | 9 |
| 13 ## Visibility | 10 To run the check as part of the "gen" command to update the build (this is what |
| 11 the bots do): |
| 14 | 12 |
| 15 Targets can control which other targets may depend on them by specifying `visibi
lity`. Visibility is always checked when running any GN command (not just `gn ch
eck`. | 13 gn gen out/mybuild --check |
| 16 | 14 |
| 17 By default, targets are "public" meaning any target can depend on them. If you s
upply a list, visibility will be listed to those targets (possibly including wil
dcards): | 15 [TOC] |
| 16 |
| 17 ## Concepts |
| 18 |
| 19 ### Visibility |
| 20 |
| 21 Targets can control which other targets may depend on them by specifying |
| 22 `visibility`. Visibility is always checked when running any GN command (not just |
| 23 `gn check`. |
| 24 |
| 25 By default, targets are "public" meaning any target can depend on them. If you |
| 26 supply a list, visibility will be listed to those targets (possibly including |
| 27 wildcards): |
| 18 | 28 |
| 19 ``` | 29 ``` |
| 20 visibility = [ | 30 visibility = [ |
| 21 ":*", # All targets in this file. | 31 ":*", # All targets in this file. |
| 22 "//content/*", # All targets in content and any subdirectory thereof. | 32 "//content/*", # All targets in content and any subdirectory thereof. |
| 23 "//tools:doom_melon", # This specific target. | 33 "//tools:doom_melon", # This specific target. |
| 24 ] | 34 ] |
| 25 ``` | 35 ``` |
| 26 | 36 |
| 27 See `gn help visibility` for more details and examples. | 37 See `gn help visibility` for more details and examples. |
| 28 | 38 |
| 29 ## Public header files | 39 ### Public header files |
| 30 | 40 |
| 31 Targets can control which headers may be included by dependent targets so as to
define a public API. If your target specifies only `sources`, then all headers l
isted there are public and can be included by all dependents. | 41 Targets can control which headers may be included by dependent targets so as to |
| 42 define a public API. If your target specifies only `sources`, then all headers |
| 43 listed there are public and can be included by all dependents. |
| 32 | 44 |
| 33 If your target defines a `public` variable, only the files listed in that list w
ill be public. Files in `sources` but not `public` (they can be in both or only
one) may not be included by dependent targets. | 45 If your target defines a `public` variable, only the files listed in that list |
| 46 will be public. Files in `sources` but not `public` (they can be in both or only |
| 47 one) may not be included by dependent targets. |
| 34 | 48 |
| 35 ``` | 49 ``` |
| 36 source_set("foo") { | 50 source_set("foo") { |
| 37 public = [ | 51 public = [ |
| 38 "foo.h", | 52 "foo.h", |
| 39 "foo_config.h", | 53 "foo_config.h", |
| 40 ] | 54 ] |
| 41 sources = [ | 55 sources = [ |
| 42 "foo.cc", | 56 "foo.cc", |
| 43 "foo.h", | 57 "foo.h", |
| 44 "bar.cc", | 58 "bar.cc", |
| 45 "bar.h", | 59 "bar.h", |
| 46 ] | 60 ] |
| 47 } | 61 } |
| 48 ``` | 62 ``` |
| 49 | 63 |
| 50 ## Public dependencies | 64 ### Public dependencies |
| 51 | 65 |
| 52 In order to include files from your target, that target must be listed in your t
arget's dependencies. By default, transitively depending on a target doesn't giv
e your files this privilege. | 66 In order to include files from your target, that target must be listed in your |
| 67 target's dependencies. By default, transitively depending on a target doesn't |
| 68 give your files this privilege. |
| 53 | 69 |
| 54 If a target exposes a dependency as part of its public API, then it can list tha
t dependency as a `public_deps`: | 70 If a target exposes a dependency as part of its public API, then it can list |
| 71 that dependency as a `public_deps`: |
| 72 |
| 55 ``` | 73 ``` |
| 56 source_set("foo") { | 74 source_set("foo") { |
| 57 sources = [ ... ] | 75 sources = [ ... ] |
| 58 public_deps = [ | 76 public_deps = [ |
| 59 "//base", | 77 "//base", |
| 60 ] | 78 ] |
| 61 deps = [ | 79 deps = [ |
| 62 "//tools/doom_melon", | 80 "//tools/doom_melon", |
| 63 ] | 81 ] |
| 64 } | 82 } |
| 65 ``` | 83 ``` |
| 66 Targets that depend on `foo` can include files from `base` but not from `doom_me
lon`. To include public headers from `doom\_melon, a target would need to depend
directly on it. | |
| 67 | 84 |
| 68 Public dependencies work transitively, so listing a target as a public dependenc
y also exposes that target's public dependencies. Along with the ability to incl
ude headers, public dependencies forward the `public_configs` which allow settin
gs like defines and include directories to apply to dependents. | 85 Targets that depend on `foo` can include files from `base` but not from |
| 86 `doom_melon`. To include public headers from `doom\_melon, a target would need |
| 87 to depend directly on it. |
| 69 | 88 |
| 70 # Putting it all together | 89 Public dependencies work transitively, so listing a target as a public |
| 90 dependency also exposes that target's public dependencies. Along with the |
| 91 ability to include headers, public dependencies forward the `public_configs` |
| 92 which allow settings like defines and include directories to apply to |
| 93 dependents. |
| 94 |
| 95 ## Putting it all together |
| 71 | 96 |
| 72 In order to include a header from target Y in a file that is part of target X: | 97 In order to include a header from target Y in a file that is part of target X: |
| 73 | 98 |
| 74 * X must be in Y's `visibility` list (or B must have no `visibility` defined). | 99 * X must be in Y's `visibility` list (or B must have no `visibility` defined). |
| 75 * The header must be in Y's `public` headers (or Y must have no `public` varia
ble defined). | 100 * The header must be in Y's `public` headers (or Y must have no `public` |
| 76 * X must depend directly on Y, or there must be a path from X to Y following o
nly public dependencies. | 101 variable defined). |
| 102 * X must depend directly on Y, or there must be a path from X to Y following |
| 103 only public dependencies. |
| 77 | 104 |
| 78 ### What gets checked | 105 ### What gets checked |
| 79 | 106 |
| 80 Chrome currently doesn't come close to passing a `gn check` pass. You can check
specific targets or subtrees for issues: | 107 Chrome currently doesn't come close to passing a `gn check` pass. You can check |
| 81 ``` | 108 specific targets or subtrees for issues: |
| 82 gn check out/mybuild //base | |
| 83 | 109 |
| 84 gn check out/mybuild "//mojo/*" | 110 gn check out/mybuild //base |
| 85 ``` | 111 |
| 112 gn check out/mybuild "//mojo/*" |
| OLD | NEW |