Chromium 和 Cargo 的生态对比

Rust 社区通常使用 cargocrates.io 中的库。Chromium 是使用 gnninja 以及一组精选依赖项而构建。

使用 Rust 编写代码时,您可以选择:

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 在哪些场景下具有优势,并评估这些场景的风险状况。
  • 讨论在使用 gnninja 以及离线 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 different rustc 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 provides cargo 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 处理配置文件。
      • 免责声明:必须使用 cargo 的一个原因是在构建 Rust 工具链时,无法使用 gn 来构建和引导 Rust 标准库。)
      • run_gnrt.py 使用 Chromium 的 cargorustc 副本。虽然 gnrt 依赖于从互联网下载的第三方库,但 run_gnrt.py 规定 cargo 仅允许使用 Cargo.lock 中以 --locked 标记的内容。

学生可将以下内容认定为隐式信任或明确信任:

  • rustc(Rust 编译器)则依赖于 LLVM 库、Clang 编译器、“rustc”源代码(从 GitHub 获取,并由 Rust 编译器团队审核)、为引导而下载的二进制 Rust 编译器
  • rustup(值得注意的是,rustup 是在 https://github.com/rust-lang/ 保护下组织开发的,与 rustc 相同。)
  • cargorustfmt
  • 各种内部基础架构(用于构建 rustc 的聊天机器人、用于将预构建工具链分发给 Chromium 工程师的系统等。)
  • cargo Auditcargo vet 等 Cargo 工具
  • 包含到 //third_party/rust 的 Rust 库(由 security@chromium.org 进行审核)
  • 其他 Rust 库(一些小众但很受欢迎又常用的库)