Skip to main content

rust_library

name

def name(
*,
name: str,
default_target_platform: None | str = ...,
target_compatible_with: list[str] = ...,
compatible_with: list[str] = ...,
exec_compatible_with: list[str] = ...,
visibility: list[str] = ...,
within_view: list[str] = ...,
metadata: OpaqueMetadata = ...,
tests: list[str] = ...,
modifiers: OpaqueMetadata = ...,
_apple_platforms: dict[str, str] = ...,
_cxx_toolchain: str = ...,
_exec_os_type: str = ...,
_rust_internal_tools_toolchain: str = ...,
_rust_toolchain: str = ...,
_target_os_type: str = ...,
_workspaces: list[str] = ...,
clippy_configuration: None | str = ...,
contacts: list[str] = ...,
coverage: bool = ...,
crate: None | str = ...,
crate_dynamic: None | str = ...,
crate_root: None | str = ...,
default_host_platform: None | str = ...,
default_platform: None | str = ...,
deps: list[str] = ...,
doc_deps: list[str] = ...,
doc_env: dict[str, str] = ...,
doc_link_style: None | str = ...,
doc_linker_flags: list[str] = ...,
doc_named_deps: list[(str, str)] | dict[str, str] = ...,
doctests: None | bool = ...,
edition: None | str = ...,
env: dict[str, str] = ...,
exported_linker_flags: list[str] = ...,
exported_post_linker_flags: list[str] = ...,
features: list[str] = ...,
flagged_deps: list[(str, list[str])] = ...,
incremental_enabled: bool = ...,
labels: list[str] = ...,
licenses: list[str] = ...,
link_style: None | str = ...,
linker_flags: list[str] = ...,
mapped_srcs: dict[str, str] = ...,
named_deps: list[(str, str)] | dict[str, str] = ...,
preferred_linkage: str = ...,
proc_macro: bool = ...,
resources: list[str] | dict[str, str] = ...,
rustc_flags: list[str] = ...,
rustdoc_flags: list[str] = ...,
separate_debug_info: bool = ...,
soname: None | str = ...,
srcs: list[str] = ...,
supports_python_dlopen: None | bool = ...,
) -> None

A rust_library() rule builds a native library from the supplied set of Rust source files and dependencies.

Parameters

  • name: name of the target

  • default_target_platform: specifies the default target platform, used when no platforms are specified on the command line

  • target_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • exec_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with an execution platform

  • visibility: a list of visibility patterns restricting what targets can depend on this one

  • within_view: a list of visibility patterns restricting what this target can depend on

  • metadata: a key-value map of metadata associated with this target

  • tests: a list of targets that provide tests for this one

  • modifiers: an array of modifiers associated with this target

  • _workspaces: Internal implementation detail of Rust workspaces. This should not be set manually and will be replaced in favor of metadata in a future version of buck2.

  • crate_root: Set the name of the top-level source file for the crate, which can be used to override the default (see srcs).

  • deps: The set of dependencies of this rule. Currently, this supports rust_library and prebuilt_rust_library rules.

  • doc_deps: The set of dependencies of this rule. Currently, this supports rust_library and prebuilt_rust_library rules.

  • doc_env: Set environment variables for this rule's invocations of rustc. The environment variable values may include macros which are expanded.

  • doc_link_style: Determines whether to build and link this rule's dependencies statically or dynamically. Can be either static, static_pic or shared.

  • doc_linker_flags: The set of additional flags to pass to the linker.

  • doc_named_deps: Add crate dependencies and define a local name by which to use that dependency by. This allows a crate to have multiple dependencies with the same crate name. For example: named_deps = {"local_name", ":some_rust_crate" }. The dependencies may also be non-Rust, but the alias is ignored. It has no effect on the symbols provided by a C/C++ library.

  • edition: Set the language edition to be used for this rule. Can be set to any edition the compiler supports (2018 right now). If unset it uses the compiler's default (2015).

  • env: Set environment variables for this rule's invocations of rustc. The environment variable values may include macros which are expanded.

  • exported_linker_flags: A set of additional flag to pass before this item on the link line, even if this items is compiled to a DSO.

  • exported_post_linker_flags: A set of additional flag to pass after this item on the link line, even if this items is compiled to a DSO.

  • features: The set of features to be enabled for this rule.

    These are passed to rustc with --cfg feature="{feature}", and can be used in the code with #[cfg(feature = "{feature}")].

  • link_style: Determines whether to build and link this rule's dependencies statically or dynamically. Can be either static, static_pic or shared.

  • linker_flags: The set of additional flags to pass to the linker.

  • mapped_srcs: Add source files along with a local path mapping. Rust is sensitive to the layout of source files, as the directory structure follows the module structure. However this is awkward if the source file is, for example, generated by another rule. In this case, you can set up a mapping from the actual source path to something that makes sense locally. For example mapped_srcs = {":generate-module", "src/generated.rs" }. These are added to the regular srcs, so a file should not be listed in both.

  • named_deps: Add crate dependencies and define a local name by which to use that dependency by. This allows a crate to have multiple dependencies with the same crate name. For example: named_deps = {"local_name", ":some_rust_crate" }. The dependencies may also be non-Rust, but the alias is ignored. It has no effect on the symbols provided by a C/C++ library.

  • rustc_flags: The set of additional compiler flags to pass to rustc.

  • soname: Sets the soname ("shared object name") of any shared library produced from this rule. The default value is based on the full rule name. The macro $(ext) will be replaced with a platform-appropriate extension. An argument can be provided, which is a library version. For example soname = 'libfoo.$(ext 2.3)' will be libfoo.2.3.dylib on Mac and libfoo.so.2.3 on Linux.

  • srcs: The set of Rust source files to be compiled by this rule.

    One of the source files is the root module of the crate. By default this is lib.rs for libraries, main.rs for executables, or the crate's name with .rs appended. This can be overridden with the crate_root rule parameter.

Details

If you invoke a build with the check flavor, then Buck will invoke rustc to check the code (typecheck, produce warnings, etc), but won't generate an executable code. When applied to binaries it produces no output; for libraries it produces metadata for consumers of the library.

Note: Buck is currently tested with (and therefore supports) version 1.32.0 of Rust.

Examples:

For more examples, check out our integration tests.


rust_library(
name='greeting',
srcs=[
'greeting.rs',
],
deps=[
':join',
],
)