Skip to content

Commit bfb0dcc

Browse files
authored
add flag to allow putting out binary whenever (#312)
I'd like to build Rust code like this into `wasm32-unknown-unknown`. https://github.com/Shikugawa/envoy-wasm-rust-playground/blob/master/example/src/lib.rs This code is build with this build config. https://github.com/Shikugawa/envoy-wasm-rust-playground/blob/master/example/Cargo.toml We want to put out binary but not want to build with `--cargo-type=lib` but `--cargo-type=cdylib`. In the current implementation, we can't put out binary without using `--cargo-type=bin`. This was a critical problem for us so that patched this. I'm considering to use this like this. This will put out `test_rust.wasm` with this command. ``` bazel build //:test_rust --platforms=@io_bazel_rules_rust//rust/platform:wasm ``` ``` rust_binary( name = "test_rust", srcs = ["test_rust.rs"], crate_type = "cdylib", out_binary = True, ) ```
1 parent cd73f34 commit bfb0dcc

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

rust/private/rust.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ def _rust_binary_impl(ctx):
148148
else:
149149
output = ctx.actions.declare_file(ctx.label.name)
150150

151+
crate_type = getattr(ctx.attr, "crate_type")
152+
151153
return rustc_compile_action(
152154
ctx = ctx,
153155
toolchain = toolchain,
154156
crate_info = CrateInfo(
155157
name = crate_name,
156-
type = "bin",
158+
type = crate_type,
157159
root = _crate_root_src(ctx, "main.rs"),
158160
srcs = ctx.files.srcs,
159161
deps = ctx.attr.deps,
@@ -454,6 +456,10 @@ _rust_binary_attrs = {
454456
cfg = "host",
455457
allow_single_file = True,
456458
),
459+
"crate_type": attr.string(
460+
default = "bin",
461+
),
462+
"out_binary": attr.bool(),
457463
}
458464

459465
rust_binary = rule(

rust/private/rustc.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ def rustc_compile_action(
373373
files = dep_info.transitive_dylibs.to_list() + getattr(ctx.files, "data", []),
374374
collect_data = True,
375375
)
376+
377+
out_binary = False
378+
if hasattr(ctx.attr, "out_binary"):
379+
out_binary = getattr(ctx.attr, "out_binary")
376380

377381
return [
378382
crate_info,
@@ -381,7 +385,7 @@ def rustc_compile_action(
381385
# nb. This field is required for cc_library to depend on our output.
382386
files = depset([crate_info.output]),
383387
runfiles = runfiles,
384-
executable = crate_info.output if crate_info.type == "bin" else None,
388+
executable = crate_info.output if crate_info.type == "bin" or out_binary else None,
385389
),
386390
]
387391

0 commit comments

Comments
 (0)