blob: dc30a23086bd01d4c76f2330ecaacc6d9cd0af37 [file] [log] [blame]
Tim Windelschmidt5853fc92024-07-24 11:47:58 +00001From 8b464f085c9b255d81ee0e8501914ff7cb3cadd8 Mon Sep 17 00:00:00 2001
Tim Windelschmidt223609c2024-01-12 22:59:20 +01002From: Tim Windelschmidt <tim@monogon.tech>
3Date: Fri, 12 Jan 2024 15:41:50 +0100
Tim Windelschmidt5853fc92024-07-24 11:47:58 +00004Subject: [PATCH 1/2] Support no_std in Prost toolchain
Lorenz Brund141d182023-10-02 15:07:01 +02005
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
Tim Windelschmidt5853fc92024-07-24 11:47:58 +000012index c3a7d4e0..6f7449d0 100644
Lorenz Brund141d182023-10-02 15:07:01 +020013--- a/proto/prost/private/prost.bzl
14+++ b/proto/prost/private/prost.bzl
Tim Windelschmidt5853fc92024-07-24 11:47:58 +000015@@ -65,6 +65,9 @@ def _compile_proto(ctx, crate_name, proto_info, deps, prost_toolchain, rustfmt_t
Lorenz Brund141d182023-10-02 15:07:01 +020016 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")
Tim Windelschmidt223609c2024-01-12 22:59:20 +010018
Lorenz Brund141d182023-10-02 15:07:01 +020019+ 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)
Tim Windelschmidt5853fc92024-07-24 11:47:58 +000025@@ -372,6 +375,7 @@ def _rust_prost_toolchain_impl(ctx):
Lorenz Brund141d182023-10-02 15:07:01 +020026 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 )]
Tim Windelschmidt223609c2024-01-12 22:59:20 +010031
Lorenz Brund141d182023-10-02 15:07:01 +020032 rust_prost_toolchain = rule(
Tim Windelschmidt5853fc92024-07-24 11:47:58 +000033@@ -423,6 +427,10 @@ rust_prost_toolchain = rule(
Lorenz Brund141d182023-10-02 15:07:01 +020034 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+ ),
Tim Windelschmidt5853fc92024-07-24 11:47:58 +000041 }, **proto_toolchains.if_legacy_toolchain({
42 "_legacy_proto_toolchain": attr.label(
43 default = "//proto/protobuf:legacy_proto_toolchain",
Lorenz Brund141d182023-10-02 15:07:01 +020044diff --git a/proto/prost/private/protoc_wrapper.rs b/proto/prost/private/protoc_wrapper.rs
Tim Windelschmidt5853fc92024-07-24 11:47:58 +000045index 9c41892c..5d67640d 100644
Lorenz Brund141d182023-10-02 15:07:01 +020046--- a/proto/prost/private/protoc_wrapper.rs
47+++ b/proto/prost/private/protoc_wrapper.rs
Tim Windelschmidt223609c2024-01-12 22:59:20 +010048@@ -117,7 +117,7 @@ struct Module {
Lorenz Brund141d182023-10-02 15:07:01 +020049 /// }
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();
Tim Windelschmidt223609c2024-01-12 22:59:20 +010055
Lorenz Brund141d182023-10-02 15:07:01 +020056 for path in prost_outputs.iter() {
Tim Windelschmidt223609c2024-01-12 22:59:20 +010057@@ -189,7 +189,8 @@ fn generate_lib_rs(prost_outputs: &BTreeSet<PathBuf>, is_tonic: bool) -> String
Lorenz Brund141d182023-10-02 15:07:01 +020058 }
59 }
Tim Windelschmidt223609c2024-01-12 22:59:20 +010060
Lorenz Brund141d182023-10-02 15:07:01 +020061- 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 }
Tim Windelschmidt223609c2024-01-12 22:59:20 +010067@@ -443,6 +444,9 @@ struct Args {
Lorenz Brund141d182023-10-02 15:07:01 +020068 /// Whether to generate tonic code.
69 is_tonic: bool,
Tim Windelschmidt223609c2024-01-12 22:59:20 +010070
Lorenz Brund141d182023-10-02 15:07:01 +020071+ // 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 }
Tim Windelschmidt223609c2024-01-12 22:59:20 +010077@@ -463,6 +467,7 @@ impl Args {
Lorenz Brund141d182023-10-02 15:07:01 +020078 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;
Tim Windelschmidt223609c2024-01-12 22:59:20 +010082
Lorenz Brund141d182023-10-02 15:07:01 +020083 let mut extra_args = Vec::new();
Tim Windelschmidt223609c2024-01-12 22:59:20 +010084
85@@ -485,6 +490,10 @@ impl Args {
Lorenz Brund141d182023-10-02 15:07:01 +020086 is_tonic = true;
Tim Windelschmidt223609c2024-01-12 22:59:20 +010087 return;
Lorenz Brund141d182023-10-02 15:07:01 +020088 }
89+ if arg == "--is_nostd" {
90+ is_nostd = true;
Tim Windelschmidt223609c2024-01-12 22:59:20 +010091+ return;
Lorenz Brund141d182023-10-02 15:07:01 +020092+ }
Tim Windelschmidt223609c2024-01-12 22:59:20 +010093
Lorenz Brund141d182023-10-02 15:07:01 +020094 if !arg.contains('=') {
95 extra_args.push(arg);
Tim Windelschmidt223609c2024-01-12 22:59:20 +010096@@ -613,6 +622,7 @@ impl Args {
Lorenz Brund141d182023-10-02 15:07:01 +020097 rustfmt,
98 proto_paths,
99 is_tonic,
100+ is_nostd,
101 label: label.unwrap(),
102 extra_args,
103 })
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100104@@ -718,6 +728,7 @@ fn main() {
Lorenz Brund141d182023-10-02 15:07:01 +0200105 rustfmt,
106 proto_paths,
107 is_tonic,
108+ is_nostd,
109 extra_args,
110 } = Args::parse().expect("Failed to parse args");
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100111
112@@ -830,7 +841,7 @@ fn main() {
Lorenz Brund141d182023-10-02 15:07:01 +0200113 .expect("Failed to compute proto package info");
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100114
Lorenz Brund141d182023-10-02 15:07:01 +0200115 // 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
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100121--
Tim Windelschmidt5853fc92024-07-24 11:47:58 +00001222.44.1
Lorenz Brund141d182023-10-02 15:07:01 +0200123