GN is a meta-build system that generates NinjaBuild files so that you can build Chromium with Ninja
Adding a build file
Create a tools/gn/tutorial/BUILD.gn file and enter the following:
executable("hello_world") {
sources = [
"hello_world.cc",
]
}
There should already be a hello_world.cc file in that directory, containing what you expect. That's it! Now we just need to tell the build about this file. Open the BUILD.gn file in the root directory and add the label of this target to the dependencies of one of the root groups (a “group” target is a meta-target that is just a collection of other targets):
group("root") {
deps = [
...
"//url",
"//tools/gn/tutorial:hello_world",
]
}
You can see the label of your target is “//” (indicating the source root), followed by the directory name, a colon, and the target name.
Testing your addition
From the command line in the source root directory:
gn gen out/Default
ninja -C out/Default hello_world
out/Default/hello_world
GN encourages target names for static libraries that aren't globally unique. To build one of these, you can pass the label with no leading “//” to ninja:
ninja -C out/Default tools/gn/tutorial:hello_world
Declaring dependencies
Let's make a static library that has a function to say hello to random people. There is a source file hello.cc in that directory which has a function to do this. Open the tools/gn/tutorial/BUILD.gn file and add the static library to the bottom of the existing file:
static_library("hello") {
sources = [
"hello.cc",
]
}
Now let's add an executable that depends on this library:
executable("say_hello") {
sources = [
"say_hello.cc",
]
deps = [
":hello",
]
}
This executable includes one source file and depends on the previous static library. The static library is referenced by its label in the deps. You could have used the full label //tools/gn/tutorial:hello but if you're referencing a target in the same build file, you can use the shortcut :hello.
Test the static library version
ninja -C out/Default say_hello
out/Default/say_hello
Compiler settings
Our hello library has a new feature, the ability to say hello to two people at once. This feature is controlled by defining TWO_PEOPLE. We can add defines like so:
static_library("hello") {
sources = [
"hello.cc",
]
defines = [
"TWO_PEOPLE",
]
}
Putting settings in a config
However, users of the library also need to know about this define, and putting it in the static library target defines it only for the files there. If somebody else includes hello.h, they won't see the new definition. To see the new definition, everybody will have to define TWO_PEOPLE.
GN has a concept called a “config” which encapsulates settings. Let's create one that defines our preprocessor define:
config("hello_config") {
defines = [
"TWO_PEOPLE",
]
}
To apply these settings to your target, you only need to add the config's label to the list of configs in the target:
static_library("hello") {
...
configs += [
":hello_config",
]
}
Note that you need “+=” here instead of “=” since the build configuration has a default set of configs applied to each target that set up the default build stuff. You want to add to this list rather than overwrite it. To see the default configs, you can use the print function in the build file or the desc command-line subcommand (see below for examples of both).
Dependent configs
This nicely encapsulates our settings, but still requires everybody that uses our library to set the config on themselves. It would be nice if everybody that depends on our hello library can get this automatically. Change your library definition to:
static_library("hello") {
sources = [
"hello.cc",
]
all_dependent_configs = [
":hello_config"
]
}
Add a new build argument
You declare which arguments you accept and specify default values via declare_args.
declare_args() {
enable_teleporter = true
enable_doom_melon = false
}
Don‘t know what’s going on?
You can run GN in verbose mode to see lots of messages about what it's doing. Use -v for this.
Print debugging
There is a print command which just writes to stdout:
static_library("hello") {
...
print(configs)
}
The “desc” command
You can run gn desc to get information about a given target:
gn desc out/Default //tools/gn/tutorial:say_hello
will print out lots of exciting information. You can also print just one section. Lets say you wanted to know where your TWO_PEOPLE define came from on the say_hello target:
> gn desc out/Default //tools/gn/tutorial:say_hello defines --blame
...lots of other stuff omitted...
From //tools/gn/tutorial:hello_config
(Added by //tools/gn/tutorial/BUILD.gn:12)
TWO_PEOPLE
Performance
You can see what took a long time by running it with the --time command line flag. This will output a summary of timings for various things.
You can also make a trace of how the build files were executed:
gn --tracelog=mylog.trace
references:
https://chromium.googlesource.com/chromium/src/tools/gn/
https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/quick_start.md#Adding-a-build-file
Adding a build file
Create a tools/gn/tutorial/BUILD.gn file and enter the following:
executable("hello_world") {
sources = [
"hello_world.cc",
]
}
There should already be a hello_world.cc file in that directory, containing what you expect. That's it! Now we just need to tell the build about this file. Open the BUILD.gn file in the root directory and add the label of this target to the dependencies of one of the root groups (a “group” target is a meta-target that is just a collection of other targets):
group("root") {
deps = [
...
"//url",
"//tools/gn/tutorial:hello_world",
]
}
You can see the label of your target is “//” (indicating the source root), followed by the directory name, a colon, and the target name.
Testing your addition
From the command line in the source root directory:
gn gen out/Default
ninja -C out/Default hello_world
out/Default/hello_world
GN encourages target names for static libraries that aren't globally unique. To build one of these, you can pass the label with no leading “//” to ninja:
ninja -C out/Default tools/gn/tutorial:hello_world
Declaring dependencies
Let's make a static library that has a function to say hello to random people. There is a source file hello.cc in that directory which has a function to do this. Open the tools/gn/tutorial/BUILD.gn file and add the static library to the bottom of the existing file:
static_library("hello") {
sources = [
"hello.cc",
]
}
Now let's add an executable that depends on this library:
executable("say_hello") {
sources = [
"say_hello.cc",
]
deps = [
":hello",
]
}
This executable includes one source file and depends on the previous static library. The static library is referenced by its label in the deps. You could have used the full label //tools/gn/tutorial:hello but if you're referencing a target in the same build file, you can use the shortcut :hello.
Test the static library version
ninja -C out/Default say_hello
out/Default/say_hello
Compiler settings
Our hello library has a new feature, the ability to say hello to two people at once. This feature is controlled by defining TWO_PEOPLE. We can add defines like so:
static_library("hello") {
sources = [
"hello.cc",
]
defines = [
"TWO_PEOPLE",
]
}
Putting settings in a config
However, users of the library also need to know about this define, and putting it in the static library target defines it only for the files there. If somebody else includes hello.h, they won't see the new definition. To see the new definition, everybody will have to define TWO_PEOPLE.
GN has a concept called a “config” which encapsulates settings. Let's create one that defines our preprocessor define:
config("hello_config") {
defines = [
"TWO_PEOPLE",
]
}
To apply these settings to your target, you only need to add the config's label to the list of configs in the target:
static_library("hello") {
...
configs += [
":hello_config",
]
}
Note that you need “+=” here instead of “=” since the build configuration has a default set of configs applied to each target that set up the default build stuff. You want to add to this list rather than overwrite it. To see the default configs, you can use the print function in the build file or the desc command-line subcommand (see below for examples of both).
Dependent configs
This nicely encapsulates our settings, but still requires everybody that uses our library to set the config on themselves. It would be nice if everybody that depends on our hello library can get this automatically. Change your library definition to:
static_library("hello") {
sources = [
"hello.cc",
]
all_dependent_configs = [
":hello_config"
]
}
Add a new build argument
You declare which arguments you accept and specify default values via declare_args.
declare_args() {
enable_teleporter = true
enable_doom_melon = false
}
Don‘t know what’s going on?
You can run GN in verbose mode to see lots of messages about what it's doing. Use -v for this.
Print debugging
There is a print command which just writes to stdout:
static_library("hello") {
...
print(configs)
}
The “desc” command
You can run gn desc to get information about a given target:
gn desc out/Default //tools/gn/tutorial:say_hello
will print out lots of exciting information. You can also print just one section. Lets say you wanted to know where your TWO_PEOPLE define came from on the say_hello target:
> gn desc out/Default //tools/gn/tutorial:say_hello defines --blame
...lots of other stuff omitted...
From //tools/gn/tutorial:hello_config
(Added by //tools/gn/tutorial/BUILD.gn:12)
TWO_PEOPLE
Performance
You can see what took a long time by running it with the --time command line flag. This will output a summary of timings for various things.
You can also make a trace of how the build files were executed:
gn --tracelog=mylog.trace
references:
https://chromium.googlesource.com/chromium/src/tools/gn/
https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/quick_start.md#Adding-a-build-file
Hi,
ReplyDeletethanks for the wonderful tutorial. I have a question about chaning the c runtime library from libc to musl, can you tell me on how to do that?
thanks.
-- Living Mobile --: What Is Gn Build System? - Basics >>>>> Download Now
ReplyDelete>>>>> Download Full
-- Living Mobile --: What Is Gn Build System? - Basics >>>>> Download LINK
>>>>> Download Now
-- Living Mobile --: What Is Gn Build System? - Basics >>>>> Download Full
>>>>> Download LINK Qs