Bazel 构建

Reference

Bazel is an open-source build and test tool similar to Make, Maven, and Gradle.
It uses a human-readable, high-level build language. Bazel supports projects in
multiple languages and builds outputs for multiple platforms. Bazel supports large
codebases across multiple repositories, and large numbers of users.

Advantage

  • High-level build language.
  • Bazel is fasst and reliable.
  • Bazel is muti-platform supported
  • Scales
  • Extensiable

Usages

  1. Install Bazel

sudo pacman -S bazel

  1. Set up a project
  • The WORKSPACE file, which identifies the directory and its contents as bazel workspace
    and lives at the root of the project's directory.
  • One or more BUILD files which tell Bazel how to build different parts of the project.
    (A directory within the workspace that contains a BUILD file is a package.)
    To designate a directory as a Bazel workspace, create an empty file named WORKSPACE in that directory.
  1. Write a BUILD file
    A BUILD file contains several different types of instructions for Bazel. The
    most important type is the build rule, which tells Bazel how to build the
    desired outputs, such as executable binaries or libraries. Each instance of a
    build rule in the BUILD file is called a target and points to a specific set
    of source files and dependencies. A target can also point to other targets.

  2. Run Bazel

  • Build the project

bazel build //main:app-name
Notice the target label - the //main: part is the location of our BUILD file
relative to the root of the workspace.
substitue app-name with your target name.

  • Test binary

bazel-bin/main/app-name

  1. Review the dependency graph

A successful build has all of its dependencies explicitly stated in the BUILD file.
Bazel use those statements to create the project's dependency graph. which enables accurate
incremental builds.

bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" --output graph

Before visualization, you should make sure you have already got xdot and grahphviz installed.
You can generate and view the graphy by piping the text output above straight to xdot:

xdot <(bazel query --notool_deps --noimplicit_deps "deps(//main:hello-world)" \
  --output graph)
  1. Use labels to reference targets

Bazel use labels to reference targets, like //main:hello-world and //lib:hello-time
The syntax is :

//path/to/package:target-name

If the target is a rule target, then path/to/package is the path to the directory containing the
BUILD file, and target-name is what you named the target in the BUILD file(the name attribute)
If the target is a file target, then path/to/package is the path to the root of the package
, and target-name is the name of the target file, including full path.

When referencing targets at the repository root, the package path is empty, just use //:target-name.
When referencing targets within the same BUILD file, you can even skip the // workspace root identifier and
just use :target-name.

to be finished

posted @ 2021-08-26 09:08  司空亦墨  阅读(139)  评论(0编辑  收藏  举报