Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] #3237: Expose cargo's output in wasm_builder_cli to have build progress information #4007

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/wasm_builder_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ iroha_wasm_builder.workspace = true

clap = { workspace = true, features = ["derive"] }
color-eyre.workspace = true
spinoff = { workspace = true, features = ["binary", "dots12"] }
spinoff = { workspace = true, features = ["binary"] }
owo-colors = { workspace = true, features = ["supports-colors"] }
21 changes: 5 additions & 16 deletions tools/wasm_builder_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() -> color_eyre::Result<()> {
Cli::Check {
common: CommonArgs { path },
} => {
let builder = Builder::new(&path);
let builder = Builder::new(&path).show_output();
builder.check()?;
}
Cli::Build {
Expand All @@ -52,26 +52,15 @@ fn main() -> color_eyre::Result<()> {
optimize,
outfile,
} => {
let builder = Builder::new(&path);
let builder = Builder::new(&path).show_output();
let builder = if format { builder.format() } else { builder };

let output = {
let mut sp = spinoff::Spinner::new_with_stream(
spinoff::spinners::Dots12,
"Building the smartcontract",
None,
spinoff::Streams::Stderr,
);
// not showing the spinner here, cargo does a progress bar for us

match builder.build() {
Ok(output) => {
sp.success("Smartcontract is built");
output
}
err => {
sp.fail("Building failed");
err?
}
Ok(output) => output,
err => err?,
}
};

Expand Down
75 changes: 51 additions & 24 deletions wasm_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct Builder<'path, 'out_dir> {
out_dir: Option<&'out_dir Path>,
/// Flag to enable smartcontract formatting
format: bool,
/// Flag controlling whether to show output of the build process
show_output: bool,
}

impl<'path, 'out_dir> Builder<'path, 'out_dir> {
Expand All @@ -60,6 +62,7 @@ impl<'path, 'out_dir> Builder<'path, 'out_dir> {
path: relative_path.as_ref(),
out_dir: None,
format: false,
show_output: false,
}
}

Expand All @@ -84,6 +87,14 @@ impl<'path, 'out_dir> Builder<'path, 'out_dir> {
self
}

/// Enable showing output of the build process.
///
/// Disabled by default.
pub fn show_output(mut self) -> Self {
self.show_output = true;
self
}

/// Apply `cargo check` to the smartcontract.
///
/// # Errors
Expand Down Expand Up @@ -116,6 +127,7 @@ impl<'path, 'out_dir> Builder<'path, 'out_dir> {
|out_dir| Ok(Cow::Borrowed(out_dir)),
)?,
format: self.format,
show_output: self.show_output,
})
}

Expand Down Expand Up @@ -169,6 +181,7 @@ mod internal {
pub absolute_path: PathBuf,
pub out_dir: Cow<'out_dir, Path>,
pub format: bool,
pub show_output: bool,
}

impl Builder<'_> {
Expand Down Expand Up @@ -223,13 +236,11 @@ mod internal {
}

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")
check_command(
self.show_output,
cargo_command().current_dir(&self.absolute_path).arg("fmt"),
"cargo fmt",
)
}

fn get_base_command(&self, cmd: &'static str) -> std::process::Command {
Expand All @@ -243,12 +254,9 @@ mod internal {
}

fn check_smartcontract(&self) -> Result<()> {
let command_output = self
.get_base_command("check")
.output()
.wrap_err("Failed to run `cargo check`")?;
let command = &mut self.get_base_command("check");

check_command_output(&command_output, "cargo check")
check_command(self.show_output, command, "cargo check")
}

fn build_smartcontract(self) -> Result<Output> {
Expand All @@ -271,13 +279,12 @@ mod internal {
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")?;
check_command(
self.show_output,
self.get_base_command("build")
.env("CARGO_TARGET_DIR", self.out_dir.as_ref()),
"cargo build",
)?;

Ok(Output {
wasm_file,
Expand Down Expand Up @@ -410,15 +417,35 @@ fn cargo_command() -> Command {
cargo
}

fn check_command_output(command_output: &std::process::Output, command_name: &str) -> Result<()> {
if !command_output.status.success() {
fn check_command_output(output: &std::process::Output, command_name: &str) -> Result<()> {
if output.status.success() {
Ok(())
} else {
bail!(
"`{}` returned non zero exit code ({}). Stderr:\n{}",
command_name,
command_output.status,
String::from_utf8_lossy(&command_output.stderr)
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
}

Ok(())
fn check_command(show_output: bool, command: &mut Command, command_name: &str) -> Result<()> {
if show_output {
let status = command
.status()
.wrap_err(format!("Failed to run `{command_name}`"))?;
if status.success() {
Ok(())
} else {
bail!(
"`{command_name}` returned non zero exit code ({status}). See messages above for the probable error",
);
}
} else {
let output = command
.output()
.wrap_err(format!("Failed to run `{command_name}`"))?;
check_command_output(&output, command_name)
}
}
Loading