Skip to content

Commit

Permalink
Fix linker error when building complex contracts (#199)
Browse files Browse the repository at this point in the history
* Build sub-contracts into sub-folders of `target/ink/`

* Reduce code duplication with util function

* Make `absolute_directory` method on `ManifestPath`

* Revert unnecessary changes

* Ensure workspace root is also the canonical path
  • Loading branch information
cmichi authored Feb 24, 2021
1 parent 41ef37b commit b4d61f6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
13 changes: 2 additions & 11 deletions src/cmd/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ use contract_metadata::{
SourceLanguage, SourceWasm, User,
};
use semver::Version;
use std::{
fs,
path::{Path, PathBuf},
};
use std::{fs, path::PathBuf};
use url::Url;

const METADATA_FILE: &str = "metadata.json";
Expand Down Expand Up @@ -123,12 +120,6 @@ impl GenerateMetadataCommand {
if self.unstable_options.original_manifest {
generate_metadata(&self.crate_metadata.manifest_path)?;
} else {
let manifest_dir = match self.crate_metadata.manifest_path.directory() {
Some(dir) => dir,
None => Path::new("./"),
};
let absolute_package_path = manifest_dir.canonicalize()?;

Workspace::new(
&self.crate_metadata.cargo_meta,
&self.crate_metadata.root_package.id,
Expand All @@ -139,7 +130,7 @@ impl GenerateMetadataCommand {
.with_profile_release_lto(false)?;
Ok(())
})?
.with_metadata_gen_package(absolute_package_path)?
.with_metadata_gen_package(self.crate_metadata.manifest_path.absolute_directory()?)?
.using_temp(generate_metadata)?;
}

Expand Down
8 changes: 7 additions & 1 deletion src/crate_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ impl CrateMetadata {
/// Parses the contract manifest and returns relevant metadata.
pub fn collect(manifest_path: &ManifestPath) -> Result<Self> {
let (metadata, root_package) = get_cargo_metadata(manifest_path)?;
let target_directory = metadata.target_directory.as_path().join("ink");
let mut target_directory = metadata.target_directory.as_path().join("ink");

// Normalize the package name.
let package_name = root_package.name.replace("-", "_");

let absolute_manifest_path = manifest_path.absolute_directory()?;
let absolute_workspace_root = metadata.workspace_root.canonicalize()?;
if absolute_manifest_path != absolute_workspace_root {
target_directory = target_directory.join(package_name.clone());
}

// {target_dir}/wasm32-unknown-unknown/release/{package_name}.wasm
let mut original_wasm = target_directory.clone();
original_wasm.push("wasm32-unknown-unknown");
Expand Down
36 changes: 36 additions & 0 deletions src/workspace/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ impl ManifestPath {
None
}
}

/// Returns the absolute directory path of the manifest.
pub fn absolute_directory(&self) -> Result<PathBuf, std::io::Error> {
let directory = match self.directory() {
Some(dir) => dir,
None => Path::new("./"),
};
directory.canonicalize()
}
}

impl TryFrom<&PathBuf> for ManifestPath {
Expand Down Expand Up @@ -404,3 +413,30 @@ fn crate_type_exists(crate_type: &str, crate_types: &[value::Value]) -> bool {
.iter()
.any(|v| v.as_str().map_or(false, |s| s == crate_type))
}

#[cfg(test)]
mod test {
use super::ManifestPath;
use crate::util::tests::with_tmp_dir;
use std::fs;

#[test]
fn must_return_absolute_path_from_absolute_path() {
with_tmp_dir(|path| {
// given
let cargo_toml_path = path.join("Cargo.toml");
let _ = fs::File::create(&cargo_toml_path).expect("file creation failed");
let manifest_path =
ManifestPath::new(cargo_toml_path).expect("manifest path creation failed");

// when
let absolute_path = manifest_path
.absolute_directory()
.expect("absolute path extraction failed");

// then
assert_eq!(absolute_path.as_path(), path);
Ok(())
})
}
}

0 comments on commit b4d61f6

Please sign in to comment.