blob: d3e7f15f17683551a3350d3e8c10fd87420c58e9 [file] [log] [blame] [view]
Geoffrey Martin-Noble4aeb2e62021-05-18 22:42:251# Introduction
2
3*Warning* The Bazel build is experimental and best-effort, supported in line
4with the policy for
5[LLVM's peripheral support tier](https://llvm.org/docs/SupportPolicy.html).
6LLVM's official build system is CMake. If in doubt use that. If you make changes
7to LLVM, you're expected to update the CMake build but you don't need to update
8Bazel build files. Reviewers should not ask authors to update Bazel build files
9unless the author has opted in to support Bazel. Keeping the Bazel build files
10up-to-date is on the people who use the Bazel build.
11
12[Bazel](https://bazel.build/) is a multi-language build system focused on
13reproducible builds to enable dependency analysis and caching for fast
14incremental builds.
15
16The main motivation behind the existence of an LLVM Bazel build is that a number
17of projects that depend on LLVM use Bazel, and Bazel works best when it knows
18about the whole source tree (as opposed to installing artifacts coming from
19another build system). Community members are also welcome to use Bazel for their
20own development as long as they continue to maintain the official CMake build
21system. See also, the
22[proposal](https://github.com/llvm/llvm-www/blob/main/proposals/LP0002-BazelBuildConfiguration.md)
23for adding this configuration.
24
25# Quick Start
26
271. `git clone https://github.com/llvm/llvm-project.git; cd llvm-project` if
28 you don't have a checkout yet.
Shivam Guptac3f19802021-07-30 15:36:14292. Install Bazel at the version indicated by [.bazelversion](./.bazelversion),
Geoffrey Martin-Noble4aeb2e62021-05-18 22:42:2530 following the official instructions, if you don't have it installed yet:
Quinn Phamc71fbdd2021-11-03 19:41:2431 https://docs.bazel.build/versions/main/install.html.
Arthur Eubanks4cca3de2023-07-11 18:43:4332 * You can also install and use
33 [bazelisk](https://github.com/bazelbuild/bazelisk) which automates
34 downloading the proper bazel version
Geoffrey Martin-Noble4aeb2e62021-05-18 22:42:25353. `cd utils/bazel`
Angel Zhangda5264e2024-07-17 18:47:53364. The `bazel build` command depends on the local compiler you want to use.
37 * For **clang**, go to step 5.
38 * For **gcc** or **MSVC**, go to step 6
395. If you are using **clang**, it is expected that lld is also available.
40 The `--config=generic_clang` flag by default sets the compiler to be `clang`
41 binary on the `PATH`.
42 ```
43 bazel build --config=generic_clang @llvm-project//...
44 ```
45 To provide a specific path to your `clang`, use the `--repo_env` Bazel flag.
46 For example:
47 ```
48 bazel build --config=generic_clang --repo_env=CC=/usr/bin/clang --repo_env=CXX=/usr/bin/clang++ @llvm-project//...
49 ```
506. If you are using **gcc** or **MSVC**, instead of `--config=generic_clang`
51 , pass `--config=generic_gcc` or `--config=generic_msvc`, which sets the
52 compiler to be `gcc` binary on the `PATH`.
53 ```
54 bazel build --config=generic_gcc @llvm-project//...
55 ```
56 To provide a specific path to your `gcc`, use the `--repo_env` Bazel flag.
57 For example:
58 ```
59 bazel build --config=generic_gcc --repo_env=CC=/usr/bin/gcc --repo_env=CXX=/usr/bin/g++ @llvm-project//...
60 ```
Geoffrey Martin-Noble4aeb2e62021-05-18 22:42:2561
62# Configuration
63
64The repository `.bazelrc` will import user-specific settings from a
65`user.bazelrc` file (in addition to the standard locations). Adding your typical
66config setting is recommended.
67
68```.bazelrc
69build --config=generic_clang
70```
71
72You can enable
Quinn Phamc71fbdd2021-11-03 19:41:2473[disk caching](https://docs.bazel.build/versions/main/remote-caching.html#disk-cache),
Geoffrey Martin-Noble4aeb2e62021-05-18 22:42:2574which will cache build results
75
76```.bazelrc
77build --disk_cache=~/.cache/bazel-disk-cache
78```
79
80You can instruct Bazel to use a ramdisk for its sandboxing operations via
Quinn Phamc71fbdd2021-11-03 19:41:2481[--sandbox_base](https://docs.bazel.build/versions/main/command-line-reference.html#flag--sandbox_base),
Arthur Eubanksb04d01c2022-08-22 17:02:3082which can help avoid IO bottlenecks for the symlink strategy used for
Geoffrey Martin-Noble4aeb2e62021-05-18 22:42:2583sandboxing. This is especially important with many inputs and many cores (see
84https://github.com/bazelbuild/bazel/issues/11868):
85
86```.bazelrc
87build --sandbox_base=/dev/shm
88```
89
90Bear in mind that this requires that your ramdisk is of sufficient size to hold
91any temporary files. Anecdotally, 1GB should be sufficient.
92
93# Coverage
94
95The LLVM, MLIR, and Clang subprojects have configurations for Linux (Clang and
96GCC), Mac (Clang and GCC), and Windows (MSVC). Configuration options that are
97platform-specific are selected for in defines. Many are also hardcoded to the
98values currently used by all supported configurations. If there is a
99configuration you'd like to use that isn't supported, please send a patch.
100
Geoffrey Martin-Noble088fbc02022-02-11 16:59:11101# Continuous Testing
102
Arthur Eubanks3f2f23c2022-08-11 21:12:34103A [Buildkite pipeline](https://buildkite.com/llvm-project/upstream-bazel)
Geoffrey Martin-Noble088fbc02022-02-11 16:59:11104runs the full Bazel build on every commit to the main branch. Notifications of
105failures are sent to the
106[llvm-bazel-alerts google group](https://groups.google.com/g/llvm-bazel-alerts),
107which anyone is free to join. Currently, the behavior is just to send an email
108on each failure using Buildkite's built-in notification system, so if you
109subscribe, it is highly recommended that you set up email filters or some other
110mechanism to not flood your inbox. More sophisticated notifications, e.g. only
111on status change or routed based on blamelist are TODO (contributions welcome).
112
Geoffrey Martin-Noble088fbc02022-02-11 16:59:11113# Usage in Downstream Projects
Geoffrey Martin-Noble4aeb2e62021-05-18 22:42:25114
Geoffrey Martin-Noble9cc1ddd2021-06-30 23:46:55115To use in dependent projects using Bazel, you can import LLVM and then use the
116provided configuration rule. See example usage in the `examples/` directory.