fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 1 | Project: /_project.yaml |
| 2 | Book: /_book.yaml |
| 3 | |
| 4 | # Intro to Bazel |
| 5 | |
Googler | 3b9ed6e | 2022-11-08 02:19:42 -0800 | [diff] [blame] | 6 | {% include "_buttons.html" %} |
| 7 | |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 8 | Bazel is an open-source build and test tool similar to Make, Maven, and Gradle. |
| 9 | It uses a human-readable, high-level build language. Bazel supports projects in |
| 10 | multiple languages and builds outputs for multiple platforms. Bazel supports |
| 11 | large codebases across multiple repositories, and large numbers of users. |
| 12 | |
| 13 | ## Benefits {:#benefits} |
| 14 | |
| 15 | Bazel offers the following advantages: |
| 16 | |
| 17 | * **High-level build language.** Bazel uses an abstract, human-readable |
| 18 | language to describe the build properties of your project at a high |
| 19 | semantical level. Unlike other tools, Bazel operates on the *concepts* |
| 20 | of libraries, binaries, scripts, and data sets, shielding you from the |
| 21 | complexity of writing individual calls to tools such as compilers and |
| 22 | linkers. |
| 23 | |
| 24 | * **Bazel is fast and reliable.** Bazel caches all previously done work and |
| 25 | tracks changes to both file content and build commands. This way, Bazel |
| 26 | knows when something needs to be rebuilt, and rebuilds only that. To further |
| 27 | speed up your builds, you can set up your project to build in a highly |
| 28 | parallel and incremental fashion. |
| 29 | |
| 30 | * **Bazel is multi-platform.** Bazel runs on Linux, macOS, and Windows. Bazel |
| 31 | can build binaries and deployable packages for multiple platforms, including |
| 32 | desktop, server, and mobile, from the same project. |
| 33 | |
| 34 | * **Bazel scales.** Bazel maintains agility while handling builds with 100k+ |
| 35 | source files. It works with multiple repositories and user bases in the tens |
| 36 | of thousands. |
| 37 | |
| 38 | * **Bazel is extensible.** Many [languages](/rules) are |
| 39 | supported, and you can extend Bazel to support any other language or |
| 40 | framework. |
| 41 | |
| 42 | ## Using Bazel {:#using-bazel} |
| 43 | |
| 44 | To build or test a project with Bazel, you typically do the following: |
| 45 | |
| 46 | 1. **Set up Bazel.** Download and [install Bazel](/install). |
| 47 | |
| 48 | 2. **Set up a project [workspace](/concepts/build-ref#workspaces)**, which is a |
| 49 | directory where Bazel looks for build inputs and `BUILD` files, and where it |
| 50 | stores build outputs. |
| 51 | |
| 52 | 3. **Write a `BUILD` file**, which tells Bazel what to build and how to |
| 53 | build it. |
| 54 | |
| 55 | You write your `BUILD` file by declaring build targets using |
| 56 | [Starlark](/rules/language), a domain-specific language. (See example |
| 57 | [here](https://github.com/bazelbuild/bazel/blob/master/examples/cpp/BUILD){: .external}.) |
| 58 | |
| 59 | A build target specifies a set of input artifacts that Bazel will build plus |
| 60 | their dependencies, the build rule Bazel will use to build it, and options |
| 61 | that configure the build rule. |
| 62 | |
| 63 | A build rule specifies the build tools Bazel will use, such as compilers and |
| 64 | linkers, and their configurations. Bazel ships with a number of build rules |
| 65 | covering the most common artifact types in the supported languages on |
| 66 | supported platforms. |
| 67 | |
| 68 | 4. **Run Bazel** from the [command line](/reference/command-line-reference). Bazel |
| 69 | places your outputs within the workspace. |
| 70 | |
| 71 | In addition to building, you can also use Bazel to run |
Googler | df7617e | 2022-12-20 09:44:29 -0800 | [diff] [blame] | 72 | [tests](/reference/test-encyclopedia) and [query](/query/guide) the build |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 73 | to trace dependencies in your code. |
| 74 | |
| 75 | ## Bazel build process {:#bazel-build-process} |
| 76 | |
| 77 | When running a build or a test, Bazel does the following: |
| 78 | |
| 79 | 1. **Loads** the `BUILD` files relevant to the target. |
| 80 | |
| 81 | 2. **Analyzes** the inputs and their |
| 82 | [dependencies](/concepts/dependencies), applies the specified build |
Googler | 0c4cf50 | 2022-12-19 13:43:43 -0800 | [diff] [blame] | 83 | rules, and produces an [action](/extending/concepts#evaluation-model) |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 84 | graph. |
| 85 | |
| 86 | 3. **Executes** the build actions on the inputs until the final build outputs |
| 87 | are produced. |
| 88 | |
| 89 | Since all previous build work is cached, Bazel can identify and reuse cached |
| 90 | artifacts and only rebuild or retest what's changed. To further enforce |
| 91 | correctness, you can set up Bazel to run builds and tests |
Googler | 0c4cf50 | 2022-12-19 13:43:43 -0800 | [diff] [blame] | 92 | [hermetically](/basics/hermeticity) through sandboxing, minimizing skew |
| 93 | and maximizing [reproducibility](/run/build#correct-incremental-rebuilds). |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 94 | |
| 95 | ### Action graph {:#action-graph} |
| 96 | |
| 97 | The action graph represents the build artifacts, the relationships between them, |
| 98 | and the build actions that Bazel will perform. Thanks to this graph, Bazel can |
Googler | 0c4cf50 | 2022-12-19 13:43:43 -0800 | [diff] [blame] | 99 | [track](/run/build#build-consistency) changes to |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 100 | file content as well as changes to actions, such as build or test commands, and |
| 101 | know what build work has previously been done. The graph also enables you to |
Googler | df7617e | 2022-12-20 09:44:29 -0800 | [diff] [blame] | 102 | easily [trace dependencies](/query/guide) in your code. |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 103 | |
| 104 | ## Getting started tutorials {:#getting-started-tutorials} |
| 105 | |
Googler | 0c4cf50 | 2022-12-19 13:43:43 -0800 | [diff] [blame] | 106 | To get started with Bazel, see [Getting Started](/start/) or jump |
fwe | acae1cd | 2022-02-17 09:45:38 -0800 | [diff] [blame] | 107 | directly to the Bazel tutorials: |
| 108 | |
Googler | 0c4cf50 | 2022-12-19 13:43:43 -0800 | [diff] [blame] | 109 | * [Tutorial: Build a C++ Project](/start/cpp) |
| 110 | * [Tutorial: Build a Java Project](/start/java) |
Amet Umerov | a95f45d | 2023-05-22 11:55:17 -0700 | [diff] [blame] | 111 | * [Tutorial: Build an Android Application](/start/android-app) |
| 112 | * [Tutorial: Build an iOS Application](/start/ios-app) |