@@ -3,17 +3,31 @@ LLVM toolchain for Bazel [
18
32
19
33
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")
24
38
25
39
llvm_toolchain(
26
40
name = " llvm_toolchain" ,
27
- llvm_version = " 8 .0.0" ,
41
+ llvm_version = " 12 .0.0" ,
28
42
)
29
43
30
44
load(" @llvm_toolchain//:toolchains.bzl" , " llvm_register_toolchains" )
31
45
32
46
llvm_register_toolchains()
33
47
```
34
48
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
+
35
57
The toolchain can automatically detect your OS and arch type, and use the right
36
58
pre-built binary distribution from llvm.org. The detection is currently
37
59
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:
42
64
See in-code documentation in [ rules.bzl] ( toolchain/rules.bzl ) for available
43
65
attributes to ` llvm_toolchain ` .
44
66
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.
48
118
49
119
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 ` .
53
122
54
123
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
88
197
89
198
Other examples of toolchain configuration:
90
199
0 commit comments