diff --git a/Cargo.lock b/Cargo.lock index 13e6b716e662..90415606bd35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21520,6 +21520,7 @@ dependencies = [ "console", "filetime", "frame-metadata", + "jobserver", "merkleized-metadata", "parity-scale-codec", "parity-wasm", diff --git a/Cargo.toml b/Cargo.toml index 1699bd97a8ee..9b14dfd3cdfb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -803,6 +803,7 @@ is-terminal = { version = "0.4.9" } is_executable = { version = "1.0.1" } isahc = { version = "1.2" } itertools = { version = "0.11" } +jobserver = { version = "0.1.26" } jsonpath_lib = { version = "0.3" } jsonrpsee = { version = "0.23.2" } jsonrpsee-core = { version = "0.23.2" } diff --git a/substrate/utils/wasm-builder/Cargo.toml b/substrate/utils/wasm-builder/Cargo.toml index f084400c12e8..28fdebf209bd 100644 --- a/substrate/utils/wasm-builder/Cargo.toml +++ b/substrate/utils/wasm-builder/Cargo.toml @@ -27,6 +27,7 @@ filetime = { workspace = true } wasm-opt = { workspace = true } parity-wasm = { workspace = true } polkavm-linker = { workspace = true } +jobserver = { workspace = true } # Dependencies required for the `metadata-hash` feature. merkleized-metadata = { optional = true, workspace = true } diff --git a/substrate/utils/wasm-builder/src/builder.rs b/substrate/utils/wasm-builder/src/builder.rs index 37c6c4aa7431..eb761a103d62 100644 --- a/substrate/utils/wasm-builder/src/builder.rs +++ b/substrate/utils/wasm-builder/src/builder.rs @@ -346,6 +346,8 @@ fn build_project( check_for_runtime_version_section: bool, #[cfg(feature = "metadata-hash")] enable_metadata_hash: Option, ) { + // Init jobserver as soon as possible + crate::wasm_project::get_jobserver(); let cargo_cmd = match crate::prerequisites::check(target) { Ok(cmd) => cmd, Err(err_msg) => { diff --git a/substrate/utils/wasm-builder/src/wasm_project.rs b/substrate/utils/wasm-builder/src/wasm_project.rs index 20fa9fc1aa33..63887389fb1e 100644 --- a/substrate/utils/wasm-builder/src/wasm_project.rs +++ b/substrate/utils/wasm-builder/src/wasm_project.rs @@ -31,6 +31,7 @@ use std::{ ops::Deref, path::{Path, PathBuf}, process, + sync::OnceLock, }; use strum::{EnumIter, IntoEnumIterator}; use toml::value::Table; @@ -899,6 +900,12 @@ fn build_bloaty_blob( } } + // Inherit jobserver in child cargo command to ensure we don't try to use more concurrency than + // available + if let Some(c) = get_jobserver() { + c.configure(&mut build_cmd); + } + println!("{}", colorize_info_message("Information that should be included in a bug report.")); println!("{} {:?}", colorize_info_message("Executing build command:"), build_cmd); println!("{} {}", colorize_info_message("Using rustc version:"), cargo_cmd.rustc_version()); @@ -1178,3 +1185,13 @@ fn copy_blob_to_target_directory(cargo_manifest: &Path, blob_binary: &WasmBinary ) .expect("Copies blob binary to `WASM_TARGET_DIRECTORY`."); } + +// Get jobserver from parent cargo command +pub fn get_jobserver() -> &'static Option { + static JOBSERVER: OnceLock> = OnceLock::new(); + + JOBSERVER.get_or_init(|| { + // Unsafe because it deals with raw fds + unsafe { jobserver::Client::from_env() } + }) +}