# Rust Rules
<div class="toc">
<h2>Rules</h2>
<ul>
<li><a href="#rust_library">rust_library</a></li>
<li><a href="#rust_binary">rust_binary</a></li>
<li><a href="#rust_test">rust_test</a></li>
<li><a href="#rust_bench_test">rust_bench_test</a></li>
<li><a href="#rust_doc">rust_doc</a></li>
<li><a href="#rust_doc_test">rust_doc_test</a></li>
</ul>
</div>
## Overview
These build rules are used for building [Rust][rust] projects with Bazel.
[rust]: http://www.rust-lang.org/
<a name="setup"></a>
## Setup
To use the Rust rules, simply copy the contents of `rust.WORKSPACE` to your
`WORKSPACE` file.
<a name="roadmap"></a>
## Roadmap
* Add `rust_toolchain` rule to make it easy to use a custom Rust toolchain.
* Add tool for taking `Cargo.toml` and generating a `WORKSPACE` file with
workspace rules for pulling external dependencies.
* Improve expressiveness of features and support for [Cargo's feature
groups](http://doc.crates.io/manifest.html#the-[features]-section).
* Add `cargo_crate` workspace rule for pulling crates from
[Cargo](https://crates.io/).
<a name="rust_library"></a>
## rust_library
```python
rust_library(name, srcs, deps, data, crate_features, rustc_flags)
```
<table class="table table-condensed table-bordered table-params">
<colgroup>
<col class="col-param" />
<col class="param-description" />
</colgroup>
<thead>
<tr>
<th colspan="2">Attributes</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>name</code></td>
<td>
<code>Name, required</code>
<p>A unique name for this rule.</p>
<p>
This name will also be used as the name of the library crate built by
this rule.
</p>
</td>
</tr>
<tr>
<td><code>srcs</code></td>
<td>
<code>List of labels, required</code>
<p>List of Rust <code>.rs</code> source files used to build the
library.</p>
<p>
If <code>srcs</code> contains more than one file, then there must be
a file either named <code>lib.rs</code>. Otherwise,
<code>crate_root</code> must be set to the source file that is the
root of the crate to be passed to <code>rustc</code> to build this
crate.
</p>
</td>
</tr>
<tr>
<td><code>crate_root</code></td>
<td>
<code>Label, optional</code>
<p>
The file that will be passed to <code>rustc</code> to be used for
building this crate.
</p>
<p>
If <code>crate_root</code> is not set, then this rule will look for
a <code>lib.rs</code> file or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file.
</p>
</td>
</td>
<tr>
<td><code>deps</code></td>
<td>
<code>List of labels, optional</code>
<p>List of other libraries to be linked to this library target.</p>
<p>
These can be either other <code>rust_library</code> targets or
<code>cc_library</code> targets if linking a native library.
</p>
</td>
</tr>
<tr>
<td><code>data</code></td>
<td>
<code>List of labels, optional</code>
<p>List of files used by this rule at runtime.</p>
<p>
This attribute can be used to specify any data files that are embedded
into the library, such as via the
<a href="https://doc.rust-lang.org/std/macro.include_str!.html target="_blank"><code>include_str!</code></a>
macro.
</p>
</td>
</tr>
<tr>
<td><code>crate_features</code></td>
<td>
<code>List of strings, optional</code>
<p>List of features to enable for this crate.</p>
<p>
Features are defined in the code using the
<code>#[cfg(feature = "foo")]</code> configuration option. The
features listed here will be passed to <code>rustc</code> with
<code>--cfg feature="${feature_name}"</code> flags.
</p>
</td>
</tr>
<tr>
<td><code>rustc_flags</code></td>
<td>
<code>List of strings, optional</code>
<p>List of compiler flags passed to <code>rustc</code>.</p>
</td>
</tr>
</tbody>
</table>
### Example
Suppose you have the following directory structure for a simple Rust library
crate:
```
[workspace]/
WORKSPACE
hello_lib/
BUILD
src/
greeter.rs
lib.rs
```
`hello_lib/src/greeter.rs`:
```rust
pub struct Greeter {
greeting: String,
}
impl Greeter {
pub fn new(greeting: &str) -> Greeter {
Greeter { greeting: greeting.to_string(), }
}
pub fn greet(&self, thing: &str) {
println!("{} {}", &self.greeting, thing);
}
}
```
`hello_lib/src/lib.rs`:
```rust
pub mod greeter;
```
`hello_lib/BUILD`:
```python
package(default_visibility = ["//visibility:public"])
load("/tools/build_rules/rust/rust", "rust_library")
rust_library(
name = "hello_lib",
srcs = [
"src/greeter.rs",
"src/lib.rs",
],
)
```
Build the library:
```
$ bazel build //hello_lib
INFO: Found 1 target...
Target //examples/rust/hello_lib:hello_lib up-to-date:
bazel-bin/examples/rust/hello_lib/libhello_lib.rlib
INFO: Elapsed time: 1.245s, Critical Path: 1.01s
```
<a name="rust_binary"></a>
## rust_binary
```
rust_binary(name, srcs, deps, data, crate_features, rustc_flags)
```
<table class="table table-condensed table-bordered table-params">
<colgroup>
<col class="col-param" />
<col class="param-description" />
</colgroup>
<thead>
<tr>
<th colspan="2">Attributes</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>name</code></td>
<td>
<code>Name, required</code>
<p>A unique name for this rule.</p>
<p>
This name will also be used as the name of the binary crate built by
this rule.
</p>
</td>
</tr>
<tr>
<td><code>srcs</code></td>
<td>
<code>List of labels, required</code>
<p>List of Rust <code>.rs</code> source files used to build the
binary.</p>
<p>
If <code>srcs</code> contains more than one file, then there must be
a file either named <code>main.rs</code>. Otherwise,
<code>crate_root</code> must be set to the source file that is the
root of the crate to be passed to <code>rustc</code> to build this
crate.
</p>
</td>
</tr>
<tr>
<td><code>crate_root</code></td>
<td>
<code>Label, optional</code>
<p>
The file that will be passed to <code>rustc</code> to be used for
building this crate.
</p>
<p>
If <code>crate_root</code> is not set, then this rule will look for
a <code>main.rs</code> file or the single file in <code>srcs</code>
if <code>srcs</code> contains only one file.
</p>
</td>
</td>
<tr>
<td><code>deps</code></td>
<td>
<code>List of labels, optional</code>
<p>List of other libraries to be linked to this library target.</p>
<p>
These must be <code>rust_library</code> targets.
</p>
</td>
</tr>
<tr>
<td><code>data</code></td>
<td>
<code>List of labels, optional</code>
<p>List of files used by this rule at runtime.</p>
<p>
This attribute can be used to specify any data files that are embedded
into the library, such as via the
<a href="https://doc.rust-lang.org/std/macro.include_str!.html target="_blank"><code>include_str!</code></a>
macro.
</p>
</td>
</tr>
<tr>
<td><code>crate_features</code></td>
<td>
<code>List of strings, optional</code>
<p>List of features to enable for this crate.</p>
<p>
没有合适的资源?快使用搜索试试~ 我知道了~
bazel-0.1.2.tar_3.gz
0 下载量 67 浏览量
2024-04-07
15:27:22
上传
评论
收藏 34.12MB GZ 举报
温馨提示
Bazel 是一个多语言、开源的构建工具,旨在支持大型软件项目的自动化构建和测试。Bazel 的设计理念着重于提高构建的速度和可靠性,同时支持多平台构建。它支持多种编程语言,包括但不限于 Java、C++、Python 和 Go。Bazel 使用一种名为 BUILD 的高级构建语言来描述项目的构建过程,使得构建配置既灵活又易于理解。 Bazel 的核心特性之一是其强大的依赖分析和管理能力。通过精确地跟踪项目中每个组件的依赖关系,Bazel 能够确保构建的增量性和确定性。这意味着当源代码发生变化时,Bazel 只会重新构建受到影响的部分,从而显著提高构建效率。此外,Bazel 支持远程缓存,允许跨多个构建共享部分构建结果,进一步加速构建过程。 Bazel 还特别注重于构建的可重复性。通过将构建环境封装化,Bazel 确保了构建过程不会受到外部环境的影响,从而使得构建结果在不同环境中是一致的。这一点对于确保软件质量和便于问题追踪尤为重要。
资源推荐
资源详情
资源评论






























收起资源包目录





































































































共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论


程序员Chino的日记
- 粉丝: 4237
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- gongrzhe_json-mcp-server-AI人工智能资源
- OurChat-Rust资源
- workerman-硬件开发资源
- Android Course Work-移动应用开发资源
- 分布式、嵌入式和实时Java系统的探索与实践
- Geeker-Admin-Typescript资源
- GoFlyAdmin(Go语言快速开发框架)-Go资源
- 航空票务综合管理与预订服务系统 航空客运票务管理与票务预订综合系统 航空票务信息管理及预订服务系统 航空票务一体化管理与服务系统 航空票务综合管理及预订处理系统 航空票务管理与在线预订服务系统 航空票
- ThingsGateway-C#资源
- 2023年电大操作系统形考任务.doc
- PhalApi-PHP资源
- ChatArea-JavaScript资源
- 鸿合多学科交互式电子白板软件使用说明书(最新版本).doc
- 精选网络信息系统安全检查表.doc
- 数据库系统概论-数据库安全性.ppt
- 完成版TCL网络营销传播手册.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
