blob: 4021477e207eb17addb57915b488af7ecf69b0b9 [file] [log] [blame] [view]
David Chen4c7f35d2015-06-18 12:28:02 +00001# Rust Rules for Bazel
2
3## Overview
4
5These build rules are used for building [Rust][rust] projects with Bazel.
6
7* [Setup](#setup)
8* [Basic Example](#basic-example)
9* [Build Rule Reference](#reference)
10 * [`rust_library`](#reference-rust_library)
11 * [`rust_binary`](#reference-rust_binary)
12 * [`rust_test`](#reference-rust_test)
13* [Roadmap](#roadmap)
14
15[rust]: http://www.rust-lang.org/
16
17<a name="setup"></a>
18## Setup
19
20Install Rust following the instructions on the [Rust website][rust]. The version
21of Rust currently supported by these build rules is Rust 1.0.0. These build
22rules also assume that Rust is installed either either `/usr` or `/usr/local`.
23
24<a name="basic-example"></a>
25## Basic Example
26
27Suppose you have the following directory structure for a simple Rust library
28crate:
29
30```
31[workspace]/
32 WORKSPACE
33 hello_lib/
34 BUILD
35 src/
36 greeter.rs
37 lib.rs
38```
39
40`hello_lib/src/greeter.rs`:
41
42```rust
43pub struct Greeter {
44 greeting: String,
45}
46
47impl Greeter {
48 pub fn new(greeting: &str) -> Greeter {
49 Greeter { greeting: greeting.to_string(), }
50 }
51
52 pub fn greet(&self, thing: &str) {
53 println!("{} {}", &self.greeting, thing);
54 }
55}
56```
57
58`hello_lib/src/lib.rs`:
59
60
61```rust
62pub mod greeter;
63```
64
65`hello_lib/BUILD`:
66
67```python
68package(default_visibility = ["//visibility:public"])
69
70load("/tools/build_rules/rust/rust", "rust_library")
71
72rust_library(
73 name = "hello_lib",
74 srcs = [
75 "src/greeter.rs",
76 "src/lib.rs",
77 ],
78)
79```
80
81Build the library:
82
83```
84$ bazel build //hello_lib
85INFO: Found 1 target...
86Target //examples/rust/hello_lib:hello_lib up-to-date:
87 bazel-bin/examples/rust/hello_lib/libhello_lib.rlib
88INFO: Elapsed time: 1.245s, Critical Path: 1.01s
89```
90
91Now, let's add a binary crate that uses the `hello_lib` library. The directory
92structure now looks like the following:
93
94```
95[workspace]/
96 WORKSPACE
97 hello_lib/
98 BUILD
99 src/
100 greeter.rs
101 lib.rs
102 hello_world/
103 BUILD
104 src/
105 main.rs
106```
107
108`hello_world/src/main.rs`:
109
110```rust
111extern crate hello_lib;
112
113use hello_lib::greeter;
114
115fn main() {
116 let hello = greeter::Greeter::new("Hello");
117 hello.greet("world");
118}
119```
120
121`hello_world/BUILD`:
122
123```python
124load("/tools/build_rules/rust/rust", "rust_binary")
125
126rust_binary(
127 name = "hello_world",
128 srcs = ["src/main.rs"],
129 deps = ["//hello_lib"],
130)
131```
132
133Build and run `hello_world`:
134
135```
136$ bazel run //hello_world
137INFO: Found 1 target...
138Target //examples/rust/hello_world:hello_world up-to-date:
139 bazel-bin/examples/rust/hello_world/hello_world
140INFO: Elapsed time: 1.308s, Critical Path: 1.22s
141
142INFO: Running command line: bazel-bin/examples/rust/hello_world/hello_world
143Hello world
144```
145
146<a name="reference"></a>
147## Build Rule Reference
148
149<a name="reference-rust_library"></a>
150### `rust_library`
151
152`rust_library(name, srcs, deps, data, features, rustc_flags)`
153
154<table>
155 <thead>
156 <tr>
157 <th>Attribute</th>
158 <th>Description</th>
159 </tr>
160 </thead>
161 <tbody>
162 <tr>
163 <td><code>name</code></td>
164 <td>
165 <code>Name, required</code>
166 <p>A unique name for this rule.</p>
167 <p>
168 This name will also be used as the name of the library crate built by
169 this rule.
170 </p>
171 </td>
172 </tr>
173 <tr>
174 <td><code>srcs</code></td>
175 <td>
176 <code>List of labels, required</code>
177 <p>List of Rust <code>.rs</code> source files used to build the
178 library.</p>
179 <p>
180 There must be a file either named <code>lib.rs</code> or with a name
181 matching the name of this crate. For example, if the name of a given
182 rule is <code>foo</code>, then there must be a file named
183 <code>lib.rs</code> or <code>foo.rs</code> in <code>srcs</code>.
184 This file will be passed to <code>rustc</code> as the crate root.
185 </p>
186 </td>
187 </tr>
188 <tr>
189 <td><code>deps</code></td>
190 <td>
191 <code>List of labels, optional</code>
192 <p>List of other libraries to be linked to this library target.</p>
193 <p>
194 These can be either other <code>rust_library</code> targets or
195 <code>cc_library</code> targets if linking a native library.
196 </p>
197 </td>
198 </tr>
199 <tr>
200 <td><code>data</code></td>
201 <td>
202 <code>List of labels, optional</code>
203 <p>List of files used by this rule at runtime.</p>
204 <p>
205 This attribute can be used to specify any data files that are embedded
206 into the library, such as via the
207 <a href="https://doc.rust-lang.org/std/macro.include_str!.html target="_blank"><code>include_str!</code></a>
208 macro.
209 </p>
210 </td>
211 </tr>
212 <tr>
213 <td><code>features</code></td>
214 <td>
215 <code>List of strings, optional</code>
216 <p>List of features to enable for this crate.</p>
217 <p>
218 Features are defined in the code using the
219 <code>#[cfg(feature = "foo")]</code> configuration option. The
220 features listed here will be passed to <code>rustc</code> with
221 <code>--cfg feature="${feature_name}"</code> flags.
222 </p>
223 </td>
224 </tr>
225 <tr>
226 <td><code>rustc_flags</code></td>
227 <td>
228 <code>List of strings, optional</code>
229 <p>List of compiler flags passed to <code>rustc</code>.</p>
230 </td>
231 </tr>
232 </tbody>
233</table>
234
235<a name="reference-rust_binary"></a>
236### `rust_binary`
237
238`rust_binary(name, srcs, deps, data, features, rustc_flags)`
239
240<table>
241 <thead>
242 <tr>
243 <th>Attribute</th>
244 <th>Description</th>
245 </tr>
246 </thead>
247 <tbody>
248 <tr>
249 <td><code>name</code></td>
250 <td>
251 <code>Name, required</code>
252 <p>A unique name for this rule.</p>
253 <p>
254 This name will also be used as the name of the binary crate built by
255 this rule.
256 </p>
257 </td>
258 </tr>
259 <tr>
260 <td><code>srcs</code></td>
261 <td>
262 <code>List of labels, required</code>
263 <p>List of Rust <code>.rs</code> source files used to build the
264 binary.</p>
265 <p>
266 There must be a file either named <code>main.rs</code> or with a name
267 matching the name of this crate that contains the <code>main</code>
268 function. For example, if the name of a given
269 rule is <code>foo</code>, then there must be a file named
270 <code>main.rs</code> or <code>foo.rs</code> in <code>srcs</code>.
271 This file will be passed to <code>rustc</code> as the crate root.
272 </p>
273 </td>
274 </tr>
275 <tr>
276 <td><code>deps</code></td>
277 <td>
278 <code>List of labels, optional</code>
279 <p>List of other libraries to be linked to this library target.</p>
280 <p>
281 These must be <code>rust_library</code> targets.
282 </p>
283 </td>
284 </tr>
285 <tr>
286 <td><code>data</code></td>
287 <td>
288 <code>List of labels, optional</code>
289 <p>List of files used by this rule at runtime.</p>
290 <p>
291 This attribute can be used to specify any data files that are embedded
292 into the library, such as via the
293 <a href="https://doc.rust-lang.org/std/macro.include_str!.html target="_blank"><code>include_str!</code></a>
294 macro.
295 </p>
296 </td>
297 </tr>
298 <tr>
299 <td><code>features</code></td>
300 <td>
301 <code>List of strings, optional</code>
302 <p>List of features to enable for this crate.</p>
303 <p>
304 Features are defined in the code using the
305 <code>#[cfg(feature = "foo")]</code> configuration option. The
306 features listed here will be passed to <code>rustc</code> with
307 <code>--cfg feature="${feature_name}"</code> flags.
308 </p>
309 </td>
310 </tr>
311 <tr>
312 <td><code>rustc_flags</code></td>
313 <td>
314 <code>List of strings, optional</code>
315 <p>List of compiler flags passed to <code>rustc</code>.</p>
316 </td>
317 </tr>
318 </tbody>
319</table>
320
321<a name="reference-rust_test"></a>
322### `rust_test`
323
324`rust_test(name, srcs, deps, data, features, rustc_flags)`
325
326<table>
327 <thead>
328 <tr>
329 <th>Attribute</th>
330 <th>Description</th>
331 </tr>
332 </thead>
333 <tbody>
334 <tr>
335 <td><code>name</code></td>
336 <td>
337 <code>Name, required</code>
338 <p>A unique name for this rule.</p>
339 <p>
340 This name will also be used as the name of the binary test crate
341 built by this rule.
342 </p>
343 </td>
344 </tr>
345 <tr>
346 <td><code>srcs</code></td>
347 <td>
348 <code>List of labels, required</code>
349 <p>List of Rust <code>.rs</code> source files used to build the
350 library.</p>
351 <p>
352 There must be a file either with a name matching the name of this
353 test. For example, if the name of a <code>rust_test</code> rule is
354 <code>foo</code>, then there must be a file named <code>foo.rs</code>
355 in <code>srcs</code>. This file will be passed to <code>rustc</code>
356 as the crate root.
357 </p>
358 </td>
359 </tr>
360 <tr>
361 <td><code>deps</code></td>
362 <td>
363 <code>List of labels, optional</code>
364 <p>List of other libraries to be linked to this test target.</p>
365 <p>
366 These must be <code>rust_library</code> targets.
367 </p>
368 </td>
369 </tr>
370 <tr>
371 <td><code>data</code></td>
372 <td>
373 <code>List of labels, optional</code>
374 <p>List of files used by this rule at runtime.</p>
375 <p>
376 This attribute can be used to specify any data files that are embedded
377 into the library, such as via the
378 <a href="https://doc.rust-lang.org/std/macro.include_str!.html target="_blank"><code>include_str!</code></a>
379 macro.
380 </p>
381 </td>
382 </tr>
383 <tr>
384 <td><code>features</code></td>
385 <td>
386 <code>List of strings, optional</code>
387 <p>List of features to enable for this crate.</p>
388 <p>
389 Features are defined in the code using the
390 <code>#[cfg(feature = "foo")]</code> configuration option. The
391 features listed here will be passed to <code>rustc</code> with
392 <code>--cfg feature="${feature_name}"</code> flags.
393 </p>
394 </td>
395 </tr>
396 <tr>
397 <td><code>rustc_flags</code></td>
398 <td>
399 <code>List of strings, optional</code>
400 <p>List of compiler flags passed to <code>rustc</code>.</p>
401 </td>
402 </tr>
403 </tbody>
404</table>
405
406<a name="#roadmap"></a>
407## Roadmap
408
409### Near-term roadmap
410
411* Implement `rust_bench_test` rule for running benchmarks.
412* Enable `rust_test` to depend solely on a `rust_library` since many projects
413 intermix `#[test]` methods in implementation source.
414* Improve documentation with more detailed examples.
415* Implement `rust_doc` rule for generating [rustdoc][rustdoc] documentation.
416
417[rustdoc]: https://doc.rust-lang.org/book/documentation.html#about-rustdoc
418
419### Longer-term roadmap
420
421* Add tool for taking `Cargo.toml` and generating a `WORKSPACE` file with
422 workspace rules for pulling external dependencies.
423* Improve expressiveness of features and support for [Cargo's feature
424 groups](http://doc.crates.io/manifest.html#the-[features]-section).
425* Add `cargo_crate` workspace rule for pulling crates from
426 [Cargo](https://crates.io/).