blob: 71c205890804f4f1cfc80ff487dc0beecd86aaa3 [file] [log] [blame]
Lorenz Brund141d182023-10-02 15:07:01 +02001From 8cc9584796b41c3b5c9d213b751de9762963ebbf Mon Sep 17 00:00:00 2001
2From: Lorenz Brun <lorenz@monogon.tech>
3Date: Mon, 2 Oct 2023 15:35:38 +0200
4Subject: [PATCH] Support no_std in Prost toolchain
5
6---
7 proto/prost/private/prost.bzl | 8 ++++++++
8 proto/prost/private/protoc_wrapper.rs | 17 ++++++++++++++---
9 2 files changed, 22 insertions(+), 3 deletions(-)
10
11diff --git a/proto/prost/private/prost.bzl b/proto/prost/private/prost.bzl
12index 6cd3d522..ae7ead91 100644
13--- a/proto/prost/private/prost.bzl
14+++ b/proto/prost/private/prost.bzl
15@@ -64,6 +64,9 @@ def _compile_proto(ctx, crate_name, proto_info, deps, prost_toolchain, rustfmt_t
16 additional_args.add("--descriptor_set={}".format(proto_info.direct_descriptor_set.path))
17 additional_args.add_all(prost_toolchain.prost_opts, format_each = "--prost_opt=%s")
18
19+ if prost_toolchain.is_nostd:
20+ additional_args.add("--is_nostd")
21+
22 if prost_toolchain.tonic_plugin:
23 tonic_plugin = prost_toolchain.tonic_plugin[DefaultInfo].files_to_run
24 additional_args.add(prost_toolchain.tonic_plugin_flag % tonic_plugin.executable.path)
25@@ -371,6 +374,7 @@ def _rust_prost_toolchain_impl(ctx):
26 tonic_plugin = ctx.attr.tonic_plugin,
27 tonic_plugin_flag = ctx.attr.tonic_plugin_flag,
28 tonic_runtime = ctx.attr.tonic_runtime,
29+ is_nostd = ctx.attr.is_nostd,
30 )]
31
32 rust_prost_toolchain = rule(
33@@ -423,6 +427,10 @@ rust_prost_toolchain = rule(
34 doc = "The Tonic runtime crates to use.",
35 providers = [[rust_common.crate_info], [rust_common.crate_group_info]],
36 ),
37+ "is_nostd": attr.bool(
38+ doc = "If a no_std tag should be put into the generated code.",
39+ default = False,
40+ ),
41 },
42 )
43
44diff --git a/proto/prost/private/protoc_wrapper.rs b/proto/prost/private/protoc_wrapper.rs
45index eb0c3319..716ba211 100644
46--- a/proto/prost/private/protoc_wrapper.rs
47+++ b/proto/prost/private/protoc_wrapper.rs
48@@ -116,7 +116,7 @@ struct Module {
49 /// }
50 /// }
51 /// ```
52-fn generate_lib_rs(prost_outputs: &BTreeSet<PathBuf>, is_tonic: bool) -> String {
53+fn generate_lib_rs(prost_outputs: &BTreeSet<PathBuf>, is_tonic: bool, is_nostd: bool) -> String {
54 let mut module_info = BTreeMap::new();
55
56 for path in prost_outputs.iter() {
57@@ -188,7 +188,8 @@ fn generate_lib_rs(prost_outputs: &BTreeSet<PathBuf>, is_tonic: bool) -> String
58 }
59 }
60
61- let mut content = "// @generated\n\n".to_string();
62+ let mut content = if is_nostd { "#![no_std]\n".to_string() } else { "".to_string() };
63+ content.push_str(&"// @generated\n\n");
64 write_module(&mut content, &module_info, "", 0);
65 content
66 }
67@@ -442,6 +443,9 @@ struct Args {
68 /// Whether to generate tonic code.
69 is_tonic: bool,
70
71+ // Whether to put a no_std tag into the generated code.
72+ is_nostd: bool,
73+
74 /// Extra arguments to pass to protoc.
75 extra_args: Vec<String>,
76 }
77@@ -462,6 +466,7 @@ impl Args {
78 let mut label: Option<String> = None;
79 let mut tonic_or_prost_opts = Vec::new();
80 let mut is_tonic = false;
81+ let mut is_nostd = false;
82
83 let mut extra_args = Vec::new();
84
85@@ -487,6 +492,10 @@ impl Args {
86 is_tonic = true;
87 continue;
88 }
89+ if arg == "--is_nostd" {
90+ is_nostd = true;
91+ continue;
92+ }
93
94 if !arg.contains('=') {
95 extra_args.push(arg);
96@@ -599,6 +608,7 @@ impl Args {
97 rustfmt,
98 proto_paths,
99 is_tonic,
100+ is_nostd,
101 label: label.unwrap(),
102 extra_args,
103 })
104@@ -704,6 +714,7 @@ fn main() {
105 rustfmt,
106 proto_paths,
107 is_tonic,
108+ is_nostd,
109 extra_args,
110 } = Args::parse().expect("Failed to parse args");
111
112@@ -816,7 +827,7 @@ fn main() {
113 .expect("Failed to compute proto package info");
114
115 // Write outputs
116- fs::write(&out_librs, generate_lib_rs(&rust_files, is_tonic)).expect("Failed to write file.");
117+ fs::write(&out_librs, generate_lib_rs(&rust_files, is_tonic, is_nostd)).expect("Failed to write file.");
118 fs::write(
119 package_info_file,
120 extern_paths
121--
1222.41.0
123