rust_binary {
name: "hello_bzip_bindgen_include",
srcs: [
// The primary rust source file must come first in this list.
"src/lib.rs",
// The module providing the bindgen bindings is
// included in srcs prepended by ":".
":libbuzz_bindgen",
],
// Dependencies need to be redeclared when generated source is used via srcs.
shared_libs: [
"libbuzz",
],
}
#![allow(clippy::all)]#![allow(non_upper_case_globals)]#![allow(non_camel_case_types)]#![allow(non_snake_case)]#![allow(unused)]#![allow(missing_docs)]// Note that "bzip_bindings.rs" here must match the source_stem property from// the rust_bindgen module.include!(concat!(env!("OUT_DIR"),"/bzip_bindings.rs"));
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Source generators\n\nThis page provides a high-level view of how generated source is supported and\nhow it can be used in the build system.\n\nAll source generators provide similar build-system functionality. The three\nbuild-system supported source-generation use cases are generating C bindings\nusing bindgen, AIDL interfaces, and protobuf interfaces.\n\nCrates from generated source\n----------------------------\n\nEvery Rust module that generates source code can be used as a crate, exactly as\nif it were defined as a `rust_library`. (This means it can be defined as a\ndependency in the `rustlibs`, `rlibs`, and `dylibs` properties.) The best usage\npattern for platform code is to employ generated source as a crate. Although the\n`include!` macro is supported for generated source, its primary purpose is to\nsupport third-party code that resides in `external/`.\n\nThere are cases where platform code might still use generated source through the\n`include!()` macro, such as when you use a `genrule` module to generate source\nin a unique fashion.\n\nUse include!() to include generated source\n------------------------------------------\n\nUsing generated source as a crate is covered by the examples in each specific\n(respective) module page. This section shows how to reference generated source\nthrough the `include!()` macro. Note that this process is similar for all source\ngenerators.\n| **Important:** Build system support for `include!()` is primarily intended to support third-party crates which rely on Cargo providing `OUT_DIR`. This behavior is emulated by Android's build system to support these crates without modification. **AOSP\n| platform code must use generated source as a crate unless unable to do so.**\n\n#### Prerequisite\n\nThis example is based on the assumption that you have defined a `rust_bindgen`\nmodule (`libbuzz_bindgen`) and can proceed to the [Steps for including generated source](#steps-include-generated-source)\nfor using the`include!()` macro. If you haven't, please go to [Defining a rust bindgen module](/docs/setup/build/rust/building-rust-modules/source-code-generators/bindgen-modules#defining-rust-bindgen-module),\ncreate `libbuzz_bindgen`, then return here.\n\nNote that the build-file portions of this are applicable for all source generators.\n\n#### Steps for including generated source\n\nCreate `external/rust/hello_bindgen/Android.bp` with the following contents: \n\n rust_binary {\n name: \"hello_bzip_bindgen_include\",\n srcs: [\n // The primary rust source file must come first in this list.\n \"src/lib.rs\",\n\n // The module providing the bindgen bindings is\n // included in srcs prepended by \":\".\n \":libbuzz_bindgen\",\n ],\n\n // Dependencies need to be redeclared when generated source is used via srcs.\n shared_libs: [\n \"libbuzz\",\n ],\n }\n\n| **Important**: When you use generated sources as above, dependencies don't propagate. You must re-declare, in the dependent library, any dependencies required for compilation.\n\nCreate `external/rust/hello_bindgen/src/bindings.rs` with the following contents: \n\n #![allow(clippy::all)]\n #![allow(non_upper_case_globals)]\n #![allow(non_camel_case_types)]\n #![allow(non_snake_case)]\n #![allow(unused)]\n #![allow(missing_docs)]\n\n // Note that \"bzip_bindings.rs\" here must match the source_stem property from\n // the rust_bindgen module.\n include!(concat!(env!(\"OUT_DIR\"), \"/bzip_bindings.rs\"));\n\nCreate `external/rust/hello_bindgen/src/lib.rs` with the following contents: \n\n mod bindings;\n\n fn main() {\n let mut x = bindings::foo { x: 2 };\n unsafe { bindings::fizz(1, &mut x as *mut bindings::foo) }\n }\n\nWhy crates for generated source\n-------------------------------\n\nUnlike C/C++ compilers, `rustc` only accepts a single source file\nrepresenting an entry point to a binary or library. It expects that the source\ntree is structured such that all required source files can be automatically\ndiscovered. This means that generated source must either be placed in the source\ntree, or provided through an include directive in source: \n\n include!(\"/path/to/hello.rs\");\n\nThe Rust community depends on `build.rs` scripts, and assumptions about\nthe Cargo build environment, to [work with this difference](https://doc.rust-lang.org/cargo/reference/build-script-examples.html#code-generation).\nWhen it builds, the `cargo` command sets an [`OUT_DIR` environment variable](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates)\ninto which `build.rs` scripts are expected to place generated source code. Use the\nfollowing command to include source code: \n\n include!(concat!(env!(\"OUT_DIR\"), \"/hello.rs\"));\n\nThis presents a challenge for Soong as outputs for each module are placed in\ntheir own `out/` directory^[1](#fn1)^. There isn't a single `OUT_DIR` where\ndependencies output their generated source.\n\nFor platform code, AOSP prefers packaging generated source into a crate that can\nbe imported, for several reasons:\n\n- Prevent generated source file names from colliding.\n- Reduce [boilerplate code](https://github.com/rust-lang/rust-bindgen/blob/main/book/src/tutorial-4.md) checked-in throughout the tree that requires maintenance. Any boilerplate that's required to make the [generated source compile](https://android.googlesource.com/platform/build/soong/+/6eff900b67a0ed7e639ae304b1d453b620e9b79d/rust/protobuf.go) into a crate can be centrally maintained.\n- Avoid implicit^[2](#fn2)^ interactions between generated code and the surrounding crate.\n- Reduce pressure on memory and disk by dynamically linking commonly used generated sources.\n\nAs a result, all of Android's Rust source generation module types produce code\nthat can be compiled and used [as a crate](https://android.googlesource.com/platform/system/bt/+/92fa0f9ac23d547cfa62ddbf9e92e7755e1b1df6/gd/rust/facade/Android.bp).\nSoong still supports third-party crates without modification if all\nthe generated source dependencies for a module get copied into a single per-module\ndirectory, similar to Cargo. In such cases, Soong sets the `OUT_DIR` environment variable\nto that directory when compiling the module, so the generated source can be found.\nHowever, for the reasons already described, it's best practice to only use\nthis mechanism in platform code when it's absolutely necessary. \n\n*** ** * ** ***\n\n1. This doesn't present any problems for C/C++ and similar languages, as the\n path to the generated source is provided directly to the compiler. [↩](#fnref1)\n\n2. Since `include!` works by textual inclusion, it might reference values from\n the enclosing namespace, modify the namespace, or use constructs like `#![foo]`.\n These implicit interactions can be difficult to maintain. Always prefer\n macros when interaction with the rest of the crate is truly required. [↩](#fnref2)"]]