blob: 557a7c2a3b38bdac48df3e4e0c3817794a84998b [file] [log] [blame]
Tim Windelschmidt1f51cf42024-10-01 17:04:28 +02001From a3a14a5fe78a5f3d366fe2d0f3fcfb9ededc587b Mon Sep 17 00:00:00 2001
2From: Lorenz Brun <lorenz@monogon.tech>
3Date: Wed, 25 Sep 2024 02:37:42 +0200
4Subject: [PATCH 2/4] Support no_std in Prost toolchain
5
6---
7 proto/prost/private/prost.bzl | 8 ++++++++
8 proto/prost/private/protoc_wrapper.rs | 19 +++++++++++++++++--
9 2 files changed, 25 insertions(+), 2 deletions(-)
10
11diff --git a/proto/prost/private/prost.bzl b/proto/prost/private/prost.bzl
12index cf5e11c9..61d0bc63 100644
13--- a/proto/prost/private/prost.bzl
14+++ b/proto/prost/private/prost.bzl
15@@ -67,6 +67,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@@ -387,6 +390,7 @@ def _rust_prost_toolchain_impl(ctx):
26 tonic_plugin_flag = ctx.attr.tonic_plugin_flag,
27 tonic_runtime = ctx.attr.tonic_runtime,
28 include_transitive_deps = ctx.attr.include_transitive_deps,
29+ is_nostd = ctx.attr.is_nostd,
30 )]
31
32 rust_prost_toolchain = rule(
33@@ -442,6 +446,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 }, **proto_toolchains.if_legacy_toolchain({
42 "_legacy_proto_toolchain": attr.label(
43 default = "//proto/protobuf:legacy_proto_toolchain",
44diff --git a/proto/prost/private/protoc_wrapper.rs b/proto/prost/private/protoc_wrapper.rs
45index ff4decd4..2c32ea35 100644
46--- a/proto/prost/private/protoc_wrapper.rs
47+++ b/proto/prost/private/protoc_wrapper.rs
48@@ -152,8 +152,13 @@ fn generate_lib_rs(
49 prost_outputs: &BTreeSet<PathBuf>,
50 is_tonic: bool,
51 direct_dep_crate_names: Vec<String>,
52+ is_nostd: bool,
53 ) -> String {
54- let mut contents = vec!["// @generated".to_string(), "".to_string()];
55+ let mut contents = vec![
56+ if is_nostd { "#![no_std]".to_string() } else { "".to_string() },
57+ "// @generated".to_string(),
58+ "".to_string(),
59+ ];
60 for crate_name in direct_dep_crate_names {
61 contents.push(format!("pub use {crate_name};"));
62 }
63@@ -442,6 +447,9 @@ struct Args {
64 /// Whether to generate tonic code.
65 is_tonic: bool,
66
67+ // Whether to put a no_std tag into the generated code.
68+ is_nostd: bool,
69+
70 /// Extra arguments to pass to protoc.
71 extra_args: Vec<String>,
72 }
73@@ -463,6 +471,7 @@ impl Args {
74 let mut tonic_or_prost_opts = Vec::new();
75 let mut direct_dep_crate_names = Vec::new();
76 let mut is_tonic = false;
77+ let mut is_nostd = false;
78
79 let mut extra_args = Vec::new();
80
81@@ -485,6 +494,10 @@ impl Args {
82 is_tonic = true;
83 return;
84 }
85+ if arg == "--is_nostd" {
86+ is_nostd = true;
87+ return;
88+ }
89
90 if !arg.contains('=') {
91 extra_args.push(arg);
92@@ -621,6 +634,7 @@ impl Args {
93 proto_paths,
94 direct_dep_crate_names,
95 is_tonic,
96+ is_nostd,
97 label: label.unwrap(),
98 extra_args,
99 })
100@@ -727,6 +741,7 @@ fn main() {
101 proto_paths,
102 direct_dep_crate_names,
103 is_tonic,
104+ is_nostd,
105 extra_args,
106 } = Args::parse().expect("Failed to parse args");
107
108@@ -841,7 +856,7 @@ fn main() {
109 // Write outputs
110 fs::write(
111 &out_librs,
112- generate_lib_rs(&rust_files, is_tonic, direct_dep_crate_names),
113+ generate_lib_rs(&rust_files, is_tonic, direct_dep_crate_names, is_nostd),
114 )
115 .expect("Failed to write file.");
116 fs::write(
117--
1182.44.1
119