Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic concepts and principles of bazel?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

bazel basic concept and principle is what, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

0x01 Background

Bazel has been widely used in the field of cloud computing open source software construction such as k8s, kubevirt, etc., this article to share the basic knowledge of bazel from a novice's point of view, its existence value. In contrast, how it differs from other existing build systems and what scenarios it applies to.

0x02 Build System

BUILD is the corresponding English, which is what we call compilation packaging, that is, the written code will be processed by the compiler to generate binary, forming a software package that can run normally (such as deb or rpm). For compiled languages, building is almost a part of everyday work.

It is an auxiliary process that produces no value in itself.

It is also a necessary process, without which CI is empty talk.

Based on these two characteristics, we want the ideal build system to have the following characteristics:

As quickly as possible. Can facilitate developers, write code quickly out of the package to verify changes.

Easy to maintain, can clearly show dependencies, add a module to the system, or reduce a module, can be very easy to maintain.

In contrast to ideal systems, let's look at the development of building technology. Software building technology has evolved in tandem with IT technology, through three stages: early, mid, and modern (purely personal).

I. Initial stage

The software is small in scale, and the concept of software engineering has not yet been proposed.

The requirements for construction are low, or not required. The code written can be directly executed after assembly and compilation, and there is no pipeline. The code of this period is makefile, or write script to compile and execute directly. The software is characterized by a single language, small scale, and basically no requirements for construction.

II. Medium-term

The software started growing. The existing makefile mechanism has been difficult to meet the needs. In the Linux kernel, for example, makefiles are no longer sufficient, so kernel developers add their own build extensions to the kernel to accommodate the growing kernel code. At the same time, more modern build tools like cmake have emerged. The main reason is that when the code size increases, the build process starts to consume a lot of developer energy. The tool represented by cmake can semi-automatically manage dependencies, generate makefiles, improve the productivity of compilers, and reduce duplication of labor. At this time, the build system can basically meet the requirements, but it has begun to show deficiencies.

III. Modernization

As software grew in size, large systems began to move toward multilingual collaboration. Write the lower layers (data plane) in compiled languages (C, C++, Java, etc.) that run efficiently, and write the upper layers (management plane) in dynamic non-compiled languages that develop efficiently. Multilingualism doesn't work for cmake. Modern build systems such as bazel appear to solve problems that cmake can't.

In the process of an organization's gradual growth, you will encounter the same problems that big companies experience in IT, so the driving force for understanding a technology must be the problem, not Google's production must be suitable for you. Here too, I came to know Bazel because I encountered a problem that the current technology could not solve.

[bazel](https://www.bazel.build/) Google open source build tool. It implements a build system that is close to understanding by abstracting the build process.

Fast build.

bazel supports incremental compilation, caching, and distributed extensions. These three points are sufficient for rapid construction. As long as enough hardware resources are invested, your build can be very fast.

Clear dependencies.

Makefile can also be defined. However, all the rules defined in makefile need manual maintenance, especially the dependencies scattered in multiple code repositories. It is difficult for anyone to grasp the dependency relationship globally. But bazel clearly shows the current dependencies in the form of a dependency graph. And is automatically established, can query.

In addition to the above two advantages, the most important point is bazel support for multi-language construction. The following explains how bazel models the build process by introducing bazel's basic objects (concepts).

0x03 Concept

3.1 build

The construction process is an action. There are inputs and outputs, as shown below.

There are many building processes in a large system, with the top responsible for producing the final output. There is a bottom layer responsible for the construction of a small module. There are interdependencies between different constructs. The product of A is an input to B, then B depends on A, and construction requires that A be constructed before B. These dependencies eventually emerge in large modules as a dependency net. A depends on B C D, but B also depends on C.

3.2 The main concepts in Bazel are as follows.

WORKSPACE A file that needs to be defined for each project, located in the root directory of the project. It can be an empty file or it can load some external dependencies. Build actions defined in actionrule. All performed during the operational phase. BUILD exists in each small code repository and defines the build elements in the current repository. Input, output, and build behavior. It is the smallest building block. bzl custom rule suffix. Additional rulebases for the external rulebazel convention, written in skylark (a subset of Python). Rulebazel's build rules are located in the BUILD file. Each rule contains inputs and outputs, as well as build actions. Currently, there are mature building rule libraries such as C/C++, Java, Golang, Python, etc., which can be easily obtained from github. See the official documentation for a system object defined in the BUILD file for the existing rulebase package. Packages have visibility properties that you can use to control what properties are exposed to the outside world. Build graph Build dependency graph, which is the dependency graph mentioned earlier. Consists of targets defined in individual BUILD files. 0x04 Principle

Now that you have the basics, explain how bazel works. bazel works in client/server mode, and the server automatically exits after a period of inactivity. Model the entire build process in WORKSPACE[#WORKSPACE] and BUILD files. bazel works in three phases. This is the first time I've seen it. What happened next.

loading phase loading phase

bazel traverses all subfolders of the current project, finds BUILD files, loads external dependencies, and generates packages and targets one by one.

analysis phase analysis phase

Create an in-memory build graph based on the goals and input and output information defined in each BUILD.

executing phase

Run the actions defined by each rule one by one according to the build graph to produce the final goal.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report