| From 865b50b442368ba2a91a53d68b946e04022875e4 Mon Sep 17 00:00:00 2001 |
| From: Lorenz Brun <lorenz@monogon.tech> |
| Date: Wed, 25 Sep 2024 02:37:42 +0200 |
| Subject: [PATCH] Support no_std in Prost toolchain |
| |
| --- |
| extensions/prost/private/prost.bzl | 8 ++++++++ |
| extensions/prost/private/protoc_wrapper.rs | 23 ++++++++++++++++++++-- |
| 2 files changed, 29 insertions(+), 2 deletions(-) |
| |
| diff --git a/extensions/prost/private/prost.bzl b/extensions/prost/private/prost.bzl |
| index c5917107b..b06e126e6 100644 |
| --- a/extensions/prost/private/prost.bzl |
| +++ b/extensions/prost/private/prost.bzl |
| @@ -89,6 +89,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_no_std: |
| + additional_args.add("--is_no_std") |
| + |
| 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) |
| @@ -459,6 +462,7 @@ def _rust_prost_toolchain_impl(ctx): |
| tonic_runtime = ctx.attr.tonic_runtime, |
| include_transitive_deps = ctx.attr.include_transitive_deps, |
| compile_well_known_types = ctx.attr.compile_well_known_types, |
| + is_no_std = ctx.attr.is_no_std, |
| )] |
| |
| rust_prost_toolchain = rule( |
| @@ -474,6 +478,10 @@ rust_prost_toolchain = rule( |
| doc = "Whether to include transitive dependencies. If set to True, all transitive dependencies will directly accessible by the dependent crate.", |
| default = False, |
| ), |
| + "is_no_std": attr.bool( |
| + doc = "If a no_std tag should be put into the generated code.", |
| + default = False, |
| + ), |
| "prost_opts": attr.string_list( |
| doc = "Additional options to add to Prost.", |
| ), |
| diff --git a/extensions/prost/private/protoc_wrapper.rs b/extensions/prost/private/protoc_wrapper.rs |
| index 878f1c30c..8f09c77fc 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_no_std: bool, |
| ) -> String { |
| - let mut contents = vec!["// @generated".to_string(), "".to_string()]; |
| + let mut contents = vec![ |
| + if is_no_std { |
| + "#![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};")); |
| } |
| @@ -471,6 +480,9 @@ struct Args { |
| /// Whether to generate tonic code. |
| is_tonic: bool, |
| |
| + // Whether to put a no_std tag into the generated code. |
| + is_no_std: bool, |
| + |
| /// Extra arguments to pass to protoc. |
| extra_args: Vec<String>, |
| } |
| @@ -494,6 +506,7 @@ impl Args { |
| let mut direct_dep_crate_names = Vec::new(); |
| let mut is_tonic = false; |
| let mut compile_well_known_types = false; |
| + let mut is_no_std = false; |
| |
| let mut extra_args = Vec::new(); |
| |
| @@ -516,6 +529,10 @@ impl Args { |
| is_tonic = true; |
| return; |
| } |
| + if arg == "--is_no_std" { |
| + is_no_std = true; |
| + return; |
| + } |
| |
| if arg == "--compile_well_known_types" { |
| compile_well_known_types = true; |
| @@ -664,6 +681,7 @@ impl Args { |
| proto_paths, |
| direct_dep_crate_names, |
| is_tonic, |
| + is_no_std, |
| label: label.unwrap(), |
| extra_args, |
| compile_well_known_types, |
| @@ -769,6 +787,7 @@ fn main() { |
| proto_paths, |
| direct_dep_crate_names, |
| is_tonic, |
| + is_no_std, |
| extra_args, |
| compile_well_known_types, |
| } = Args::parse().expect("Failed to parse args"); |
| @@ -947,6 +966,7 @@ fn main() { |
| is_tonic, |
| direct_dep_crate_names, |
| additional_content, |
| + is_no_std, |
| ), |
| ) |
| .expect("Failed to write file."); |
| @@ -1002,7 +1022,6 @@ fn escape_keyword(s: String) -> String { |
| |
| #[cfg(test)] |
| mod test { |
| - |
| use super::*; |
| |
| use prost_types::{FieldDescriptorProto, ServiceDescriptorProto}; |
| -- |
| 2.49.0 |