Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds DEBUG support for wasm builderp #5

Open
wants to merge 1 commit into
base: moonbeam-polkadot-v1.1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions substrate/utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
//! required as we walk up from the target directory until we find a `Cargo.toml`. If the target
//! directory is changed for the build, this environment variable can be used to point to the
//! actual workspace.
//! - `WASM_BUILD_STD` - Sets whether the Rust's standard library crates will also be built. This is
//! necessary to make sure the standard library crates only use the exact WASM feature set that
//! our executor supports. Enabled by default.
//! - `CARGO_NET_OFFLINE` - If `true`, `--offline` will be passed to all processes launched to
//! prevent network access. Useful in offline environments.
//!
Expand Down Expand Up @@ -158,6 +161,9 @@ const FORCE_WASM_BUILD_ENV: &str = "FORCE_WASM_BUILD";
/// Environment variable that hints the workspace we are building.
const WASM_BUILD_WORKSPACE_HINT: &str = "WASM_BUILD_WORKSPACE_HINT";

/// Environment variable to set whether we'll build `core`/`std`.
const WASM_BUILD_STD: &str = "WASM_BUILD_STD";

/// Write to the given `file` if the `content` is different.
fn write_file_if_changed(file: impl AsRef<Path>, content: impl AsRef<str>) {
if fs::read_to_string(file.as_ref()).ok().as_deref() != Some(content.as_ref()) {
Expand Down Expand Up @@ -282,6 +288,12 @@ impl CargoCommand {
self.version
}

/// Returns whether this version of the toolchain supports nightly features.
fn supports_nightly_features(&self) -> bool {
self.version.map(|version| version.is_nightly).unwrap_or(false) ||
env::var("RUSTC_BOOTSTRAP").is_ok()
}

/// Check if the supplied cargo command supports our Substrate wasm environment.
///
/// This means that either the cargo version is at minimum 1.68.0 or this is a nightly cargo.
Expand Down Expand Up @@ -332,3 +344,21 @@ impl std::ops::Deref for CargoCommandVersioned {
fn color_output_enabled() -> bool {
env::var(crate::WASM_BUILD_NO_COLOR).is_err()
}

/// Fetches a boolean environment variable. Will exit the process if the value is invalid.
fn get_bool_environment_variable(name: &str) -> Option<bool> {
let value = env::var_os(name)?;

// We're comparing `OsString`s here so we can't use a `match`.
if value == "1" {
Some(true)
} else if value == "0" {
Some(false)
} else {
build_helper::warning!(
"the '{}' environment variable has an invalid value; it must be either '1' or '0'",
name
);
std::process::exit(1);
}
}
20 changes: 20 additions & 0 deletions substrate/utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,25 @@ fn build_project(
build_cmd.arg("--offline");
}

// Our executor currently only supports the WASM MVP feature set, however nowadays
// when compiling WASM the Rust compiler has more features enabled by default.
//
// We do set the `-C target-cpu=mvp` flag to make sure that *our* code gets compiled
// in a way that is compatible with our executor, however this doesn't affect Rust's
// standard library crates (`std`, `core` and `alloc`) which are by default precompiled
// and still can make use of these extra features.
//
// So here we force the compiler to also compile the standard library crates for us
// to make sure that they also only use the MVP features.
if crate::get_bool_environment_variable(crate::WASM_BUILD_STD).unwrap_or(true) {
// Unfortunately this is still a nightly-only flag, but FWIW it is pretty widely used
// so it's unlikely to break without a replacement.
build_cmd.arg("-Z").arg("build-std");
if !cargo_cmd.supports_nightly_features() {
build_cmd.env("RUSTC_BOOTSTRAP", "1");
}
}

println!("{}", colorize_info_message("Information that should be included in a bug report."));
println!("{} {:?}", colorize_info_message("Executing build command:"), build_cmd);
println!("{} {}", colorize_info_message("Using rustc version:"), cargo_cmd.rustc_version());
Expand Down Expand Up @@ -877,6 +896,7 @@ fn generate_rerun_if_changed_instructions(
println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_RUSTFLAGS_ENV);
println!("cargo:rerun-if-env-changed={}", crate::WASM_TARGET_DIRECTORY);
println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_TOOLCHAIN);
println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_STD);
}

/// Track files and paths related to the given package to rerun `build.rs` on any relevant change.
Expand Down
Loading