From 063138463c8751717f3d056b02e7e2c84945dcd8 Mon Sep 17 00:00:00 2001 From: i1i1 Date: Tue, 5 Apr 2022 16:09:31 +0300 Subject: [PATCH 1/2] Fix subspace_wasm_tools --- crates/subspace-runtime/build.rs | 11 ------- crates/subspace-wasm-tools/src/lib.rs | 43 ++++++++++++++++++++++----- test/subspace-test-runtime/build.rs | 11 ------- 3 files changed, 36 insertions(+), 29 deletions(-) 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..a5db81a16f261 100644 --- a/crates/subspace-wasm-tools/src/lib.rs +++ b/crates/subspace-wasm-tools/src/lib.rs @@ -1,15 +1,38 @@ -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 = if let Ok(target_dir) = + env::var("CARGO_TARGET_DIR").or_else(|_| env::var("WASM_TARGET_DIRECTORY")) + { + // Propagate target directory to wasm build + target_dir.into() + } else { + let out_dir = env::var("OUT_DIR").expect("Always set by cargo"); + 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") + }; + 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 +42,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 +57,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() From 1763db8c43748c43b95783faa6db65f24d1a3f84 Mon Sep 17 00:00:00 2001 From: i1i1 Date: Wed, 6 Apr 2022 00:36:37 +0300 Subject: [PATCH 2/2] Update target directory finding control flow and add comments --- crates/subspace-wasm-tools/src/lib.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/subspace-wasm-tools/src/lib.rs b/crates/subspace-wasm-tools/src/lib.rs index a5db81a16f261..6812372e365f4 100644 --- a/crates/subspace-wasm-tools/src/lib.rs +++ b/crates/subspace-wasm-tools/src/lib.rs @@ -18,17 +18,19 @@ pub fn create_runtime_bundle_inclusion_file( bundle_const_name: &str, target_file_name: &str, ) { - let target_dir = if let Ok(target_dir) = - env::var("CARGO_TARGET_DIR").or_else(|_| env::var("WASM_TARGET_DIRECTORY")) - { - // Propagate target directory to wasm build - target_dir.into() - } else { - let out_dir = env::var("OUT_DIR").expect("Always set by cargo"); - 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") - }; + 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); }