Bazel learning summary (1)

Bazel is an open source build and test tool, which can be regarded as the same thing as Make, Maven, and Gradle. However, Bazel's build language is more advanced, more readable, and supports multi-language and cross-platform. Bazel supports a large code base and a large number of users across multiple repositories.

1. The advantages of Bazel are shown in the following 5 aspects:

1) High-level build language: Unlike other tools, Bazel uses the concepts of libraries, binary files, scripts, and data sets to avoid the complexity of writing files to call compilers and linkers, so Bazel is describing the construction of the project The properties are more readable.

2) Fast and reliable: Bazel caches previous operations, tracks file content and modification of compilation commands. In this way, Bazel knows when it needs to be rebuilt, and only rebuilds the modified places. To further speed up the build, you can also set the project to be built in a highly parallel and incremental manner.

3) Cross-platform: Bazel can run on Linux, macOS and Windows. Bazel can build binaries and deployable packages for multiple platforms (including desktop, server, and mobile) from the same project.

4) Large scale: Bazel maintains flexibility when dealing with the construction of 100k+ source files, and can work with multiple repositories and tens of thousands of user groups.

5) Extensible: Supports multiple languages, and Bazel can be extended to support any other languages ​​or frameworks.

Generally speaking, it has many benefits, is convenient to use, and can still perform well even with a large amount of code or a large number of developers.

2. How to use Bazel?

1) It must be installed first:

1、添加包源:
sudo apt install curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
2、安装:
sudo apt update && sudo apt install bazel

sudo apt update && sudo apt full-upgrade

sudo apt install bazel-1.0.0 (默认安装最新版本,这里指定了版本1.0.0)

2) Set up a workspace, which is the directory where Bazel finds the build input and build files and stores the build output.

3) Write a BUILD that tells Bazel what to build and how to build.

Bazel uses Starlark syntax to write workspace/BUILD files, which are similar to makeifle in Make and CMakeLists in CMake.

package(default_visibility = ["//visibility:public"])

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")

cc_library(
    name = "hello-lib",
    srcs = ["hello-lib.cc"],
    hdrs = ["hello-lib.h"],
)

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [":hello-lib"],
)

cc_test(
    name = "hello-success_test",
    srcs = ["hello-world.cc"],
    deps = [":hello-lib"],
)

cc_test(
    name = "hello-fail_test",
    srcs = ["hello-fail.cc"],
    deps = [":hello-lib"],
)

filegroup(
    name = "srcs",
    srcs = glob(["**"]),
)

The build target specifies a set of input artifacts that Bazel will build, their dependencies, the build rules that Bazel will use to build it, and the options for configuring the build rules.
The build rules specify the build tools (such as compilers and linkers) and their configuration that Bazel will use. Bazel comes with many build rules that cover the most common types of artifacts in the supported languages ​​on the supported platforms.

4) Run

运行命令类似:
bazel [<startup options>] <command> [<args>]

bazel [<startup options>] <command> [<args>] -- [<target patterns>]

Third, the operation process of Bazel can refer to this article Bazel learning summary (1)

Fourth, Bazel has something called an action graph, which should be DAG. The
action graph represents the construction artifacts, the relationship between them, and the construction actions that Bazel will perform. With the help of this graph, Bazel can track changes to file content and changes to operations (such as build or test commands), and know what build work has been done before. The graph also enables you to easily track dependencies in the code.

Guess you like

Origin blog.csdn.net/whuzhang16/article/details/111831822