Skip to content

Commit

Permalink
Put build artifacts under target/ink/ (use-ink#122)
Browse files Browse the repository at this point in the history
* Put build artifacts under `target/ink/`

* Add test

* Add new `target_directory` field on metadata struct

* Remove unnecessary `push`

* Remove redundant binding
  • Loading branch information
Michael Müller authored Dec 14, 2020
1 parent 082e649 commit 4c156d9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
13 changes: 10 additions & 3 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn build_cargo_project(
);

let cargo_build = |manifest_path: &ManifestPath| {
let target_dir = &crate_metadata.cargo_meta.target_directory;
let target_dir = &crate_metadata.target_directory;
util::invoke_cargo(
"build",
&[
Expand Down Expand Up @@ -313,7 +313,7 @@ fn execute(
dest_wasm: maybe_dest_wasm,
dest_metadata: None,
dest_bundle: None,
target_directory: crate_metadata.cargo_meta.target_directory,
target_directory: crate_metadata.target_directory,
optimization_result: maybe_optimization_result,
build_artifact,
};
Expand Down Expand Up @@ -378,14 +378,21 @@ mod tests {
cmd::new::execute("new_project", Some(path)).expect("new project creation failed");
let manifest_path =
ManifestPath::new(&path.join("new_project").join("Cargo.toml")).unwrap();
super::execute(
let res = super::execute(
&manifest_path,
None,
true,
BuildArtifacts::All,
UnstableFlags::default(),
)
.expect("build failed");

// we can't use `/target/ink` here, since this would match
// for `/target` being the root path. but since `ends_with`
// always matches whole path components we can be sure
// the path can never be e.g. `foo_target/ink` -- the assert
// would fail for that.
assert!(res.target_directory.ends_with("target/ink"));
Ok(())
})
}
Expand Down
29 changes: 15 additions & 14 deletions src/cmd/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ impl GenerateMetadataCommand {
pub fn exec(&self) -> Result<BuildResult> {
util::assert_channel()?;

let cargo_meta = &self.crate_metadata.cargo_meta;
let out_path_metadata = cargo_meta.target_directory.join(METADATA_FILE);
let target_directory = self.crate_metadata.target_directory.clone();
let out_path_metadata = target_directory.join(METADATA_FILE);

let fname_bundle = format!("{}.contract", self.crate_metadata.package_name);
let out_path_bundle = cargo_meta.target_directory.join(fname_bundle);

let target_directory = cargo_meta.target_directory.clone();
let out_path_bundle = target_directory.join(fname_bundle);

// build the extended contract project metadata
let ExtendedMetadataResult {
Expand Down Expand Up @@ -120,15 +118,18 @@ impl GenerateMetadataCommand {
if self.unstable_options.original_manifest {
generate_metadata(&self.crate_metadata.manifest_path)?;
} else {
Workspace::new(&cargo_meta, &self.crate_metadata.root_package.id)?
.with_root_package_manifest(|manifest| {
manifest
.with_added_crate_type("rlib")?
.with_profile_release_lto(false)?;
Ok(())
})?
.with_metadata_gen_package()?
.using_temp(generate_metadata)?;
Workspace::new(
&self.crate_metadata.cargo_meta,
&self.crate_metadata.root_package.id,
)?
.with_root_package_manifest(|manifest| {
manifest
.with_added_crate_type("rlib")?
.with_profile_release_lto(false)?;
Ok(())
})?
.with_metadata_gen_package()?
.using_temp(generate_metadata)?;
}

let dest_bundle = if self.build_artifact == BuildArtifacts::All {
Expand Down
9 changes: 7 additions & 2 deletions src/crate_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,29 @@ pub struct CrateMetadata {
pub documentation: Option<Url>,
pub homepage: Option<Url>,
pub user: Option<Map<String, Value>>,
pub target_directory: PathBuf,
}

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 mut target_directory = metadata.target_directory.clone();
target_directory.push("ink");

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

// {target_dir}/wasm32-unknown-unknown/release/{package_name}.wasm
let mut original_wasm = metadata.target_directory.clone();
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.set_extension("wasm");

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

Expand Down Expand Up @@ -86,6 +90,7 @@ impl CrateMetadata {
documentation,
homepage,
user,
target_directory,
};
Ok(crate_metadata)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ enum Command {
Deploy {
#[structopt(flatten)]
extrinsic_opts: ExtrinsicOpts,
/// Path to wasm contract code, defaults to `./target/<name>-pruned.wasm`
/// Path to wasm contract code, defaults to `./target/ink/<name>-pruned.wasm`
#[structopt(parse(from_os_str))]
wasm_path: Option<PathBuf>,
},
Expand Down

0 comments on commit 4c156d9

Please sign in to comment.