Skip to main content

rust_library

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

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.

Function Signature​

def rust_library(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str | target_name_glob] = [],
within_view: list[str | target_name_glob] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
incoming_transition: None | str = None,
_workspaces: list[str] = [],
clippy_configuration: None | str = None,
clippy_flags: list[str] = [],
contacts: list[str] = [],
coverage: bool = False,
crate: None | str = None,
crate_dynamic: None | str = None,
crate_root: None | str = None,
default_deps: str = "deps",
default_platform: None | str = None,
deps: list[str] = [],
doc_deps: list[str] = [],
doc_env: dict[str, str] = {},
doc_link_style: None | str = None,
doc_linker_flags: list[str] = [],
doc_named_deps: list[(str, str)] | dict[str, str] = {},
doc_remote_execution: None | dict[str, None | bool | int | str | list[dict[str, str]] | dict[str, int | str | list[str] | dict[str, str]]] = None,
doc_supports_test_execution_caching: bool = True,
doctests: None | bool = None,
edition: None | str = None,
env: dict[str, str] = {},
exported_linker_flags: list[str] = [],
exported_post_linker_flags: list[str] = [],
features: list[str] = [],
flagged_deps: list[(str, list[str])] = [],
include_in_android_merge_map_output: bool = True,
incremental_enabled: bool = False,
labels: list[str] = [],
licenses: list[str] = [],
link_style: None | str = None,
link_whole: None | bool = None,
linker_flags: list[str] = [],
mapped_srcs: dict[str, str] = {},
multi_arch_toolchain: None | str = None,
named_deps: list[(str, str)] | dict[str, str] = {},
preferred_linkage: str = "any",
proc_macro: bool = False,
resources: list[str] | dict[str, str] = [],
rustc_flags: list[str] = [],
rustdoc_flags: list[str] = [],
separate_debug_info: bool = False,
soname: None | str = None,
srcs: list[str] = [],
srcs_filegroup: None | str = None,
supports_python_dlopen: None | bool = None,
use_content_based_paths: bool = True,
uses_restricted_rustc_flags: bool = False,
) -> None

Parameters​

  • name: (required)

    name of the target

  • default_target_platform: (defaults to: None)

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

  • target_compatible_with: (defaults to: [])

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

  • compatible_with: (defaults to: [])

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

  • exec_compatible_with: (defaults to: [])

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

  • visibility: (defaults to: [])

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

  • within_view: (defaults to: ["PUBLIC"])

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

  • metadata: (defaults to: {})

    a key-value map of metadata associated with this target

  • tests: (defaults to: [])

    a list of targets that provide tests for this one

  • modifiers: (defaults to: [])

    an array of modifiers associated with this target

  • incoming_transition: (defaults to: None)

    specifies the incoming transition to apply to this target

  • _workspaces: (defaults to: [])

    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.

  • clippy_flags: (defaults to: [])

    Flags passed to clippy but not rustc, ahead of rustc_flags so those can override them.

  • contacts: (defaults to: [])

    A list of organizational contacts for this rule. These could be individuals who you would contact in the event of a failure or other issue with the rule.

    contacts = [ 'Joe Sixpack', 'Erika Mustermann' ]
  • crate: (defaults to: None)

    The name of the crate, which is the name that other crates use to refer to this library. Defaults to the name of the target. Hyphens are replaced with underscores.

  • crate_dynamic: (defaults to: None)

    The label of a target whose output is a file that contains the name of the crate, for a crate whose name is only known once the build runs. Other crates refer to this library by the name in that file. The output files of the library are still named after crate or the name of its target.

  • crate_root: (defaults to: None)

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

  • default_deps: (defaults to: "deps")

    Whether to include the default deps from the toolchain, and where to include them (deps, exported_deps, etc).

  • deps: (defaults to: [])

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

  • doc_deps: (defaults to: [])

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

  • doc_env: (defaults to: {})

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

  • doc_link_style: (defaults to: None)

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

  • doc_linker_flags: (defaults to: [])

    The set of additional flags to pass to the linker.

  • doc_named_deps: (defaults to: {})

    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.

  • doc_remote_execution: (defaults to: None)

    Remote execution properties for this library's rustdoc test. Only a property dict is accepted, not the name of a profile on the remote test execution toolchain, so that rust_library does not have to depend on that toolchain. When unset, the rustdoc test carries no executor and runs wherever the test framework would otherwise place it.

  • doctests: (defaults to: None)

    Whether the documentation tests of this library are run as a test of this target. Defaults to the doctests setting of the Rust toolchain. Documentation tests are not run when cross-compiling, or with a Rust toolchain that does not set nightly_features.

  • edition: (defaults to: None)

    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: (defaults to: {})

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

  • exported_linker_flags: (defaults to: [])

    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: (defaults to: [])

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

  • features: (defaults to: [])

    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}")].

  • incremental_enabled: (defaults to: False)

    If True, the crate is compiled with the incremental compilation of rustc (-C incremental). The compilation then always runs locally, and its outputs are not uploaded to a cache.

  • labels: (defaults to: [])

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query().

  • licenses: (defaults to: [])

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query

  • link_style: (defaults to: None)

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

  • link_whole: (defaults to: None)

    If True, a native link that takes this library as a static library, such as the link of a cxx_binary, takes every object file in it, including the object files that nothing refers to (for example with --whole-archive). By default the linker only takes the object files that it needs.

  • linker_flags: (defaults to: [])

    The set of additional flags to pass to the linker.

  • mapped_srcs: (defaults to: {})

    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: (defaults to: {})

    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.

  • proc_macro: (defaults to: False)

    If True, this library is a procedural macro crate. It is compiled with --crate-type=proc-macro, and the crates that depend on it can use the macros that it defines.

  • resources: (defaults to: [])

    Files that the crate needs at runtime. Each entry is a file or a target whose output is used. An executable built by rust_binary or rust_test that has resources, of its own or of the libraries it depends on, gets a JSON file <executable>.resources.json next to it. The file maps the name of each resource to its path relative to the executable. A resource is named by the package path followed by its path relative to the package, or for a target by the path of its output. Use a dictionary to give the part after the package path yourself.

  • rustc_flags: (defaults to: [])

    The set of additional compiler flags to pass to rustc.

  • rustdoc_flags: (defaults to: [])

    Flags to pass to rustdoc for this crate, after the rustdoc_flags of the Rust toolchain. They are used when building the documentation (the doc subtarget), and for a rust_library also when running its documentation tests.

  • soname: (defaults to: None)

    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: (defaults to: [])

    The set of Rust source files to be compiled by this target.

    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 attribute.

  • srcs_filegroup: (defaults to: None)

    Directory of sources already combined together from srcs + mapped_srcs. This allows sources to be located in a different Buck package than the one containing the rust_library target which compiles those sources.

    When using srcs_filegroup, the attributes srcs and mapped_srcs cannot also be passed, and crate_root must be passed.

Examples​

For more examples, check out our integration tests.


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