mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 03:07:56 +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,7 +15,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::{write_file_if_changed, CargoCommandVersioned, OFFLINE};
|
||||
use crate::{write_file_if_changed, CargoCommandVersioned, RuntimeTarget, OFFLINE};
|
||||
|
||||
use build_helper::rerun_if_changed;
|
||||
use cargo_metadata::{DependencyKind, Metadata, MetadataCommand};
|
||||
@@ -112,6 +112,7 @@ fn crate_metadata(cargo_manifest: &Path) -> Metadata {
|
||||
///
|
||||
/// The path to the compact runtime binary and the bloaty runtime binary.
|
||||
pub(crate) fn create_and_compile(
|
||||
target: RuntimeTarget,
|
||||
project_cargo_toml: &Path,
|
||||
default_rustflags: &str,
|
||||
cargo_cmd: CargoCommandVersioned,
|
||||
@@ -120,11 +121,12 @@ pub(crate) fn create_and_compile(
|
||||
check_for_runtime_version_section: bool,
|
||||
) -> (Option<WasmBinary>, WasmBinaryBloaty) {
|
||||
let runtime_workspace_root = get_wasm_workspace_root();
|
||||
let runtime_workspace = runtime_workspace_root.join("wbuild");
|
||||
let runtime_workspace = runtime_workspace_root.join(target.build_subdirectory());
|
||||
|
||||
let crate_metadata = crate_metadata(project_cargo_toml);
|
||||
|
||||
let project = create_project(
|
||||
target,
|
||||
project_cargo_toml,
|
||||
&runtime_workspace,
|
||||
&crate_metadata,
|
||||
@@ -132,13 +134,58 @@ pub(crate) fn create_and_compile(
|
||||
features_to_enable,
|
||||
);
|
||||
|
||||
let build_config = BuildConfiguration::detect(&project);
|
||||
let build_config = BuildConfiguration::detect(target, &project);
|
||||
|
||||
// Build the bloaty runtime blob
|
||||
build_bloaty_blob(&build_config.blob_build_profile, &project, default_rustflags, cargo_cmd);
|
||||
let raw_blob_path = build_bloaty_blob(
|
||||
target,
|
||||
&build_config.blob_build_profile,
|
||||
&project,
|
||||
default_rustflags,
|
||||
cargo_cmd,
|
||||
);
|
||||
|
||||
let (final_blob_binary, bloaty_blob_binary) = match target {
|
||||
RuntimeTarget::Wasm => compile_wasm(
|
||||
project_cargo_toml,
|
||||
&project,
|
||||
bloaty_blob_out_name_override,
|
||||
check_for_runtime_version_section,
|
||||
&build_config,
|
||||
),
|
||||
RuntimeTarget::Riscv => {
|
||||
let out_name = bloaty_blob_out_name_override
|
||||
.unwrap_or_else(|| get_blob_name(target, project_cargo_toml));
|
||||
let out_path = project.join(format!("{out_name}.polkavm"));
|
||||
fs::copy(raw_blob_path, &out_path).expect("copying the runtime blob should never fail");
|
||||
(None, WasmBinaryBloaty(out_path))
|
||||
},
|
||||
};
|
||||
|
||||
generate_rerun_if_changed_instructions(
|
||||
project_cargo_toml,
|
||||
&project,
|
||||
&runtime_workspace,
|
||||
final_blob_binary.as_ref(),
|
||||
&bloaty_blob_binary,
|
||||
);
|
||||
|
||||
if let Err(err) = adjust_mtime(&bloaty_blob_binary, final_blob_binary.as_ref()) {
|
||||
build_helper::warning!("Error while adjusting the mtime of the blob binaries: {}", err)
|
||||
}
|
||||
|
||||
(final_blob_binary, bloaty_blob_binary)
|
||||
}
|
||||
|
||||
fn compile_wasm(
|
||||
project_cargo_toml: &Path,
|
||||
project: &Path,
|
||||
bloaty_blob_out_name_override: Option<String>,
|
||||
check_for_runtime_version_section: bool,
|
||||
build_config: &BuildConfiguration,
|
||||
) -> (Option<WasmBinary>, WasmBinaryBloaty) {
|
||||
// Get the name of the bloaty runtime blob.
|
||||
let bloaty_blob_default_name = get_blob_name(project_cargo_toml);
|
||||
let bloaty_blob_default_name = get_blob_name(RuntimeTarget::Wasm, project_cargo_toml);
|
||||
let bloaty_blob_out_name =
|
||||
bloaty_blob_out_name_override.unwrap_or_else(|| bloaty_blob_default_name.clone());
|
||||
|
||||
@@ -183,19 +230,6 @@ pub(crate) fn create_and_compile(
|
||||
});
|
||||
|
||||
let final_blob_binary = compact_compressed_blob_path.or(compact_blob_path);
|
||||
|
||||
generate_rerun_if_changed_instructions(
|
||||
project_cargo_toml,
|
||||
&project,
|
||||
&runtime_workspace,
|
||||
final_blob_binary.as_ref(),
|
||||
&bloaty_blob_binary,
|
||||
);
|
||||
|
||||
if let Err(err) = adjust_mtime(&bloaty_blob_binary, final_blob_binary.as_ref()) {
|
||||
build_helper::warning!("Error while adjusting the mtime of the blob binaries: {}", err)
|
||||
}
|
||||
|
||||
(final_blob_binary, bloaty_blob_binary)
|
||||
}
|
||||
|
||||
@@ -314,8 +348,12 @@ fn get_crate_name(cargo_manifest: &Path) -> String {
|
||||
}
|
||||
|
||||
/// Returns the name for the blob binary.
|
||||
fn get_blob_name(cargo_manifest: &Path) -> String {
|
||||
get_crate_name(cargo_manifest).replace('-', "_")
|
||||
fn get_blob_name(target: RuntimeTarget, cargo_manifest: &Path) -> String {
|
||||
let crate_name = get_crate_name(cargo_manifest);
|
||||
match target {
|
||||
RuntimeTarget::Wasm => crate_name.replace('-', "_"),
|
||||
RuntimeTarget::Riscv => crate_name,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the root path of the wasm workspace.
|
||||
@@ -336,6 +374,7 @@ fn get_wasm_workspace_root() -> PathBuf {
|
||||
}
|
||||
|
||||
fn create_project_cargo_toml(
|
||||
target: RuntimeTarget,
|
||||
wasm_workspace: &Path,
|
||||
workspace_root_path: &Path,
|
||||
crate_name: &str,
|
||||
@@ -396,17 +435,18 @@ fn create_project_cargo_toml(
|
||||
}
|
||||
|
||||
let mut package = Table::new();
|
||||
package.insert("name".into(), format!("{}-wasm", crate_name).into());
|
||||
package.insert("name".into(), format!("{}-blob", crate_name).into());
|
||||
package.insert("version".into(), "1.0.0".into());
|
||||
package.insert("edition".into(), "2021".into());
|
||||
|
||||
wasm_workspace_toml.insert("package".into(), package.into());
|
||||
|
||||
let mut lib = Table::new();
|
||||
lib.insert("name".into(), wasm_binary.into());
|
||||
lib.insert("crate-type".into(), vec!["cdylib".to_string()].into());
|
||||
|
||||
wasm_workspace_toml.insert("lib".into(), lib.into());
|
||||
if target == RuntimeTarget::Wasm {
|
||||
let mut lib = Table::new();
|
||||
lib.insert("name".into(), wasm_binary.into());
|
||||
lib.insert("crate-type".into(), vec!["cdylib".to_string()].into());
|
||||
wasm_workspace_toml.insert("lib".into(), lib.into());
|
||||
}
|
||||
|
||||
let mut dependencies = Table::new();
|
||||
|
||||
@@ -527,6 +567,7 @@ fn has_runtime_wasm_feature_declared(
|
||||
///
|
||||
/// The path to the created wasm project.
|
||||
fn create_project(
|
||||
target: RuntimeTarget,
|
||||
project_cargo_toml: &Path,
|
||||
wasm_workspace: &Path,
|
||||
crate_metadata: &Metadata,
|
||||
@@ -535,7 +576,7 @@ fn create_project(
|
||||
) -> PathBuf {
|
||||
let crate_name = get_crate_name(project_cargo_toml);
|
||||
let crate_path = project_cargo_toml.parent().expect("Parent path exists; qed");
|
||||
let wasm_binary = get_blob_name(project_cargo_toml);
|
||||
let wasm_binary = get_blob_name(target, project_cargo_toml);
|
||||
let wasm_project_folder = wasm_workspace.join(&crate_name);
|
||||
|
||||
fs::create_dir_all(wasm_project_folder.join("src"))
|
||||
@@ -552,6 +593,7 @@ fn create_project(
|
||||
enabled_features.extend(features_to_enable.into_iter());
|
||||
|
||||
create_project_cargo_toml(
|
||||
target,
|
||||
&wasm_project_folder,
|
||||
workspace_root_path,
|
||||
&crate_name,
|
||||
@@ -560,10 +602,20 @@ fn create_project(
|
||||
enabled_features.into_iter(),
|
||||
);
|
||||
|
||||
write_file_if_changed(
|
||||
wasm_project_folder.join("src/lib.rs"),
|
||||
"#![no_std] pub use wasm_project::*;",
|
||||
);
|
||||
match target {
|
||||
RuntimeTarget::Wasm => {
|
||||
write_file_if_changed(
|
||||
wasm_project_folder.join("src/lib.rs"),
|
||||
"#![no_std] pub use wasm_project::*;",
|
||||
);
|
||||
},
|
||||
RuntimeTarget::Riscv => {
|
||||
write_file_if_changed(
|
||||
wasm_project_folder.join("src/main.rs"),
|
||||
"#![no_std] #![no_main] pub use wasm_project::*;",
|
||||
);
|
||||
},
|
||||
}
|
||||
|
||||
if let Some(crate_lock_file) = find_cargo_lock(project_cargo_toml) {
|
||||
// Use the `Cargo.lock` of the main project.
|
||||
@@ -641,14 +693,15 @@ impl BuildConfiguration {
|
||||
/// # Note
|
||||
///
|
||||
/// Can be overriden by setting [`crate::WASM_BUILD_TYPE_ENV`].
|
||||
fn detect(wasm_project: &Path) -> Self {
|
||||
fn detect(target: RuntimeTarget, wasm_project: &Path) -> Self {
|
||||
let (name, overriden) = if let Ok(name) = env::var(crate::WASM_BUILD_TYPE_ENV) {
|
||||
(name, true)
|
||||
} else {
|
||||
// First go backwards to the beginning of the target directory.
|
||||
// Then go forwards to find the "wbuild" directory.
|
||||
// Then go forwards to find the build subdirectory.
|
||||
// We need to go backwards first because when starting from the root there
|
||||
// might be a chance that someone has a "wbuild" directory somewhere in the path.
|
||||
// might be a chance that someone has a directory somewhere in the path with the same
|
||||
// name.
|
||||
let name = wasm_project
|
||||
.components()
|
||||
.rev()
|
||||
@@ -656,9 +709,9 @@ impl BuildConfiguration {
|
||||
.collect::<Vec<_>>()
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|c| c.as_os_str() != "wbuild")
|
||||
.take_while(|c| c.as_os_str() != target.build_subdirectory())
|
||||
.last()
|
||||
.expect("We put the wasm project within a `target/.../wbuild` path; qed")
|
||||
.expect("We put the runtime project within a `target/.../[rw]build` path; qed")
|
||||
.as_os_str()
|
||||
.to_str()
|
||||
.expect("All our profile directory names are ascii; qed")
|
||||
@@ -711,22 +764,34 @@ fn offline_build() -> bool {
|
||||
|
||||
/// Build the project and create the bloaty runtime blob.
|
||||
fn build_bloaty_blob(
|
||||
target: RuntimeTarget,
|
||||
blob_build_profile: &Profile,
|
||||
project: &Path,
|
||||
default_rustflags: &str,
|
||||
cargo_cmd: CargoCommandVersioned,
|
||||
) {
|
||||
) -> PathBuf {
|
||||
let manifest_path = project.join("Cargo.toml");
|
||||
let mut build_cmd = cargo_cmd.command();
|
||||
|
||||
let rustflags = format!(
|
||||
"-C target-cpu=mvp -C target-feature=-sign-ext -C link-arg=--export-table {} {}",
|
||||
default_rustflags,
|
||||
env::var(crate::WASM_BUILD_RUSTFLAGS_ENV).unwrap_or_default(),
|
||||
);
|
||||
let mut rustflags = String::new();
|
||||
match target {
|
||||
RuntimeTarget::Wasm => {
|
||||
rustflags.push_str(
|
||||
"-C target-cpu=mvp -C target-feature=-sign-ext -C link-arg=--export-table ",
|
||||
);
|
||||
},
|
||||
RuntimeTarget::Riscv => {
|
||||
rustflags.push_str("-C target-feature=+lui-addi-fusion -C relocation-model=pie -C link-arg=--emit-relocs -C link-arg=--unique ");
|
||||
},
|
||||
}
|
||||
|
||||
rustflags.push_str(default_rustflags);
|
||||
rustflags.push_str(" --cfg substrate_runtime ");
|
||||
rustflags.push_str(&env::var(crate::WASM_BUILD_RUSTFLAGS_ENV).unwrap_or_default());
|
||||
|
||||
build_cmd
|
||||
.args(&["rustc", "--target=wasm32-unknown-unknown"])
|
||||
.arg("rustc")
|
||||
.arg(format!("--target={}", target.rustc_target()))
|
||||
.arg(format!("--manifest-path={}", manifest_path.display()))
|
||||
.env("RUSTFLAGS", rustflags)
|
||||
// Manually set the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir
|
||||
@@ -737,6 +802,9 @@ fn build_bloaty_blob(
|
||||
// our own `RUSTFLAGS` and thus, we need to remove this. Otherwise cargo favors this
|
||||
// env variable.
|
||||
.env_remove("CARGO_ENCODED_RUSTFLAGS")
|
||||
// Make sure if we're called from within a `build.rs` the host toolchain won't override a
|
||||
// rustup toolchain we've picked.
|
||||
.env_remove("RUSTC")
|
||||
// We don't want to call ourselves recursively
|
||||
.env(crate::SKIP_BUILD_ENV, "");
|
||||
|
||||
@@ -778,6 +846,52 @@ fn build_bloaty_blob(
|
||||
if build_cmd.status().map(|s| s.success()).is_err() {
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let blob_name = get_blob_name(target, &manifest_path);
|
||||
let target_directory = project
|
||||
.join("target")
|
||||
.join(target.rustc_target())
|
||||
.join(blob_build_profile.directory());
|
||||
match target {
|
||||
RuntimeTarget::Riscv => {
|
||||
let elf_path = target_directory.join(&blob_name);
|
||||
let elf_metadata = match elf_path.metadata() {
|
||||
Ok(path) => path,
|
||||
Err(error) =>
|
||||
panic!("internal error: couldn't read the metadata of {elf_path:?}: {error}"),
|
||||
};
|
||||
|
||||
let polkavm_path = target_directory.join(format!("{}.polkavm", blob_name));
|
||||
if polkavm_path
|
||||
.metadata()
|
||||
.map(|polkavm_metadata| {
|
||||
polkavm_metadata.modified().unwrap() >= elf_metadata.modified().unwrap()
|
||||
})
|
||||
.unwrap_or(true)
|
||||
{
|
||||
let blob_bytes =
|
||||
std::fs::read(elf_path).expect("binary always exists after its built");
|
||||
|
||||
let mut config = polkavm_linker::Config::default();
|
||||
config.set_strip(true); // TODO: This shouldn't always be done.
|
||||
|
||||
let program = match polkavm_linker::program_from_elf(config, &blob_bytes) {
|
||||
Ok(program) => program,
|
||||
Err(error) => {
|
||||
println!("Failed to link the runtime blob; this is probably a bug!");
|
||||
println!("Linking error: {error}");
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
|
||||
std::fs::write(&polkavm_path, program.as_bytes())
|
||||
.expect("writing the blob to a file always works");
|
||||
}
|
||||
|
||||
polkavm_path
|
||||
},
|
||||
RuntimeTarget::Wasm => target_directory.join(format!("{}.wasm", blob_name)),
|
||||
}
|
||||
}
|
||||
|
||||
fn compact_wasm(
|
||||
@@ -786,7 +900,7 @@ fn compact_wasm(
|
||||
cargo_manifest: &Path,
|
||||
out_name: &str,
|
||||
) -> Option<WasmBinary> {
|
||||
let default_out_name = get_blob_name(cargo_manifest);
|
||||
let default_out_name = get_blob_name(RuntimeTarget::Wasm, cargo_manifest);
|
||||
let in_path = project
|
||||
.join("target/wasm32-unknown-unknown")
|
||||
.join(inner_profile.directory())
|
||||
@@ -973,6 +1087,7 @@ fn generate_rerun_if_changed_instructions(
|
||||
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);
|
||||
println!("cargo:rerun-if-env-changed={}", crate::RUNTIME_TARGET);
|
||||
}
|
||||
|
||||
/// Track files and paths related to the given package to rerun `build.rs` on any relevant change.
|
||||
@@ -1018,7 +1133,7 @@ fn copy_blob_to_target_directory(cargo_manifest: &Path, blob_binary: &WasmBinary
|
||||
|
||||
fs::copy(
|
||||
blob_binary.wasm_binary_path(),
|
||||
target_dir.join(format!("{}.wasm", get_blob_name(cargo_manifest))),
|
||||
target_dir.join(format!("{}.wasm", get_blob_name(RuntimeTarget::Wasm, cargo_manifest))),
|
||||
)
|
||||
.expect("Copies blob binary to `WASM_TARGET_DIRECTORY`.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user