Skip to content

Commit

Permalink
Patract's modification with Debug feature
Browse files Browse the repository at this point in the history
* feat(build/debug): Add debug flag for embedding debuginfo

* retain Custom/Name Section in wasm for debug mode

* when in debug, wasm-opt default use `OptimizationPasses::Zero`

* feat(doc/installation): add installation guide to README
  • Loading branch information
atenjin committed May 26, 2021
1 parent baaaead commit 35bb268
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub struct BuildCommand {
/// The CLI argument always takes precedence over the profile value.
#[structopt(long = "optimization-passes")]
optimization_passes: Option<OptimizationPasses>,
/// Enable debug info in the wasm bundle, will contain the Custom Section and Name Section.
#[structopt(short = "d", long = "debug")]
debug: bool,
}

impl BuildCommand {
Expand All @@ -106,7 +109,13 @@ impl BuildCommand {
match manifest.get_profile_optimization_passes() {
// if no setting is found, neither on the cli nor in the profile,
// then we use the default
None => OptimizationPasses::default(),
None => {
if self.debug {
OptimizationPasses::Zero
} else {
OptimizationPasses::default()
}
},
Some(opt_passes) => opt_passes,
}
}
Expand All @@ -118,6 +127,7 @@ impl BuildCommand {
self.build_artifact,
unstable_flags,
optimization_passes,
self.debug,
)
}
}
Expand Down Expand Up @@ -146,6 +156,7 @@ impl CheckCommand {
BuildArtifacts::CheckOnly,
unstable_flags,
OptimizationPasses::Zero,
false,
)
}
}
Expand All @@ -171,15 +182,23 @@ fn exec_cargo_for_wasm_target(
command: &str,
verbosity: Verbosity,
unstable_flags: &UnstableFlags,
debug: bool,
) -> Result<()> {
util::assert_channel()?;

// set linker args via RUSTFLAGS.
// Currently will override user defined RUSTFLAGS from .cargo/config. See https://github.com/paritytech/cargo-contract/issues/98.
if debug {
std::env::set_var(
"RUSTFLAGS",
"-C link-arg=-z -C link-arg=stack-size=65536 -C link-arg=--import-memory -C opt-level=1 -C debuginfo=2",
);
} else {
std::env::set_var(
"RUSTFLAGS",
"-C link-arg=-z -C link-arg=stack-size=65536 -C link-arg=--import-memory",
);
}

let cargo_build = |manifest_path: &ManifestPath| {
let target_dir = &crate_metadata.target_directory;
Expand Down Expand Up @@ -269,8 +288,14 @@ fn strip_custom_sections(module: &mut Module) {
});
}

fn strip_reloc_section(module: &mut Module) {
module
.sections_mut()
.retain(|section| !matches!(section, Section::Reloc(_)));
}

/// Performs required post-processing steps on the wasm artifact.
fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> {
fn post_process_wasm(crate_metadata: &CrateMetadata, debug: bool) -> Result<()> {
// Deserialize wasm module from a file.
let mut module =
parity_wasm::deserialize_file(&crate_metadata.original_wasm).context(format!(
Expand All @@ -286,7 +311,11 @@ fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> {
anyhow::bail!("Optimizer failed");
}
ensure_maximum_memory_pages(&mut module, MAX_MEMORY_PAGES)?;
if debug {
strip_reloc_section(&mut module);
} else {
strip_custom_sections(&mut module);
}

validate_wasm::validate_import_section(&module)?;

Expand All @@ -306,6 +335,7 @@ fn post_process_wasm(crate_metadata: &CrateMetadata) -> Result<()> {
fn optimize_wasm(
crate_metadata: &CrateMetadata,
optimization_passes: OptimizationPasses,
debug: bool,
) -> Result<OptimizationResult> {
let mut dest_optimized = crate_metadata.dest_wasm.clone();
dest_optimized.set_file_name(format!(
Expand All @@ -316,6 +346,7 @@ fn optimize_wasm(
crate_metadata.dest_wasm.as_os_str(),
&dest_optimized.as_os_str(),
optimization_passes,
debug,
)?;

if !dest_optimized.exists() {
Expand Down Expand Up @@ -348,6 +379,7 @@ fn do_optimization(
dest_wasm: &OsStr,
dest_optimized: &OsStr,
optimization_level: OptimizationPasses,
debug: bool,
) -> Result<()> {
// check `wasm-opt` is installed
let which = which::which("wasm-opt");
Expand Down Expand Up @@ -388,6 +420,7 @@ fn do_optimization(
// the memory is initialized to zeroes, otherwise it won't run the
// memory-packing pre-pass.
.arg("--zero-filled-memory")
.args(if debug { &["--debuginfo"][..] } else { &[][..] })
.output()
.map_err(|err| {
anyhow::anyhow!(
Expand Down Expand Up @@ -529,6 +562,7 @@ pub(crate) fn execute(
build_artifact: BuildArtifacts,
unstable_flags: UnstableFlags,
optimization_passes: OptimizationPasses,
debug: bool,
) -> Result<BuildResult> {
let crate_metadata = CrateMetadata::collect(&manifest_path)?;

Expand All @@ -541,30 +575,30 @@ pub(crate) fn execute(
format!("[1/{}]", build_artifact.steps()).bold(),
"Building cargo project".bright_green().bold()
);
exec_cargo_for_wasm_target(&crate_metadata, "build", verbosity, &unstable_flags)?;
exec_cargo_for_wasm_target(&crate_metadata, "build", verbosity, &unstable_flags, debug)?;

maybe_println!(
verbosity,
" {} {}",
format!("[2/{}]", build_artifact.steps()).bold(),
"Post processing wasm file".bright_green().bold()
);
post_process_wasm(&crate_metadata)?;
post_process_wasm(&crate_metadata, debug)?;

maybe_println!(
verbosity,
" {} {}",
format!("[3/{}]", build_artifact.steps()).bold(),
"Optimizing wasm file".bright_green().bold()
);
let optimization_result = optimize_wasm(&crate_metadata, optimization_passes)?;
let optimization_result = optimize_wasm(&crate_metadata, optimization_passes, debug)?;

Ok(optimization_result)
};

let (opt_result, metadata_result) = match build_artifact {
BuildArtifacts::CheckOnly => {
exec_cargo_for_wasm_target(&crate_metadata, "check", verbosity, &unstable_flags)?;
exec_cargo_for_wasm_target(&crate_metadata, "check", verbosity, &unstable_flags, debug)?;
(None, None)
}
BuildArtifacts::CodeOnly => {
Expand Down

0 comments on commit 35bb268

Please sign in to comment.