-
Notifications
You must be signed in to change notification settings - Fork 332
/
compile.rs
221 lines (191 loc) · 7.07 KB
/
compile.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::fs::remove_dir_all;
use std::fs::{copy, create_dir_all};
use std::path::{Path, PathBuf};
use std::process;
use git2::Repository;
use tempdir::TempDir;
use walkdir::WalkDir;
use argh::FromArgs;
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "compile")]
/// Compile
pub struct CompileCmd {
#[argh(option, short = 's')]
/// path to the Cosmos SDK
sdk: PathBuf,
#[argh(option, short = 'i')]
/// path to the Cosmos IBC proto files
ibc: PathBuf,
#[argh(option, short = 'o')]
/// path to output the generated Rust sources into
out: PathBuf,
}
impl CompileCmd {
pub fn run(&self) {
let tmp = TempDir::new("ibc-proto").unwrap();
Self::output_version(&self.sdk, tmp.as_ref(), "COSMOS_SDK_COMMIT");
Self::output_version(&self.ibc, tmp.as_ref(), "COSMOS_IBC_COMMIT");
Self::compile_sdk_protos(&self.sdk, tmp.as_ref());
Self::compile_ibc_protos(&self.ibc, &self.sdk, tmp.as_ref());
Self::copy_generated_files(tmp.as_ref(), &self.out);
}
fn output_version(dir: &Path, out_dir: &Path, commit_file: &str) {
let repo = Repository::open(dir).unwrap();
let commit = repo.head().unwrap();
let rev = commit.shorthand().unwrap();
let path = out_dir.join(commit_file);
std::fs::write(path, rev).unwrap();
}
fn compile_ibc_protos(ibc_dir: &Path, sdk_dir: &Path, out_dir: &Path) {
println!(
"[info ] Compiling IBC .proto files to Rust into '{}'...",
out_dir.display()
);
// Paths
let proto_paths = [
// ibc-go proto files
format!("{}/proto/ibc", ibc_dir.display()),
];
let proto_includes_paths = [
format!("{}/proto", ibc_dir.display()),
format!("{}/proto/cosmos", sdk_dir.display()),
format!("{}/third_party/proto", ibc_dir.display()),
];
// List available proto files
let mut protos: Vec<PathBuf> = vec![];
for proto_path in &proto_paths {
println!("Looking for proto files in {:?}", proto_path);
protos.append(
&mut WalkDir::new(proto_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type().is_file()
&& e.path().extension().is_some()
&& e.path().extension().unwrap() == "proto"
})
.map(|e| e.into_path())
.collect(),
);
}
println!("Found the following protos:");
// Show which protos will be compiled
for proto in &protos {
println!("\t-> {:?}", proto);
}
println!("[info ] Compiling..");
// List available paths for dependencies
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();
let compilation = tonic_build::configure()
.build_client(true)
.build_server(false)
.format(false)
.out_dir(out_dir)
.extern_path(".tendermint", "::tendermint_proto")
.compile(&protos, &includes);
match compilation {
Ok(_) => {
println!("Successfully compiled proto files");
}
Err(e) => {
println!("Failed to compile:{:?}", e.to_string());
process::exit(1);
}
}
}
fn compile_sdk_protos(sdk_dir: &Path, out_dir: &Path) {
println!(
"[info ] Compiling Cosmos-SDK .proto files to Rust into '{}'...",
out_dir.display()
);
let root = env!("CARGO_MANIFEST_DIR");
// Paths
let proto_paths = [
format!("{}/../proto/definitions/mock", root),
format!("{}/proto/cosmos/auth", sdk_dir.display()),
format!("{}/proto/cosmos/gov", sdk_dir.display()),
format!("{}/proto/cosmos/tx", sdk_dir.display()),
format!("{}/proto/cosmos/base", sdk_dir.display()),
format!("{}/proto/cosmos/staking", sdk_dir.display()),
format!("{}/proto/cosmos/upgrade", sdk_dir.display()),
];
let proto_includes_paths = [
format!("{}/../proto", root),
format!("{}/proto", sdk_dir.display()),
format!("{}/third_party/proto", sdk_dir.display()),
];
// List available proto files
let mut protos: Vec<PathBuf> = vec![];
for proto_path in &proto_paths {
println!("Looking for proto files in {:?}", proto_path);
protos.append(
&mut WalkDir::new(proto_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type().is_file()
&& e.path().extension().is_some()
&& e.path().extension().unwrap() == "proto"
})
.map(|e| e.into_path())
.collect(),
);
}
println!("Found the following protos:");
// Show which protos will be compiled
for proto in &protos {
println!("\t-> {:?}", proto);
}
println!("[info ] Compiling..");
// List available paths for dependencies
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();
let compilation = tonic_build::configure()
.build_client(true)
.build_server(false)
.format(false)
.out_dir(out_dir)
.extern_path(".tendermint", "::tendermint_proto")
.compile(&protos, &includes);
match compilation {
Ok(_) => {
println!("Successfully compiled proto files");
}
Err(e) => {
println!("Failed to compile:{:?}", e.to_string());
process::exit(1);
}
}
}
fn copy_generated_files(from_dir: &Path, to_dir: &Path) {
println!(
"[info ] Copying generated files into '{}'...",
to_dir.display()
);
// Remove old compiled files
remove_dir_all(&to_dir).unwrap_or_default();
create_dir_all(&to_dir).unwrap();
// Copy new compiled files (prost does not use folder structures)
let errors = WalkDir::new(from_dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.map(|e| {
copy(
e.path(),
format!(
"{}/{}",
to_dir.display(),
&e.file_name().to_os_string().to_str().unwrap()
),
)
})
.filter_map(|e| e.err())
.collect::<Vec<_>>();
if !errors.is_empty() {
for e in errors {
println!("[error] Error while copying compiled file: {}", e);
}
panic!("[error] Aborted.");
}
}
}