| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 1 | From f9f2bdfd156f62c67af78ed7c0a46ae67ec148e2 Mon Sep 17 00:00:00 2001 |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 2 | From: Lorenz Brun <lorenz@monogon.tech> |
| 3 | Date: Wed, 25 Sep 2024 02:37:42 +0200 |
| 4 | Subject: [PATCH 2/4] Support no_std in Prost toolchain |
| 5 | |
| 6 | --- |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 7 | extensions/prost/private/prost.bzl | 8 ++++++++ |
| 8 | extensions/prost/private/protoc_wrapper.rs | 18 +++++++++++++++++- |
| 9 | 2 files changed, 25 insertions(+), 1 deletion(-) |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 10 | |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 11 | diff --git a/extensions/prost/private/prost.bzl b/extensions/prost/private/prost.bzl |
| 12 | index f06719db..6d9d4969 100644 |
| 13 | --- a/extensions/prost/private/prost.bzl |
| 14 | +++ b/extensions/prost/private/prost.bzl |
| 15 | @@ -86,6 +86,9 @@ def _compile_proto( |
| 16 | additional_args.add("--additional_srcs={}".format(",".join([f.path for f in all_additional_srcs.to_list()]))) |
| 17 | additional_args.add_all(prost_toolchain.prost_opts + prost_opts, format_each = "--prost_opt=%s") |
| 18 | |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 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) |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 25 | @@ -442,6 +445,7 @@ def _rust_prost_toolchain_impl(ctx): |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 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 | )] |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 31 | |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 32 | rust_prost_toolchain = rule( |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 33 | @@ -497,6 +501,10 @@ rust_prost_toolchain = rule( |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 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( |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 43 | default = Label("//private:legacy_proto_toolchain"), |
| 44 | diff --git a/extensions/prost/private/protoc_wrapper.rs b/extensions/prost/private/protoc_wrapper.rs |
| 45 | index a0403f82..1dded7aa 100644 |
| 46 | --- a/extensions/prost/private/protoc_wrapper.rs |
| 47 | +++ b/extensions/prost/private/protoc_wrapper.rs |
| 48 | @@ -156,8 +156,17 @@ fn generate_lib_rs( |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 49 | is_tonic: bool, |
| 50 | direct_dep_crate_names: Vec<String>, |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 51 | additional_content: String, |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 52 | + is_nostd: bool, |
| 53 | ) -> String { |
| 54 | - let mut contents = vec!["// @generated".to_string(), "".to_string()]; |
| 55 | + let mut contents = vec![ |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 56 | + if is_nostd { |
| 57 | + "#![no_std]".to_string() |
| 58 | + } else { |
| 59 | + "".to_string() |
| 60 | + }, |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 61 | + "// @generated".to_string(), |
| 62 | + "".to_string(), |
| 63 | + ]; |
| 64 | for crate_name in direct_dep_crate_names { |
| 65 | contents.push(format!("pub use {crate_name};")); |
| 66 | } |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 67 | @@ -457,6 +462,9 @@ struct Args { |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 68 | /// Whether to generate tonic code. |
| 69 | is_tonic: bool, |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 70 | |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 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 | } |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 77 | @@ -479,6 +487,7 @@ impl Args { |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 78 | let mut tonic_or_prost_opts = Vec::new(); |
| 79 | let mut direct_dep_crate_names = Vec::new(); |
| 80 | let mut is_tonic = false; |
| 81 | + let mut is_nostd = false; |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 82 | |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 83 | let mut extra_args = Vec::new(); |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 84 | |
| 85 | @@ -501,6 +510,10 @@ impl Args { |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 86 | is_tonic = true; |
| 87 | return; |
| 88 | } |
| 89 | + if arg == "--is_nostd" { |
| 90 | + is_nostd = true; |
| 91 | + return; |
| 92 | + } |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 93 | |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 94 | if !arg.contains('=') { |
| 95 | extra_args.push(arg); |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 96 | @@ -644,6 +657,7 @@ impl Args { |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 97 | proto_paths, |
| 98 | direct_dep_crate_names, |
| 99 | is_tonic, |
| 100 | + is_nostd, |
| 101 | label: label.unwrap(), |
| 102 | extra_args, |
| 103 | }) |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 104 | @@ -748,6 +762,7 @@ fn main() { |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 105 | proto_paths, |
| 106 | direct_dep_crate_names, |
| 107 | is_tonic, |
| 108 | + is_nostd, |
| 109 | extra_args, |
| 110 | } = Args::parse().expect("Failed to parse args"); |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 111 | |
| 112 | @@ -917,6 +932,7 @@ fn main() { |
| 113 | is_tonic, |
| 114 | direct_dep_crate_names, |
| 115 | additional_content, |
| 116 | + is_nostd, |
| 117 | ), |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 118 | ) |
| 119 | .expect("Failed to write file."); |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame] | 120 | -- |
| 121 | 2.47.0 |
| Tim Windelschmidt | 1f51cf4 | 2024-10-01 17:04:28 +0200 | [diff] [blame] | 122 | |