Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Fix subspace_wasm_tools for builds outside of subspace workspace (p…
Browse files Browse the repository at this point in the history
…aritytech#325)

* Fix subspace_wasm_tools

* Update target directory finding control flow and add comments
  • Loading branch information
nazar-pc authored Apr 5, 2022
2 parents 39973cd + 1763db8 commit 1ac638f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
11 changes: 0 additions & 11 deletions crates/subspace-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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()
Expand Down
45 changes: 38 additions & 7 deletions crates/subspace-wasm-tools/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
use std::{env, fs};
use std::{env, fs, path::PathBuf};

fn get_relative_target(dir: PathBuf) -> Option<PathBuf> {
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",
Expand All @@ -19,18 +44,24 @@ 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;
let execution_wasm_bundle_rs_contents = format!(
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(
Expand Down
11 changes: 0 additions & 11 deletions test/subspace-test-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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()
Expand Down

0 comments on commit 1ac638f

Please sign in to comment.