>

#### Introduction --- As stated in the [official site][], Ninja is a small build system with a focus on speed.

There are multiplay ways to install ninja on our system, the simplest way is to download Ninja binary from here and place it in a system PATH.

This article introduces how to build ninja from source.
There are some dependencies before we can build ninja from source, the dependencies are: graphviz, gtest, git, re2c and python2.7+.


#### Fetch the source --- Once we have install these dependencies, follow the below steps:
$ git clone git://github.com/ninja-build/ninja.git && cd ninja
$ git checkout release
$ cat README


$ ls
COPYING  HACKING.md  README  RELEASING  bootstrap.py  configure.py  doc/  misc/  src/

For more details see HACKING.md.


#### Build ninja --- To build, it is sufficient to just run:
$ ./configure.py --bootstrap

The above command will generate a binary Ninja in current directory, installation is not necessary because the Ninja binary is all we need, there is not extra installation process, the above command is all done and complete, we can place the binary into any directory that are in system path environment. (e.g. copy ninja to /usr/bin to make it globally accessible)

More options

–bootstrap bootstrap a ninja binary from nothing
–verbose enable verbose build
–platform choose known platforms
–host choose host known_platforms
–debug enable debugging extras
–profile enable profiling
–with-gtest
–with-python use EXE as the Python interpreter
–force-pselect ppoll() is used by default where available

Note that, if we to enable features like Bash completion or Emacs and Vim editing modes, some files in misc/ must be copied to appropriate locations.


#### Test ninja ---- Before we test ninja, we can take a glance at `ninja.build` file, this is a file similar to Makefile, it declares build rules according to what we need. For example, it declares rules **`all`**, and **`ninja_test`**.
build ninja_test: link $builddir/build_log_test.o $builddir/build_test.o $
    $builddir/clean_test.o $builddir/clparser_test.o $
    $builddir/depfile_parser_test.o $builddir/deps_log_test.o $
    $builddir/disk_interface_test.o $builddir/edit_distance_test.o $
    $builddir/graph_test.o $builddir/lexer_test.o $
    $builddir/manifest_parser_test.o $builddir/ninja_test.o $
    $builddir/state_test.o $builddir/subprocess_test.o $builddir/test.o $
    $builddir/util_test.o | $builddir/libninja.a 
  libs = -lninja

build all: phony ninja ninja_test build_log_perftest canon_perftest $
    depfile_parser_perftest hash_collision_bench manifest_parser_perftest

In this case, we can simply run ./ninja ninja_test to generate a executable binary ninja_test, and we run ./ninja_test to finish the test.

$ ./ninja_test
[214/226] SubprocessTest.SetWithLotsRaise [ulimit -n] above 1025 (currently 1024) to make this test go
[226/226] ElideMiddle.ElideInTheMiddle
passed

also, we can run:

$ ./ninja all
[10/10] LINK canon_perftest

after all test is done, we copy the ninja to a system path to finish the installation

$ sudo cp ninja /usr/bin

#### build.ninja --- **build.ninja** has similar rules to Makefile, we can write it manually after we get a bit grasp of its syntax, or we can use a simple [Python based generator][] to generate it.

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
from ninja_syntax import Writer

with open("build.ninja", "w") as buildfile:
n = Writer(buildfile)

if platform.is_msvc():
n.rule("link",
command="$cxx $in $libs /nologo /link $ldflags /out:$out",
description="LINK $out")
else:
n.rule("link",
command="$cxx $ldflags -o $out $in $libs",
description="LINK $out")

Besides, Ninja is already supported by some of the most popular meta build systems like CMake and Gyp. If we have a CMake based project and Ninja is also available in system PATH, all we need to do is to choose Ninja as the generator:

$ cd myproj
$ mkdir build && cd build
$ cmake -GNinja ../
$ ninja

When we execute ninja to build the project, there’s no need to specify the number of parallel jobs (-j [jobs]) because Ninja automatically chooses the value based on the number of cores available.





Reference

Ninja, a small build system with a focus on speed
The Performance of Open Source Software | Ninja
Ninja, a new build system