blob: abf545dacbd5dc1e20fa389a6d47afdfc8b4cb57 [file] [log] [blame] [view]
fweacae1cd2022-02-17 09:45:38 -08001Project: /_project.yaml
2Book: /_book.yaml
3
4# Intro to Bazel
5
Googler3b9ed6e2022-11-08 02:19:42 -08006{% include "_buttons.html" %}
7
fweacae1cd2022-02-17 09:45:38 -08008Bazel is an open-source build and test tool similar to Make, Maven, and Gradle.
9It uses a human-readable, high-level build language. Bazel supports projects in
10multiple languages and builds outputs for multiple platforms. Bazel supports
11large codebases across multiple repositories, and large numbers of users.
12
13## Benefits {:#benefits}
14
15Bazel 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
44To build or test a project with Bazel, you typically do the following:
45
461. **Set up Bazel.** Download and [install Bazel](/install).
47
482. **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
523. **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
684. **Run Bazel** from the [command line](/reference/command-line-reference). Bazel
69 places your outputs within the workspace.
70
71In addition to building, you can also use Bazel to run
Googlerdf7617e2022-12-20 09:44:29 -080072[tests](/reference/test-encyclopedia) and [query](/query/guide) the build
fweacae1cd2022-02-17 09:45:38 -080073to trace dependencies in your code.
74
75## Bazel build process {:#bazel-build-process}
76
77When running a build or a test, Bazel does the following:
78
791. **Loads** the `BUILD` files relevant to the target.
80
812. **Analyzes** the inputs and their
82 [dependencies](/concepts/dependencies), applies the specified build
Googler0c4cf502022-12-19 13:43:43 -080083 rules, and produces an [action](/extending/concepts#evaluation-model)
fweacae1cd2022-02-17 09:45:38 -080084 graph.
85
863. **Executes** the build actions on the inputs until the final build outputs
87 are produced.
88
89Since all previous build work is cached, Bazel can identify and reuse cached
90artifacts and only rebuild or retest what's changed. To further enforce
91correctness, you can set up Bazel to run builds and tests
Googler0c4cf502022-12-19 13:43:43 -080092[hermetically](/basics/hermeticity) through sandboxing, minimizing skew
93and maximizing [reproducibility](/run/build#correct-incremental-rebuilds).
fweacae1cd2022-02-17 09:45:38 -080094
95### Action graph {:#action-graph}
96
97The action graph represents the build artifacts, the relationships between them,
98and the build actions that Bazel will perform. Thanks to this graph, Bazel can
Googler0c4cf502022-12-19 13:43:43 -080099[track](/run/build#build-consistency) changes to
fweacae1cd2022-02-17 09:45:38 -0800100file content as well as changes to actions, such as build or test commands, and
101know what build work has previously been done. The graph also enables you to
Googlerdf7617e2022-12-20 09:44:29 -0800102easily [trace dependencies](/query/guide) in your code.
fweacae1cd2022-02-17 09:45:38 -0800103
104## Getting started tutorials {:#getting-started-tutorials}
105
Googler0c4cf502022-12-19 13:43:43 -0800106To get started with Bazel, see [Getting Started](/start/) or jump
fweacae1cd2022-02-17 09:45:38 -0800107directly to the Bazel tutorials:
108
Googler0c4cf502022-12-19 13:43:43 -0800109* [Tutorial: Build a C++ Project](/start/cpp)
110* [Tutorial: Build a Java Project](/start/java)
Amet Umerova95f45d2023-05-22 11:55:17 -0700111* [Tutorial: Build an Android Application](/start/android-app)
112* [Tutorial: Build an iOS Application](/start/ios-app)