你好 Rust 示例

以下是建立依賴 Rust 函式庫的 Rust 二進位檔案的範例。

Rust 模組目前僅限於build/soong/rust/config/allowed_list.go中定義的特定目錄。以下helloWorld模組使用external/rust目錄來避免修改此清單。

從 AOSP 原始碼根目錄

mkdir -p external/rust/hello_rust/src/
mkdir -p external/rust/libsimple_printer/src/

使用以下內容建立external/rust/hello_rust/src/hello_rust.rs

use simple_printer;
fn main() {
 simple_printer::print_hello_rust();
}

使用以下內容建立external/rust/libsimple_printer/src/lib.rs檔案:

pub fn print_hello_rust() {
  println!("Hello Rust!");
}

建立一個包含以下內容的external/rust/hello_rust/Android.bp檔案:

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.
}

建立一個包含以下內容的external/rust/libsimple_printer/Android.bp檔案:

// 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"],
}

最後,建立你的hello_rust模組:

source build/envsetup.sh
lunch aosp_arm64-eng
m hello_rust