diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 971514a0c0..dc19ba5342 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -137,12 +137,14 @@ def _rust_binary_impl(ctx): else: output = ctx.actions.declare_file(crate_name) + crate_type = getattr(ctx.attr, "crate_type") + return rustc_compile_action( ctx = ctx, toolchain = toolchain, crate_info = CrateInfo( name = crate_name, - type = "bin", + type = crate_type, root = _crate_root_src(ctx, "main.rs"), srcs = ctx.files.srcs, deps = ctx.attr.deps, @@ -435,6 +437,10 @@ _rust_binary_attrs = { cfg = "host", allow_single_file = True, ), + "crate_type": attr.string( + default = "bin", + ), + "out_binary": attr.bool(), } rust_binary = rule( diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 3c3a1e4982..cb52599155 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -345,6 +345,10 @@ def rustc_compile_action( files = dep_info.transitive_dylibs.to_list() + getattr(ctx.files, "data", []), collect_data = True, ) + + out_binary = False + if hasattr(ctx.attr, "out_binary"): + out_binary = getattr(ctx.attr, "out_binary") return [ crate_info, @@ -353,7 +357,7 @@ def rustc_compile_action( # nb. This field is required for cc_library to depend on our output. files = depset([crate_info.output]), runfiles = runfiles, - executable = crate_info.output if crate_info.type == "bin" else None, + executable = crate_info.output if crate_info.type == "bin" or out_binary else None, ), ]