Skip to content

Instantly share code, notes, and snippets.

@raizam
Last active February 2, 2020 00:59
Show Gist options
  • Save raizam/3fea3d254c3a78fdfb35ec460eee5516 to your computer and use it in GitHub Desktop.
Save raizam/3fea3d254c3a78fdfb35ec460eee5516 to your computer and use it in GitHub Desktop.

Getting started with GN

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.

Preriquisites

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

The /build directory

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.

Quick start

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.

Next steps

For a basic example with static/dynamic libraries dependencies, have a look here :https://github.com/timniederhausen/gn-build/tree/testsrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment