This repository contains Rust crates imported from crates.io for use by the Android platform.
The files in this repository are managed by a tool, which you can run from this directory:
./crate_tool help
Most of the files here should not be edited manually, and pre-upload checks will catch and prevent such changes.
Do not edit Android.bp files directly. Instead, edit cargo_embargo.json and run ./crate_tool regenerate <crate name>
.
Refer to the cargo_embargo documentation for more information.
Do not use external_updater
. Use crate_tool
as follows:
This will print every newer version of every crate:
./crate_tool updatable-crates
To check all newer versions of a crate for potential problems:
./crate_tool analyze-updates <crate name>
To update a crate to a specified version:
./crate_tool update <crate name> <version>
This does not do any repo
or git
operations, so if the update is successful, you will need to run repo start
, git commit
, repo upload
, etc.
Several problems can occur when updating a crate:
patches/
directory.cargo_embargo
may fail, requiring edits to cargo_embargo.json
The update command does not try to build the Android tree, but a recommended local test before repo upload
is to build everything under external/rust
:
source build/envsetup.sh lunch aosp_husky-trunk_staging-eng cd external/rust mm
Instead of running ./crate_tool update <crate name> <version>
, you can edit pseudo_crate/Cargo.toml
directly and run ./crate_tool regenerate <crate name>
. This can be useful when pairs of crates need to be updated in lockstep. This can happen, for example, when a crate has an associated proc_macro crate. So to update foo
and foo_derive
together, edit the versions of both in pseudo_crate/Cargo.toml
, then run:
./crate_tool regenerate foo foo_derive
If you don't have a specific crate you need to update, but want to help keep the crate repository up-to-date, the crate tool can suggest crate updates that seem likely to succeed:
./crate_tool suggest-updates
You should avoid creating patches, if possible. Every patch file is an ongoing operational burden that makes it more difficult to keep our crates up-to-date.
If a patch is absolutely necessary, you should, if possible, send a pull request to the upstream crate, so we can eliminate the Android patch in the future, when upgrading.
To create a patch for crate “foo”, edit the files directly. Then do:
git diff --relative=crates/foo -- crates/foo/<file1> crates/foo/<file2> > patches/<name>.patch`
If you stage or commit the change and the patch, you should see no new changes when you run “regenerate”.
The source code for crate_tool
is here.
The basic principle of the tool is that every crate directory in crates/ must be exactly reproducible by an automatic process from a known set of inputs:
METADATA
, TEST_MAPPING
, and MODULE_LICENSE_*
files.patches/
directory.cargo_embargo.json
, which is used to generate Android.bp
.Therefore, what ./crate_tool regenerate <crate name>
does is:
cargo vendor
METADATA
, TEST_MAPPING
, etc.Android.bp
by running cargo_embargo.The pre-upload check does exactly the same thing, but without the final step. Instead of replacing the directory contents, it checks that what it generated from scratch matches the actual crate directory contents.
Crate update are also built on top of regenerate
. What ./crate_tool update <crate name> <version>
does is:
cargo remove <crate_name>
cargo add <crate_name>@=<version>
./crate_tool regenerate <crate name>