diff --git a/crates/subspace-runtime/build.rs b/crates/subspace-runtime/build.rs
index a4dd21d5aeef1..6fe85277e4f4a 100644
--- a/crates/subspace-runtime/build.rs
+++ b/crates/subspace-runtime/build.rs
@@ -14,26 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-use std::env;
use substrate_wasm_builder::WasmBuilder;
fn main() {
- let relative_target_dir = env!("CARGO_MANIFEST_DIR").to_owned() + "/../../target";
- let target_dir = option_env!("CARGO_TARGET_DIR")
- .or(option_env!("WASM_TARGET_DIRECTORY"))
- .unwrap_or(&relative_target_dir);
-
subspace_wasm_tools::create_runtime_bundle_inclusion_file(
- target_dir,
"parachain-template-runtime",
"EXECUTION_WASM_BUNDLE",
"execution_wasm_bundle.rs",
);
- if env::var("WASM_TARGET_DIRECTORY").is_err() {
- env::set_var("WASM_TARGET_DIRECTORY", target_dir);
- }
-
WasmBuilder::new()
.with_current_project()
.export_heap_base()
diff --git a/crates/subspace-wasm-tools/src/lib.rs b/crates/subspace-wasm-tools/src/lib.rs
index 4909545ea99d8..6812372e365f4 100644
--- a/crates/subspace-wasm-tools/src/lib.rs
+++ b/crates/subspace-wasm-tools/src/lib.rs
@@ -1,15 +1,40 @@
-use std::{env, fs};
+use std::{env, fs, path::PathBuf};
+
+fn get_relative_target(dir: PathBuf) -> Option {
+ if dir.join("Cargo.lock").is_file() && dir.join("target").is_dir() {
+ return Some(dir.join("target"));
+ }
+ dir.parent()
+ .map(ToOwned::to_owned)
+ .and_then(get_relative_target)
+}
/// Creates a `target_file_name` in `OUT_DIR` that will contain constant `bundle_const_name` with
/// runtime of `runtime_crate_name` stored in it.
///
/// Must be called before Substrate's WASM builder.
pub fn create_runtime_bundle_inclusion_file(
- target_dir: &str,
runtime_crate_name: &str,
bundle_const_name: &str,
target_file_name: &str,
) {
+ let target_dir = env::var("CARGO_TARGET_DIR")
+ .or_else(|_| env::var("WASM_TARGET_DIRECTORY"))
+ .map(Into::into)
+ .unwrap_or_else(|| {
+ let out_dir = env::var("OUT_DIR").expect("Always set by cargo");
+ // Call get `get_relative_target` twice if possible, as wasm target directory is
+ // contained in host target directory, and we actually need host one.
+ get_relative_target(out_dir.into())
+ .map(|target_dir| get_relative_target(target_dir.clone()).unwrap_or(target_dir))
+ .expect("Out dir is always inside the target directory")
+ });
+
+ // Propagate target directory to wasm build
+ if env::var("WASM_TARGET_DIRECTORY").is_err() {
+ env::set_var("WASM_TARGET_DIRECTORY", &target_dir);
+ }
+
// Make correct profile accessible in child processes.
env::set_var(
"WBUILD_PROFILE",
@@ -19,10 +44,14 @@ pub fn create_runtime_bundle_inclusion_file(
// Create a file that will include execution runtime into consensus runtime
let profile = env::var("WBUILD_PROFILE").expect("Set above; qed");
- let execution_wasm_bundle_path = format!(
- "{target_dir}/{profile}/wbuild/{runtime_crate_name}/{}.compact.wasm",
- runtime_crate_name.replace('-', "_")
- );
+ let execution_wasm_bundle_path = target_dir
+ .join(profile)
+ .join("wbuild")
+ .join(runtime_crate_name)
+ .join(format!(
+ "{}.compact.wasm",
+ runtime_crate_name.replace('-', "_")
+ ));
let execution_wasm_bundle_rs_path =
env::var("OUT_DIR").expect("Set by cargo; qed") + "/" + target_file_name;
@@ -30,7 +59,9 @@ pub fn create_runtime_bundle_inclusion_file(
r#"
pub const {bundle_const_name}: &[u8] = include_bytes!("{}");
"#,
- execution_wasm_bundle_path.escape_default()
+ execution_wasm_bundle_path
+ .to_string_lossy()
+ .escape_default()
);
fs::write(
diff --git a/test/subspace-test-runtime/build.rs b/test/subspace-test-runtime/build.rs
index a1c00c570696b..6c7ae9f8e7284 100644
--- a/test/subspace-test-runtime/build.rs
+++ b/test/subspace-test-runtime/build.rs
@@ -14,26 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-use std::env;
use substrate_wasm_builder::WasmBuilder;
fn main() {
- let relative_target_dir = env!("CARGO_MANIFEST_DIR").to_owned() + "/../../target";
- let target_dir = option_env!("CARGO_TARGET_DIR")
- .or(option_env!("WASM_TARGET_DIRECTORY"))
- .unwrap_or(&relative_target_dir);
-
subspace_wasm_tools::create_runtime_bundle_inclusion_file(
- target_dir,
"cirrus-test-runtime",
"EXECUTION_WASM_BUNDLE",
"execution_wasm_bundle.rs",
);
- if env::var("WASM_TARGET_DIRECTORY").is_err() {
- env::set_var("WASM_TARGET_DIRECTORY", target_dir);
- }
-
WasmBuilder::new()
.with_current_project()
.export_heap_base()