Build rules

Rust code is usually built using cargo. Chromium builds with gn and ninja for efficiency —- its static rules allow maximum parallelism. Rust is no exception.

Adding Rust code to Chromium

In some existing Chromium BUILD.gn file, declare a rust_static_library:

  1. import("//build/rust/rust_static_library.gni")
  2. rust_static_library("my_rust_lib") {
  3. crate_root = "lib.rs"
  4. sources = [ "lib.rs" ]
  5. }

You can also add deps on other Rust targets. Later we’ll use this to depend upon third party code.

You must specify both the crate root, and a full list of sources. The crate_root is the file given to the Rust compiler representing the root file of the compilation unit —- typically lib.rs. sources is a complete list of all source files which ninja needs in order to determine when rebuilds are necessary.

(There’s no such thing as a Rust source_set, because in Rust, an entire crate is a compilation unit. A static_library is the smallest unit.)

Students might be wondering why we need a gn template, rather than using gn’s built-in support for Rust static libraries. The answer is that this template provides support for CXX interop, Rust features, and unit tests, some of which we’ll use later.