gn and ninja introduction

Introduction

gn and ninja are both build systems. A build system is a tool that automates the process of compiling and linking code.

  • gn: gn is a build system that is used to build the Chromium web browser and the Android operating system. It is a declarative build system, which means that it uses a text file to describe the build process.
  • ninja: ninja is a build system that is used to build large and complex applications. It is fast and efficient, and it is well-suited for building applications that are written in multiple languages.

Here is a table that compares gn and ninja:

Feature gn ninja
Build system Declarative Imperative
Languages C++, Python Multiple languages
Speed Fast Very fast
Efficiency Efficient Very efficient
Complex applications Well-suited for building complex applications Well-suited for building very complex applications

Here are some additional information about gn and ninja:

  • gn is a newer build system than ninja, but it has quickly become the standard build system for Chromium and Android. It is easy to learn and use, and it is very powerful.
  • ninja is a more mature build system than gn, but it is not as widely used. It is more complex than gn, but it is also more powerful.

gn

GN grammar is the syntax used to write gn build files. gn build files are text files that describe the build process for a project.

GN grammar is based on the YAML language. YAML is a human-readable data serialization language.

The basic structure of a GN build file is as follows:

buildconfig:
  name: "my-project"
  configs:
    default:
      # The default configuration

    release:
      # The release configuration

    development:
      # The development configuration

The buildconfig section defines the name of the project and the list of configurations. The configs section defines each configuration.

Each configuration consists of a set of properties. The properties define the build options for the configuration.

Here is an example of a GN build file:

buildconfig:
  name: "my-project"
  configs:
    default:
      # The default configuration
      target_os: "linux"
      target_cpu: "x86_64"

    release:
      # The release configuration
      target_os: "linux"
      target_cpu: "x86_64"
      optimization: "speed"

    development:
      # The development configuration
      target_os: "linux"
      target_cpu: "x86_64"
      optimization: "none"

This file defines a project called my-project with three configurations: default, release, and development.

The default configuration targets Linux and x86_64.

The release configuration targets Linux and x86_64, and it uses the speed optimization level.

The development configuration targets Linux and x86_64, and it uses the none optimization level.

For more information on GN grammar, please refer to the GN documentation: https://gn.googlesource.com/gn/+/refs/heads/master/docs/reference.md

ninja

Ninja grammar is the syntax used to write ninja build files. Ninja build files are text files that describe the build process for a project.

Ninja grammar is based on the C-like language. C-like is a programming language that is similar to C.

The basic structure of a Ninja build file is as follows:

rule <rule_name>:
  command: <command>
  description: <description>

build <target_name>:
  <rule_name> <dependencies>

The rule section defines a rule. The rule defines the command that is used to build a target.

The build section defines a target. The target specifies the dependencies of the target and the rules that are used to build the target.

Here is an example of a Ninja build file:

rule cc:
  command: gcc -c <source> -o <target>
  description: Compile C source

build my_binary:
  cc my_source.c -o my_binary

This file defines a rule called cc. The cc rule is used to compile C source files.

The build section defines a target called my_binary. The my_binary target depends on the my_source.c source file. The my_binary target is built by using the cc rule.

For more information on Ninja grammar, please refer to the Ninja documentation: https://ninja-build.org/manual.html

combine

Here are the steps on how to use GN config and generate Ninja file and then compile with Ninja:

  1. Create a GN build file. The GN build file is a text file that describes the build process for a project.
  2. Run the gn gen command to generate the Ninja build files. The gn gen command takes two arguments: the output directory and the configuration name.
  3. Run the ninja command to compile the project. The ninja command takes one argument: the output directory.

Here is an example of how to use the gn gen and ninja commands:

# Create a GN build file
touch my_project.gn

# Generate the Ninja build files for the default configuration
gn gen out/default

# Compile the project
ninja -C out/default

This will create a Ninja build file called my_project.ninja in the out/default directory. The ninja command will then compile the project in the out/default directory.

You can also use the gn command to generate the Ninja build files for a different configuration. For example, to generate the Ninja build files for the release configuration, you would run the following command:

gn gen out/release

Then, you would run the ninja command to compile the project in the out/release directory.

Ultimately, the best tool for you will depend on your specific needs.