diff --git a/substrate/utils/wasm-builder/src/builder.rs b/substrate/utils/wasm-builder/src/builder.rs index 51780b3540d9b..126c60bacedc3 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, 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: Vec, 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..70ade335f5b51 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: &[String], default_rustflags: &str, cargo_cmd: CargoCommandVersioned, features_to_enable: Vec, @@ -149,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, @@ -160,6 +162,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 +172,7 @@ pub(crate) fn create_and_compile( target, &build_config.blob_build_profile, &project, + default_cargo_flags, default_rustflags, cargo_cmd, None, @@ -182,6 +186,7 @@ pub(crate) fn create_and_compile( target, &build_config.blob_build_profile, &project, + default_cargo_flags, default_rustflags, cargo_cmd, ) @@ -827,6 +832,7 @@ fn build_bloaty_blob( target: RuntimeTarget, blob_build_profile: &Profile, project: &Path, + default_cargo_flags: &[String], default_rustflags: &str, cargo_cmd: CargoCommandVersioned, #[cfg(feature = "metadata-hash")] metadata_hash: Option<[u8; 32]>, @@ -868,6 +874,7 @@ fn build_bloaty_blob( .arg("rustc") .arg(format!("--target={}", target.rustc_target(&cargo_cmd))) .arg(format!("--manifest-path={}", manifest_path.display())) + .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