mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 22:47:56 +00:00
WASM runtime switch to import memory (#4737)
* WASM runtime switch to import memory Up to now runtimes have exported their memory. To unify it with sandboxing, this pr switches runtimes to import memory as well. From a functional perspective, exporting/importing memory makes no difference to the runtime. To provide backwards compatibility, WASM exported memory is still supported. * Revert debug stuff * Revert some stuff
This commit is contained in:
@@ -25,7 +25,10 @@
|
||||
//!
|
||||
//! For more information see <https://crates.io/substrate-wasm-builder>
|
||||
|
||||
use std::{env, process::{Command, self}, fs, path::{PathBuf, Path}};
|
||||
use std::{
|
||||
env, process::{Command, self}, fs, path::{PathBuf, Path}, hash::{Hash, Hasher},
|
||||
collections::hash_map::DefaultHasher,
|
||||
};
|
||||
|
||||
/// Environment variable that tells us to skip building the WASM binary.
|
||||
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";
|
||||
@@ -47,6 +50,225 @@ fn replace_back_slashes<T: ToString>(path: T) -> String {
|
||||
path.to_string().replace("\\", "/")
|
||||
}
|
||||
|
||||
/// Returns the manifest dir from the `CARGO_MANIFEST_DIR` env.
|
||||
fn get_manifest_dir() -> PathBuf {
|
||||
env::var("CARGO_MANIFEST_DIR")
|
||||
.expect("`CARGO_MANIFEST_DIR` is always set for `build.rs` files; qed")
|
||||
.into()
|
||||
}
|
||||
|
||||
/// First step of the [`WasmBuilder`] to select the project to build.
|
||||
pub struct WasmBuilderSelectProject {
|
||||
/// This parameter just exists to make it impossible to construct
|
||||
/// this type outside of this crate.
|
||||
_ignore: (),
|
||||
}
|
||||
|
||||
impl WasmBuilderSelectProject {
|
||||
/// Use the current project as project for building the WASM binary.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the `CARGO_MANIFEST_DIR` variable is not set. This variable
|
||||
/// is always set by `Cargo` in `build.rs` files.
|
||||
pub fn with_current_project(self) -> WasmBuilderSelectSource {
|
||||
WasmBuilderSelectSource(get_manifest_dir().join("Cargo.toml"))
|
||||
}
|
||||
|
||||
/// Use the given `path` as project for building the WASM binary.
|
||||
///
|
||||
/// Returns an error if the given `path` does not points to a `Cargo.toml`.
|
||||
pub fn with_project(
|
||||
self,
|
||||
path: impl Into<PathBuf>,
|
||||
) -> Result<WasmBuilderSelectSource, &'static str> {
|
||||
let path = path.into();
|
||||
|
||||
if path.ends_with("Cargo.toml") {
|
||||
Ok(WasmBuilderSelectSource(path))
|
||||
} else {
|
||||
Err("Project path must point to the `Cargo.toml` of the project")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Second step of the [`WasmBuilder`] to set the source of the `wasm-builder`.
|
||||
pub struct WasmBuilderSelectSource(PathBuf);
|
||||
|
||||
impl WasmBuilderSelectSource {
|
||||
/// Use the given `path` as source for `wasm-builder`.
|
||||
///
|
||||
/// The `path` must be relative and point to the directory that contains the `Cargo.toml` for
|
||||
/// `wasm-builder`.
|
||||
pub fn with_wasm_builder_from_path(self, path: &'static str) -> WasmBuilder {
|
||||
WasmBuilder {
|
||||
source: WasmBuilderSource::Path(path),
|
||||
rust_flags: Vec::new(),
|
||||
file_name: None,
|
||||
project_cargo_toml: self.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Use the given `repo` and `rev` as source for `wasm-builder`.
|
||||
pub fn with_wasm_builder_from_git(self, repo: &'static str, rev: &'static str) -> WasmBuilder {
|
||||
WasmBuilder {
|
||||
source: WasmBuilderSource::Git { repo, rev },
|
||||
rust_flags: Vec::new(),
|
||||
file_name: None,
|
||||
project_cargo_toml: self.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Use the given `version` to fetch `wasm-builder` source from crates.io.
|
||||
pub fn with_wasm_builder_from_crates(self, version: &'static str) -> WasmBuilder {
|
||||
WasmBuilder {
|
||||
source: WasmBuilderSource::Crates(version),
|
||||
rust_flags: Vec::new(),
|
||||
file_name: None,
|
||||
project_cargo_toml: self.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Use the given `version` to fetch `wasm-builder` source from crates.io or use
|
||||
/// the given `path` as source.
|
||||
///
|
||||
/// The `path` must be relative and point to the directory that contains the `Cargo.toml` for
|
||||
/// `wasm-builder`.
|
||||
pub fn with_wasm_builder_from_crates_or_path(
|
||||
self,
|
||||
version: &'static str,
|
||||
path: &'static str,
|
||||
) -> WasmBuilder {
|
||||
WasmBuilder {
|
||||
source: WasmBuilderSource::CratesOrPath { version, path },
|
||||
rust_flags: Vec::new(),
|
||||
file_name: None,
|
||||
project_cargo_toml: self.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Use the given `source` as source for `wasm-builder`.
|
||||
pub fn with_wasm_builder_source(self, source: WasmBuilderSource) -> WasmBuilder {
|
||||
WasmBuilder {
|
||||
source,
|
||||
rust_flags: Vec::new(),
|
||||
file_name: None,
|
||||
project_cargo_toml: self.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The builder for building a wasm binary.
|
||||
///
|
||||
/// The builder itself is seperated into multiple structs to make the setup type safe.
|
||||
///
|
||||
/// Building a wasm binary:
|
||||
///
|
||||
/// 1. Call [`WasmBuilder::new`] to create a new builder.
|
||||
/// 2. Select the project to build using the methods of [`WasmBuilderSelectProject`].
|
||||
/// 3. Select the source of the `wasm-builder` crate using the methods of
|
||||
/// [`WasmBuilderSelectSource`].
|
||||
/// 4. Set additional `RUST_FLAGS` or a different name for the file containing the WASM code
|
||||
/// using methods of [`Self`].
|
||||
/// 5. Build the WASM binary using [`Self::build`].
|
||||
pub struct WasmBuilder {
|
||||
/// Where should we pull the `wasm-builder` crate from.
|
||||
source: WasmBuilderSource,
|
||||
/// Flags that should be appended to `RUST_FLAGS` env variable.
|
||||
rust_flags: Vec<String>,
|
||||
/// The name of the file that is being generated in `OUT_DIR`.
|
||||
///
|
||||
/// Defaults to `wasm_binary.rs`.
|
||||
file_name: Option<String>,
|
||||
/// The path to the `Cargo.toml` of the project that should be build
|
||||
/// for wasm.
|
||||
project_cargo_toml: PathBuf,
|
||||
}
|
||||
|
||||
impl WasmBuilder {
|
||||
/// Create a new instance of the builder.
|
||||
pub fn new() -> WasmBuilderSelectProject {
|
||||
WasmBuilderSelectProject {
|
||||
_ignore: (),
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable exporting `__heap_base` as global variable in the WASM binary.
|
||||
///
|
||||
/// This adds `-Clink-arg=--export=__heap_base` to `RUST_FLAGS`.
|
||||
pub fn export_heap_base(mut self) -> Self {
|
||||
self.rust_flags.push("-Clink-arg=--export=__heap_base".into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the name of the file that will be generated in `OUT_DIR`.
|
||||
///
|
||||
/// This file needs to be included to get access to the build WASM binary.
|
||||
///
|
||||
/// If this function is not called, `file_name` defaults to `wasm_binary.rs`
|
||||
pub fn set_file_name(mut self, file_name: impl Into<String>) -> Self {
|
||||
self.file_name = Some(file_name.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Instruct the linker to import the memory into the WASM binary.
|
||||
///
|
||||
/// This adds `-C link-arg=--import-memory` to `RUST_FLAGS`.
|
||||
pub fn import_memory(mut self) -> Self {
|
||||
self.rust_flags.push("-C link-arg=--import-memory".into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Append the given `flag` to `RUST_FLAGS`.
|
||||
///
|
||||
/// `flag` is appended as is, so it needs to be a valid flag.
|
||||
pub fn append_to_rust_flags(mut self, flag: impl Into<String>) -> Self {
|
||||
self.rust_flags.push(flag.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Build the WASM binary.
|
||||
pub fn build(self) {
|
||||
if check_skip_build() {
|
||||
// If we skip the build, we still want to make sure to be called when an env variable
|
||||
// changes
|
||||
generate_rerun_if_changed_instructions();
|
||||
return;
|
||||
}
|
||||
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
|
||||
let file_path = out_dir.join(self.file_name.unwrap_or_else(|| "wasm_binary.rs".into()));
|
||||
|
||||
// Hash the path to the project cargo toml.
|
||||
let mut hasher = DefaultHasher::new();
|
||||
self.project_cargo_toml.hash(&mut hasher);
|
||||
|
||||
let project_name = env::var("CARGO_PKG_NAME").expect("`CARGO_PKG_NAME` is set by cargo!");
|
||||
// Make sure the `wasm-builder-runner` path is unique by concatenating the name of the
|
||||
// project that is compiling the WASM binary with the hash of the path to the project that
|
||||
// should be compiled as WASM binary.
|
||||
let project_folder = get_workspace_root()
|
||||
.join(format!("{}{}", project_name, hasher.finish()));
|
||||
|
||||
if check_provide_dummy_wasm_binary() {
|
||||
provide_dummy_wasm_binary(&file_path);
|
||||
} else {
|
||||
create_project(
|
||||
&project_folder,
|
||||
&file_path,
|
||||
self.source,
|
||||
&self.project_cargo_toml,
|
||||
&self.rust_flags.into_iter().map(|f| format!("{} ", f)).collect::<String>(),
|
||||
);
|
||||
run_project(&project_folder);
|
||||
}
|
||||
|
||||
// As last step we need to generate our `rerun-if-changed` stuff. If a build fails, we don't
|
||||
// want to spam the output!
|
||||
generate_rerun_if_changed_instructions();
|
||||
}
|
||||
}
|
||||
|
||||
/// The `wasm-builder` dependency source.
|
||||
pub enum WasmBuilderSource {
|
||||
/// The relative path to the source code from the current manifest dir.
|
||||
@@ -96,46 +318,21 @@ impl WasmBuilderSource {
|
||||
/// Build the currently built project as WASM binary and extend `RUSTFLAGS` with the given rustflags.
|
||||
///
|
||||
/// For more information, see [`build_current_project`].
|
||||
#[deprecated(
|
||||
since = "1.0.5",
|
||||
note = "Please switch to [`WasmBuilder`]",
|
||||
)]
|
||||
pub fn build_current_project_with_rustflags(
|
||||
file_name: &str,
|
||||
wasm_builder_source: WasmBuilderSource,
|
||||
default_rustflags: &str,
|
||||
default_rust_flags: &str,
|
||||
) {
|
||||
if check_skip_build() {
|
||||
// If we skip the build, we still want to make sure to be called when an env variable changes
|
||||
generate_rerun_if_changed_instructions();
|
||||
return;
|
||||
}
|
||||
|
||||
let manifest_dir = PathBuf::from(
|
||||
env::var("CARGO_MANIFEST_DIR").expect(
|
||||
"`CARGO_MANIFEST_DIR` is always set for `build.rs` files; qed"
|
||||
)
|
||||
);
|
||||
|
||||
let cargo_toml_path = manifest_dir.join("Cargo.toml");
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
|
||||
let file_path = out_dir.join(file_name);
|
||||
let project_name = env::var("CARGO_PKG_NAME").expect("`CARGO_PKG_NAME` is set by cargo!");
|
||||
let project_folder = get_workspace_root().join(project_name);
|
||||
|
||||
if check_provide_dummy_wasm_binary() {
|
||||
provide_dummy_wasm_binary(&file_path);
|
||||
} else {
|
||||
create_project(
|
||||
&project_folder,
|
||||
&file_path,
|
||||
&manifest_dir,
|
||||
wasm_builder_source,
|
||||
&cargo_toml_path,
|
||||
default_rustflags,
|
||||
);
|
||||
run_project(&project_folder);
|
||||
}
|
||||
|
||||
// As last step we need to generate our `rerun-if-changed` stuff. If a build fails, we don't
|
||||
// want to spam the output!
|
||||
generate_rerun_if_changed_instructions();
|
||||
WasmBuilder::new()
|
||||
.with_current_project()
|
||||
.with_wasm_builder_source(wasm_builder_source)
|
||||
.append_to_rust_flags(default_rust_flags)
|
||||
.set_file_name(file_name)
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Build the currently built project as WASM binary.
|
||||
@@ -145,7 +342,12 @@ pub fn build_current_project_with_rustflags(
|
||||
/// `file_name` - The name of the file being generated in the `OUT_DIR`. The file contains the
|
||||
/// constant `WASM_BINARY` which contains the build wasm binary.
|
||||
/// `wasm_builder_path` - Path to the wasm-builder project, relative to `CARGO_MANIFEST_DIR`.
|
||||
#[deprecated(
|
||||
since = "1.0.5",
|
||||
note = "Please switch to [`WasmBuilder`]",
|
||||
)]
|
||||
pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSource) {
|
||||
#[allow(deprecated)]
|
||||
build_current_project_with_rustflags(file_name, wasm_builder_source, "");
|
||||
}
|
||||
|
||||
@@ -171,7 +373,6 @@ fn get_workspace_root() -> PathBuf {
|
||||
fn create_project(
|
||||
project_folder: &Path,
|
||||
file_path: &Path,
|
||||
manifest_dir: &Path,
|
||||
wasm_builder_source: WasmBuilderSource,
|
||||
cargo_toml_path: &Path,
|
||||
default_rustflags: &str,
|
||||
@@ -193,7 +394,7 @@ fn create_project(
|
||||
|
||||
[workspace]
|
||||
"#,
|
||||
wasm_builder_source = wasm_builder_source.to_cargo_source(manifest_dir),
|
||||
wasm_builder_source = wasm_builder_source.to_cargo_source(&get_manifest_dir()),
|
||||
)
|
||||
).expect("WASM build runner `Cargo.toml` writing can not fail; qed");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user