Many tools in the Rust ecosystem assume you are using cargo. For tools like this which don't have a counterpart in our build, there is a utility for generating Cargo.toml
files. You can run cargo itself (cargo check
for instance) using it.
This functionality is maintained by volunteers. GN and Cargo have some design mismatches that may result in the generated Cargo.toml
files not working at all or needing manual tweaks. Because of this, cargo in the Fuchsia tree is not officially supported; things may break from time to time.
Cargo.toml
filesIn order to generate the cargo files based on the build graph of GN, add --cargo-toml-gen
to the fx set
and fx args
commands. This adds a few seconds at gn gen
time. For example:
fx set PRODUCT.BOARD --cargo-toml-gen <other fx args> fx build
Note: If a Cargo.toml
is required to complete an fx build
, such as in the case cbindgen
needs to be run to generate new C bindings for a Rust crate, you may use the //build/rust:cargo_toml_gen
build target instead. This target will only build the Cargo.toml
files.
Most editors require the Cargo.toml
file to be in a location that is adjacent to the src/
directory. Symlinks to these files can be generated using the following commands, where //garnet/foo/path/to/target:label
is the GN target that you want to work on:
fx gen-cargo garnet/foo/path/to/target:some_label
Note: The above will not work for rustc_staticlib
targets, e.g. Rust crates used to generate C bindings generally use the rustc_staticlib
target template. For rustc_staticlib
targets, you should use the following command instead.
fx gen-cargo garnet/foo/path/to/target:_some_label_rustc_static
Note that this label must point to a rustc_...
GN template (not a Fuchsia package or other GN target). For example:
rustc_binary("some_label") { ... }
Some plugins require a .cargo/config
file to allow cargo to operate correctly for Fuchsia (e.g. to run cargo check
). To easily generate this file, use the fargo
tool.
Configure rustup
to use the Fuchsia Rust toolchain by running:
rustup toolchain link fuchsia $($FUCHSIA_DIR/scripts/youcompleteme/paths.py VSCODE_RUST_TOOLCHAIN) rustup default fuchsia
Clone and install the fargo
tool within your $FUCHSIA_DIR
by following the getting started instructions for fargo.
Create your config:
cd $FUCHSIA_DIR && fargo write-config # Note the caveats about changing architecture in the fargo readme # https://fuchsia.googlesource.com/fargo/#creating-a-cargo_config