From 3709909899580a7e6fc6c7e25ee70bb14846ffd6 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Fri, 21 May 2021 21:58:40 +0200 Subject: [PATCH 1/4] Always use for Wasm output file name --- src/cmd/build.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/crate_metadata.rs | 13 ++++++++++--- src/workspace/manifest.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 25bfc025..4d96e7ee 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -606,6 +606,7 @@ mod tests_ci_only { #[cfg(unix)] use std::os::unix::fs::PermissionsExt; use std::{ + ffi::OsStr, io::Write, path::{Path, PathBuf}, }; @@ -902,4 +903,42 @@ mod tests_ci_only { Ok(()) }) } + + #[test] + fn contract_lib_name_different_from_package_name_must_build() { + with_new_contract_project(|manifest_path| { + // given + let mut manifest = + Manifest::new(manifest_path.clone()).expect("manifest creation failed"); + let _ = manifest + .set_lib_name("some_lib_name") + .expect("setting lib name failed"); + let _ = manifest + .set_package_name("some_package_name") + .expect("setting pacakge name failed"); + manifest + .write(&manifest_path) + .expect("writing manifest failed"); + + // when + let cmd = BuildCommand { + manifest_path: Some(manifest_path.into()), + build_artifact: BuildArtifacts::All, + verbosity: VerbosityFlags::default(), + unstable_options: UnstableOptions::default(), + optimization_passes: None, + }; + let res = cmd.exec().expect("build failed"); + + // then + assert_eq!( + res.dest_wasm + .expect("`dest_wasm` does not exist") + .file_name(), + Some(OsStr::new("some_lib_name.wasm")) + ); + + Ok(()) + }) + } } diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index 26bf7f5f..e6367c6f 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -45,8 +45,15 @@ impl CrateMetadata { let (metadata, root_package) = get_cargo_metadata(manifest_path)?; let mut target_directory = metadata.target_directory.as_path().join("ink"); - // Normalize the package name. + // Normalize the package and lib name. let package_name = root_package.name.replace("-", "_"); + let lib_name = &root_package + .targets + .iter() + .find(|target| target.kind.iter().any(|t| t == "cdylib")) + .expect("lib name not found") + .name + .replace("-", "_"); let absolute_manifest_path = manifest_path.absolute_directory()?; let absolute_workspace_root = metadata.workspace_root.canonicalize()?; @@ -58,12 +65,12 @@ impl CrateMetadata { let mut original_wasm = target_directory.clone(); original_wasm.push("wasm32-unknown-unknown"); original_wasm.push("release"); - original_wasm.push(package_name.clone()); + original_wasm.push(lib_name.clone()); original_wasm.set_extension("wasm"); // {target_dir}/{package_name}.wasm let mut dest_wasm = target_directory.clone(); - dest_wasm.push(package_name.clone()); + dest_wasm.push(lib_name.clone()); dest_wasm.set_extension("wasm"); let ink_version = metadata diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index fc2a52ed..7db38880 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -224,6 +224,32 @@ impl Manifest { .insert("version".into(), value::Value::String(version.into()))) } + /// Set the `lib` name to `name`. + #[cfg(feature = "test-ci-only")] + #[cfg(test)] + pub fn set_lib_name(&mut self, name: &str) -> Result> { + Ok(self + .toml + .get_mut("lib") + .ok_or_else(|| anyhow::anyhow!("[lib] section not found"))? + .as_table_mut() + .ok_or_else(|| anyhow::anyhow!("[lib] should be a table"))? + .insert("name".into(), value::Value::String(name.into()))) + } + + /// Set the `package` name to `name`. + #[cfg(feature = "test-ci-only")] + #[cfg(test)] + pub fn set_package_name(&mut self, name: &str) -> Result> { + Ok(self + .toml + .get_mut("package") + .ok_or_else(|| anyhow::anyhow!("[package] section not found"))? + .as_table_mut() + .ok_or_else(|| anyhow::anyhow!("[package] should be a table"))? + .insert("name".into(), value::Value::String(name.into()))) + } + /// Set `[profile.release]` lto flag pub fn with_profile_release_lto(&mut self, enabled: bool) -> Result<&mut Self> { let lto = self -- GitLab From 32f72f80642454b4f635ffb2500d612c0c24fda6 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 25 May 2021 06:52:41 +0200 Subject: [PATCH 2/4] Use `contract_artifact_name` instead of `package_name` --- src/cmd/build.rs | 2 +- src/cmd/metadata.rs | 4 ++-- src/crate_metadata.rs | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 4d96e7ee..82af7d0d 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -308,7 +308,7 @@ fn optimize_wasm( optimization_passes: OptimizationPasses, ) -> Result { let mut dest_optimized = crate_metadata.dest_wasm.clone(); - dest_optimized.set_file_name(format!("{}-opt.wasm", crate_metadata.package_name)); + dest_optimized.set_file_name(format!("{}-opt.wasm", crate_metadata.contract_artifact_name)); let _ = do_optimization( crate_metadata.dest_wasm.as_os_str(), &dest_optimized.as_os_str(), diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 94cb4d10..5808019a 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -67,7 +67,7 @@ pub(crate) fn execute( let target_directory = crate_metadata.target_directory.clone(); let out_path_metadata = target_directory.join(METADATA_FILE); - let fname_bundle = format!("{}.contract", crate_metadata.package_name); + let fname_bundle = format!("{}.contract", crate_metadata.contract_artifact_name); let out_path_bundle = target_directory.join(fname_bundle); // build the extended contract project metadata @@ -401,7 +401,7 @@ mod tests { assert_eq!(expected_wasm, wasm.as_str().unwrap()); assert_eq!(expected_language, language.as_str().unwrap()); assert_eq!(expected_compiler, compiler.as_str().unwrap()); - assert_eq!(crate_metadata.package_name, name.as_str().unwrap()); + assert_eq!(crate_metadata.contract_artifact_name, name.as_str().unwrap()); assert_eq!( crate_metadata.root_package.version.to_string(), version.as_str().unwrap() diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index e6367c6f..562c10da 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -28,7 +28,7 @@ use url::Url; pub struct CrateMetadata { pub manifest_path: ManifestPath, pub cargo_meta: cargo_metadata::Metadata, - pub package_name: String, + pub contract_artifact_name: String, pub root_package: Package, pub original_wasm: PathBuf, pub dest_wasm: PathBuf, @@ -58,17 +58,19 @@ impl CrateMetadata { let absolute_manifest_path = manifest_path.absolute_directory()?; let absolute_workspace_root = metadata.workspace_root.canonicalize()?; if absolute_manifest_path != absolute_workspace_root { + // If the contract is a package in a workspace, we use the package name + // as the name of the sub-folder where we put the `.contract` bundle. target_directory = target_directory.join(package_name.clone()); } - // {target_dir}/wasm32-unknown-unknown/release/{package_name}.wasm + // {target_dir}/wasm32-unknown-unknown/release/{lib_name}.wasm let mut original_wasm = target_directory.clone(); original_wasm.push("wasm32-unknown-unknown"); original_wasm.push("release"); original_wasm.push(lib_name.clone()); original_wasm.set_extension("wasm"); - // {target_dir}/{package_name}.wasm + // {target_dir}/{lib_name}.wasm let mut dest_wasm = target_directory.clone(); dest_wasm.push(lib_name.clone()); dest_wasm.set_extension("wasm"); @@ -98,7 +100,7 @@ impl CrateMetadata { manifest_path: manifest_path.clone(), cargo_meta: metadata, root_package, - package_name, + contract_artifact_name: lib_name.to_string(), original_wasm: original_wasm.into(), dest_wasm: dest_wasm.into(), ink_version, -- GitLab From cb080a080238de05c9078711bd1dfc1b2a66b883 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 25 May 2021 07:52:08 +0200 Subject: [PATCH 3/4] Apply `cargo fmt` --- src/cmd/build.rs | 5 ++++- src/cmd/metadata.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 82af7d0d..cabdb1bd 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -308,7 +308,10 @@ fn optimize_wasm( optimization_passes: OptimizationPasses, ) -> Result { let mut dest_optimized = crate_metadata.dest_wasm.clone(); - dest_optimized.set_file_name(format!("{}-opt.wasm", crate_metadata.contract_artifact_name)); + dest_optimized.set_file_name(format!( + "{}-opt.wasm", + crate_metadata.contract_artifact_name + )); let _ = do_optimization( crate_metadata.dest_wasm.as_os_str(), &dest_optimized.as_os_str(), diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 5808019a..4fca166d 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -401,7 +401,10 @@ mod tests { assert_eq!(expected_wasm, wasm.as_str().unwrap()); assert_eq!(expected_language, language.as_str().unwrap()); assert_eq!(expected_compiler, compiler.as_str().unwrap()); - assert_eq!(crate_metadata.contract_artifact_name, name.as_str().unwrap()); + assert_eq!( + crate_metadata.contract_artifact_name, + name.as_str().unwrap() + ); assert_eq!( crate_metadata.root_package.version.to_string(), version.as_str().unwrap() -- GitLab From 27e48de5a70b2c0b207cd639f224f9ede5974e1e Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 25 May 2021 08:04:51 +0200 Subject: [PATCH 4/4] Satisfy clippy --- src/crate_metadata.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index 562c10da..9b47b7d1 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -60,7 +60,7 @@ impl CrateMetadata { if absolute_manifest_path != absolute_workspace_root { // If the contract is a package in a workspace, we use the package name // as the name of the sub-folder where we put the `.contract` bundle. - target_directory = target_directory.join(package_name.clone()); + target_directory = target_directory.join(package_name); } // {target_dir}/wasm32-unknown-unknown/release/{lib_name}.wasm -- GitLab