rust_binary
A rust_binary() rule builds a native executable 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_binary(
*,
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] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
incoming_transition: None | str = None,
_apple_platforms: dict[str, str] = {},
_build_info: dict[str, typing.Any] = {},
_cxx_toolchain: str = "gh_facebook_buck2_shims_meta//:cxx",
_exec_os_type: str = "prelude//os_lookup/targets:os_lookup",
_rust_internal_tools_toolchain: str = "prelude//rust/tools:internal_tools_toolchain",
_rust_toolchain: str = "gh_facebook_buck2_shims_meta//:rust",
_target_os_type: str = "prelude//os_lookup/targets:os_lookup",
_workspaces: list[str] = [],
allow_cache_upload: None | bool = None,
anonymous_link_groups: bool = True,
auto_link_groups: bool = True,
clippy_configuration: None | str = None,
contacts: list[str] = [],
coverage: bool = False,
crate: None | str = None,
crate_root: None | str = None,
default_host_platform: None | str = None,
default_platform: None | str = None,
deps: list[str] = [],
edition: None | str = None,
enable_distributed_thinlto: bool = False,
env: dict[str, str] = {},
extra_dwp_flags: list[str] = [],
features: list[str] = [],
flagged_deps: list[(str, list[str])] = [],
incremental_enabled: bool = False,
labels: list[str] = [],
licenses: list[str] = [],
link_group: None | str = None,
link_group_map: None | str | list[(str, list[(None | str | list[None | str], str, None | str | list[str], None | str)], None | dict[str, typing.Any])] = None,
link_group_min_binary_node_count: None | int = None,
link_style: None | str = None,
linker_flags: list[str] = [],
mapped_srcs: dict[str, str] = {},
named_deps: list[(str, str)] | dict[str, str] = {},
resources: list[str] | dict[str, str] = [],
rpath: bool = False,
rustc_flags: list[str] = [],
rustdoc_flags: list[str] = [],
separate_debug_info: bool = False,
srcs: list[str] = [],
transformation_spec: None | str = None,
) -> 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
-
_build_info
: (defaults to:{}
)Build info that is passed along here will be late-stamped into a fb_build_info section on the output binary
-
_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.
-
allow_cache_upload
: (defaults to:None
)Whether to allow uploading the output of this rule to be uploaded to cache when the action is executed locally if the configuration allows (i.e. there is a cache configured and the client has permission to write to it).
-
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
). -
deps
: (defaults to:[]
)The set of dependencies of this rule. Currently, this supports rust_library and prebuilt_rust_library rules.
-
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.
-
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}")]
. -
link_style
: (defaults to:None
)Determines whether to build and link this rule's dependencies statically or dynamically. Can be either
static
,static_pic
orshared
. -
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 regularsrcs
, 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. -
rpath
: (defaults to:False
)Set the "rpath" in the executable when using a shared link style.
-
rustc_flags
: (defaults to:[]
)The set of additional compiler flags to pass to
rustc
. -
srcs
: (defaults to:[]
)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 thecrate_root
rule parameter. -
transformation_spec
: (defaults to:None
)Dependency to a transformation spec that will selectively change parts of dependency graph
Examples
For more examples, check out our integration tests.
rust_binary(
name='greet',
srcs=[
'greet.rs',
],
deps=[
':greeting',
],
)
rust_library(
name='greeting',
srcs=[
'greeting.rs',
],
deps=[
':join',
],
)
rust_library(
name='join',
srcs=[
'join.rs',
],
)