Code Monkey home page Code Monkey logo

minimal_gn's Introduction

A Minimal GN configuration

The GN build tool is used by the Chromium and Fuchsia projects (and others) in conjunction with the Ninja build tool to build software.

GN is not particularly designed to be easy to use for small projects, and it is not designed to be a "batteries included" system: you have to provide all of the rules and toolchain definitions yourself. GN is intended to be used for projects that are big enough that you'll probably want to do that anyway.

That said, it's still useful to see what a minimal GN configuration needs to be.

The smallest GN configuration needs three files:

  • The dotfile. Conventionally, the dotfile lives in the source root, known as "//", i.e., "//.gn". The dotfile can set up some project specific settings for GN, but most importantly tells GN where to file the buildconfig file.
  • The buildconfig file, which tell GN what toolchain to use by default. It can also set up other global variables.
  • A BUILD.gn file containing the actual targets to build and the toolchain definitions to use to build them.

Good style (and maintenance in practice) would usually separate out the toolchain definitions into its own file, but this project keeps them all together for simplicity.

This (almost) minimal project will build two executables, one written in C and one written in C++.

GN generates a set of Ninja files to run the build (hence the name, Generate Ninja). You cannot generate the files into the source root, but any other directory should be fine. By convention, GN uses //out or a subdirectory of //out, but you can also build into a directory outside of the source root just fine. Running gn gen will create an arg.gn file in the output directory if one doesn't already exist. You can use that file to set arguments that alter how your build works.

The generated GN files include:

  • A top-level build.ninja file, which includes all the others.
  • A build.ninja.d file containing the dependencies of the build.ninja file itself; changing any of these dependencies will cause Ninja to re-run GN to regenerate (update) the build files and then reload them before going on to build things.
  • A toolchain.ninja file for the rules the default toolchain will use and any action targets that will run in that toolchain.
  • A .ninja file for each BUILD.gn file that contains targets that are compiled as part of that BUILD.gn.

Example:

$ gn gen out
Done. Made 2 targets from 2 files in 3ms
$ find out -type f | sort
out/.ninja_log
out/args.gn
out/build.ninja
out/build.ninja.d
out/obj/hello.ninja
out/toolchain.ninja
$ ninja -C out -j 1 -v
ninja: Entering directory `out'
[0/1] $PATH_TO_GN/gn --root=./.. -q --regeneration gen .
[1/5] python3 ../generate_world.py ./gen world.cc world.h
[2/5] touch obj/world.stamp
[3/5] clang++ -MMD -MF obj/hello.o.d -Igen -o obj/hello.o -c ../hello.cc
[4/5] clang++ -MMD -MF obj/world.o.d -Igen -o obj/world.o -c gen/world.cc
[5/5] clang++ obj/hello.o obj/world.o -lc++ -o hello
$ out/hello
Hello, world.
$

That's all there is to it!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.