mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
Initial support for building RISC-V runtimes targeting PolkaVM (#3179)
This PR adds initial support for building RISC-V runtimes targeting PolkaVM. - Setting the `SUBSTRATE_RUNTIME_TARGET=riscv` environment variable will now build a RISC-V runtime instead of a WASM runtime. - This only adds support for *building* runtimes; running them will need a PolkaVM-based executor, which I will add in a future PR. - Only building the minimal runtime is supported (building the Polkadot runtime doesn't work *yet* due to one of the dependencies). - The builder now sets a `substrate_runtime` cfg flag when building the runtimes, with the idea being that instead of doing `#[cfg(not(feature = "std"))]` or `#[cfg(target_arch = "wasm32")]` to detect that we're building a runtime you'll do `#[cfg(substrate_runtime)]`. (Switching the whole codebase to use this will be done in a future PR; I deliberately didn't do this here to keep this PR minimal and reviewable.) - Further renaming of things (e.g. types, environment variables and proc macro attributes having "wasm" in their name) to be target-agnostic will also be done in a future refactoring PR (while keeping backwards compatibility where it makes sense; I don't intend to break anyone's workflow or create unnecessary churn). - This PR also fixes two bugs in the `wasm-builder` crate: * The `RUSTC` environment variable is now removed when invoking the compiler. This prevents the toolchain version from being overridden when called from a `build.rs` script. * When parsing the `rustup toolchain list` output the `(default)` is now properly stripped and not treated as part of the version. - I've also added a minimal CI job that makes sure this doesn't break in the future. (cc @paritytech/ci) cc @athei ------ Also, just a fun little tidbit: quickly comparing the size of the built runtimes it seems that the PolkaVM runtime is slightly smaller than the WASM one. (`production` build, with the `names` section substracted from the WASM's size to keep things fair, since for the PolkaVM runtime we're currently stripping out everything) - `.wasm`: 625505 bytes - `.wasm` (after wasm-opt -O3): 563205 bytes - `.wasm` (after wasm-opt -Os): 562987 bytes - `.wasm` (after wasm-opt -Oz): 536852 bytes - `.polkavm`: ~~580338 bytes~~ 550476 bytes (after enabling extra target features; I'll add those in another PR once we have an executor working) --------- Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
@@ -15,14 +15,19 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::{write_file_if_changed, CargoCommand, CargoCommandVersioned};
|
||||
use crate::{write_file_if_changed, CargoCommand, CargoCommandVersioned, RuntimeTarget};
|
||||
|
||||
use console::style;
|
||||
use std::{fs, path::Path};
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use tempfile::tempdir;
|
||||
|
||||
/// Print an error message.
|
||||
fn print_error_message(message: &str) -> String {
|
||||
/// Colorizes an error message, if color output is enabled.
|
||||
fn colorize_error_message(message: &str) -> String {
|
||||
if super::color_output_enabled() {
|
||||
style(message).red().bold().to_string()
|
||||
} else {
|
||||
@@ -30,121 +35,165 @@ fn print_error_message(message: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Colorizes an auxiliary message, if color output is enabled.
|
||||
fn colorize_aux_message(message: &str) -> String {
|
||||
if super::color_output_enabled() {
|
||||
style(message).yellow().bold().to_string()
|
||||
} else {
|
||||
message.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks that all prerequisites are installed.
|
||||
///
|
||||
/// Returns the versioned cargo command on success.
|
||||
pub(crate) fn check() -> Result<CargoCommandVersioned, String> {
|
||||
let cargo_command = crate::get_cargo_command();
|
||||
pub(crate) fn check(target: RuntimeTarget) -> Result<CargoCommandVersioned, String> {
|
||||
let cargo_command = crate::get_cargo_command(target);
|
||||
match target {
|
||||
RuntimeTarget::Wasm => {
|
||||
if !cargo_command.supports_substrate_runtime_env(target) {
|
||||
return Err(colorize_error_message(
|
||||
"Cannot compile a WASM runtime: no compatible Rust compiler found!\n\
|
||||
Install at least Rust 1.68.0 or a recent nightly version.",
|
||||
));
|
||||
}
|
||||
|
||||
if !cargo_command.supports_substrate_wasm_env() {
|
||||
return Err(print_error_message(
|
||||
"Cannot compile the WASM runtime: no compatible Rust compiler found!\n\
|
||||
Install at least Rust 1.68.0 or a recent nightly version.",
|
||||
))
|
||||
check_wasm_toolchain_installed(cargo_command)
|
||||
},
|
||||
RuntimeTarget::Riscv => {
|
||||
if !cargo_command.supports_substrate_runtime_env(target) {
|
||||
return Err(colorize_error_message(
|
||||
"Cannot compile a RISC-V runtime: no compatible Rust compiler found!\n\
|
||||
Install a toolchain from here and try again: https://github.com/paritytech/rustc-rv32e-toolchain/",
|
||||
));
|
||||
}
|
||||
|
||||
let dummy_crate = DummyCrate::new(&cargo_command, target);
|
||||
let version = dummy_crate.get_rustc_version();
|
||||
Ok(CargoCommandVersioned::new(cargo_command, version))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
struct DummyCrate<'a> {
|
||||
cargo_command: &'a CargoCommand,
|
||||
temp: tempfile::TempDir,
|
||||
manifest_path: PathBuf,
|
||||
target: RuntimeTarget,
|
||||
}
|
||||
|
||||
impl<'a> DummyCrate<'a> {
|
||||
/// Creates a minimal dummy crate.
|
||||
fn new(cargo_command: &'a CargoCommand, target: RuntimeTarget) -> Self {
|
||||
let temp = tempdir().expect("Creating temp dir does not fail; qed");
|
||||
let project_dir = temp.path();
|
||||
fs::create_dir_all(project_dir.join("src")).expect("Creating src dir does not fail; qed");
|
||||
|
||||
let manifest_path = project_dir.join("Cargo.toml");
|
||||
write_file_if_changed(
|
||||
&manifest_path,
|
||||
r#"
|
||||
[package]
|
||||
name = "dummy-crate"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
"#,
|
||||
);
|
||||
|
||||
write_file_if_changed(project_dir.join("src/main.rs"), "fn main() {}");
|
||||
DummyCrate { cargo_command, temp, manifest_path, target }
|
||||
}
|
||||
|
||||
check_wasm_toolchain_installed(cargo_command)
|
||||
}
|
||||
|
||||
/// Creates a minimal dummy crate at the given path and returns the manifest path.
|
||||
fn create_minimal_crate(project_dir: &Path) -> std::path::PathBuf {
|
||||
fs::create_dir_all(project_dir.join("src")).expect("Creating src dir does not fail; qed");
|
||||
|
||||
let manifest_path = project_dir.join("Cargo.toml");
|
||||
write_file_if_changed(
|
||||
&manifest_path,
|
||||
r#"
|
||||
[package]
|
||||
name = "wasm-test"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
"#,
|
||||
);
|
||||
|
||||
write_file_if_changed(project_dir.join("src/main.rs"), "fn main() {}");
|
||||
manifest_path
|
||||
}
|
||||
|
||||
fn check_wasm_toolchain_installed(
|
||||
cargo_command: CargoCommand,
|
||||
) -> Result<CargoCommandVersioned, String> {
|
||||
let temp = tempdir().expect("Creating temp dir does not fail; qed");
|
||||
let manifest_path = create_minimal_crate(temp.path()).display().to_string();
|
||||
|
||||
let prepare_command = |subcommand| {
|
||||
let mut cmd = cargo_command.command();
|
||||
fn prepare_command(&self, subcommand: &str) -> Command {
|
||||
let mut cmd = self.cargo_command.command();
|
||||
// Chdir to temp to avoid including project's .cargo/config.toml
|
||||
// by accident - it can happen in some CI environments.
|
||||
cmd.current_dir(&temp);
|
||||
cmd.args(&[
|
||||
subcommand,
|
||||
"--target=wasm32-unknown-unknown",
|
||||
"--manifest-path",
|
||||
&manifest_path,
|
||||
]);
|
||||
cmd.current_dir(&self.temp);
|
||||
cmd.arg(subcommand)
|
||||
.arg(format!("--target={}", self.target.rustc_target()))
|
||||
.args(&["--manifest-path", &self.manifest_path.display().to_string()]);
|
||||
|
||||
if super::color_output_enabled() {
|
||||
cmd.arg("--color=always");
|
||||
}
|
||||
|
||||
// manually set the `CARGO_TARGET_DIR` to prevent a cargo deadlock
|
||||
let target_dir = temp.path().join("target").display().to_string();
|
||||
let target_dir = self.temp.path().join("target").display().to_string();
|
||||
cmd.env("CARGO_TARGET_DIR", &target_dir);
|
||||
|
||||
// Make sure the host's flags aren't used here, e.g. if an alternative linker is specified
|
||||
// in the RUSTFLAGS then the check we do here will break unless we clear these.
|
||||
cmd.env_remove("CARGO_ENCODED_RUSTFLAGS");
|
||||
cmd.env_remove("RUSTFLAGS");
|
||||
// Make sure if we're called from within a `build.rs` the host toolchain won't override a
|
||||
// rustup toolchain we've picked.
|
||||
cmd.env_remove("RUSTC");
|
||||
cmd
|
||||
};
|
||||
|
||||
let err_msg =
|
||||
print_error_message("Rust WASM toolchain is not properly installed; please install it!");
|
||||
let build_result = prepare_command("build").output().map_err(|_| err_msg.clone())?;
|
||||
if !build_result.status.success() {
|
||||
return match String::from_utf8(build_result.stderr) {
|
||||
Ok(ref err) if err.contains("the `wasm32-unknown-unknown` target may not be installed") =>
|
||||
Err(print_error_message("Cannot compile the WASM runtime: the `wasm32-unknown-unknown` target is not installed!\n\
|
||||
You can install it with `rustup target add wasm32-unknown-unknown` if you're using `rustup`.")),
|
||||
|
||||
// Apparently this can happen when we're running on a non Tier 1 platform.
|
||||
Ok(ref err) if err.contains("linker `rust-lld` not found") =>
|
||||
Err(print_error_message("Cannot compile the WASM runtime: `rust-lld` not found!")),
|
||||
|
||||
Ok(ref err) => Err(format!(
|
||||
"{}\n\n{}\n{}\n{}{}\n",
|
||||
err_msg,
|
||||
style("Further error information:").yellow().bold(),
|
||||
style("-".repeat(60)).yellow().bold(),
|
||||
err,
|
||||
style("-".repeat(60)).yellow().bold(),
|
||||
)),
|
||||
|
||||
Err(_) => Err(err_msg),
|
||||
};
|
||||
}
|
||||
|
||||
let mut run_cmd = prepare_command("rustc");
|
||||
run_cmd.args(&["-q", "--", "--version"]);
|
||||
fn get_rustc_version(&self) -> String {
|
||||
let mut run_cmd = self.prepare_command("rustc");
|
||||
run_cmd.args(&["-q", "--", "--version"]);
|
||||
run_cmd
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.unwrap_or_else(|| "unknown rustc version".into())
|
||||
}
|
||||
|
||||
let version = run_cmd
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
.unwrap_or_else(|| "unknown rustc version".into());
|
||||
|
||||
if crate::build_std_required() {
|
||||
let mut sysroot_cmd = prepare_command("rustc");
|
||||
fn get_sysroot(&self) -> Option<String> {
|
||||
let mut sysroot_cmd = self.prepare_command("rustc");
|
||||
sysroot_cmd.args(&["-q", "--", "--print", "sysroot"]);
|
||||
if let Some(sysroot) =
|
||||
sysroot_cmd.output().ok().and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
{
|
||||
sysroot_cmd.output().ok().and_then(|o| String::from_utf8(o.stdout).ok())
|
||||
}
|
||||
|
||||
fn try_build(&self) -> Result<(), Option<String>> {
|
||||
let Ok(result) = self.prepare_command("build").output() else { return Err(None) };
|
||||
if !result.status.success() {
|
||||
return Err(Some(String::from_utf8_lossy(&result.stderr).into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn check_wasm_toolchain_installed(
|
||||
cargo_command: CargoCommand,
|
||||
) -> Result<CargoCommandVersioned, String> {
|
||||
let dummy_crate = DummyCrate::new(&cargo_command, RuntimeTarget::Wasm);
|
||||
|
||||
if let Err(error) = dummy_crate.try_build() {
|
||||
let basic_error_message = colorize_error_message(
|
||||
"Rust WASM toolchain is not properly installed; please install it!",
|
||||
);
|
||||
return match error {
|
||||
None => Err(basic_error_message),
|
||||
Some(error) if error.contains("the `wasm32-unknown-unknown` target may not be installed") => {
|
||||
Err(colorize_error_message("Cannot compile the WASM runtime: the `wasm32-unknown-unknown` target is not installed!\n\
|
||||
You can install it with `rustup target add wasm32-unknown-unknown` if you're using `rustup`."))
|
||||
},
|
||||
// Apparently this can happen when we're running on a non Tier 1 platform.
|
||||
Some(ref error) if error.contains("linker `rust-lld` not found") =>
|
||||
Err(colorize_error_message("Cannot compile the WASM runtime: `rust-lld` not found!")),
|
||||
Some(error) => Err(format!(
|
||||
"{}\n\n{}\n{}\n{}{}\n",
|
||||
basic_error_message,
|
||||
colorize_aux_message("Further error information:"),
|
||||
colorize_aux_message(&"-".repeat(60)),
|
||||
error,
|
||||
colorize_aux_message(&"-".repeat(60)),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
let version = dummy_crate.get_rustc_version();
|
||||
if crate::build_std_required() {
|
||||
if let Some(sysroot) = dummy_crate.get_sysroot() {
|
||||
let src_path =
|
||||
Path::new(sysroot.trim()).join("lib").join("rustlib").join("src").join("rust");
|
||||
if !src_path.exists() {
|
||||
return Err(print_error_message(
|
||||
return Err(colorize_error_message(
|
||||
"Cannot compile the WASM runtime: no standard library sources found!\n\
|
||||
You can install them with `rustup component add rust-src` if you're using `rustup`.",
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user