Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rustc --crate-type for smoother multi-target compilation in build stage #1329

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn wasm_pack_local_version() -> Option<String> {
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,
Expand All @@ -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");
Expand All @@ -101,14 +102,17 @@ 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.
}
}

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")?;

child::run(cmd, "cargo rustc").context("Compiling your crate to WebAssembly failed")?;
Ok(())
}

Expand All @@ -118,7 +122,7 @@ pub fn cargo_build_wasm(
/// 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
///
Expand All @@ -142,6 +146,48 @@ pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String]

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 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
if extra_options.iter().any(|opt| opt.contains("--crate-type")) {
return;
}

cmd.arg("--crate-type").arg("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"));
assert!(!cmd.get_args().any(|arg| arg.to_str().unwrap() == "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"));
assert!(cmd.get_args().any(|arg| arg.to_str().unwrap() == "cdylib"));
}
}
Loading