نمونه سلام زنگ

در ادامه مثالی از ساخت یک فایل باینری 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