-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
146 lines (128 loc) · 5.42 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2023 MASSA LABS <[email protected]>
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "tonic-build")]
tonic::build()?;
Ok(())
}
#[cfg(feature = "tonic-build")]
mod tonic {
use glob::glob;
use std::{
env,
path::{Path, PathBuf},
};
/// This function is responsible for building the Massa protobuf
pub fn build() -> Result<(), Box<dyn std::error::Error>> {
let parent_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
// Generate ABI bindings
let protos_path = parent_dir.join("massa-proto/proto/abis");
let protos = find_protos(protos_path)?;
let proto_include_paths = [
parent_dir.join("massa-proto/proto/abis"),
parent_dir.join("massa-proto/proto/commons"),
parent_dir.join("massa-proto/proto/third_party"),
];
tonic_build::configure()
.build_server(false)
.build_transport(false)
.build_client(false)
.include_file("_abi_includes.rs")
.out_dir("src/")
.compile(&protos, &proto_include_paths)
.map_err(|e| format!("ABI protobuf compilation error: {:?}", e))?;
// Generate PUBLIC API file descriptor set
let public_api_path = parent_dir.join("massa-proto/proto/apis/massa/api/v1/public.proto");
let proto_include_paths = [
parent_dir.join("massa-proto/proto/apis"),
parent_dir.join("massa-proto/proto/commons"),
parent_dir.join("massa-proto/proto/third_party"),
];
tonic_build::configure()
.build_server(true)
.build_transport(true)
.build_client(true)
.type_attribute(
".google.api.HttpRule",
"#[cfg(not(doctest))]\n\
#[allow(dead_code)]\n\
pub struct HttpRuleComment{}\n\
/// HACK: see docs in [`HttpRuleComment`] ignored in doctest pass",
)
.include_file("_api_includes.rs")
.file_descriptor_set_path("src/api_public.bin")
.out_dir("src/")
.compile(&vec![public_api_path], &proto_include_paths)
.map_err(|e| format!("PUBLIC API protobuf compilation error: {:?}", e))?;
// Generate PRIVATE API file descriptor set
let private_api_path = parent_dir.join("massa-proto/proto/apis/massa/api/v1/private.proto");
let proto_include_paths = [
parent_dir.join("massa-proto/proto/apis"),
parent_dir.join("massa-proto/proto/commons"),
parent_dir.join("massa-proto/proto/third_party"),
];
tonic_build::configure()
.build_server(true)
.build_transport(true)
.build_client(true)
.type_attribute(
".google.api.HttpRule",
"#[cfg(not(doctest))]\n\
#[allow(dead_code)]\n\
pub struct HttpRuleComment{}\n\
/// HACK: see docs in [`HttpRuleComment`] ignored in doctest pass",
)
.include_file("_api_includes.rs")
.file_descriptor_set_path("src/api_private.bin")
.out_dir("src/")
.compile(&vec![private_api_path], &proto_include_paths)
.map_err(|e| format!("PRIVATE API protobuf compilation error: {:?}", e))?;
// Generate API bindings
let protos_path = parent_dir.join("massa-proto/proto/apis");
let protos = find_protos(protos_path)?;
let proto_include_paths = [
parent_dir.join("massa-proto/proto/apis"),
parent_dir.join("massa-proto/proto/commons"),
parent_dir.join("massa-proto/proto/third_party"),
];
tonic_build::configure()
.build_server(true)
.build_transport(true)
.build_client(true)
.type_attribute(
".google.api.HttpRule",
"#[cfg(not(doctest))]\n\
#[allow(dead_code)]\n\
pub struct HttpRuleComment{}\n\
/// HACK: see docs in [`HttpRuleComment`] ignored in doctest pass",
)
.include_file("_api_includes.rs")
.out_dir("src/")
.compile(&protos, &proto_include_paths)
.map_err(|e| format!("API protobuf compilation error: {:?}", e))?;
// Generate COMMONS bindings
let protos_path = parent_dir.join("massa-proto/proto/commons");
let protos = find_protos(protos_path)?;
let proto_include_paths = [parent_dir.join("massa-proto/proto/commons")];
tonic_build::configure()
.build_server(false)
.build_transport(false)
.build_client(false)
.include_file("_commons_includes.rs")
.out_dir("src/")
.compile(&protos, &proto_include_paths)
.map_err(|e| format!("COMMONS protobuf compilation error: {:?}", e))?;
Ok(())
}
fn find_protos(dir_path: impl AsRef<Path>) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
let glob_pattern = format!("{}/**/*.proto", dir_path.as_ref().display());
let paths: Vec<_> = glob(&glob_pattern)?.filter_map(Result::ok).collect();
if paths.is_empty() {
return Err(format!(
"no .proto files found in the specified directory: {}",
dir_path.as_ref().display()
)
.into());
}
Ok(paths)
}
}