Basics of Bazel

Tags
bazel
Engineering
Created
Oct 7, 2023 04:57 AM
Edited
Oct 6, 2023
Description
package/target and bazel run/build/test/query

What is Bazel? & Why do we use Bazel?

  • A build, test, deploy tool
  • So easy to use with monorepo
  • Support multiple languages
  • Compatible with custom codes

What is a package?

  • a "package" refers to a directory that contains build and source files that define one or more targets
my_project/
├── WORKSPACE
├── BUILD
├── main/
│   ├── BUILD
│   └── main.go
└── lib/
    ├── BUILD
    └── utils.go
  • my_project/: The root directory of the Bazel workspace.
  • WORKSPACE: The workspace file that Bazel uses to define external dependencies.
  • BUILD: A build file in the root directory.
  • main/: A package directory that contains the main application code.
  • lib/: A package directory that contains utility functions.
# my_project/main/BUILD
package(default_visibility = ["//visibility:public"])

go_binary(
    name = "my_app",
    srcs = ["main.go"],
    deps = ["//lib:utils"],
)
The main package (main/) contains the main application code (main.go). It references the lib:utils target from the lib package.
 

What is a target in bazel?

  • A buildable and testable unit

How to define a target

# Example BUILD file with dependencies
cc_library(
    name = "my_library",
    srcs = ["my_source_file.cpp"],
    hdrs = ["my_header_file.h"],
    deps = [":other_library"],
)

cc_library(
    name = "other_library",
    srcs = ["other_source_file.cpp"],
    hdrs = ["other_header_file.h"],
)

# bazel build //path/to:my_library

bazel run

  • bazel run: This command is used to build and execute a target in one step. It is typically used for running applications or scripts directly from the build process. When you use bazel run, Bazel will build the specified target and then execute it immediately.
bazel run //path/to:my_binary_target

bazel build

  • bazel build: This command is used to build one or more targets, but it does not execute them immediately. Instead, it compiles the code and creates the build artifacts in the output directory, which can then be run separately using other commands or tools.
bazel build //path/to:my_target

bazel test

  • Build and execute tests
bazel test //path/to:my_test_target
bazel test //path/to:all_tests

bazel query

In Bazel, the bazel query command is used to perform queries on the build graph and retrieve information about targets, packages, dependencies, and more within a Bazel workspace. The command bazel query <package>:all is used to retrieve a list of all targets within a specified package.
Here's what each part of the command does:
  • bazel query: This is the command itself that you're executing.
  • <package>: This is the name of the package for which you want to retrieve information.
  • :all: This is a special target name that represents all targets within the specified package.
When you run bazel query <package>:all, Bazel will traverse the build graph and list all the target labels (names) within the specified package. This includes every target defined in that package's BUILD files.
For example, if you have the following directory structure in your Bazel workspace:
goCopy code
my_project/
├── BUILD
├── WORKSPACE
└── package/
    ├── BUILD
    ├── target1.bzl
    └── target2.bzl

Running the command bazel query //package:all from the root of the workspace would list all targets defined within the package directory, including those defined in target1.bzl and target2.bzl, if they exist.
Keep in mind that the :all target doesn't represent a real target in terms of building; it's just a convenient way to list all targets within a package using the bazel query command. You can replace :all with the name of a specific target to query information about that target instead.

—keep_going

  • Tell Bazel to continue querying even if it encounters errors during the query execution
bazel query --keep_going ...

kind

  • To filter the query results based on the target kind
bazel query 'kind("cc_library")' # return a list of cc_library targets defined in your workspace