-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathlib.rs
424 lines (373 loc) · 13.3 KB
/
lib.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Crate with helper tool to build smartcontracts (e.g. triggers and executors) for Iroha 2.
//!
//! See [`Builder`] for more details.
use std::{
borrow::Cow,
env,
ffi::OsStr,
path::{Path, PathBuf},
process::Command,
};
use eyre::{bail, eyre, Context as _, Result};
use path_absolutize::Absolutize;
/// Current toolchain used to build smartcontracts
const TOOLCHAIN: &str = "+nightly-2023-06-25";
/// WASM Builder for smartcontracts (e.g. triggers and executors).
///
/// # Example
///
/// ```no_run
/// use iroha_wasm_builder::Builder;
/// use eyre::Result;
///
/// fn main() -> Result<()> {
/// let bytes = Builder::new("relative/path/to/smartcontract/")
/// .out_dir("path/to/out/dir") // Optional: Set output directory
/// .format() // Optional: Enable smartcontract formatting
/// .build()? // Run build
/// .optimize()? // Optimize WASM output
/// .into_bytes()?; // Get resulting WASM bytes
///
/// // ...
///
/// Ok(())
/// }
/// ```
#[derive(Debug)]
#[must_use]
pub struct Builder<'path, 'out_dir> {
/// Path to the smartcontract or to the directory of smartcontracts
path: &'path Path,
/// Build output directory
out_dir: Option<&'out_dir Path>,
/// Flag to enable smartcontract formatting
format: bool,
}
impl<'path, 'out_dir> Builder<'path, 'out_dir> {
/// Initialize [`Builder`] with path to smartcontract.
///
/// `relative_path` should be relative to `CARGO_MANIFEST_DIR`.
pub fn new<P>(relative_path: &'path P) -> Self
where
P: AsRef<Path> + ?Sized,
{
Self {
path: relative_path.as_ref(),
out_dir: None,
format: false,
}
}
/// Set smartcontract build output directory.
///
/// By default the output directory will be assigned either from `IROHA_WASM_BUILDER_OUT_DIR` or
/// `OUT_DIR` environment variables, in this order.
/// If neither is set, then the `target` directory will be used.
pub fn out_dir<O>(mut self, out_dir: &'out_dir O) -> Self
where
O: AsRef<Path> + ?Sized,
{
self.out_dir = Some(out_dir.as_ref());
self
}
/// Enable smartcontract formatting using `cargo fmt`.
///
/// Disabled by default.
pub fn format(mut self) -> Self {
self.format = true;
self
}
/// Apply `cargo check` to the smartcontract.
///
/// # Errors
///
/// Can fail due to multiple reasons like invalid path, failed formatting, failed build, etc.
pub fn check(self) -> Result<()> {
self.into_internal()?.check()
}
/// Build smartcontract and get resulting wasm.
///
/// # Errors
///
/// Can fail due to multiple reasons like invalid path, failed formatting,
/// failed build, etc.
///
/// Will also return error if ran on workspace and not on the concrete package.
pub fn build(self) -> Result<Output> {
self.into_internal()?.build()
}
fn into_internal(self) -> Result<internal::Builder<'out_dir>> {
let abs_path = Self::absolute_path(self.path)?;
Ok(internal::Builder {
absolute_path: abs_path
.canonicalize()
.wrap_err_with(|| format!("Failed to canonicalize path: {}", abs_path.display()))?,
out_dir: self.out_dir.map_or_else(
|| -> Result<_> { Ok(Cow::Owned(Self::default_out_dir()?)) },
|out_dir| Ok(Cow::Borrowed(out_dir)),
)?,
format: self.format,
})
}
fn default_out_dir() -> Result<PathBuf> {
env::var_os("IROHA_WASM_BUILDER_OUT_DIR")
.or_else(|| env::var_os("OUT_DIR"))
.map(PathBuf::from)
.map_or_else(
|| {
const PATH: &str = "target";
let abs_path = Self::absolute_path(PATH)?;
std::fs::DirBuilder::new()
.recursive(true)
.create(&abs_path)
.wrap_err_with(|| {
format!("Failed to create directory at path: {}", abs_path.display())
})?;
abs_path.canonicalize().wrap_err_with(|| {
format!("Failed to canonicalize path: {}", abs_path.display())
})
},
Ok,
)
}
fn absolute_path(relative_path: impl AsRef<Path>) -> Result<PathBuf> {
// TODO: replace with [std::path::absolute](https://doc.rust-lang.org/stable/std/path/fn.absolute.html)
// when it's stabilized
relative_path
.as_ref()
.absolutize()
.map(|abs_path| abs_path.to_path_buf())
.wrap_err_with(|| {
format!(
"Failed to construct absolute path for: {}",
relative_path.as_ref().display(),
)
})
}
}
mod internal {
//! Internal implementation of [`Builder`](super::Builder).
use std::borrow::Cow;
use super::*;
#[derive(Debug)]
pub struct Builder<'out_dir> {
pub absolute_path: PathBuf,
pub out_dir: Cow<'out_dir, Path>,
pub format: bool,
}
impl Builder<'_> {
pub fn check(self) -> Result<()> {
self.maybe_format()?;
self.check_smartcontract().wrap_err_with(|| {
format!(
"Failed to check the smartcontract at path: {}",
self.absolute_path.display()
)
})
}
pub fn build(self) -> Result<Output> {
self.maybe_format()?;
let absolute_path = self.absolute_path.clone();
self.build_smartcontract().wrap_err_with(|| {
format!(
"Failed to build the smartcontract at path: {}",
absolute_path.display()
)
})
}
fn maybe_format(&self) -> Result<()> {
if self.format {
self.format_smartcontract().wrap_err_with(|| {
format!(
"Failed to format the smartcontract at path: {}",
self.absolute_path.display()
)
})?;
}
Ok(())
}
fn build_options() -> impl Iterator<Item = &'static str> {
[
"--release",
"-Z",
"build-std",
"-Z",
"build-std-features=panic_immediate_abort",
"-Z",
"unstable-options",
"--target",
"wasm32-unknown-unknown",
]
.into_iter()
}
fn format_smartcontract(&self) -> Result<()> {
let command_output = cargo_command()
.current_dir(&self.absolute_path)
.arg("fmt")
.output()
.wrap_err("Failed to run `cargo fmt`")?;
check_command_output(&command_output, "cargo fmt")
}
fn get_base_command(&self, cmd: &'static str) -> std::process::Command {
let mut command = cargo_command();
command
.current_dir(&self.absolute_path)
.arg(cmd)
.args(Self::build_options());
command
}
fn check_smartcontract(&self) -> Result<()> {
let command_output = self
.get_base_command("check")
.output()
.wrap_err("Failed to run `cargo check`")?;
check_command_output(&command_output, "cargo check")
}
fn build_smartcontract(self) -> Result<Output> {
let package_name = self
.retrieve_package_name()
.wrap_err("Failed to retrieve package name")?;
let full_out_dir = self.out_dir.join("wasm32-unknown-unknown/release/");
let wasm_file = full_out_dir.join(package_name).with_extension("wasm");
let previous_hash = if wasm_file.exists() {
let hash = sha256::try_digest(wasm_file.as_path()).wrap_err_with(|| {
format!(
"Failed to compute sha256 digest of wasm file: {}",
wasm_file.display()
)
})?;
Some(hash)
} else {
None
};
let command_output = self
.get_base_command("build")
.env("CARGO_TARGET_DIR", self.out_dir.as_ref())
.output()
.wrap_err("Failed to run `cargo build`")?;
check_command_output(&command_output, "cargo build")?;
Ok(Output {
wasm_file,
previous_hash,
})
}
fn retrieve_package_name(&self) -> Result<String> {
use std::str::FromStr as _;
let manifest_output = cargo_command()
.current_dir(&self.absolute_path)
.arg("read-manifest")
.output()
.wrap_err("Failed to run `cargo read-manifest`")?;
check_command_output(&manifest_output, "cargo read-manifest")?;
let manifest = String::from_utf8(manifest_output.stdout)
.wrap_err("Failed to convert `cargo read-manifest` output to string")?;
serde_json::Value::from_str(&manifest)
.wrap_err("Failed to parse `cargo read-manifest` output")?
.get("name")
.map(serde_json::Value::to_string)
.map(|name| name.trim_matches('"').to_owned())
.ok_or_else(|| {
eyre!("Failed to retrieve package name from `cargo read-manifest` output")
})
}
}
}
/// Build output representing wasm binary.
#[derive(Debug)]
pub struct Output {
/// Path to the non-optimized `.wasm` file.
wasm_file: PathBuf,
/// Hash of the `self.wasm_file` on previous iteration if there is some.
previous_hash: Option<String>,
}
impl Output {
/// Optimize wasm output.
///
/// # Errors
///
/// Fails if internal tool fails to optimize wasm binary.
pub fn optimize(self) -> Result<Self> {
let optimized_file = PathBuf::from(format!(
"{parent}{separator}{file}_optimized.{ext}",
parent = self
.wasm_file
.parent()
.map_or_else(String::default, |p| p.display().to_string()),
separator = std::path::MAIN_SEPARATOR,
file = self
.wasm_file
.file_stem()
.map_or_else(|| "output".into(), OsStr::to_string_lossy),
ext = self
.wasm_file
.extension()
.map_or_else(|| "wasm".into(), OsStr::to_string_lossy),
));
let current_hash = sha256::try_digest(self.wasm_file.as_path()).wrap_err_with(|| {
format!(
"Failed to compute sha256 digest of wasm file: {}",
self.wasm_file.display()
)
})?;
match self.previous_hash {
Some(previous_hash) if optimized_file.exists() && current_hash == previous_hash => {
// Do nothing because original `.wasm` file wasn't changed
// so `_optimized.wasm` should stay the same
}
_ => {
let optimizer = wasm_opt::OptimizationOptions::new_optimize_for_size();
optimizer.run(self.wasm_file, optimized_file.as_path())?;
}
}
Ok(Self {
wasm_file: optimized_file,
previous_hash: Some(current_hash),
})
}
/// Consume [`Output`] and get the underling bytes.
///
/// # Errors
///
/// Fails if the output file cannot be read.
pub fn into_bytes(self) -> Result<Vec<u8>> {
use std::{fs::File, io::Read as _};
let mut wasm_file = File::open(self.wasm_file)?;
let mut wasm_data = Vec::new();
wasm_file
.read_to_end(&mut wasm_data)
.wrap_err("Failed to read data from the output wasm file")?;
Ok(wasm_data)
}
/// Get the file path of the underlying WASM
#[must_use]
pub fn wasm_file_path(&self) -> &PathBuf {
&self.wasm_file
}
}
// TODO: Remove cargo invocation (#2152)
fn cargo_command() -> Command {
let mut cargo = Command::new("cargo");
cargo
// Removing environment variable to avoid
// `error: infinite recursion detected` when running `cargo lints`
.env_remove("RUST_RECURSION_COUNT")
// Removing environment variable to avoid
// `error: `profiler_builtins` crate (required by compiler options) is not compatible with crate attribute `#![no_core]``
// when running with `-C instrument-coverage`
// TODO: Check if there are no problems with that
.env_remove("CARGO_ENCODED_RUSTFLAGS")
.args([
TOOLCHAIN,
]);
cargo
}
fn check_command_output(command_output: &std::process::Output, command_name: &str) -> Result<()> {
if !command_output.status.success() {
bail!(
"`{}` returned non zero exit code ({}). Stderr:\n{}",
command_name,
command_output.status,
String::from_utf8_lossy(&command_output.stderr)
);
}
Ok(())
}