Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | title: Bazel Overview |
| 4 | --- |
| 5 | |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 6 | # What is Bazel? |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 7 | |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [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. |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 12 | |
| 13 | |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 14 | # Why should I use Bazel? |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 15 | |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 16 | Bazel offers the following advantages: |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 17 | |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 18 | * **High-level build language.** Bazel uses an abstract, human-readable |
| 19 | language to describe the build properties of your project at a high |
| 20 | semantical level. Unlike other tools, Bazel operates on the *concepts* |
| 21 | of libraries, binaries, scripts, and data sets, shielding you from the |
| 22 | complexity of writing individual calls to tools such as compilers and |
| 23 | linkers. |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 24 | |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 25 | * **Bazel is fast and reliable.** Bazel caches all previously done work and |
| 26 | tracks changes to both file content and build commands. This way, Bazel |
| 27 | knows when something needs to be rebuilt, and rebuilds only that. To further |
| 28 | speed up your builds, you can set up your project to build in a highly |
| 29 | parallel and incremental fashion. |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 30 | |
Googler | 9ea02e5 | 2018-03-28 10:35:37 -0700 | [diff] [blame] | 31 | * **Bazel is multi-platform.** Bazel runs on Linux, macOS, and Windows. Bazel |
| 32 | can build binaries and deployable packages for multiple platforms, including |
| 33 | desktop, server, and mobile, from the same project. |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 34 | |
| 35 | * **Bazel scales.** Bazel maintains agility while handling builds with 100k+ |
| 36 | source files. It works with multiple repositories and user bases in the tens |
| 37 | of thousands. |
| 38 | |
| 39 | * **Bazel is extensible.** You can extend Bazel to support your language of |
| 40 | choice. |
| 41 | |
| 42 | |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 43 | # How do I use Bazel? |
| 44 | |
| 45 | To build or test a project with Bazel, you typically do the following: |
| 46 | |
| 47 | 1. **Set up Bazel.** Download and [install Bazel](https://docs.bazel.build/versions/master/install.html). |
| 48 | |
Umesh Yadav | e35047e | 2017-11-27 12:12:08 -0800 | [diff] [blame] | 49 | 2. **Set up a project [workspace](https://docs.bazel.build/versions/master/build-ref.html#workspaces)**, |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 50 | which is a directory where Bazel looks for build inputs and `BUILD` files, |
| 51 | and where it stores build outputs. |
| 52 | |
Roller, David | 0dcf425 | 2017-11-28 07:59:41 -0800 | [diff] [blame] | 53 | 3. **Write a `BUILD` file**, which tells Bazel what to build and how to |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 54 | build it. |
| 55 | |
| 56 | You write your `BUILD` file by declaring build targets using an abstract |
| 57 | Python-like language. (See example [here](https://github.com/bazelbuild/bazel/blob/master/examples/cpp/BUILD).) |
| 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](https://docs.bazel.build/versions/master/command-line-reference.html). |
| 69 | Bazel places your outputs within the workspace. |
| 70 | |
| 71 | In addition to building, you can also use Bazel to run [tests](https://docs.bazel.build/versions/master/test-encyclopedia.html) |
| 72 | and [query](https://docs.bazel.build/versions/master/query-how-to.html) the |
| 73 | build to trace dependencies in your code. |
| 74 | |
| 75 | |
| 76 | # How does Bazel work? |
| 77 | |
| 78 | When running a build or a test, Bazel does the following: |
| 79 | |
| 80 | 1. **Loads** the `BUILD` files relevant to the target. |
| 81 | |
| 82 | 2. **Analyzes** the inputs and their [dependencies](https://docs.bazel.build/versions/master/build-ref.html#dependencies), |
| 83 | applies the specified build rules, and produces an [action](https://docs.bazel.build/versions/master/skylark/concepts.html#evaluation-model) |
| 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 |
Jingwen Chen | 7633ab0 | 2018-03-05 09:30:14 -0800 | [diff] [blame] | 91 | correctness, you can set up Bazel to run builds and tests [hermetically](https://docs.bazel.build/versions/master/user-manual.html#sandboxing) |
| 92 | through sandboxing, minimizing skew and maximizing [reproducibility](https://docs.bazel.build/versions/master/user-manual.html#correctness). |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 93 | |
| 94 | |
| 95 | ## What is the 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 |
Jingwen Chen | 7633ab0 | 2018-03-05 09:30:14 -0800 | [diff] [blame] | 99 | [track](https://docs.bazel.build/versions/master/user-manual.html#build-consistency-and-incremental-builds) |
spomorski | edca92e | 2017-10-24 19:59:57 +0200 | [diff] [blame] | 100 | changes to file content as well as changes to actions, such as build or test |
| 101 | commands, and know what build work has previously been done. The graph also |
| 102 | enables you to easily [trace dependencies](https://docs.bazel.build/versions/master/query-how-to.html) |
| 103 | in your code. |
| 104 | |
| 105 | |
| 106 | # How do I get started? |
| 107 | |
| 108 | To get started with Bazel, see [Getting Started](https://docs.bazel.build/versions/master/getting-started.html) |
| 109 | or jump directly to the Bazel tutorials: |
| 110 | |
| 111 | * [Tutorial: Build a C++ Project](https://docs.bazel.build/versions/master/tutorial/cpp.html) |
| 112 | * [Tutorial: Build a Java Project](https://docs.bazel.build/versions/master/tutorial/java.html) |
| 113 | * [Tutorial: Build an Android Application](https://docs.bazel.build/versions/master/tutorial/android-app.html) |
| 114 | * [Tutorial: Build an iOS Application](https://docs.bazel.build/versions/master/tutorial/ios-app.html) |