Geoffrey Martin-Noble | 4aeb2e6 | 2021-05-18 22:42:25 | [diff] [blame] | 1 | # Introduction |
| 2 | |
| 3 | *Warning* The Bazel build is experimental and best-effort, supported in line |
| 4 | with the policy for |
| 5 | [LLVM's peripheral support tier](https://llvm.org/docs/SupportPolicy.html). |
| 6 | LLVM's official build system is CMake. If in doubt use that. If you make changes |
| 7 | to LLVM, you're expected to update the CMake build but you don't need to update |
| 8 | Bazel build files. Reviewers should not ask authors to update Bazel build files |
| 9 | unless the author has opted in to support Bazel. Keeping the Bazel build files |
| 10 | up-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 |
| 13 | reproducible builds to enable dependency analysis and caching for fast |
| 14 | incremental builds. |
| 15 | |
| 16 | The main motivation behind the existence of an LLVM Bazel build is that a number |
| 17 | of projects that depend on LLVM use Bazel, and Bazel works best when it knows |
| 18 | about the whole source tree (as opposed to installing artifacts coming from |
| 19 | another build system). Community members are also welcome to use Bazel for their |
| 20 | own development as long as they continue to maintain the official CMake build |
| 21 | system. See also, the |
| 22 | [proposal](https://github.com/llvm/llvm-www/blob/main/proposals/LP0002-BazelBuildConfiguration.md) |
| 23 | for adding this configuration. |
| 24 | |
| 25 | # Quick Start |
| 26 | |
| 27 | 1. `git clone https://github.com/llvm/llvm-project.git; cd llvm-project` if |
| 28 | you don't have a checkout yet. |
Shivam Gupta | c3f1980 | 2021-07-30 15:36:14 | [diff] [blame] | 29 | 2. Install Bazel at the version indicated by [.bazelversion](./.bazelversion), |
Geoffrey Martin-Noble | 4aeb2e6 | 2021-05-18 22:42:25 | [diff] [blame] | 30 | following the official instructions, if you don't have it installed yet: |
Quinn Pham | c71fbdd | 2021-11-03 19:41:24 | [diff] [blame] | 31 | https://docs.bazel.build/versions/main/install.html. |
Arthur Eubanks | 4cca3de | 2023-07-11 18:43:43 | [diff] [blame] | 32 | * You can also install and use |
| 33 | [bazelisk](https://github.com/bazelbuild/bazelisk) which automates |
| 34 | downloading the proper bazel version |
Geoffrey Martin-Noble | 4aeb2e6 | 2021-05-18 22:42:25 | [diff] [blame] | 35 | 3. `cd utils/bazel` |
Angel Zhang | da5264e | 2024-07-17 18:47:53 | [diff] [blame] | 36 | 4. 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 |
| 39 | 5. 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 | ``` |
| 50 | 6. 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-Noble | 4aeb2e6 | 2021-05-18 22:42:25 | [diff] [blame] | 61 | |
| 62 | # Configuration |
| 63 | |
| 64 | The repository `.bazelrc` will import user-specific settings from a |
| 65 | `user.bazelrc` file (in addition to the standard locations). Adding your typical |
| 66 | config setting is recommended. |
| 67 | |
| 68 | ```.bazelrc |
| 69 | build --config=generic_clang |
| 70 | ``` |
| 71 | |
| 72 | You can enable |
Quinn Pham | c71fbdd | 2021-11-03 19:41:24 | [diff] [blame] | 73 | [disk caching](https://docs.bazel.build/versions/main/remote-caching.html#disk-cache), |
Geoffrey Martin-Noble | 4aeb2e6 | 2021-05-18 22:42:25 | [diff] [blame] | 74 | which will cache build results |
| 75 | |
| 76 | ```.bazelrc |
| 77 | build --disk_cache=~/.cache/bazel-disk-cache |
| 78 | ``` |
| 79 | |
| 80 | You can instruct Bazel to use a ramdisk for its sandboxing operations via |
Quinn Pham | c71fbdd | 2021-11-03 19:41:24 | [diff] [blame] | 81 | [--sandbox_base](https://docs.bazel.build/versions/main/command-line-reference.html#flag--sandbox_base), |
Arthur Eubanks | b04d01c | 2022-08-22 17:02:30 | [diff] [blame] | 82 | which can help avoid IO bottlenecks for the symlink strategy used for |
Geoffrey Martin-Noble | 4aeb2e6 | 2021-05-18 22:42:25 | [diff] [blame] | 83 | sandboxing. This is especially important with many inputs and many cores (see |
| 84 | https://github.com/bazelbuild/bazel/issues/11868): |
| 85 | |
| 86 | ```.bazelrc |
| 87 | build --sandbox_base=/dev/shm |
| 88 | ``` |
| 89 | |
| 90 | Bear in mind that this requires that your ramdisk is of sufficient size to hold |
| 91 | any temporary files. Anecdotally, 1GB should be sufficient. |
| 92 | |
| 93 | # Coverage |
| 94 | |
| 95 | The LLVM, MLIR, and Clang subprojects have configurations for Linux (Clang and |
| 96 | GCC), Mac (Clang and GCC), and Windows (MSVC). Configuration options that are |
| 97 | platform-specific are selected for in defines. Many are also hardcoded to the |
| 98 | values currently used by all supported configurations. If there is a |
| 99 | configuration you'd like to use that isn't supported, please send a patch. |
| 100 | |
Geoffrey Martin-Noble | 088fbc0 | 2022-02-11 16:59:11 | [diff] [blame] | 101 | # Continuous Testing |
| 102 | |
Arthur Eubanks | 3f2f23c | 2022-08-11 21:12:34 | [diff] [blame] | 103 | A [Buildkite pipeline](https://buildkite.com/llvm-project/upstream-bazel) |
Geoffrey Martin-Noble | 088fbc0 | 2022-02-11 16:59:11 | [diff] [blame] | 104 | runs the full Bazel build on every commit to the main branch. Notifications of |
| 105 | failures are sent to the |
| 106 | [llvm-bazel-alerts google group](https://groups.google.com/g/llvm-bazel-alerts), |
| 107 | which anyone is free to join. Currently, the behavior is just to send an email |
| 108 | on each failure using Buildkite's built-in notification system, so if you |
| 109 | subscribe, it is highly recommended that you set up email filters or some other |
| 110 | mechanism to not flood your inbox. More sophisticated notifications, e.g. only |
| 111 | on status change or routed based on blamelist are TODO (contributions welcome). |
| 112 | |
Geoffrey Martin-Noble | 088fbc0 | 2022-02-11 16:59:11 | [diff] [blame] | 113 | # Usage in Downstream Projects |
Geoffrey Martin-Noble | 4aeb2e6 | 2021-05-18 22:42:25 | [diff] [blame] | 114 | |
Geoffrey Martin-Noble | 9cc1ddd | 2021-06-30 23:46:55 | [diff] [blame] | 115 | To use in dependent projects using Bazel, you can import LLVM and then use the |
| 116 | provided configuration rule. See example usage in the `examples/` directory. |