| From f9f2bdfd156f62c67af78ed7c0a46ae67ec148e2 Mon Sep 17 00:00:00 2001 |
| From: Lorenz Brun <lorenz@monogon.tech> |
| Date: Wed, 25 Sep 2024 02:37:42 +0200 |
| Subject: [PATCH 2/4] Support no_std in Prost toolchain |
| |
| --- |
| extensions/prost/private/prost.bzl | 8 ++++++++ |
| extensions/prost/private/protoc_wrapper.rs | 18 +++++++++++++++++- |
| 2 files changed, 25 insertions(+), 1 deletion(-) |
| |
| diff --git a/extensions/prost/private/prost.bzl b/extensions/prost/private/prost.bzl |
| index f06719db..6d9d4969 100644 |
| --- a/extensions/prost/private/prost.bzl |
| +++ b/extensions/prost/private/prost.bzl |
| @@ -86,6 +86,9 @@ def _compile_proto( |
| additional_args.add("--additional_srcs={}".format(",".join([f.path for f in all_additional_srcs.to_list()]))) |
| additional_args.add_all(prost_toolchain.prost_opts + prost_opts, format_each = "--prost_opt=%s") |
| |
| + if prost_toolchain.is_nostd: |
| + additional_args.add("--is_nostd") |
| + |
| if prost_toolchain.tonic_plugin: |
| tonic_plugin = prost_toolchain.tonic_plugin[DefaultInfo].files_to_run |
| additional_args.add(prost_toolchain.tonic_plugin_flag % tonic_plugin.executable.path) |
| @@ -442,6 +445,7 @@ def _rust_prost_toolchain_impl(ctx): |
| tonic_plugin_flag = ctx.attr.tonic_plugin_flag, |
| tonic_runtime = ctx.attr.tonic_runtime, |
| include_transitive_deps = ctx.attr.include_transitive_deps, |
| + is_nostd = ctx.attr.is_nostd, |
| )] |
| |
| rust_prost_toolchain = rule( |
| @@ -497,6 +501,10 @@ rust_prost_toolchain = rule( |
| doc = "The Tonic runtime crates to use.", |
| providers = [[rust_common.crate_info], [rust_common.crate_group_info]], |
| ), |
| + "is_nostd": attr.bool( |
| + doc = "If a no_std tag should be put into the generated code.", |
| + default = False, |
| + ), |
| }, **proto_toolchains.if_legacy_toolchain({ |
| "_legacy_proto_toolchain": attr.label( |
| default = Label("//private:legacy_proto_toolchain"), |
| diff --git a/extensions/prost/private/protoc_wrapper.rs b/extensions/prost/private/protoc_wrapper.rs |
| index a0403f82..1dded7aa 100644 |
| --- a/extensions/prost/private/protoc_wrapper.rs |
| +++ b/extensions/prost/private/protoc_wrapper.rs |
| @@ -156,8 +156,17 @@ fn generate_lib_rs( |
| is_tonic: bool, |
| direct_dep_crate_names: Vec<String>, |
| additional_content: String, |
| + is_nostd: bool, |
| ) -> String { |
| - let mut contents = vec!["// @generated".to_string(), "".to_string()]; |
| + let mut contents = vec![ |
| + if is_nostd { |
| + "#![no_std]".to_string() |
| + } else { |
| + "".to_string() |
| + }, |
| + "// @generated".to_string(), |
| + "".to_string(), |
| + ]; |
| for crate_name in direct_dep_crate_names { |
| contents.push(format!("pub use {crate_name};")); |
| } |
| @@ -457,6 +462,9 @@ struct Args { |
| /// Whether to generate tonic code. |
| is_tonic: bool, |
| |
| + // Whether to put a no_std tag into the generated code. |
| + is_nostd: bool, |
| + |
| /// Extra arguments to pass to protoc. |
| extra_args: Vec<String>, |
| } |
| @@ -479,6 +487,7 @@ impl Args { |
| let mut tonic_or_prost_opts = Vec::new(); |
| let mut direct_dep_crate_names = Vec::new(); |
| let mut is_tonic = false; |
| + let mut is_nostd = false; |
| |
| let mut extra_args = Vec::new(); |
| |
| @@ -501,6 +510,10 @@ impl Args { |
| is_tonic = true; |
| return; |
| } |
| + if arg == "--is_nostd" { |
| + is_nostd = true; |
| + return; |
| + } |
| |
| if !arg.contains('=') { |
| extra_args.push(arg); |
| @@ -644,6 +657,7 @@ impl Args { |
| proto_paths, |
| direct_dep_crate_names, |
| is_tonic, |
| + is_nostd, |
| label: label.unwrap(), |
| extra_args, |
| }) |
| @@ -748,6 +762,7 @@ fn main() { |
| proto_paths, |
| direct_dep_crate_names, |
| is_tonic, |
| + is_nostd, |
| extra_args, |
| } = Args::parse().expect("Failed to parse args"); |
| |
| @@ -917,6 +932,7 @@ fn main() { |
| is_tonic, |
| direct_dep_crate_names, |
| additional_content, |
| + is_nostd, |
| ), |
| ) |
| .expect("Failed to write file."); |
| -- |
| 2.47.0 |
| |