Skip to content
Merged
36 changes: 24 additions & 12 deletions crates/build/src/crate_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,46 @@ 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 and lib name.
// Normalize the package name.
let package_name = root_package.name.replace('-', "_");
let lib_name = &root_package

if let Some(lib_name) = &root_package
.targets
.iter()
.find(|target| target.kind.iter().any(|t| t == "cdylib"))
.expect("lib name not found")
.name
.replace('-', "_");
.find(|target| target.kind.iter().any(|t| t == "lib"))
{
if lib_name.name != root_package.name {
// warn user if they still specify a lib name different from the
// package name
use colored::Colorize;
eprintln!(
"{} the `name` field in the `[lib]` section of the `Cargo.toml`, \
is no longer used for the name of generated contract artifacts. \
The package name is used instead. Remove the `[lib] name` to \
stop this warning.",
"warning:".yellow().bold(),
);
}
}

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);
target_directory = target_directory.join(package_name.clone());
}

// {target_dir}/wasm32-unknown-unknown/release/{lib_name}.wasm
// {target_dir}/wasm32-unknown-unknown/release/{package_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.push(package_name.clone());
original_wasm.set_extension("wasm");

// {target_dir}/{lib_name}.wasm
// {target_dir}/{package_name}.wasm
let mut dest_wasm = target_directory.clone();
dest_wasm.push(lib_name.clone());
dest_wasm.push(package_name.clone());
dest_wasm.set_extension("wasm");

let ink_version = metadata
Expand Down Expand Up @@ -121,7 +133,7 @@ impl CrateMetadata {
manifest_path: manifest_path.clone(),
cargo_meta: metadata,
root_package,
contract_artifact_name: lib_name.to_string(),
contract_artifact_name: package_name,
original_wasm: original_wasm.into(),
dest_wasm: dest_wasm.into(),
ink_version,
Expand Down
2 changes: 1 addition & 1 deletion crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ fn exec_cargo_for_wasm_target(
Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)?
.with_root_package_manifest(|manifest| {
manifest
.with_removed_crate_type("rlib")?
.with_crate_types(["cdylib"])?
.with_profile_release_defaults(Profile::default_contract_release())?
.with_workspace()?;
Ok(())
Expand Down
39 changes: 0 additions & 39 deletions crates/build/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use serde_json::{
Value,
};
use std::{
ffi::OsStr,
fmt::Write,
fs,
path::{
Expand Down Expand Up @@ -66,7 +65,6 @@ build_tests!(
check_must_not_output_contract_artifacts_in_project_dir,
optimization_passes_from_cli_must_take_precedence_over_profile,
optimization_passes_from_profile_must_be_used,
contract_lib_name_different_from_package_name_must_build,
building_template_in_debug_mode_must_work,
building_template_in_release_mode_must_work,
keep_debug_symbols_in_debug_mode,
Expand Down Expand Up @@ -223,43 +221,6 @@ fn optimization_passes_from_profile_must_be_used(
Ok(())
}

fn contract_lib_name_different_from_package_name_must_build(
manifest_path: &ManifestPath,
) -> Result<()> {
// given
let mut manifest = TestContractManifest::new(manifest_path.clone())?;
manifest.set_lib_name("some_lib_name")?;
manifest.set_package_name("some_package_name")?;
manifest.write()?;

// when
let args = ExecuteArgs {
manifest_path: manifest_path.clone(),
verbosity: Verbosity::Default,
features: Default::default(),
build_mode: Default::default(),
network: Default::default(),
build_artifact: BuildArtifacts::All,
unstable_flags: Default::default(),
optimization_passes: Some(OptimizationPasses::Zero),
keep_debug_symbols: false,
lint: false,
output_type: OutputType::HumanReadable,
skip_wasm_validation: false,
};
let res = crate::execute(args).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(())
}

fn building_template_in_debug_mode_must_work(manifest_path: &ManifestPath) -> Result<()> {
// given
let args = ExecuteArgs {
Expand Down
22 changes: 20 additions & 2 deletions crates/build/src/workspace/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,33 @@ impl Manifest {
.toml
.get_mut("lib")
.ok_or_else(|| anyhow::anyhow!("lib section not found"))?;

let crate_types = lib
.get_mut("crate-type")
.ok_or_else(|| anyhow::anyhow!("crate-type section not found"))?;
.as_table_mut()
.ok_or_else(|| anyhow::anyhow!("lib section should be a table"))?
.entry("crate-type")
.or_insert(value::Value::Array(Default::default()));

crate_types
.as_array_mut()
.ok_or_else(|| anyhow::anyhow!("crate-types should be an Array"))
}

/// Set the contents of the `[lib] crate-types = []` section.
///
/// Overwrites any existing crate types.
pub fn with_crate_types<'a, I>(&mut self, new_crate_types: I) -> Result<&mut Self>
where
I: IntoIterator<Item = &'a str>,
{
let existing_crate_types = self.get_crate_types_mut()?;
existing_crate_types.clear();
for crate_type in new_crate_types.into_iter() {
existing_crate_types.push(crate_type.into())
}
Ok(self)
}

/// Add a value to the `[lib] crate-types = []` section.
///
/// If the value already exists, does nothing.
Expand Down
2 changes: 1 addition & 1 deletion crates/build/templates/generate-metadata/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ extern "Rust" {
fn main() -> Result<(), std::io::Error> {
let metadata = unsafe { __ink_generate_metadata() };
let contents = serde_json::to_string_pretty(&metadata)?;
print!("{}", contents);
print!("{contents}");
Ok(())
}
5 changes: 0 additions & 5 deletions crates/build/templates/new/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

[lib]
name = "{{name}}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this goes back to at least 2019 🤷

path = "lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
]

[features]
default = ["std"]
Expand Down