Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | title: Bazel Overview |
| 4 | --- |
| 5 | |
| 6 | # Bazel Overview |
| 7 | |
| 8 | Bazel is a build tool which coordinates builds and run tests. It works with |
| 9 | source files written in any language, with native support for Java, C, C++ and |
| 10 | Python. Bazel produces builds and runs tests for multiple platforms. |
| 11 | |
| 12 | ## BUILD files use a simple declarative language |
| 13 | |
| 14 | Bazel’s BUILD files describe how Bazel should build your project. They have a |
| 15 | declarative structure and use a build language similar to Python. BUILD files |
| 16 | allow you to work at a high level of the system by listing rules and their |
| 17 | attributes. The complexity of the build process is handled by these pre-existing |
| 18 | rules. You can modify rules to tweak the build process, or write new rules to |
| 19 | extend Bazel to work with any language or platform. |
| 20 | |
| 21 | Below is the content of one of the BUILD files from a Hello World program. The |
Googler | 1b91de7 | 2017-03-08 15:39:20 +0000 | [diff] [blame] | 22 | two rules used here are `cc_library` and `cc_binary`. |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 23 | |
| 24 | ``` |
| 25 | cc_library( |
| 26 | name = "hello-time", |
| 27 | srcs = ["hello-time.cc"], |
| 28 | hdrs = ["hello-time.h"], |
| 29 | ) |
| 30 | |
| 31 | cc_binary( |
| 32 | name = "hello-world", |
| 33 | srcs = ["hello-world.cc"], |
| 34 | deps = [ |
| 35 | ":hello-time", |
| 36 | "//lib:hello-greet", |
| 37 | ], |
| 38 | ) |
| 39 | ``` |
| 40 | |
Googler | 1b91de7 | 2017-03-08 15:39:20 +0000 | [diff] [blame] | 41 | ## The dependency graph describes the entire system |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 42 | |
| 43 | Build dependencies are declared explicitly in the BUILD files, allowing Bazel |
| 44 | to create an accurate dependency graph of the entire source code. The graph is |
| 45 | maintained in memory, and incremental builds and parallel execution are possible |
| 46 | because of this accurate dependency graph. |
| 47 | |
| 48 | Here’s the graph of the target ‘hello-world’ from the BUILD file above: |
| 49 | |
| 50 |  |
| 51 | |
| 52 | |
| 53 | Bazel’s query language allows you to produce images of the graph like the one |
| 54 | above. You can also use the query language to access information about build |
| 55 | dependencies and their relationships. |
| 56 | |
Googler | 1b91de7 | 2017-03-08 15:39:20 +0000 | [diff] [blame] | 57 | ## Build and tests are fast, correct, and reproducible |
Googler | 12e9947 | 2017-03-03 16:23:40 +0000 | [diff] [blame] | 58 | |
| 59 | Hermetic rules and sandboxing allows Bazel to produce correct, reproducible |
| 60 | artifacts and test results. Caching allows reuse of build artifacts and test |
| 61 | results. |
| 62 | |
| 63 | Bazel’s builds are fast. Incremental builds allows Bazel to do the minimum |
| 64 | required work for a rebuild or retest. Correct and reproducible builds allow |
| 65 | Bazel to reuse cached artifacts for whatever is not changed. If you change a |
| 66 | library, Bazel will not rebuild your entire source. |
| 67 | |
| 68 | Confidence in these correct results also means that you will never need to run |
| 69 | `bazel clean`. If you ever need to run `bazel clean`, there’s a bug in Bazel. |