Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

wasm-builder: Always build runtime with the toolchain used for the outer build #13579

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion primitives/application-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ full_crypto = [
# Don't add `panic_handler` and `alloc_error_handler` since they are expected to be provided
# by the user anyway.
"sp-io/disable_panic_handler",
"sp-io/disable_oom",
]
6 changes: 2 additions & 4 deletions primitives/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ with-tracing = [
#
# For the regular wasm runtime builds those are not used.
disable_panic_handler = []
disable_oom = []
disable_allocator = []

# This feature flag controls the runtime's behavior when encountering
# a panic or when it runs out of memory, improving the diagnostics.
# a panic improving the diagnostics.
#
# When enabled the runtime will marshal the relevant error message
# to the host through the `PanicHandler::abort_on_panic` runtime interface.
Expand All @@ -83,8 +82,7 @@ disable_allocator = []
# logs, with the caller receving a generic "wasm `unreachable` instruction executed"
# error message.
#
# This has no effect if both `disable_panic_handler` and `disable_oom`
# are enabled.
# This has no effect if `disable_panic_handler` is enabled.
#
# WARNING: Enabling this feature flag requires the `PanicHandler::abort_on_panic`
# host function to be supported by the host. Do *not* enable it for your
Expand Down
16 changes: 0 additions & 16 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(
feature = "std",
doc = "Substrate runtime standard library as compiled when linked with Rust's standard library."
Expand Down Expand Up @@ -1652,21 +1651,6 @@ pub fn panic(info: &core::panic::PanicInfo) -> ! {
}
}

/// A default OOM handler for WASM environment.
#[cfg(all(not(feature = "disable_oom"), not(feature = "std")))]
#[alloc_error_handler]
pub fn oom(_: core::alloc::Layout) -> ! {
#[cfg(feature = "improved_panic_error_reporting")]
{
panic_handler::abort_on_panic("Runtime memory exhausted.");
}
#[cfg(not(feature = "improved_panic_error_reporting"))]
{
logging::log(LogLevel::Error, "runtime", b"Runtime memory exhausted. Aborting");
core::arch::wasm32::unreachable();
}
}

/// Type alias for Externalities implementation used in tests.
#[cfg(feature = "std")]
pub type TestExternalities = sp_state_machine::TestExternalities<sp_core::Blake2Hasher>;
Expand Down
1 change: 1 addition & 0 deletions utils/wasm-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "5.0.0-dev"
authors = ["Parity Technologies <[email protected]>"]
description = "Utility for building WASM binaries"
edition = "2021"
rust-version = "1.68"
readme = "README.md"
repository = "https://github.com/paritytech/substrate/"
license = "Apache-2.0"
Expand Down
2 changes: 0 additions & 2 deletions utils/wasm-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ By using environment variables, you can configure which Wasm binaries are built
- `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build.
- `WASM_TARGET_DIRECTORY` - Will copy any build Wasm binary to the given directory. The path needs
to be absolute.
- `WASM_BUILD_TOOLCHAIN` - The toolchain that should be used to build the Wasm binaries. The
format needs to be the same as used by cargo, e.g. `nightly-2020-02-20`.
- `CARGO_NET_OFFLINE` - If `true`, `--offline` will be passed to all processes launched to prevent network access. Useful in offline environments.

Each project can be skipped individually by using the environment variable `SKIP_PROJECT_NAME_WASM_BUILD`.
Expand Down
93 changes: 7 additions & 86 deletions utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@
//! - `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build.
//! - `WASM_TARGET_DIRECTORY` - Will copy any build Wasm binary to the given directory. The path
//! needs to be absolute.
//! - `WASM_BUILD_TOOLCHAIN` - The toolchain that should be used to build the Wasm binaries. The
//! format needs to be the same as used by cargo, e.g. `nightly-2020-02-20`.
//! - `WASM_BUILD_WORKSPACE_HINT` - Hint the workspace that is being built. This is normally not
//! 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
Expand All @@ -107,7 +105,6 @@

use std::{
env, fs,
io::BufRead,
path::{Path, PathBuf},
process::Command,
};
Expand Down Expand Up @@ -143,9 +140,6 @@ const WASM_TARGET_DIRECTORY: &str = "WASM_TARGET_DIRECTORY";
/// Environment variable to disable color output of the wasm build.
const WASM_BUILD_NO_COLOR: &str = "WASM_BUILD_NO_COLOR";

/// Environment variable to set the toolchain used to compile the wasm binary.
const WASM_BUILD_TOOLCHAIN: &str = "WASM_BUILD_TOOLCHAIN";
Comment on lines -146 to -147
Copy link
Member

Choose a reason for hiding this comment

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

This clearly removes stuff that shouldn't be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I thought we don't need the override anymore and saw the chance for some simplification. But it might be necessary for the transition at least.


/// Environment variable that makes sure the WASM build is triggered.
const FORCE_WASM_BUILD_ENV: &str = "FORCE_WASM_BUILD";

Expand All @@ -172,96 +166,23 @@ fn copy_file_if_changed(src: PathBuf, dst: PathBuf) {
}
}

/// Get a cargo command that compiles with nightly
fn get_nightly_cargo() -> CargoCommand {
let env_cargo =
CargoCommand::new(&env::var("CARGO").expect("`CARGO` env variable is always set by cargo"));
let default_cargo = CargoCommand::new("cargo");
let rustup_run_nightly = CargoCommand::new_with_args("rustup", &["run", "nightly", "cargo"]);
let wasm_toolchain = env::var(WASM_BUILD_TOOLCHAIN).ok();

// First check if the user requested a specific toolchain
if let Some(cmd) = wasm_toolchain.and_then(|t| get_rustup_nightly(Some(t))) {
cmd
} else if env_cargo.is_nightly() {
env_cargo
} else if default_cargo.is_nightly() {
default_cargo
} else if rustup_run_nightly.is_nightly() {
rustup_run_nightly
} else {
// If no command before provided us with a nightly compiler, we try to search one
// with rustup. If that fails as well, we return the default cargo and let the prequisities
// check fail.
get_rustup_nightly(None).unwrap_or(default_cargo)
}
}

/// Get a nightly from rustup. If `selected` is `Some(_)`, a `CargoCommand` using the given
/// nightly is returned.
fn get_rustup_nightly(selected: Option<String>) -> Option<CargoCommand> {
let host = format!("-{}", env::var("HOST").expect("`HOST` is always set by cargo"));

let version = match selected {
Some(selected) => selected,
None => {
let output = Command::new("rustup").args(&["toolchain", "list"]).output().ok()?.stdout;
let lines = output.as_slice().lines();

let mut latest_nightly = None;
for line in lines.filter_map(|l| l.ok()) {
if line.starts_with("nightly-") && line.ends_with(&host) {
// Rustup prints them sorted
latest_nightly = Some(line.clone());
}
}

latest_nightly?.trim_end_matches(&host).into()
},
};

Some(CargoCommand::new_with_args("rustup", &["run", &version, "cargo"]))
}

/// Wraps a specific command which represents a cargo invocation.
#[derive(Debug)]
struct CargoCommand {
program: String,
args: Vec<String>,
}

impl CargoCommand {
fn new(program: &str) -> Self {
CargoCommand { program: program.into(), args: Vec::new() }
}

fn new_with_args(program: &str, args: &[&str]) -> Self {
CargoCommand {
program: program.into(),
args: args.iter().map(ToString::to_string).collect(),
}
}

fn command(&self) -> Command {
let mut cmd = Command::new(&self.program);
cmd.args(&self.args);
cmd
Command::new(&self.program)
}
}

/// Check if the supplied cargo command is a nightly version
fn is_nightly(&self) -> bool {
// `RUSTC_BOOTSTRAP` tells a stable compiler to behave like a nightly. So, when this env
// variable is set, we can assume that whatever rust compiler we have, it is a nightly
// compiler. For "more" information, see:
// https://github.com/rust-lang/rust/blob/fa0f7d0080d8e7e9eb20aa9cbf8013f96c81287f/src/libsyntax/feature_gate/check.rs#L891
env::var("RUSTC_BOOTSTRAP").is_ok() ||
self.command()
.arg("--version")
.output()
.map_err(|_| ())
.and_then(|o| String::from_utf8(o.stdout).map_err(|_| ()))
.unwrap_or_default()
.contains("-nightly")
impl Default for CargoCommand {
fn default() -> Self {
Self {
program: env::var("CARGO").expect("`CARGO` env variable is always set by cargo").into(),
}
}
}

Expand Down
7 changes: 1 addition & 6 deletions utils/wasm-builder/src/prerequisites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ fn print_error_message(message: &str) -> String {
///
/// Returns the versioned cargo command on success.
pub(crate) fn check() -> Result<CargoCommandVersioned, String> {
let cargo_command = crate::get_nightly_cargo();

if !cargo_command.is_nightly() {
return Err(print_error_message("Rust nightly not installed, please install it!"))
}

let cargo_command = CargoCommand::default();
check_wasm_toolchain_installed(cargo_command)
}

Expand Down
1 change: 0 additions & 1 deletion utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,6 @@ fn generate_rerun_if_changed_instructions(
println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_TYPE_ENV);
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);
}

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