From 6c99fa5a33fd6d4466eb3ef4064afa6ad4bd5c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Austin=20John=20Mayer=20=28=E4=BA=95=E4=B8=8A=20=E3=82=AA?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=83=86=E3=82=A3=E3=83=B3=29?= Date: Thu, 14 Sep 2023 16:14:45 +0900 Subject: [PATCH 1/5] add crate-type flag to cargo build commands --- src/build/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/build/mod.rs b/src/build/mod.rs index 691ec490..5f698b31 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -106,6 +106,8 @@ pub fn cargo_build_wasm( } } + add_crate_type(&mut cmd, extra_options); + cmd.arg("--target").arg("wasm32-unknown-unknown"); cmd.args(extra_options); child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; @@ -140,8 +142,46 @@ pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String] cmd.arg("--target").arg("wasm32-unknown-unknown"); + add_crate_type(&mut cmd, extra_options); + cmd.args(extra_options); child::run(cmd, "cargo build").context("Compilation of your program failed")?; Ok(()) } + +/// Adds --crate-type option to cargo build command, allowing users to build without specifying cdylib +/// in Cargo.toml +fn add_crate_type(cmd: &mut Command, extra_options: &[String]) { + // Avoid setting crate type flag twice if provided in extra options + if extra_options.iter().any(|opt| opt.contains("--crate-type")) { + return; + } + + cmd.arg("--crate-type cdylib"); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn adds_crate_type_flag_if_not_in_extra_opts() { + let mut cmd = Command::new("cargo"); + let extra_options = vec![String::from("--crate-type dylib")]; + + add_crate_type(&mut cmd, &extra_options); + + assert!(!cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type cdylib")); + } + + #[test] + fn does_not_add_crate_type_flag_if_in_extra_opts() { + let mut cmd = Command::new("cargo"); + let extra_options = vec![]; + + add_crate_type(&mut cmd, &extra_options); + + assert!(cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type cdylib")); + } +} From 94e0c08d06dcded90af12bb3900fb7a731c3f327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Austin=20John=20Mayer=20=28=E4=BA=95=E4=B8=8A=20=E3=82=AA?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=83=86=E3=82=A3=E3=83=B3=29?= Date: Thu, 14 Sep 2023 17:13:35 +0900 Subject: [PATCH 2/5] change build command to rustc and fix args usage --- src/build/mod.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/build/mod.rs b/src/build/mod.rs index 5f698b31..30a0963e 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -72,7 +72,7 @@ fn wasm_pack_local_version() -> Option { Some(output.to_string()) } -/// Run `cargo build` targetting `wasm32-unknown-unknown`. +/// Run `cargo rustc` targetting `wasm32-unknown-unknown`. pub fn cargo_build_wasm( path: &Path, profile: BuildProfile, @@ -82,7 +82,8 @@ pub fn cargo_build_wasm( PBAR.info(&msg); let mut cmd = Command::new("cargo"); - cmd.current_dir(path).arg("build").arg("--lib"); + // Use rustc to allow crate-type flag to be added + cmd.current_dir(path).arg("rustc").arg("--lib"); if PBAR.quiet() { cmd.arg("--quiet"); @@ -101,7 +102,7 @@ pub fn cargo_build_wasm( cmd.arg("--release"); } BuildProfile::Dev => { - // Plain cargo builds use the dev cargo profile, which includes + // cargo rustc, like cargo build, uses the dev cargo profile which includes // debug info by default. } } @@ -110,17 +111,18 @@ pub fn cargo_build_wasm( cmd.arg("--target").arg("wasm32-unknown-unknown"); cmd.args(extra_options); - child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; + dbg!(&cmd); + child::run(cmd, "cargo rustc").context("Compiling your crate to WebAssembly failed")?; Ok(()) } -/// Runs `cargo build --tests` targeting `wasm32-unknown-unknown`. +/// Runs `cargo rustc --tests` targeting `wasm32-unknown-unknown`. /// /// This generates the `Cargo.lock` file that we use in order to know which version of /// wasm-bindgen-cli to use when running tests. /// /// Note that the command to build tests and the command to run tests must use the same parameters, i.e. features to be -/// disabled / enabled must be consistent for both `cargo build` and `cargo test`. +/// disabled / enabled must be consistent for both `cargo rustc` and `cargo test`. /// /// # Parameters /// @@ -130,7 +132,7 @@ pub fn cargo_build_wasm( pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String]) -> Result<()> { let mut cmd = Command::new("cargo"); - cmd.current_dir(path).arg("build").arg("--tests"); + cmd.current_dir(path).arg("rustc").arg("--tests"); if PBAR.quiet() { cmd.arg("--quiet"); @@ -142,15 +144,15 @@ pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String] cmd.arg("--target").arg("wasm32-unknown-unknown"); - add_crate_type(&mut cmd, extra_options); + // add_crate_type(&mut cmd, extra_options); cmd.args(extra_options); - child::run(cmd, "cargo build").context("Compilation of your program failed")?; + child::run(cmd, "cargo rustc").context("Compilation of your program failed")?; Ok(()) } -/// Adds --crate-type option to cargo build command, allowing users to build without specifying cdylib +/// Adds --crate-type option to cargo rustc command, allowing users to build without specifying cdylib /// in Cargo.toml fn add_crate_type(cmd: &mut Command, extra_options: &[String]) { // Avoid setting crate type flag twice if provided in extra options @@ -158,7 +160,7 @@ fn add_crate_type(cmd: &mut Command, extra_options: &[String]) { return; } - cmd.arg("--crate-type cdylib"); + cmd.arg("--crate-type").arg("cdylib"); } #[cfg(test)] @@ -172,7 +174,8 @@ mod tests { add_crate_type(&mut cmd, &extra_options); - assert!(!cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type cdylib")); + assert!(!cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type")); + assert!(!cmd.get_args().any(|arg| arg.to_str().unwrap() == "cdylib")); } #[test] @@ -182,6 +185,7 @@ mod tests { add_crate_type(&mut cmd, &extra_options); - assert!(cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type cdylib")); + assert!(cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type")); + assert!(cmd.get_args().any(|arg| arg.to_str().unwrap() == "cdylib")); } } From f31405c101818fb84785efc57b664c9f3342be08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Austin=20John=20Mayer=20=28=E4=BA=95=E4=B8=8A=20=E3=82=AA?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=83=86=E3=82=A3=E3=83=B3=29?= Date: Thu, 14 Sep 2023 17:20:15 +0900 Subject: [PATCH 3/5] remove cdylib from tests --- src/build/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/build/mod.rs b/src/build/mod.rs index 30a0963e..edaf6c79 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -116,7 +116,7 @@ pub fn cargo_build_wasm( Ok(()) } -/// Runs `cargo rustc --tests` targeting `wasm32-unknown-unknown`. +/// Runs `cargo build --tests` targeting `wasm32-unknown-unknown`. /// /// This generates the `Cargo.lock` file that we use in order to know which version of /// wasm-bindgen-cli to use when running tests. @@ -132,7 +132,7 @@ pub fn cargo_build_wasm( pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String]) -> Result<()> { let mut cmd = Command::new("cargo"); - cmd.current_dir(path).arg("rustc").arg("--tests"); + cmd.current_dir(path).arg("build").arg("--tests"); if PBAR.quiet() { cmd.arg("--quiet"); @@ -144,8 +144,6 @@ pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String] cmd.arg("--target").arg("wasm32-unknown-unknown"); - // add_crate_type(&mut cmd, extra_options); - cmd.args(extra_options); child::run(cmd, "cargo rustc").context("Compilation of your program failed")?; From 28a1f6afe4502de03daa0a61aeb6b2da4e1c3999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Austin=20John=20Mayer=20=28=E4=BA=95=E4=B8=8A=20=E3=82=AA?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=83=86=E3=82=A3=E3=83=B3=29?= Date: Thu, 14 Sep 2023 17:27:07 +0900 Subject: [PATCH 4/5] remove dbg --- src/build/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build/mod.rs b/src/build/mod.rs index edaf6c79..31ed20fc 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -111,7 +111,7 @@ pub fn cargo_build_wasm( cmd.arg("--target").arg("wasm32-unknown-unknown"); cmd.args(extra_options); - dbg!(&cmd); + child::run(cmd, "cargo rustc").context("Compiling your crate to WebAssembly failed")?; Ok(()) } From f22982b49260ae7e74deb42ddfc47822b393cb3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Austin=20John=20Mayer=20=28=E4=BA=95=E4=B8=8A=20=E3=82=AA?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=83=86=E3=82=A3=E3=83=B3=29?= Date: Thu, 14 Sep 2023 17:28:40 +0900 Subject: [PATCH 5/5] apply formatting --- src/build/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/build/mod.rs b/src/build/mod.rs index 31ed20fc..6e5f1eba 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -172,7 +172,9 @@ mod tests { add_crate_type(&mut cmd, &extra_options); - assert!(!cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type")); + assert!(!cmd + .get_args() + .any(|arg| arg.to_str().unwrap() == "--crate-type")); assert!(!cmd.get_args().any(|arg| arg.to_str().unwrap() == "cdylib")); } @@ -183,7 +185,9 @@ mod tests { add_crate_type(&mut cmd, &extra_options); - assert!(cmd.get_args().any(|arg| arg.to_str().unwrap() == "--crate-type")); + assert!(cmd + .get_args() + .any(|arg| arg.to_str().unwrap() == "--crate-type")); assert!(cmd.get_args().any(|arg| arg.to_str().unwrap() == "cdylib")); } }