Chromium 和 Cargo 的生态对比
The Rust community typically uses cargo
and libraries from crates.io. Chromium is built using gn
and ninja
and a curated set of dependencies.
使用 Rust 编写代码时,您可以选择:
- 借助
//build/rust/*.gni
模板(例如rust_static_library
,我们稍后会介绍)使用gn
和ninja
。该操作会使用经过审核的 Chromium 工具链和 crate。 - 使用
cargo
,但 仅限于经过审核的 Chromium 工具链和 crate - 使用
cargo
,信任 工具链 和/或 从互联网下载的 crate
From here on we’ll be focusing on gn
and ninja
, because this is how Rust code can be built into the Chromium browser. At the same time, Cargo is an important part of the Rust ecosystem and you should keep it in your toolbox.
Mini exercise
分成各个小组开展以下活动:
- 思考
cargo
在哪些场景下具有优势,并评估这些场景的风险状况。 - 讨论在使用
gn
和ninja
以及离线cargo
等时,需要信任哪些工具、库和人群。
Ask students to avoid peeking at the speaker notes before completing the exercise. Assuming folks taking the course are physically together, ask them to discuss in small groups of 3-4 people.
与第一部分练习相关的备注/提示(“Cargo 可能具有优势的场景”):
It’s fantastic that when writing a tool, or prototyping a part of Chromium, one has access to the rich ecosystem of crates.io libraries. There is a crate for almost anything and they are usually quite pleasant to use. (
clap
for command-line parsing,serde
for serializing/deserializing to/from various formats,itertools
for working with iterators, etc.).- 借助
cargo
,您便可以轻松试用库(只需向 `Cargo.toml`` 添加一行代码然后开始编写即可) - 不妨比较下 CPAN 是如何帮助
perl
成为热门之选的。或者与python
+pip
进行比较。
- 借助
Development experience is made really nice not only by core Rust tools (e.g. using
rustup
to switch to a differentrustc
version when testing a crate that needs to work on nightly, current stable, and older stable) but also by an ecosystem of third-party tools (e.g. Mozilla providescargo vet
for streamlining and sharing security audits;criterion
crate gives a streamlined way to run benchmarks).- 借助
cargo
,您可通过cargo install --locked cargo-vet
轻松添加工具。 - 不妨与 Chrome 扩展程序或 VScode 扩展程序进行比较。
- 借助
以下是一些适合选用
cargo
,较为宽泛的通用项目示例:- Perhaps surprisingly, Rust is becoming increasingly popular in the industry for writing command line tools. The breadth and ergonomics of libraries is comparable to Python, while being more robust (thanks to the rich typesystem) and running faster (as a compiled, rather than interpreted language).
- 如要加入 Rust 生态系统,必须使用 Cargo 等标准 Rust 工具。如果希望库获得外部贡献,并且能够用于除 Chromium 之外(例如用于 Bazel 或 Android/Soong 构建环境)的项目,则应该使用 Cargo。
基于
cargo
\ 的 Chromium 相关项目示例:serde_json_lenient
(已在 Google 的其他部分进行了实验,结果是能使 PR 性能得到提升)- 字体库(例如
font-types
) gnrt
工具(我们将在本课程稍后的部分中加以介绍)使用clap
进行命令行解析,使用toml
处理配置文件。- Disclaimer: a unique reason for using
cargo
was unavailability ofgn
when building and bootstrapping Rust standard library when building Rust toolchain. run_gnrt.py
uses Chromium’s copy ofcargo
andrustc
.gnrt
depends on third-party libraries downloaded from the internet, butrun_gnrt.py
askscargo
that only--locked
content is allowed viaCargo.lock
.)
- Disclaimer: a unique reason for using
学生可将以下内容认定为隐式信任或明确信任:
rustc
(Rust 编译器)则依赖于 LLVM 库、Clang 编译器、“rustc”源代码(从 GitHub 获取,并由 Rust 编译器团队审核)、为引导而下载的二进制 Rust 编译器rustup
(值得注意的是,rustup
是在 https://github.com/rust-lang/ 保护下组织开发的,与rustc
相同。)cargo
、rustfmt
等- 各种内部基础架构(用于构建
rustc
的聊天机器人、用于将预构建工具链分发给 Chromium 工程师的系统等。) cargo Audit
、cargo vet
等 Cargo 工具- 包含到
//third_party/rust
的 Rust 库(由 security@chromium.org 进行审核) - 其他 Rust 库(一些小众但很受欢迎又常用的库)