tree: 562f10350f466dd1f6ef442fb94a3af3b56eb9de [path history] [tgz]
  1. src/
  2. templates/

Cargo GNaw

Tooling to convert Cargo.toml files into native GN rules license cargo-gnaw


$ cargo install --path ~/cargo-gnaw


$ cargo gnaw --manifest-path ~/fuchsia/third_party/rust_crates/Cargo.toml -o ~/fuchsia/third_party/rust_crates/


  • --skip-root - Skip the root package in the Cargo.toml and treat it's dependencies as the top-level targets
  • --gn-bin - Path to GN binary for formatting the output

How it works

Cargo GNaw operates on vendored crates to convert them into GN rules. The resulting file is expected to be vendored with the crates and provides targets for the GN build system to reference.

All top-level crates are given an easy to use GN alias group that references the version exposed in the Cargo.toml. Direct dependencies of the root crate can be “lifted” to the top-level by skipping the default root crate.

Build Scripts

GNaw intentionally does not handle scripts at compilation time. Any evaluation of a script is done when the crate is vendored. The resulting configuration which is usually produced by the script is put into a section in the source Cargo.toml. Options detailed below. Simple scripts (ones that only depend upon Rust's std library) evaluate and automatically provide the author with the expected configuration.

GN configs

Underneath a TOML array, a configuration should be passed as “gn.crate..”

  • rustflags - flags to pass through to rustc
  • deps - native GN dependency
  • env_vars - environment variables, usually used for pretending to be cargo
  • platform - platform this configuration targets. Uses the rust cfg format (Ex: cfg(unix))


rustflags = ["--cfg=backtrace"]

Simple Example

name = "simple"
version = "1.0.25"
authors = ["Benjamin Brittain <>"]
edition = "2018"


converts to

group("simple") { deps = [":simple-1-0-25"] } rust_library("simple-1-0-25") { crate_name = "simple" crate_root = "//tools/cargo-gnaw/src/tests/simple/src/" output_name = "simple-9ac42213326ac72d" deps = [] rustenv = [] rustflags = ["--cap-lints=allow", "--edition=2018", "-Cmetadata=9ac42213326ac72d", "-Cextra-filename=-9ac42213326ac72d"] }