tree: 562f10350f466dd1f6ef442fb94a3af3b56eb9de [path history] [tgz]
  1. src/
  2. templates/
  3. BUILD.gn
  4. README.md
tools/cargo-gnaw/README.md

Cargo GNaw

Tooling to convert Cargo.toml files into native GN rules

crates.io license docs.rs cargo-gnaw

Install

$ cargo install --path ~/cargo-gnaw

Run

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

Options

  • --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 BUILD.gn 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 build.rs scripts at compilation time. Any evaluation of a build.rs script is done when the crate is vendored. The resulting configuration which is usually produced by the build.rs script is put into a section in the source Cargo.toml. Options detailed below. Simple build.rs 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))

Example:

[[gn.crate.anyhow."1.0.25"]]
rustflags = ["--cfg=backtrace"]

Simple Example

[package]
name = "simple"
version = "1.0.25"
authors = ["Benjamin Brittain <bwb@google.com>"]
edition = "2018"

[dependencies]

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/lib.rs" output_name = "simple-9ac42213326ac72d" deps = [] rustenv = [] rustflags = ["--cap-lints=allow", "--edition=2018", "-Cmetadata=9ac42213326ac72d", "-Cextra-filename=-9ac42213326ac72d"] }