Cpp Notes

basic_concepts

The core concepts and building blocks of Bazel, which are fundamental to its design and operation, include:

Workspaces:

  • A workspace is a top-level directory containing the source files for one or more software projects and a WORKSPACE file at its root. It defines the context in which Bazel operates, encompassing the main repository and all external repositories needed for the build.

Build Files (BUILD and BUILD.bazel):

  • These files, named either BUILD or BUILD.bazel, are located in directories throughout the workspace.
  • They define how Bazel builds software, specifying what outputs (like executables, libraries, etc.) can be built from the source files.

Packages:

  • A package in Bazel is defined by a directory within the workspace that contains a BUILD file. It includes all files in the directory and its subdirectories, except those which contain their own BUILD file. Packages are the basic unit of code organization and build configuration in Bazel.

Targets:

  • Targets are the named build outputs defined in BUILD files. A target can be a file or a rule. Rules declare how outputs are built from inputs, which can include other targets, and specify dependencies and necessary build steps. Common rule types include cc_binary for C++ binaries, py_library for Python libraries, etc.

Rules and Rule Classes:

  • Rules are declarations in BUILD files that describe how to build outputs from inputs. Each rule is an instance of a rule class, which is defined by Bazel or by custom extensions. Rule classes define a kind of output (like a binary or a library) and contain logic to generate these outputs.

Actions:

  • Actions are the basic steps that Bazel takes to build outputs, like compiling a C++ source file or linking a binary. Actions are generated by rules based on their input and output specifications.

Dependencies:

  • Dependencies specify how targets relate to one another. When a target depends on other targets, Bazel ensures that those targets are built first. Dependencies are crucial for incremental builds, as they allow Bazel to rebuild only what is necessary.

Labels:

  • Labels uniquely identify targets in the build. They include the package's path, the target's name, and the kind of rule that generates the target. Labels are used to reference targets in BUILD files and Bazel commands.

Toolchains:

  • Toolchains provide the necessary tools (like compilers, linkers) and settings to build targets for a specific platform or environment. This allows Bazel to support cross-platform builds and configure builds for different environments.

Sandboxing:

  • Sandboxing isolates build actions from the rest of the system, ensuring that each action can only access its declared inputs and outputs. This feature enhances the reproducibility and reliability of builds.