Let's talk about Hongmeng's build system

The development of large-scale software systems is inseparable from a powerful construction system. What kind of construction system does Hongmeng System use? This article will demystify Open Harmoney's build system.

Let me state in advance that the build system of the Open Harmony system discussed here is not the build system used in the development of Harmony apps, that is, a tool to build a Harmony system image from source code.

Introduction to build systems

In my development career over the years, I have come into contact with many software build systems. Why do you need a build system to develop software?

Recall that we just started learning programming, usually the code size is relatively small, and the source code written is only a few files. For example, the classic code of programming entry, write it into the helloworld.c file. To compile this code, just execute the following command:

gcc helloworld.c

After compilation, an executable program of a.out will be generated. The program is a little more complicated. We can add some parameters on the command line and complete the construction of the program.

However, when the scale of the software gradually increases, the code may increase to dozens of source code files (for large system software such as Open Harmoney and Android, the source code may be as high as one million lines), and with the division of modules, some of them need to be compiled into Some static libraries need to be compiled into dynamic libraries, and finally linked into executable code. At this time, the command line method is stretched. When it comes to cross-platform development, system customization, etc., a powerful build system is essential.

The name of GNU Make, the master of the build system, must not be rejected by anyone. GNU Make defines target dependencies and compilation rules through Makefile, and can also call scripts to complete file copying, packaging and other tasks. It is quite powerful and is still the mainstream software construction tool on Unix-like systems.

However, GNU Makefiles are extremely tedious, tedious, and error-prone to write. And when it comes to multi-platform development, GNU Make is also somewhat incapable. For example, for Windows development, IDEs such as Visual Studio are usually used, and project files such as sln or proj are used.

In order to solve these problems, tools for generating Makefiles , such as cmake, AutoMake , etc., came into being. This build system is called a meta-build system.

For Linux users, the following process for building software from source should be familiar:

./configure
make
make install

The first step is to call the AutoTool tool to generate a GNU Makefile according to the system environment (there are many versions of Linux, and the installation of software development kits is also different). The second step is to start the construction process of GNU Make, and the third step is to install the program to the directory specified by the system according to the rules defined by the Makefile.

AutoTool tools are usually limited to use in Linux-like systems. CMake goes a step further. It can not only generate GNU Makefiles, but also VS project files to solve the problem of cross-platform software development.

It can be seen that the construction system gradually develops with the increase of software scale and the complexity of requirements, and the major software development teams have also developed various construction systems, among which Google engineers are the most tossed.

The Android system used the GNU Make build system in the early days, but with the superb skills of Google engineers, the writing of Android.mk has been greatly simplified. If we look at Android.mk alone, we might not realize that we are writing a GNU Makefile. But starting with Android 7.0 (Nougat), the Soong build system was introduced.

The browser engine project Chromium uses another build system.

In my early exposure to the chromium open source project, chromium used the GYP (Generate Your Projects) build system, which is also a meta-build system. Software engineers write construction project files (usually suffixed with gyp, gypi) according to GYP rules, and GYP tools generate GNU Makefiles based on gyp files.

Then the chromium project built a Ninja build system, but this Ninja is not used to replace GYP, but to replace GNU make. According to Google's official statement, the speed has been improved several times. For developers, there is no need to go into build systems like Ninja or GNU Makefile because it's just an intermediate output.

Later, Google moved to GYP and replaced it with the GN build system. The GN build system is still a meta-build system for quickly generating Ninja build files.

The source code of the GN build system is located at: https://gn.googlesource.com/gn, which needs to be accessed over the wall. If it only builds software, you can download depot_tools, which contains a complete set of build tools such as GN and Ninja.

Hongmeng's build system

A few days ago, I tossed about the system compilation of Open Harmony 2.0 source code, and by the way, I studied the construction system of Open Harmony, and found that it is a familiar old friend. Yes, the Hongmeng system uses the GN build system developed by Google. Why choose the GN build system? According to Google's official website, the GN build system has the following advantages:

  • Better readability, easier to write and maintain.

  • Faster, Google's official data is 20 times faster.

  • After modifying the GN file, the Ninja build file is automatically updated when a ninja build is performed.

  • Simpler module dependencies, providing public_deps, data_deps, etc.

  • Provides better tools to query the module dependency graph.

  • Better debugging support.

It is a wise choice for Hongmeng to adopt the GN build system. This build system has been verified by a large-scale software system such as chromium (the code scale is no less than that of the Android system), and has proven to be reliable, fast, flexible and simple to write. There is a famous saying in the software development world: don't reinvent the wheel, we don't need to develop everything from scratch.

Of course, a careful analysis of Open Harmony 2.0's build scripts reveals a mix of Bash scripts, Python scripts, and the GN build system. As far as software development is concerned, whatever tool is in your hand, just use it, and practicality comes first.

At present, there is very little information about the GN build system, you can refer to the following documents:

  1. GN build system in chromium

  2. GN Quick Start guide

  3. GN Language and Operation

The writing of GN files is actually not complicated. You can master them quickly by referring to the existing GN files. We will not expand them here.

In a later article, I'll dive into writing GN build files, so stay tuned!

Guess you like

Origin blog.csdn.net/mogoweb/article/details/118036073