From e2a49ba54c49252d4c328a3a3d757d037af31238 Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Tue, 11 Nov 2025 16:36:36 +0300 Subject: [PATCH 1/3] Add function to add cargo flags in substrate-wasm-builder --- substrate/utils/wasm-builder/src/builder.rs | 15 +++++++++++++++ substrate/utils/wasm-builder/src/wasm_project.rs | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/substrate/utils/wasm-builder/src/builder.rs b/substrate/utils/wasm-builder/src/builder.rs index 51780b3540d9b..81236053eac7f 100644 --- a/substrate/utils/wasm-builder/src/builder.rs +++ b/substrate/utils/wasm-builder/src/builder.rs @@ -53,6 +53,7 @@ impl WasmBuilderSelectProject { /// is always set by `Cargo` in `build.rs` files. pub fn with_current_project(self) -> WasmBuilder { WasmBuilder { + cargo_flags: Vec::new(), rust_flags: Vec::new(), file_name: None, project_cargo_toml: get_manifest_dir().join("Cargo.toml"), @@ -73,6 +74,7 @@ impl WasmBuilderSelectProject { if path.ends_with("Cargo.toml") && path.exists() { Ok(WasmBuilder { + cargo_flags: Vec::new(), rust_flags: Vec::new(), file_name: None, project_cargo_toml: path, @@ -101,6 +103,8 @@ impl WasmBuilderSelectProject { /// methods of [`WasmBuilder`]. /// 4. Build the WASM binary using [`Self::build`]. pub struct WasmBuilder { + /// Flags that should be appended to `cargo rustc` invocation. + cargo_flags: Vec, /// Flags that should be appended to `RUST_FLAGS` env variable. rust_flags: Vec, /// The name of the file that is being generated in `OUT_DIR`. @@ -190,6 +194,14 @@ impl WasmBuilder { self } + /// Append the given `flag` to `cargo rustc` invocation. + /// + /// `flag` is appended as is, so it needs to be a valid flag. + pub fn append_to_cargo_flags(mut self, flag: impl Into) -> Self { + self.cargo_flags.push(flag.into()); + self + } + /// Append the given `flag` to `RUST_FLAGS`. /// /// `flag` is appended as is, so it needs to be a valid flag. @@ -264,6 +276,7 @@ impl WasmBuilder { target, file_path, self.project_cargo_toml, + self.cargo_flags.join(" "), self.rust_flags.join(" "), self.features_to_enable, self.file_name, @@ -340,6 +353,7 @@ fn build_project( target: RuntimeTarget, file_name: PathBuf, project_cargo_toml: PathBuf, + default_cargo_flags: String, default_rustflags: String, features_to_enable: Vec, wasm_binary_name: Option, @@ -359,6 +373,7 @@ fn build_project( let (wasm_binary, bloaty) = crate::wasm_project::create_and_compile( target, &project_cargo_toml, + &default_cargo_flags, &default_rustflags, cargo_cmd, features_to_enable, diff --git a/substrate/utils/wasm-builder/src/wasm_project.rs b/substrate/utils/wasm-builder/src/wasm_project.rs index 9cce8b5ba5bb4..93b384331816a 100644 --- a/substrate/utils/wasm-builder/src/wasm_project.rs +++ b/substrate/utils/wasm-builder/src/wasm_project.rs @@ -117,6 +117,7 @@ fn crate_metadata(cargo_manifest: &Path) -> Metadata { pub(crate) fn create_and_compile( target: RuntimeTarget, orig_project_cargo_toml: &Path, + default_cargo_flags: &str, default_rustflags: &str, cargo_cmd: CargoCommandVersioned, features_to_enable: Vec, @@ -160,6 +161,7 @@ pub(crate) fn create_and_compile( target, &build_config.blob_build_profile, &project, + default_cargo_flags, default_rustflags, cargo_cmd, Some(hash), @@ -169,6 +171,7 @@ pub(crate) fn create_and_compile( target, &build_config.blob_build_profile, &project, + default_cargo_flags, default_rustflags, cargo_cmd, None, @@ -182,6 +185,7 @@ pub(crate) fn create_and_compile( target, &build_config.blob_build_profile, &project, + default_cargo_flags, default_rustflags, cargo_cmd, ) @@ -827,6 +831,7 @@ fn build_bloaty_blob( target: RuntimeTarget, blob_build_profile: &Profile, project: &Path, + default_cargo_flags: &str, default_rustflags: &str, cargo_cmd: CargoCommandVersioned, #[cfg(feature = "metadata-hash")] metadata_hash: Option<[u8; 32]>, @@ -868,6 +873,7 @@ fn build_bloaty_blob( .arg("rustc") .arg(format!("--target={}", target.rustc_target(&cargo_cmd))) .arg(format!("--manifest-path={}", manifest_path.display())) + .arg(default_cargo_flags) .env("RUSTFLAGS", rustflags) // Manually set the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir // exclusive). The runner project is created in `CARGO_TARGET_DIR` and executing it will From 0fa05989a48a8275a79d2cb64814582d09b67c48 Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Tue, 11 Nov 2025 19:54:46 +0300 Subject: [PATCH 2/3] Add missing argument --- substrate/utils/wasm-builder/src/wasm_project.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/utils/wasm-builder/src/wasm_project.rs b/substrate/utils/wasm-builder/src/wasm_project.rs index 93b384331816a..8047a61ae47df 100644 --- a/substrate/utils/wasm-builder/src/wasm_project.rs +++ b/substrate/utils/wasm-builder/src/wasm_project.rs @@ -150,6 +150,7 @@ pub(crate) fn create_and_compile( target, &build_config.blob_build_profile, &project, + default_cargo_flags, default_rustflags, cargo_cmd.clone(), None, From 5cda226cf107e4bd0137a07980f40e3bc59af624 Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Thu, 4 Dec 2025 03:08:59 +0300 Subject: [PATCH 3/3] Don't join cargo flags --- substrate/utils/wasm-builder/src/builder.rs | 4 ++-- substrate/utils/wasm-builder/src/wasm_project.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/substrate/utils/wasm-builder/src/builder.rs b/substrate/utils/wasm-builder/src/builder.rs index 81236053eac7f..126c60bacedc3 100644 --- a/substrate/utils/wasm-builder/src/builder.rs +++ b/substrate/utils/wasm-builder/src/builder.rs @@ -276,7 +276,7 @@ impl WasmBuilder { target, file_path, self.project_cargo_toml, - self.cargo_flags.join(" "), + self.cargo_flags, self.rust_flags.join(" "), self.features_to_enable, self.file_name, @@ -353,7 +353,7 @@ fn build_project( target: RuntimeTarget, file_name: PathBuf, project_cargo_toml: PathBuf, - default_cargo_flags: String, + default_cargo_flags: Vec, default_rustflags: String, features_to_enable: Vec, wasm_binary_name: Option, diff --git a/substrate/utils/wasm-builder/src/wasm_project.rs b/substrate/utils/wasm-builder/src/wasm_project.rs index 8047a61ae47df..70ade335f5b51 100644 --- a/substrate/utils/wasm-builder/src/wasm_project.rs +++ b/substrate/utils/wasm-builder/src/wasm_project.rs @@ -117,7 +117,7 @@ fn crate_metadata(cargo_manifest: &Path) -> Metadata { pub(crate) fn create_and_compile( target: RuntimeTarget, orig_project_cargo_toml: &Path, - default_cargo_flags: &str, + default_cargo_flags: &[String], default_rustflags: &str, cargo_cmd: CargoCommandVersioned, features_to_enable: Vec, @@ -832,7 +832,7 @@ fn build_bloaty_blob( target: RuntimeTarget, blob_build_profile: &Profile, project: &Path, - default_cargo_flags: &str, + default_cargo_flags: &[String], default_rustflags: &str, cargo_cmd: CargoCommandVersioned, #[cfg(feature = "metadata-hash")] metadata_hash: Option<[u8; 32]>, @@ -874,7 +874,7 @@ fn build_bloaty_blob( .arg("rustc") .arg(format!("--target={}", target.rustc_target(&cargo_cmd))) .arg(format!("--manifest-path={}", manifest_path.display())) - .arg(default_cargo_flags) + .args(default_cargo_flags) .env("RUSTFLAGS", rustflags) // Manually set the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir // exclusive). The runner project is created in `CARGO_TARGET_DIR` and executing it will