GN is chromium's build system and is to my knowledge the best build system for c/c++ projects.
It is not very well known for couple reasons, first because there is no starter kit availlable, gn being tied to chromium by default and also because of lack of tutorials/articles. This page provides the couple steps to get started for stand alone projects.
Besides a supported c/c++ compiler (gcc/msvc/clang), using GN requires your environment to have ninja, python 3 and gn binaries accessible from the PATH. Download links: Ninja, GN, python.
I'd recommend also to use a vscode extension to edit/format .gn
file: https://marketplace.visualstudio.com/items?itemName=npclaudiu.vscode-gn
GN stands for generate ninja, it's purpose is to sync ninja scripts to your project, but this involves multiple platform dependent tasks and configurations, defined across various scripts which aren't part of gn itself. This is where the //build
directory steps in.
The //build
directory from chromium is quite massive and too specific to it.
Luckily @timniederhausen maintains a lighter build directory which covers most needs, which will be used here.
If this is a new project:
mkdir my_project
cd my_project
git init
A GN project requires one .gn
file located in the root directory, this file is used by GN to identify the root directory from any subdirectory, and contains also couple configuration values, especially to locate the BUILDCONFIG.gn
file. A default .gn
can be created using the following command:
echo build_config="//build/config/BUILDCONFIG.gn" > .gn
git add .gn
git commit -m "Added .gn file"
Next, a //build
directory containing all the configs and toolchain scripts is needed. The one from chromium is quite massive and too specific to it, luckily @timniederhausen maintains a standalone build directory which covers most needs, which we are using here. To add the build directory:
git subtree add --prefix build https://github.com/timniederhausen/gn-build.git master --squash
At this point the project folder now contains the .gn
file and the //build
directory, and is almost ready to build. It is now time to add a BUILD.gn
file which describes the projects.
executable("hello_world")
{
sources = ["src/main.cpp"]
}
Once you've added /src/main.cpp
, you can call gn to build the ninja scripts (here in out/default
folder):
gn gen out/default
Your GN/ninja environment is now ready, call ninja to build:
ninja -C out/default
If any change were made to your project in BUILD.gn
, ninja will detect them and will call gn gen
for you.
For a basic example with static/dynamic libraries dependencies, have a look here :https://github.com/timniederhausen/gn-build/tree/testsrc