Di seguito è riportato un esempio di creazione di un file binario Rust che dipende da una libreria Rust.
I moduli Rust sono attualmente confinati a directory specifiche definite in
build/soong/rust/config/allowed_list.go.
Il seguente modulo helloWorld
utilizza la directory external/rust
per evitare
di modificare questo elenco.
Dalla radice del codice sorgente AOSP
mkdir -p external/rust/hello_rust/src/
mkdir -p external/rust/libsimple_printer/src/
Crea external/rust/hello_rust/src/hello_rust.rs
con i seguenti contenuti:
use simple_printer;
fn main() {
simple_printer::print_hello_rust();
}
Crea il file external/rust/libsimple_printer/src/lib.rs
con i seguenti contenuti:
pub fn print_hello_rust() {
println!("Hello Rust!");
}
Crea un file external/rust/hello_rust/Android.bp
con i seguenti contenuti:
rust_binary {
name: "hello_rust",
// srcs must contain a single source file, the entry point source.
srcs: ["src/hello_rust.rs"],
// rustlibs are Rust library dependencies. The type, rlib (static) or dylib
// (dynamic), are chosen automatically based on module type and target
// to avoid linkage issues.
rustlibs: ["libsimple_printer"],
// define any additional compilation flags
flags: [
"-C debug-assertions=yes",
],
// cc libraries can be linked in to rust binaries and libraries through the
// shared_libs and static_libs properties. These are not needed for this
// simple example, so they are not included.
}
Crea un file external/rust/libsimple_printer/Android.bp
con i seguenti contenuti:
// rust_library provides dylib and rlib variants.
rust_library {
//name or stem properties must be of the form lib<crate_name><any_suffix>
name: "libsimple_printer",
//crate_name must match the name used in source (e.g. extern crate <name>)
crate_name: "simple_printer",
//srcs must contain a single source file, the entry point source
srcs: ["src/lib.rs"],
}
Infine, crea il modulo hello_rust
:
source build/envsetup.sh
lunch aosp_arm64-eng
m hello_rust