Skip to content

Commit ef31d2c

Browse files
author
Siddhartha Bagaria
committed
Refresh README for release 0.6
1 parent c8cf342 commit ef31d2c

File tree

1 file changed

+153
-44
lines changed

1 file changed

+153
-44
lines changed

README.md

Lines changed: 153 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,31 @@ LLVM toolchain for Bazel [![Tests](https://github.com/grailbio/bazel-toolchain/a
33

44
-------
55

6-
Required minimum bazel version: 4.0.0
6+
The project is in a relatively stable state and in use for all code development
7+
at GRAIL and other organizations. Having said that, I am unable to give time to
8+
it at any regular cadence.
9+
10+
I rely on the community for maintenance and new feature implementations. If you
11+
are interested in being part of this project, please let me know and I can give
12+
you write access, so you can merge your changes directly.
13+
14+
If you feel like you have a better maintained fork or an alternative/derived
15+
implementation, please let me know and I can redirect people there.
16+
17+
@siddharthab
718

819
-------
920

21+
## Quickstart
22+
23+
Minimum bazel version: **4.0.0**
24+
1025
To use this toolchain, include this section in your WORKSPACE:
1126
```python
12-
# Change master to the git tag you want.
1327
http_archive(
1428
name = "com_grail_bazel_toolchain",
15-
strip_prefix = "bazel-toolchain-master",
16-
urls = ["https://github.com/grailbio/bazel-toolchain/archive/master.tar.gz"],
29+
strip_prefix = "bazel-toolchain-0.6",
30+
urls = ["https://github.com/grailbio/bazel-toolchain/archive/0.6.tar.gz"],
1731
)
1832

1933
load("@com_grail_bazel_toolchain//toolchain:deps.bzl", "bazel_toolchain_dependencies")
@@ -24,14 +38,22 @@ load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "llvm_toolchain")
2438

2539
llvm_toolchain(
2640
name = "llvm_toolchain",
27-
llvm_version = "8.0.0",
41+
llvm_version = "12.0.0",
2842
)
2943

3044
load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains")
3145

3246
llvm_register_toolchains()
3347
```
3448

49+
And add the following section to your .bazelrc file (not needed after
50+
this [issue](https://github.com/bazelbuild/bazel/issues/7260) is closed):
51+
```
52+
build --incompatible_enable_cc_toolchain_resolution
53+
```
54+
55+
## Basic Usage
56+
3557
The toolchain can automatically detect your OS and arch type, and use the right
3658
pre-built binary distribution from llvm.org. The detection is currently
3759
based on host OS and is not perfect, so some distributions, docker based
@@ -42,49 +64,136 @@ grow through community contributions. We welcome PRs! :smile:
4264
See in-code documentation in [rules.bzl](toolchain/rules.bzl) for available
4365
attributes to `llvm_toolchain`.
4466

45-
For making changes to default settings for these toolchains, edit the
46-
cc_toolchain_config template. See [tutorial](
47-
https://github.com/bazelbuild/bazel/blob/master/site/docs/tutorial/cc-toolchain-config.md).
67+
## Advanced Usage
68+
69+
#### Customizations
70+
71+
We currently offer limited customizability through attributes of the
72+
[llvm_toolchain_\* rules](toolchain/rules.bzl). You can send us a PR to add
73+
more configuration attributes.
74+
75+
A majority of the complexity of this project is to make it generic for multiple
76+
use cases. For one-off experiments with new architectures, cross-compilations,
77+
new compiler features, etc., my advice would be to look at the toolchain
78+
configurations generated by this repo, and copy-paste/edit to make your own in
79+
any package in your own workspace.
80+
81+
```
82+
bazel query --output=build @llvm_toolchain//:all | grep -v -e '^#' -e '^ generator'
83+
```
84+
85+
Besides defining your toolchain in your package BUILD file, and until this
86+
[issue](https://github.com/bazelbuild/bazel/issues/7746) is resolved, you would
87+
also need a way for bazel to access the tools in LLVM distribution as relative
88+
paths from your package without using `..` up-references. For this, you can
89+
create a symlink that uses up-references to point to the LLVM distribution
90+
directory, and also create a wrapper script for clang such that the actual
91+
clang invocation is not through the symlinked path. See the files in the
92+
`@llvm_toolchain//:` package as a reference.
93+
94+
```
95+
# See generated files for reference.
96+
ls -lR "$(bazel info output_base)/external/llvm_toolchain"
97+
98+
# Create symlink to LLVM distribution.
99+
cd _your_package_directory_
100+
ln -s ../....../external/llvm_toolchain_llvm llvm
101+
102+
# Create CC wrapper script.
103+
mkdir bin
104+
cp "$(bazel info output_base)/external/llvm_toolchain/bin/cc_wrapper.sh" bin/cc_wrapper.sh
105+
vim bin/cc_wrapper.sh # Review to ensure relative paths, etc. are good.
106+
```
107+
108+
See [bazel
109+
tutorial](https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html)
110+
for how CC toolchains work in general.
111+
112+
#### Selecting Toolchains
113+
114+
If toolchains are registered (see Quickstart section above), you do not need to
115+
do anything special for bazel to find the toolchain. You may want to check once
116+
with the `--toolchain_resolution_debug` flag to see which toolchains were
117+
selected by bazel for your target platform.
48118

49119
For overriding toolchains on the command line, please use the
50-
`--extra_toolchains` flag in lieu of the deprecated `--crosstool_top` flag.
51-
For example, `--extra_toolchains=@llvm_toolchain//:cc-toolchain-linux`. You
52-
may need to also set the `--incompatible_enable_cc_toolchain_resolution` flag.
120+
`--extra_toolchains` flag. For example,
121+
`--extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux`.
53122

54123
If you would like to use the older method of selecting toolchains, you can
55-
continue to do so with `--crosstool_top=@llvm_toolchain//:toolchain`.
56-
57-
Notes:
58-
59-
- The LLVM toolchain archive is downloaded and extracted as a separate
60-
repository with the suffix `_llvm`. Alternatively, you can also specify your
61-
own repositories for each host os-arch pair through the `toolchain_roots`
62-
attributes. Each of these repositories is typically configured through
63-
`local_repository` or `http_archive` (with `build_file` attribute as
64-
`@com_grail_bazel_toolchain//toolchain:BUILD.llvm_repo`).
65-
66-
- A sysroot can be specified through the `sysroot` attribute. This can be either
67-
a path on the user's system, or a bazel `filegroup` like label. One way to
68-
create a sysroot is to use `docker export` to get a single archive of the
69-
entire filesystem for the image you want. Another way is to use the build
70-
scripts provided by the
71-
[Chromium project](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/linux_sysroot.md).
72-
73-
- Sandboxing the toolchain introduces a significant overhead (100ms per
74-
action, as of mid 2018). To overcome this, one can use
75-
`--experimental_sandbox_base=/dev/shm`. However, not all environments might
76-
have enough shared memory available to load all the files in memory. If this
77-
is a concern, you may set the attribute for using absolute paths, which will
78-
substitute templated paths to the toolchain as absolute paths. When running
79-
bazel actions, these paths will be available from inside the sandbox as part of
80-
the / read-only mount. Note that this will make your builds non-hermetic.
81-
82-
- The toolchain is known to also work with `rules_go`.
83-
84-
- The LLVM toolchain also provides several tools like `clang-format`. You can
85-
depend on these tools directly in the bin directory of the toolchain. For
86-
example, `@llvm_toolchain_llvm//:bin/clang-format` is a valid and visible
87-
target.
124+
continue to do so with `--crosstool_top=@llvm_toolchain//:toolchain` instead
125+
of the `--incompatible_enable_cc_toolchain_resolution` flag. Follow
126+
[bazelbuild/bazel#7260](https://github.com/bazelbuild/bazel/issues/7260) for
127+
any changes in this behavior.
128+
129+
#### Bring Your Own LLVM
130+
131+
The LLVM toolchain archive is downloaded and extracted as a separate repository
132+
with the suffix `_llvm`. Alternatively, you can also specify your own
133+
repositories for each host os-arch pair through the `toolchain_roots`
134+
attributes. Each of these repositories is typically configured through
135+
`local_repository` or `http_archive` (with `build_file` attribute as
136+
`@com_grail_bazel_toolchain//toolchain:BUILD.llvm_repo`).
137+
138+
#### Sysroots
139+
140+
A sysroot can be specified through the `sysroot` attribute. This can be either
141+
a path on the user's system, or a bazel `filegroup` like label. One way to
142+
create a sysroot is to use `docker export` to get a single archive of the
143+
entire filesystem for the image you want. Another way is to use the build
144+
scripts provided by the [Chromium
145+
project](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/linux_sysroot.md).
146+
147+
#### Cross-compilation
148+
149+
The toolchain supports cross-compilation if you bring your own sysroot. When
150+
cross-compiling, we link against the libstdc++ from the sysroot
151+
(single-platform build behavior is to link against libc++ bundled with LLVM).
152+
The following pairs have been tested to work for some hello-world binaries:
153+
- {linux, x86_64} -> {linux, aarch64}
154+
- {linux, aarch64} -> {linux, x86_64}
155+
- {darwin, x86_64} -> {linux, x86_64}
156+
- {darwin, x86_64} -> {linux, aarch64}
157+
158+
A recommended approach would be to define two toolchains, one without sysroot
159+
for single-platform builds, and one with sysroot for cross-compilation builds.
160+
Then, when cross-compiling, explicitly specify the toolchain with the sysroot
161+
and the target platform. For example, see the [WORKSPACE](WORKSPACE) file and
162+
the [test script](tests/scripts/run_xcompile_tests.sh) for cross-compilation.
163+
```
164+
bazel build \
165+
--platforms=@com_grail_bazel_toolchain//platforms:linux-x86_64 \
166+
--extra_toolchains=@llvm_toolchain_with_sysroot//:cc-toolchain-x86_64-linux \
167+
//...
168+
```
169+
170+
#### Sandbox
171+
172+
Sandboxing the toolchain introduces a significant overhead (100ms per action,
173+
as of mid 2018). To overcome this, one can use
174+
`--experimental_sandbox_base=/dev/shm`. However, not all environments might
175+
have enough shared memory available to load all the files in memory. If this is
176+
a concern, you may set the attribute for using absolute paths, which will
177+
substitute templated paths to the toolchain as absolute paths. When running
178+
bazel actions, these paths will be available from inside the sandbox as part of
179+
the / read-only mount. Note that this will make your builds non-hermetic.
180+
181+
#### Compatibility
182+
183+
The toolchain is tested to work with `rules_go` and `rules_foreign_cc`.
184+
185+
#### Accessing tools
186+
187+
The LLVM distribution also provides several tools like `clang-format`. You can
188+
depend on these tools directly in the bin directory of the distribution. When
189+
using the auto-configured download (not using `toolchain_roots`), the
190+
distribution is available in the repo with the suffix `_llvm` appended to the
191+
name you used for the `llvm_toolchain` rule. For example,
192+
`@llvm_toolchain_llvm//:bin/clang-format` is a valid and visible target in the
193+
quickstart example above.
194+
195+
196+
## Prior Art
88197

89198
Other examples of toolchain configuration:
90199

0 commit comments

Comments
 (0)