Skip to content

Commit

Permalink
Merge pull request #947 from multiversx/meta4
Browse files Browse the repository at this point in the history
sc-meta emir MIR flag, print build command
  • Loading branch information
andrei-marinica authored Jan 25, 2023
2 parents d84475b + 3dcc72f commit cb9fb3f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
5 changes: 5 additions & 0 deletions framework/meta/src/cli_args/cli_args_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub struct BuildArgs {
#[arg(long = "wat", verbatim_doc_comment)]
pub wat: bool,

/// Also emit MIR files when building.
#[arg(long = "mir", verbatim_doc_comment)]
pub emit_mir: bool,

#[arg(
long = "no-imports",
help = "Skips extracting the EI imports after building the contracts.",
Expand Down Expand Up @@ -81,6 +85,7 @@ impl Default for BuildArgs {
wasm_name_suffix: None,
wasm_opt: true,
wat: false,
emit_mir: false,
extract_imports: true,
target_dir: None,
twiggy_top: false,
Expand Down
1 change: 1 addition & 0 deletions framework/meta/src/output_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod output_contract_model;
mod output_contract_wasm_build;
mod output_contract_wasm_clean;
mod output_contract_wasm_crate_gen;
mod print_util;

pub use multi_contract_serde::*;
pub use output_contract_builder::*;
Expand Down
28 changes: 25 additions & 3 deletions framework/meta/src/output_contract/output_contract_wasm_build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{fs, process::Command};

use super::OutputContract;
use crate::{cli_args::BuildArgs, meta_wasm_tools};
use crate::{
cli_args::BuildArgs, meta_wasm_tools, output_contract::print_util::print_build_command,
};

impl OutputContract {
pub fn build_contract(&self, build_args: &BuildArgs, output_path: &str) {
Expand All @@ -12,9 +14,13 @@ impl OutputContract {
if let Some(target_dir) = &build_args.target_dir {
command.args(["--target-dir", target_dir]);
}
if !build_args.wasm_symbols {
command.env("RUSTFLAGS", "-C link-arg=-s");
let rustflags = compose_rustflags(build_args);
if !rustflags.is_empty() {
command.env("RUSTFLAGS", rustflags);
}

print_build_command(self.wasm_output_name(build_args), &command);

let exit_status = command
.spawn()
.expect("failed to spawn contract build process")
Expand All @@ -25,7 +31,23 @@ impl OutputContract {

self.finalize_build(build_args, output_path);
}
}

fn compose_rustflags(build_args: &BuildArgs) -> String {
let mut rustflags = String::new();
if !build_args.wasm_symbols {
rustflags.push_str("-C link-arg=-s");
}
if build_args.emit_mir {
if !rustflags.is_empty() {
rustflags.push(' ');
}
rustflags.push_str("--emit=mir");
}
rustflags
}

impl OutputContract {
fn finalize_build(&self, build_args: &BuildArgs, output_path: &str) {
self.copy_contracts_to_output(build_args, output_path);
self.run_wasm_opt(build_args, output_path);
Expand Down
34 changes: 34 additions & 0 deletions framework/meta/src/output_contract/print_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::process::Command;

use colored::Colorize;

pub fn format_command(command: &Command) -> String {
let mut result = String::new();
for (key, opt_value) in command.get_envs() {
if let Some(value) = opt_value {
result +=
format!("{}=\"{}\" ", key.to_string_lossy(), value.to_string_lossy()).as_str();
}
}
result.push_str(command.get_program().to_string_lossy().as_ref());

for arg in command.get_args() {
result.push(' ');
result.push_str(arg.to_string_lossy().as_ref());
}

result
}

pub fn print_build_command(contract_name: String, command: &Command) {
let path = command
.get_current_dir()
.expect("missing command dir")
.canonicalize()
.expect("command dir canonicalization failed");
println!(
"{}\n{}",
format!("Building {} in {} ...", contract_name, path.display()).green(),
format_command(command).green(),
);
}

0 comments on commit cb9fb3f

Please sign in to comment.