Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
212 changes: 114 additions & 98 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ mod validate_wasm;
mod wasm_opt;
mod workspace;

#[deprecated(since = "2.0.2", note = "Use MetadataArtifacts instead")]
pub use self::metadata::MetadataArtifacts as MetadataResult;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this type, so have added this alias to be backwards compatible.


pub use self::{
args::{
BuildArtifacts,
Expand All @@ -46,7 +49,7 @@ pub use self::{
crate_metadata::CrateMetadata,
metadata::{
BuildInfo,
MetadataResult,
MetadataArtifacts,
WasmOptSettings,
},
new::new_contract_project,
Expand Down Expand Up @@ -118,7 +121,7 @@ pub struct BuildResult {
/// Path to the resulting Wasm file.
pub dest_wasm: Option<PathBuf>,
/// Result of the metadata generation.
pub metadata_result: Option<MetadataResult>,
pub metadata_result: Option<MetadataArtifacts>,
/// Path to the directory where output files are written to.
pub target_directory: PathBuf,
/// If existent the result of the optimization.
Expand Down Expand Up @@ -613,97 +616,106 @@ pub fn execute(args: ExecuteArgs) -> Result<BuildResult> {
}
};

let build = || -> Result<(Option<(OptimizationResult, BuildInfo)>, BuildSteps)> {
let mut build_steps = BuildSteps::new();
let pre_fingerprint = Fingerprint::try_from_path(&crate_metadata.original_wasm)?;
let build =
|| -> Result<(Option<OptimizationResult>, BuildInfo, PathBuf, BuildSteps)> {
let mut build_steps = BuildSteps::new();
let pre_fingerprint =
Fingerprint::try_from_path(&crate_metadata.original_wasm)?;

maybe_println!(
verbosity,
" {} {}",
format!("{build_steps}").bold(),
"Building cargo project".bright_green().bold()
);
build_steps.increment_current();
exec_cargo_for_wasm_target(
&crate_metadata,
"build",
&features,
build_mode,
network,
verbosity,
&unstable_flags,
)?;
maybe_println!(
verbosity,
" {} {}",
format!("{build_steps}").bold(),
"Building cargo project".bright_green().bold()
);
build_steps.increment_current();
exec_cargo_for_wasm_target(
&crate_metadata,
"build",
&features,
build_mode,
network,
verbosity,
&unstable_flags,
)?;

let cargo_contract_version = if let Ok(version) = Version::parse(VERSION) {
version
} else {
anyhow::bail!(
"Unable to parse version number for the currently running \
`cargo-contract` binary."
);
};

let build_info = BuildInfo {
rust_toolchain: util::rust_toolchain()?,
cargo_contract_version,
build_mode,
wasm_opt_settings: WasmOptSettings {
optimization_passes,
keep_debug_symbols,
},
};

let post_fingerprint = Fingerprint::try_from_path(&crate_metadata.original_wasm)?
let post_fingerprint = Fingerprint::try_from_path(
&crate_metadata.original_wasm,
)?
.ok_or_else(|| {
anyhow::anyhow!(
"Expected '{}' to be generated by build",
crate_metadata.original_wasm.display()
)
})?;

if pre_fingerprint == Some(post_fingerprint)
&& crate_metadata.dest_wasm.exists()
&& crate_metadata.metadata_path().exists()
&& crate_metadata.contract_bundle_path().exists()
{
tracing::info!(
"No changes in the original wasm at {}, fingerprint {:?}. \
Skipping Wasm optimization and metadata generation.",
crate_metadata.original_wasm.display(),
pre_fingerprint
);
return Ok((None, build_steps))
}

maybe_lint(&mut build_steps)?;
let dest_wasm_path = crate_metadata.dest_wasm.clone();

maybe_println!(
verbosity,
" {} {}",
format!("{build_steps}").bold(),
"Post processing wasm file".bright_green().bold()
);
build_steps.increment_current();
post_process_wasm(&crate_metadata, skip_wasm_validation, &verbosity)?;
if pre_fingerprint == Some(post_fingerprint)
&& crate_metadata.dest_wasm.exists()
{
tracing::info!(
"No changes in the original wasm at {}, fingerprint {:?}. \
Skipping Wasm optimization and metadata generation.",
crate_metadata.original_wasm.display(),
pre_fingerprint
);
return Ok((None, build_info, dest_wasm_path, build_steps))
}

maybe_println!(
verbosity,
" {} {}",
format!("{build_steps}").bold(),
"Optimizing wasm file".bright_green().bold()
);
build_steps.increment_current();
maybe_lint(&mut build_steps)?;

let handler = WasmOptHandler::new(optimization_passes, keep_debug_symbols)?;
let optimization_result = handler.optimize(
&crate_metadata.dest_wasm,
&crate_metadata.contract_artifact_name,
)?;
maybe_println!(
verbosity,
" {} {}",
format!("{build_steps}").bold(),
"Post processing wasm file".bright_green().bold()
);
build_steps.increment_current();
post_process_wasm(&crate_metadata, skip_wasm_validation, &verbosity)?;

let cargo_contract_version = if let Ok(version) = Version::parse(VERSION) {
version
} else {
anyhow::bail!(
"Unable to parse version number for the currently running \
`cargo-contract` binary."
maybe_println!(
verbosity,
" {} {}",
format!("{build_steps}").bold(),
"Optimizing wasm file".bright_green().bold()
);
};
build_steps.increment_current();

let build_info = BuildInfo {
rust_toolchain: crate::util::rust_toolchain()?,
cargo_contract_version,
build_mode,
wasm_opt_settings: WasmOptSettings {
optimization_passes,
keep_debug_symbols,
},
};
let handler = WasmOptHandler::new(optimization_passes, keep_debug_symbols)?;
let optimization_result = handler.optimize(
&crate_metadata.dest_wasm,
&crate_metadata.contract_artifact_name,
)?;

Ok((Some((optimization_result, build_info)), build_steps))
};
Ok((
Some(optimization_result),
build_info,
dest_wasm_path,
build_steps,
))
};

let (opt_result, metadata_result) = match build_artifact {
let (opt_result, metadata_result, dest_wasm) = match build_artifact {
BuildArtifacts::CheckOnly => {
let mut build_steps = BuildSteps::new();
maybe_lint(&mut build_steps)?;
Expand All @@ -723,34 +735,38 @@ pub fn execute(args: ExecuteArgs) -> Result<BuildResult> {
verbosity,
&unstable_flags,
)?;
(None, None)
(None, None, None)
}
BuildArtifacts::CodeOnly => {
let (build_result, _) = build()?;
let opt_result = build_result.map(|(opt_result, _)| opt_result);
(opt_result, None)
let (opt_result, _, dest_wasm, _) = build()?;
(opt_result, None, Some(dest_wasm))
}
BuildArtifacts::All => {
let (build_result, build_steps) = build()?;

match build_result {
Some((opt_result, build_info)) => {
let metadata_result = metadata::execute(
&crate_metadata,
opt_result.dest_wasm.as_path(),
network,
verbosity,
build_steps,
&unstable_flags,
build_info,
)?;
(Some(opt_result), Some(metadata_result))
}
None => (None, None),
let (opt_result, build_info, dest_wasm, build_steps) = build()?;

let metadata_result = MetadataArtifacts {
dest_metadata: crate_metadata.metadata_path(),
dest_bundle: crate_metadata.contract_bundle_path(),
};
// skip metadata generation if contract unchanged and all metadata artifacts exist.
if opt_result.is_some()
|| !metadata_result.dest_metadata.exists()
|| !metadata_result.dest_bundle.exists()
{
metadata::execute(
&crate_metadata,
dest_wasm.as_path(),
&metadata_result,
network,
verbosity,
build_steps,
&unstable_flags,
build_info,
)?;
}
(opt_result, Some(metadata_result), Some(dest_wasm))
}
};
let dest_wasm = opt_result.as_ref().map(|r| r.dest_wasm.clone());

Ok(BuildResult {
dest_wasm,
Expand Down Expand Up @@ -909,7 +925,7 @@ mod unit_tests {

let build_result = BuildResult {
dest_wasm: Some(PathBuf::from("/path/to/contract.wasm")),
metadata_result: Some(MetadataResult {
metadata_result: Some(MetadataArtifacts {
dest_metadata: PathBuf::from("/path/to/contract.json"),
dest_bundle: PathBuf::from("/path/to/contract.contract"),
}),
Expand Down
19 changes: 7 additions & 12 deletions crates/build/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ use std::{
};
use url::Url;

/// Metadata generation result.
/// Artifacts resulting from metadata generation.
#[derive(serde::Serialize)]
pub struct MetadataResult {
pub struct MetadataArtifacts {
/// Path to the resulting metadata file.
pub dest_metadata: PathBuf,
/// Path to the bundled file.
Expand Down Expand Up @@ -110,15 +110,13 @@ pub struct WasmOptSettings {
pub(crate) fn execute(
crate_metadata: &CrateMetadata,
final_contract_wasm: &Path,
metadata_artifacts: &MetadataArtifacts,
network: Network,
verbosity: Verbosity,
mut build_steps: BuildSteps,
unstable_options: &UnstableFlags,
build_info: BuildInfo,
) -> Result<MetadataResult> {
let out_path_metadata = crate_metadata.metadata_path();
let out_path_bundle = crate_metadata.contract_bundle_path();

) -> Result<()> {
// build the extended contract project metadata
let ExtendedMetadataResult {
source,
Expand Down Expand Up @@ -159,7 +157,7 @@ pub(crate) fn execute(
let mut metadata = metadata.clone();
metadata.remove_source_wasm_attribute();
let contents = serde_json::to_string_pretty(&metadata)?;
fs::write(&out_path_metadata, contents)?;
fs::write(&metadata_artifacts.dest_metadata, contents)?;
build_steps.increment_current();
}

Expand All @@ -170,7 +168,7 @@ pub(crate) fn execute(
"Generating bundle".bright_green().bold()
);
let contents = serde_json::to_string(&metadata)?;
fs::write(&out_path_bundle, contents)?;
fs::write(&metadata_artifacts.dest_bundle, contents)?;

Ok(())
};
Expand All @@ -191,10 +189,7 @@ pub(crate) fn execute(
.using_temp(generate_metadata)?;
}

Ok(MetadataResult {
dest_metadata: out_path_metadata,
dest_bundle: out_path_bundle,
})
Ok(())
}

/// Generate the extended contract project metadata
Expand Down
Loading