Ninja
The Ninja Build system is a build system that focuses on speed. Ninja can build massive software projects like Google Chrome and the Android operating system in less time than traditional build tools like GNU Make.
How Does it Work?
Ninja achieves its incredible speed by reducing a key performance factor in modern build systems—parsing user-created control files. Other compilation systems process control files (i.e. Makefiles in GNU Make) that direct the builder to process commands and calculate dependencies. These scripts can include complicated, and time-consuming decision-making logic. While this functionality gives the developer extraordinary control over the build process, it also consumes a significant portion of CPU time on complex projects.
Instead of hand-crafting control files, developers use tools like Gyp, GN, or CMake to generate Ninja control files. These files aren’t meant to be edited by hand and thus are arranged for minimal parsing and maximum execution speed.
History of the Ninja Build System
Ninja was written by Evan Martin at Google. It was released in 2012 under the Apache License 2.0 and added to GitHub in 2013. Its latest stable release is 1.10.2 on November 28th, 2020, and is actively developed by over two hundred contributors.
Try it now
Get Free LicenseFeatures and Advantages
The Ninja Build System is all about speed. In fact, the reason it’s so fast is that the focus is on speed and not features. Ninja includes the standard features you’d expect in a simple build system like GNU Make like dependency management, compiler directives, and variable support. Ninja’s performance improvements also provide additional benefits like command output buffering, making it easier to pair build directives with warnings our errors.
Ninja is free and open-source software.
How to Download and Install the Ninja Build System
Ninja is available on Debian-based systems (i.e. Debian, Ubuntu, and its derivatives) via the ninja-build package. To install, run:
apt install ninja-build
If you’re not on Debian or Ubuntu, Ninja provides pre-built binaries (https://github.com/ninja-build/ninja/releases) for Windows, Linux, and macOS. If you use these pre-built executables, no complicated install process is necessary–simply copy the file directly into a location in your path or reference the binary directly when you run it.
Ninja Build System Examples
Ninja build files are meant to be created by other utilities and not edited by hand. Nevertheless, you can construct an incredibly simple example file.
cflags = -Wall
rule cc
command = gcc $cflags -c $in -o $out
build hello: cc hello.c
Save this file as build.ninja then run the ninja command. In this example, the hello binary is produced by compiling the hello.c source code via GNU’s gcc. In such a simple example, you probably wouldn’t need to use Ninja as GNU Make or a similar build system is almost as fast.
Build systems like CMake can output Ninja control files. To do so, add the -GNinja argument to the CMake command. The resulting output named build.ninja can then be processed by running the ninja command.
Ninja vs Make
For small projects, GNU Make, or a similar build system, is likely more than sufficient. Since Ninja is built for performance, it makes sense to use it with complicated software projects with many source files. The additional step of generating a build.ninja file with another tool may add needless complexity to your project.
While Ninja’s control file structure is similar to make, Ninja contains far fewer features. This is by design so that Ninja can build projects much faster. That said, constructing Ninja build files by hand is not recommended because other build system control files allow for far more expressive build functionality.
Ninja Build System and Incredibuild
Even though Ninja Build System is fast, the compilers it invokes still take time to parse, compile, and link your source code project into a final binary build. Incredibuild drastically reduces compile times by cloning your build environment on remote machines and then executing your build in a scaled, parallel operation.
Incredibuild allows you to harness additional computing resources on network or cloud machines, effectively transforming each host into a supercomputer with hundreds of cores.
Bottom line
Though the lack of a detailed build control syntax may be a disadvantage for small projects, Ninja’s streamlined feature set offers outstanding performance. If speed is your primary concern, it’s hard to beat the Ninja Build System. Large software projects like Google Chrome enjoy greatly accelerated build processes thanks to Ninja, and your big project can benefit as well. Ninja, paired with Incredibuild, lets you scale your software compilation as much as you need, shrinking your build times, increasing developer productivity, and reducing time to market.
Start Free Trial