Skip to content

Commit 4aeb2e6

Browse files
committed
Introduce a Bazel build configuration
This patch introduces configuration for a Bazel BUILD in a side directory in the monorepo. This is following the approval of https://github.com/llvm/llvm-www/blob/main/proposals/LP0002-BazelBuildConfiguration.md As detailed in the README, the Bazel BUILD is not supported by the community in general, and is maintained only by interested parties. It follows the requirements of the LLVM peripheral tier: https://llvm.org/docs/SupportPolicy.html#peripheral-tier. This is largely copied from https://github.com/google/llvm-bazel, with a few filepath tweaks and the addition of the README. Reviewed By: echristo, keith, dblaikie, kuhar Differential Revision: https://reviews.llvm.org/D90352
1 parent 64cf5eb commit 4aeb2e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+17520
-0
lines changed

utils/bazel/.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
llvm-project-overlay

utils/bazel/.bazelrc

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
###############################################################################
6+
# Common flags that apply to all configurations.
7+
# Use sparingly for things common to all compilers and platforms.
8+
###############################################################################
9+
# Prevent invalid caching if input files are modified during a build.
10+
build --experimental_guard_against_concurrent_changes
11+
12+
###############################################################################
13+
# Options to select different strategies for linking potential dependent
14+
# libraries. The default leaves it disabled.
15+
###############################################################################
16+
17+
build:zlib_external --repo_env=BAZEL_LLVM_ZLIB_STRATEGY=external
18+
build:zlib_system --repo_env=BAZEL_LLVM_ZLIB_STRATEGY=system
19+
20+
build:terminfo_external --repo_env=BAZEL_LLVM_TERMINFO_STRATEGY=external
21+
build:terminfo_system --repo_env=BAZEL_LLVM_TERMINFO_STRATEGY=system
22+
23+
###############################################################################
24+
# Options for "generic_clang" builds: these options should generally apply to
25+
# builds using a Clang-based compiler, and default to the `clang` executable on
26+
# the `PATH`. While these are provided for convenience and may serve as a
27+
# reference, it would be preferable for users to configure an explicit C++
28+
# toolchain instead of relying on `.bazelrc` files.
29+
###############################################################################
30+
31+
# Set the default compiler to the `clang` binary on the `PATH`.
32+
build:generic_clang --repo_env=CC=clang
33+
34+
# C++14 standard version is required.
35+
build:generic_clang --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
36+
37+
# The Clang available on MacOS has a warning that isn't clean on MLIR code. The
38+
# warning doesn't show up with more recent Clangs, so just disable for now.
39+
build:generic_clang --cxxopt=-Wno-range-loop-analysis --host_cxxopt=-Wno-range-loop-analysis
40+
41+
# Use `-Wall` and `-Werror` for Clang.
42+
build:generic_clang --copt=-Wall --copt=-Werror --host_copt=-Wall --host_copt=-Werror
43+
# This doesn't appear to be enforced by any upstream bot.
44+
build:generic_clang --copt=-Wno-unused --host_copt=-Wno-unused
45+
46+
###############################################################################
47+
# Options for "generic_gcc" builds: these options should generally apply to
48+
# builds using a GCC-based compiler, and default to the `gcc` executable on
49+
# the `PATH`. While these are provided for convenience and may serve as a
50+
# reference, it would be preferable for users to configure an explicit C++
51+
# toolchain instead of relying on `.bazelrc` files.
52+
###############################################################################
53+
54+
# Set the default compiler to the `gcc` binary on the `PATH`.
55+
build:generic_gcc --repo_env=CC=gcc
56+
57+
# C++14 standard version is required.
58+
build:generic_gcc --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
59+
60+
# Disable GCC warnings that are noisy and/or false positives on LLVM code.
61+
# These need to be global as some code triggering these is in header files.
62+
build:generic_gcc --copt=-Wno-unused-parameter --host_copt=-Wno-unused-parameter
63+
build:generic_gcc --copt=-Wno-comment --host_copt=-Wno-comment
64+
build:generic_gcc --cxxopt=-Wno-class-memaccess --host_cxxopt=-Wno-class-memaccess
65+
build:generic_gcc --copt=-Wno-maybe-uninitialized --host_copt=-Wno-maybe-uninitialized
66+
build:generic_gcc --copt=-Wno-misleading-indentation --host_copt=-Wno-misleading-indentation
67+
68+
# Use `-Werror` for GCC to make sure warnings don't slip past.
69+
build:generic_gcc --copt=-Werror --host_copt=-Werror
70+
71+
###############################################################################
72+
# Windows specific flags for building with VC.
73+
###############################################################################
74+
75+
build:msvc --copt=/WX --host_copt=/WX # Treat warnings as errors...
76+
# ...but disable the ones that are violated
77+
build:msvc --copt=/wd4141 --host_copt=/wd4141 # inline used more than once
78+
build:msvc --copt=/wd4244 --host_copt=/wd4244 # conversion type -> type
79+
build:msvc --copt=/wd4267 --host_copt=/wd4267 # conversion size_t -> type
80+
build:msvc --copt=/wd4273 --host_copt=/wd4273 # multiple definitions with different dllimport
81+
build:msvc --copt=/wd4319 --host_copt=/wd4319 # zero-extending after complement
82+
build:msvc --copt=/wd4624 --host_copt=/wd4624 # destructor was implicitly defined as deleted
83+
build:msvc --copt=/wd4804 --host_copt=/wd4804 # comparisons between bool and int
84+
build:msvc --copt=/wd4805 --host_copt=/wd4805 # comparisons between bool and int
85+
86+
build:msvc --linkopt=/WX --host_linkopt=/WX # Treat warnings as errors...
87+
# ...but disable the ones that are violated.
88+
build:msvc --linkopt=/IGNORE:4001 --host_linkopt=/IGNORE:4001 # no object files
89+
90+
# Yay for security warnings. Boo for non-standard.
91+
build:msvc --copt=/D_CRT_SECURE_NO_WARNINGS --host_copt=/D_CRT_SECURE_NO_WARNINGS
92+
93+
###############################################################################
94+
95+
###############################################################################
96+
# Configuration for building remotely using Remote Build Execution (RBE)
97+
# Based on https://github.com/bazelbuild/bazel-toolchains/blob/master/bazelrc/bazel-1.0.0.bazelrc
98+
###############################################################################
99+
100+
build:rbe --remote_instance_name=projects/llvm-bazel/instances/default_instance
101+
102+
# Depending on how many machines are in the remote execution instance, setting
103+
# this higher can make builds faster by allowing more jobs to run in parallel.
104+
# Setting it too high can result in jobs that timeout, however, while waiting
105+
# for a remote machine to execute them.
106+
build:rbe --jobs=150
107+
108+
# Set several flags related to specifying the platform, toolchain and java
109+
# properties.
110+
# These flags should only be used as is for the rbe-ubuntu16-04 container
111+
# and need to be adapted to work with other toolchain containers.
112+
build:rbe --host_javabase=@rbe_default//java:jdk
113+
build:rbe --javabase=@rbe_default//java:jdk
114+
build:rbe --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8
115+
build:rbe --java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8
116+
build:rbe --crosstool_top=@rbe_default//cc:toolchain
117+
build:rbe --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
118+
# Platform flags:
119+
# The toolchain container used for execution is defined in the target indicated
120+
# by "extra_execution_platforms", "host_platform" and "platforms".
121+
# More about platforms: https://docs.bazel.build/versions/master/platforms.html
122+
build:rbe --extra_toolchains=@rbe_default//config:cc-toolchain
123+
build:rbe --extra_execution_platforms=@rbe_default//config:platform
124+
build:rbe --host_platform=@rbe_default//config:platform
125+
build:rbe --platforms=@rbe_default//config:platform
126+
127+
build:rbe --define=EXECUTOR=remote
128+
129+
# Enable remote execution so actions are performed on the remote systems.
130+
build:rbe --remote_executor=grpcs://remotebuildexecution.googleapis.com
131+
132+
# Enforce stricter environment rules, which eliminates some non-hermetic
133+
# behavior and therefore improves both the remote cache hit rate and the
134+
# correctness and repeatability of the build.
135+
build:rbe --incompatible_strict_action_env=true
136+
137+
# Set a higher timeout value, just in case.
138+
build:rbe --remote_timeout=3600
139+
140+
# Local disk cache is incompatible with remote execution (for obvious reasons).
141+
build:rbe --disk_cache=""
142+
143+
# Enable authentication. This will pick up application default credentials by
144+
# default. You can use --google_credentials=some_file.json to use a service
145+
# account credential instead.
146+
build:rbe --google_default_credentials=true
147+
148+
# The user.bazelrc file is not checked in but available for local mods.
149+
# Always keep this at the end of the file so that user flags override.
150+
try-import %workspace%/user.bazelrc

utils/bazel/.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.0.0

utils/bazel/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Bazel artifacts
2+
/bazel-*
3+
4+
# Per-user bazelrc files
5+
user.bazelrc

utils/bazel/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
# Required to reference .bzl files in this package

utils/bazel/README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.
29+
2. Install Bazel at the version indicated by [.bazelversion](./bazelversion),
30+
following the official instructions, if you don't have it installed yet:
31+
https://docs.bazel.build/versions/master/install.html.
32+
3. `cd utils/bazel`
33+
4. `bazel build --config=generic_clang @llvm-project//...` (if building on Unix
34+
with Clang). `--config=generic_gcc` and `--config=msvc` are also available.
35+
36+
37+
# Configuration
38+
39+
The repository `.bazelrc` will import user-specific settings from a
40+
`user.bazelrc` file (in addition to the standard locations). Adding your typical
41+
config setting is recommended.
42+
43+
```.bazelrc
44+
build --config=generic_clang
45+
```
46+
47+
You can enable
48+
[disk caching](https://docs.bazel.build/versions/master/remote-caching.html#disk-cache),
49+
which will cache build results
50+
51+
```.bazelrc
52+
build --disk_cache=~/.cache/bazel-disk-cache
53+
```
54+
55+
You can instruct Bazel to use a ramdisk for its sandboxing operations via
56+
[--sandbox_base](https://docs.bazel.build/versions/master/command-line-reference.html#flag--sandbox_base),
57+
which can help avoid IO bottlenecks for the symlink stragegy used for
58+
sandboxing. This is especially important with many inputs and many cores (see
59+
https://github.com/bazelbuild/bazel/issues/11868):
60+
61+
```.bazelrc
62+
build --sandbox_base=/dev/shm
63+
```
64+
65+
Bear in mind that this requires that your ramdisk is of sufficient size to hold
66+
any temporary files. Anecdotally, 1GB should be sufficient.
67+
68+
# Coverage
69+
70+
The LLVM, MLIR, and Clang subprojects have configurations for Linux (Clang and
71+
GCC), Mac (Clang and GCC), and Windows (MSVC). Configuration options that are
72+
platform-specific are selected for in defines. Many are also hardcoded to the
73+
values currently used by all supported configurations. If there is a
74+
configuration you'd like to use that isn't supported, please send a patch.
75+
76+
# Usage
77+
78+
To use in dependent projects using Bazel, you can import LLVM (e.g. as a
79+
submodule or using `http_archive`) and then use the provided configuration rule.
80+
81+
FIXME: This needs to be updated to a commit that exists once such a commit
82+
exists.
83+
FIXME: It shouldn't be necessary to configure `http_archive` twice for the same
84+
archive (though caching means this isn't too expensive).
85+
86+
```starlark
87+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
88+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
89+
90+
LLVM_COMMIT = "0a1f0ee78122fc0642e8a1a18e1b2bc89c813387"
91+
92+
LLVM_SHA256 = "4f59737ccfdad2cfb4587d796ce97c1eb5433de7ea0f57f248554b83e92d81d2"
93+
94+
http_archive(
95+
name = "llvm-project-raw",
96+
build_file_content = "#empty",
97+
sha256 = LLVM_SHA256,
98+
strip_prefix = "llvm-project-" + LLVM_COMMIT,
99+
urls = ["https://github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT)],
100+
)
101+
102+
http_archive(
103+
name = "llvm-bazel",
104+
sha256 = LLVM_SHA256,
105+
strip_prefix = "llvm-project-{}/utils/bazel".format(LLVM_COMMIT),
106+
urls = ["https://github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT)],
107+
)
108+
109+
load("@llvm-bazel//:configure.bzl", "llvm_configure")
110+
111+
llvm_configure(
112+
name = "llvm-project",
113+
src_path = ".",
114+
src_workspace = "@llvm-project-raw//:WORKSPACE",
115+
)
116+
117+
load("@llvm-bazel//:terminfo.bzl", "llvm_terminfo_system")
118+
119+
maybe(
120+
llvm_terminfo_system,
121+
name = "llvm_terminfo",
122+
)
123+
124+
load("@llvm-bazel//:zlib.bzl", "llvm_zlib_system")
125+
126+
maybe(
127+
llvm_zlib_system,
128+
name = "llvm_zlib",
129+
)
130+
```

utils/bazel/WORKSPACE

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
load(":configure.bzl", "llvm_configure")
6+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
7+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
8+
9+
llvm_configure(
10+
name = "llvm-project",
11+
overlay_path = "llvm-project-overlay",
12+
src_path = "../..",
13+
)
14+
15+
load(":terminfo.bzl", "llvm_terminfo_from_env")
16+
17+
maybe(
18+
llvm_terminfo_from_env,
19+
name = "llvm_terminfo",
20+
)
21+
22+
maybe(
23+
http_archive,
24+
name = "zlib",
25+
build_file = "//third_party_build:zlib.BUILD",
26+
sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
27+
strip_prefix = "zlib-1.2.11",
28+
urls = [
29+
"https://storage.googleapis.com/mirror.tensorflow.org/zlib.net/zlib-1.2.11.tar.gz",
30+
"https://zlib.net/zlib-1.2.11.tar.gz",
31+
],
32+
)
33+
34+
load(":zlib.bzl", "llvm_zlib_from_env")
35+
36+
maybe(
37+
llvm_zlib_from_env,
38+
name = "llvm_zlib",
39+
external_zlib = "@zlib",
40+
)
41+
42+
maybe(
43+
http_archive,
44+
name = "vulkan_headers",
45+
build_file = "//third_party_build:vulkan_headers.BUILD",
46+
sha256 = "19f491784ef0bc73caff877d11c96a48b946b5a1c805079d9006e3fbaa5c1895",
47+
strip_prefix = "Vulkan-Headers-9bd3f561bcee3f01d22912de10bb07ce4e23d378",
48+
urls = [
49+
"https://github.com/KhronosGroup/Vulkan-Headers/archive/9bd3f561bcee3f01d22912de10bb07ce4e23d378.tar.gz",
50+
],
51+
)
52+
53+
load(":vulkan_sdk.bzl", "vulkan_sdk_setup")
54+
55+
maybe(
56+
vulkan_sdk_setup,
57+
name = "vulkan_sdk",
58+
)
59+
60+
http_archive(
61+
name = "bazel_skylib",
62+
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
63+
urls = [
64+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
65+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
66+
],
67+
)
68+
69+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
70+
71+
bazel_skylib_workspace()
72+
73+
http_archive(
74+
name = "bazel_toolchains",
75+
sha256 = "1adf5db506a7e3c465a26988514cfc3971af6d5b3c2218925cd6e71ee443fc3f",
76+
strip_prefix = "bazel-toolchains-4.0.0",
77+
urls = [
78+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/releases/download/4.0.0/bazel-toolchains-4.0.0.tar.gz",
79+
"https://github.com/bazelbuild/bazel-toolchains/releases/download/4.0.0/bazel-toolchains-4.0.0.tar.gz",
80+
],
81+
)
82+
83+
load("@bazel_toolchains//rules:rbe_repo.bzl", "rbe_autoconfig")
84+
rbe_autoconfig(name = "rbe_default")

0 commit comments

Comments
 (0)