src/chromium/cargo.md
The Rust community typically uses cargo and libraries from crates.io.
Chromium is built using gn and ninja and a curated set of dependencies.
When writing code in Rust, your choices are:
gn and ninja with the help of the templates from //build/rust/*.gni
(e.g. rust_static_library that we'll meet later). This uses Chromium's
audited toolchain and crates.cargo, but
restrict yourself to Chromium's audited toolchain and cratescargo, trusting a toolchain and/or
crates downloaded from the internetFrom 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.
Split into small groups and:
cargo may offer an advantage and assess the risk
profile of these scenarios.gn and ninja, offline cargo, etc.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.
Notes/hints related to the first part of the exercise ("scenarios where Cargo may offer an advantage"):
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 typically quite pleasant to use. (clap for
command-line parsing, serde for serializing/deserializing to/from various
formats, itertools for working with iterators, etc.).
cargo makes it easy to try a library (just add a single line to
Cargo.toml and start writing code)perl a popular choice. Or
comparing with 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 makes it easy to add a tool via cargo install --locked cargo-vet.Broad, generic examples of projects where cargo may be the right choice:
Examples of Chromium-related projects that are cargo-based:
serde_json_lenient (experimented with in other parts of Google which
resulted in PRs with performance improvements)font-typesgnrt tool (we will meet it later in the course) which depends on clap
for command-line parsing and on toml for configuration files.
cargo was unavailability of gn
when building and bootstrapping Rust standard library when building Rust
toolchain.run_gnrt.py uses Chromium's copy of cargo and rustc. gnrt depends
on third-party libraries downloaded from the internet, but run_gnrt.py
asks cargo that only --locked content is allowed via Cargo.lock.)Students may identify the following items as being implicitly or explicitly trusted:
rustc (the Rust compiler) which in turn depends on the LLVM libraries, the
Clang compiler, the rustc sources (fetched from GitHub, reviewed by Rust
compiler team), binary Rust compiler downloaded for bootstrappingrustup (it may be worth pointing out that rustup is developed under the
umbrella of the https://github.com/rust-lang/ organization - same as rustc)cargo, rustfmt, etc.rustc, system for
distributing the prebuilt toolchain to Chromium engineers, etc.)cargo audit, cargo vet, etc.//third_party/rust (audited by
[email protected])