rust_mobile_library

Rust library materialized by Apple and Android native consumers.

Description#

Describes one first-party Rust library target that Apple and Android consumers can materialize for their platform. Apple consumers declare a staticlib compile, and Android consumers declare a cdylib compile. Each consumer only declares the variant it needs, so Android-only builds do not require the Apple Rust target and Apple-only builds do not require the Android linker.

The target emits native provider fields for Apple and Android app targets. It also emits rust_mobile_crate, which lets another rust_mobile_library consume it. Dependencies are materialized recursively for the platform that requested the native output, so one mobile crate graph can serve both platforms without compiling the unused variant. It does not emit rust_crate, because there is no single rlib output for downstream host Rust compilation.

Attributes#

AttributeTypeRequiredDefaultDescription
crate_namestringnotarget nameRust crate name passed to rustc; - and . are rewritten as _ when omitted
crate_rootstringnosrc/lib.rsPackage-relative library root
editionstringno2021Rust edition passed to rustc
featureslist<string>no[]Cargo feature names lowered to --cfg=feature=... flags
crate_featureslist<string>no[]Bazel-compatible alias for features
apple_targetstringyesRust target triple for the Apple static library compile
android_targetstringyesRust target triple for the Android shared library compile
envmap<string, string>no{}Environment variables for rustc, matching Buck2's env attribute
rustc_envmap<string, string>no{}Bazel-compatible rustc environment variables
rustc_env_fileslist<string>no[]Files with NAME=value entries merged into the rustc environment before env and rustc_env
rustc_flagslist<string>no[]Additional rustc flags appended after Once-managed flags
cap_lintsstringnoemptyOptional rustc lint cap passed as --cap-lints; generated Cargo dependencies use allow
linker_flagslist<string>no[]Additional linker flags lowered to -C link-arg=...
native_linkoptslist<string>no[]Linker flags propagated to Apple app or framework targets
exported_linker_flagslist<string>no[]Buck-compatible alias for native linker flags propagated to downstream native consumers
exported_post_linker_flagslist<string>no[]Buck-compatible propagated linker flags appended after normal exported linker flags
linker_scriptstringnoemptyPackage-relative linker script passed to each platform linker and included in compile action inputs
android_abistringnoinferredAndroid Application Binary Interface directory for the Android shared library output, such as arm64-v8a
android_apiintno23Android platform level used to select the Android Native Development Kit clang wrapper
android_ndkstringnoANDROID_NDK_HOMEAndroid Native Development Kit root used to find clang wrapper linkers
datalist<string>no[]Runtime data file globs propagated through each materialized platform provider
compile_datalist<string>no[]Bazel-compatible compile-time data file globs included in each platform rustc action input set
build_scriptstringnoemptyPackage-relative Cargo build script path run before each platform compile

Dependency Edges#

EdgeAcceptsDescription
depsrust_mobile_crateRust mobile crate dependencies compiled recursively for the same Apple or Android target as the consumer

Providers#

The target emits rust_mobile_crate, native_linkable, apple_linkable, and android_native_library.

Provider Record#

FieldTypeMeaning
label_idstringOriginal target label id
mobile_depslist<record>Deferred Rust mobile dependency providers materialized for the requesting platform
transitive_sourceslist<string>Rust sources from this target and its mobile crate dependencies
transitive_datalist<string>Runtime data propagated from this target and its mobile crate dependencies

Apple consumers materialize archive, staticlib, transitive_archives, and transitive_linkopts while collecting their link inputs. Android consumers materialize android_abi, dylib, android_native_libraries, and transitive_android_native_libraries while collecting native libraries.

Capabilities#

CapabilityOutput groups
buildnone

Outputs#

OutputLocation
Apple static library.once/out/<consumer>/rust-mobile/<target>/apple/lib<crate_name>.a or .lib
Android shared library.once/out/<consumer>/rust-mobile/<target>/android/lib<crate_name>.so

Example#

toml
[[target]]
name = "SharedRust"
kind = "rust_mobile_library"
deps = ["./SharedCore"]
srcs = ["shared/src/**/*.rs"]
[target.attrs]
crate_name = "shared"
crate_root = "shared/src/lib.rs"
edition = "2021"
apple_target = "aarch64-apple-ios-sim"
android_target = "aarch64-linux-android"
android_abi = "arm64-v8a"
android_api = 24
[[target]]
name = "SharedCore"
kind = "rust_mobile_library"
srcs = ["core/src/**/*.rs"]
[target.attrs]
crate_name = "shared_core"
crate_root = "core/src/lib.rs"
apple_target = "aarch64-apple-ios-sim"
android_target = "aarch64-linux-android"
android_abi = "arm64-v8a"