اعتبارًا من 27 آذار (مارس) 2025، ننصحك باستخدام android-latest-release
بدلاً من aosp-main
لإنشاء AOSP والمساهمة فيه. لمزيد من المعلومات، يُرجى الاطّلاع على التغييرات في AOSP.
وحدات المكتبة
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
هناك نوعان من وحدات مكتبة Rust، أحدهما يُنشئ مكتبات Rust و
الآخر يُنشئ مكتبات متوافقة مع C. بالإضافة إلى ذلك، لأغراض
نظام الإنشاء، يمكنك اعتبار وحدات الماكرو البروتوكولية في Rust (proc-macros
) نوعًا special
من المكتبات.
rust_library
تُنشئ وحدة rust_library
مكتبات Rust لاستخدامها من قِبل وحدات Rust الأخرى.
بالإضافة إلى أنواع _host
، تحتوي مكتبات Rust على أنواع وحدات تتحكّم في
عمليات الربط المتاحة.
نوع وحدة مكتبة Rust |
التعريف |
rust_library |
يوفّر كلا خيارَي المكتبة، rlib وdylib . ينصح فريق AOSP باستخدام نوع الوحدات
هذا لمكتبات Rust، لأنّه يسمح للوحدات
بالعمل بشكل صحيح عند إدراجها كعنصر تابع ضمن السمة rustlibs
. |
rust_library_rlib |
يوفّر هذا النوع فقط من مكتبة Rustrlib ،
ولا يمكن ضمان أن تعمل الوحدات التي تقدّم أنواعrlib فقط مع سمة
rustlibs . |
rust_library_dylib |
يوفّر فقط الصيغة dylib من مكتبة Rust،
ولا يمكن ضمان أن تعمل الوحدات التي تقدّم صِيَغ dylib فقط
مع السمة rustlibs . |
rust_ffi
تُنشئ وحدة rust_ffi
مكتبات متوافقة مع C للتوافق مع وحدات CC.
بالإضافة إلى الصيغ _host
، تحتوي مكتبات Rust FFI على أنواع وحدات تتحكم في عملية الربط المتاحة، كما هو موضّح في الجدول التالي.
نوع وحدة مكتبة Rust FFI |
التعريف |
rust_ffi |
يوفّر كلا الصيغتَين من مكتبة C: الثابتة والمشترَكة. |
rust_ffi_shared |
يوفّر هذا الخيار الصيغة C من المكتبة المشتركة فقط. |
rust_ffi_static |
لا يوفّر سوى نوع المكتبة الثابتة C. |
للحصول على مثال أساسي لاستخدام rust_ffi
لاستدعاء Rust من C، اطّلِع على صفحة
أنماط Rust في Android.
للحصول على معلومات عن الاستخدامات الأكثر تقدمًا، يُرجى الانتقال إلى مستندات Rust الرسمية.
rust_proc_macro
يمكن أن تكون وحدات الماكرو الإجرائية في Rust (proc-macros) مفيدة لتوسيع نطاق المُجمِّع لتنفيذ عمليات تحويل الرموز البرمجية المصدر أو توفير سمات جديدة. يمكنك الاطّلاع على مزيد من المعلومات
حول هذه العناصر في صفحة وحدات الماكرو البرمجية
ضمن مستندات Rust الرسمية.
لأغراض نظام الإنشاء، تعمل وحدة rust_proc_macro
بطريقة مشابهة
لوحدات rust_library
. بالنسبة إلى الوحدات التي تعتمد على rust_proc_macros
،
أضِف اسم الوحدة إلى السمة proc_macros
.
بما أنّ proc_macros
هي إضافات للمُجمِّع، فإنّها تستهدف بالضرورة المضيف ولا تُنشئ
أي رمز يمكن تشغيله على جهاز.
خصائص مكتبة Rust البارزة
إنّ الخصائص المحدّدة في الجدول أدناه هي بالإضافة إلى الخصائص الشائعة والمهمة
التي تنطبق على جميع الوحدات. هذه الوحدات إما مهمة بشكل خاص لوحدات مكتبة Rust
، أو تُظهر سلوكًا فريدًا خاصًا بنوع وحدة rust_library
.
خصائص مكتبة Rust |
التعريف |
البادئة / الاسم |
يتحكّم السمة stem في اسم ملف مكتبة الإخراج،
والذي يتم ضبطه تلقائيًا على name .
يفرض مُجمِّع Rust متطلبات معيّنة على أسماء ملفات المكتبة، ونتيجةً لذلك، يفرض نظام الإنشاء هذه المتطلبات لتجنُّب مشاكل الترجمة.
يجب أن يمتثل اسم ملف الإخراج لتنسيق lib<crate_name><suffix> .
(هناك تبعية هنا للسمة
crate_name ). |
crate_name |
هذه السمة مطلوبة للوحدات التي تُنشئ مكتبة.
بالإضافة إلى ذلك، لها علاقة باسم ملف الإخراج. (اطّلِع على تعريف stem ). |
export_include_dirs |
بالنسبة إلى وحدات rust_ffi ، تحدِّد هذه السمة قائمة بسلسلتَين تمثلان مسارات تضمين نسبية: مسارات تحتوي على رؤوس يمكن لوحدات cc التابعة استخدامها. |
ربط rust_library أو rust_ffi
يتم تلقائيًا ربط وحدات مكتبة Rust التي تستهدف الأجهزة ديناميكيًا
باستخدام libstd
. ومع ذلك، يتم ربط وحدات المضيف دائمًا بشكل ثابت
بـ libstd
.
يعتمد الربط المستخدَم لتبعيات rustlibs
على إعدادات الربط المفضّلة للوحدة الأساسية. (على سبيل المثال، سيؤدي استخدام rust_binary
مع prefer_rlib: true
إلى
استخدام صيغ مكتبة Rust التي تربط rustlibs
باسم rlibs
).
للسماح بإنشاء وحدات التبعيات الأساسية التي لا تعتمد على أي مكتبات برمجية dynami
rust (مثل الملفات التنفيذية الثابتة)، يوفّر rlibs
نوعَي ربط libstd
الديناميكي والثابت. يربط
Soong خيار المنتج الصحيح تلقائيًا.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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,["# Library modules\n\nThere are two types of Rust library modules, one that produces Rust libraries and\none that produces C-compatible libraries. Additionally, for build system\npurposes you can consider Rust procedural macros (`proc-macros`) as a special\ntype of library.\n\n### rust_library\n\nThe `rust_library` module produces Rust libraries for use by other Rust modules.\n\nIn addition to the `_host` variants, Rust libraries have module types that control\nthe available linkage.\n\n| Rust Library Module Type | Definition |\n|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **rust_library** | Provides both library variants, `rlib` and `dylib`. AOSP recommends this module type for Rust libraries, as it allows modules to work correctly when listed as a dependency under the `rustlibs` property |\n| **rust_library_rlib** | Provides only the `rlib` variant of a Rust library; modules providing only `rlib` variants can't be guaranteed to work with the `rustlibs` property. |\n| **rust_library_dylib** | Provides only the `dylib` variant of a Rust library; modules providing only `dylib` variants can't be guaranteed to work with the `rustlibs` property. |\n\n### rust_ffi\n\nThe `rust_ffi` module produces C-compatible libraries to interop with CC modules.\n\nIn addition to the `_host` variants, Rust FFI libraries have module types that\ncontrol the available linkage, shown in the following table.\n\n| Rust FFI Library Module Type | Definition |\n|------------------------------|------------------------------------------------------|\n| **rust_ffi** | Provides both C library variants: static and shared. |\n| **rust_ffi_shared** | Provides only the C shared library variant. |\n| **rust_ffi_static** | Provides only the C static library variant. |\n\nFor a basic example of using `rust_ffi` for calling Rust from C, see the\n[Android Rust Patterns](/docs/setup/build/rust/building-rust-modules/android-rust-patterns) page.\n\nFor information on more advanced usage, visit the [official Rust documentation](https://doc.rust-lang.org/nomicon/ffi.html#calling-rust-code-from-c).\n\n### rust_proc_macro\n\nRust procedural macros (proc-macros) can be useful for extending the compiler to\nperform source code transformations or providing new attributes. More information\ncan be found on these in the [Procedural Macros](https://doc.rust-lang.org/reference/procedural-macros.html)\npage of the official Rust documentation.\n\nFor the purposes of the build system, the `rust_proc_macro` module works similarly\nto the `rust_library` modules. For modules that depend on `rust_proc_macros`,\nadd the module name to the `proc_macros` property.\n\nAs `proc_macros` are compiler plugins, they necessarily target the host and don't\nproduce any code that would run on a device.\n\nNotable Rust library properties\n-------------------------------\n\nThe properties defined in the table below are in addition to the [Important common properties](/docs/setup/build/rust/building-rust-modules/android-rust-modules#important-common-properties)\nthat apply to all modules. These are either particularly important to Rust\nlibrary modules, or exhibit unique behavior specific to the `rust_library` module type.\n\n| Rust Library Properties | Definition |\n|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **stem / name** | The `stem` property controls the output library filename, which otherwise defaults to `name`. The Rust compiler imposes certain requirements on library filenames, and as a result the build system enforces these requirements to avoid compilation issues. The output filename must conform to the format `lib\u003ccrate_name\u003e\u003csuffix\u003e`. (There's a dependency here on the [`crate_name`](/docs/setup/build/rust/building-rust-modules/android-rust-modules#crate-name) property). |\n| **crate_name** | This is a required property for library-producing modules; it additionally has a relationship to the output filename. (See the [`stem`](/docs/setup/build/rust/building-rust-modules/android-rust-modules#stem) definition.) |\n| **export_include_dirs** | For `rust_ffi` modules, this property defines a list of strings representing relative include paths: paths which contain headers that dependent `cc` modules can use. |\n\nLinkage of rust_library or rust_ffi\n-----------------------------------\n\nBy default, Rust library modules targeting devices are always linked dynamically\nagainst `libstd`. Host modules, however, are always linked statically\nagainst `libstd`.\n\nThe linkage used for `rustlibs` dependencies depends on the root module's\nlinkage preferences. (For example, a `rust_binary` with `prefer_rlib: true` will\nuse Rust library variants that link `rustlibs` as `rlibs`.)\n\nTo allow production of root dependency modules that don't rely on any dynamic\nrust libraries (such as static executables), `rlibs` provides both dynamic and\nstatic `libstd` linkage variants. The correct variant is automatically linked by\nSoong."]]