Skip to content

Commit

Permalink
Replace xbuild with cargo build-std (use-ink#99)
Browse files Browse the repository at this point in the history
* Replace xbuild with cargo build-std

* Comment about overridden RUSTFLAGS

Co-authored-by: Michael Mueller <mich@elmueller.net>
  • Loading branch information
ascjones and cmichi authored Nov 5, 2020
1 parent 5220d6d commit 4c25e3a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 78 deletions.
44 changes: 1 addition & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ codec = { package = "parity-scale-codec", version = "1.3.4" }
which = "4.0.2"
colored = "2.0.0"
toml = "0.5.6"
cargo-xbuild = "0.6.0"
rustc_version = "0.2.3"
blake2 = "0.9.0"
contract-metadata = { version = "0.1.0", path = "./metadata" }
Expand Down
55 changes: 21 additions & 34 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ const MAX_MEMORY_PAGES: u32 = 16;

/// Builds the project in the specified directory, defaults to the current directory.
///
/// Uses [`cargo-xbuild`](https://github.com/rust-osdev/cargo-xbuild) for maximum optimization of
/// the resulting Wasm binary.
/// Uses the unstable cargo feature [`build-std`](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std)
/// to build the standard library with [`panic_immediate_abort`](https://github.com/johnthagen/min-sized-rust#remove-panic-string-formatting-with-panic_immediate_abort)
/// which reduces the size of the Wasm binary by not including panic strings and formatting code.
///
/// # Cargo.toml optimizations
///
Expand All @@ -54,42 +55,28 @@ fn build_cargo_project(
) -> Result<()> {
util::assert_channel()?;

// set RUSTFLAGS, read from environment var by cargo-xbuild
// set linker args via RUSTFLAGS.
// Currently will override user defined RUSTFLAGS from .cargo/config. See https://github.com/paritytech/cargo-contract/issues/98.
std::env::set_var(
"RUSTFLAGS",
"-C link-arg=-z -C link-arg=stack-size=65536 -C link-arg=--import-memory",
);

let verbosity = verbosity.map(|v| match v {
Verbosity::Verbose => xargo_lib::Verbosity::Verbose,
Verbosity::Quiet => xargo_lib::Verbosity::Quiet,
});

let xbuild = |manifest_path: &ManifestPath| {
let manifest_path = Some(manifest_path);
let target = Some("wasm32-unknown-unknown");
let cargo_build = |manifest_path: &ManifestPath| {
let target_dir = &crate_metadata.cargo_meta.target_directory;
let other_args = [
"--no-default-features",
"--release",
&format!("--target-dir={}", target_dir.to_string_lossy()),
];
let args = xargo_lib::Args::new(target, manifest_path, verbosity, &other_args)
.map_err(|e| anyhow::anyhow!("{}", e))
.context("Creating xargo args")?;

let config = xargo_lib::Config {
sysroot_path: target_dir.join("sysroot"),
memcpy: false,
panic_immediate_abort: true,
};

let exit_status = xargo_lib::build(args, "build", Some(config))
.map_err(|e| anyhow::anyhow!("{}", e))
.context("Building with xbuild")?;
if !exit_status.success() {
anyhow::bail!("xbuild failed with status {}", exit_status)
}
util::invoke_cargo(
"build",
&[
"--target=wasm32-unknown-unknown",
"-Zbuild-std",
"-Zbuild-std-features=panic_immediate_abort",
"--no-default-features",
"--release",
&format!("--target-dir={}", target_dir.to_string_lossy()),
],
manifest_path.directory(),
verbosity,
)?;
Ok(())
};

Expand All @@ -100,7 +87,7 @@ fn build_cargo_project(
"with 'original-manifest' enabled, the contract binary may not be of optimal size."
.bold()
);
xbuild(&crate_metadata.manifest_path)?;
cargo_build(&crate_metadata.manifest_path)?;
} else {
Workspace::new(&crate_metadata.cargo_meta, &crate_metadata.root_package.id)?
.with_root_package_manifest(|manifest| {
Expand All @@ -109,7 +96,7 @@ fn build_cargo_project(
.with_profile_release_defaults(Profile::default_contract_release())?;
Ok(())
})?
.using_temp(xbuild)?;
.using_temp(cargo_build)?;
}

// clear RUSTFLAGS
Expand Down

0 comments on commit 4c25e3a

Please sign in to comment.