Skip to content

Commit bd961f8

Browse files
lorenzfionera
authored andcommitted
Support no_std in Prost toolchain
1 parent 7584b08 commit bd961f8

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

extensions/prost/private/prost.bzl

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ def _compile_proto(
8686
additional_args.add("--additional_srcs={}".format(",".join([f.path for f in all_additional_srcs.to_list()])))
8787
additional_args.add_all(prost_toolchain.prost_opts + prost_opts, format_each = "--prost_opt=%s")
8888

89+
if prost_toolchain.is_no_std:
90+
additional_args.add("--is_no_std")
91+
8992
if prost_toolchain.tonic_plugin:
9093
tonic_plugin = prost_toolchain.tonic_plugin[DefaultInfo].files_to_run
9194
additional_args.add(prost_toolchain.tonic_plugin_flag % tonic_plugin.executable.path)
@@ -442,6 +445,7 @@ def _rust_prost_toolchain_impl(ctx):
442445
tonic_plugin_flag = ctx.attr.tonic_plugin_flag,
443446
tonic_runtime = ctx.attr.tonic_runtime,
444447
include_transitive_deps = ctx.attr.include_transitive_deps,
448+
is_no_std = ctx.attr.is_no_std,
445449
)]
446450

447451
rust_prost_toolchain = rule(
@@ -453,6 +457,10 @@ rust_prost_toolchain = rule(
453457
doc = "Whether to include transitive dependencies. If set to True, all transitive dependencies will directly accessible by the dependent crate.",
454458
default = False,
455459
),
460+
"is_no_std": attr.bool(
461+
doc = "If a no_std tag should be put into the generated code.",
462+
default = False,
463+
),
456464
"prost_opts": attr.string_list(
457465
doc = "Additional options to add to Prost.",
458466
),

extensions/prost/private/protoc_wrapper.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,17 @@ fn generate_lib_rs(
156156
is_tonic: bool,
157157
direct_dep_crate_names: Vec<String>,
158158
additional_content: String,
159+
is_no_std: bool,
159160
) -> String {
160-
let mut contents = vec!["// @generated".to_string(), "".to_string()];
161+
let mut contents = vec![
162+
if is_no_std {
163+
"#![no_std]".to_string()
164+
} else {
165+
"".to_string()
166+
},
167+
"// @generated".to_string(),
168+
"".to_string(),
169+
];
161170
for crate_name in direct_dep_crate_names {
162171
contents.push(format!("pub use {crate_name};"));
163172
}
@@ -457,6 +466,9 @@ struct Args {
457466
/// Whether to generate tonic code.
458467
is_tonic: bool,
459468

469+
// Whether to put a no_std tag into the generated code.
470+
is_no_std: bool,
471+
460472
/// Extra arguments to pass to protoc.
461473
extra_args: Vec<String>,
462474
}
@@ -479,6 +491,7 @@ impl Args {
479491
let mut tonic_or_prost_opts = Vec::new();
480492
let mut direct_dep_crate_names = Vec::new();
481493
let mut is_tonic = false;
494+
let mut is_no_std = false;
482495

483496
let mut extra_args = Vec::new();
484497

@@ -501,6 +514,10 @@ impl Args {
501514
is_tonic = true;
502515
return;
503516
}
517+
if arg == "--is_no_std" {
518+
is_no_std = true;
519+
return;
520+
}
504521

505522
if !arg.contains('=') {
506523
extra_args.push(arg);
@@ -644,6 +661,7 @@ impl Args {
644661
proto_paths,
645662
direct_dep_crate_names,
646663
is_tonic,
664+
is_no_std,
647665
label: label.unwrap(),
648666
extra_args,
649667
})
@@ -748,6 +766,7 @@ fn main() {
748766
proto_paths,
749767
direct_dep_crate_names,
750768
is_tonic,
769+
is_no_std,
751770
extra_args,
752771
} = Args::parse().expect("Failed to parse args");
753772

@@ -917,6 +936,7 @@ fn main() {
917936
is_tonic,
918937
direct_dep_crate_names,
919938
additional_content,
939+
is_no_std,
920940
),
921941
)
922942
.expect("Failed to write file.");
@@ -972,7 +992,6 @@ fn escape_keyword(s: String) -> String {
972992

973993
#[cfg(test)]
974994
mod test {
975-
976995
use super::*;
977996

978997
use prost_types::{FieldDescriptorProto, ServiceDescriptorProto};

0 commit comments

Comments
 (0)