Skip to content

Commit

Permalink
Merge pull request #1428 from rafaelbeckel/master
Browse files Browse the repository at this point in the history
feat: Implements custom build profile (closes #1111)
  • Loading branch information
drager authored Oct 30, 2024
2 parents 0227505 + ad86c68 commit 513feba
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ pub fn wasm_bindgen_build(
profile: BuildProfile,
extra_options: &Vec<String>,
) -> Result<()> {
let release_or_debug = match profile {
let profile_name = match profile.clone() {
BuildProfile::Release | BuildProfile::Profiling => "release",
BuildProfile::Dev => "debug",
BuildProfile::Custom(profile_name) => &profile_name.clone(),
};

let out_dir = out_dir.to_str().unwrap();
Expand All @@ -41,7 +42,7 @@ pub fn wasm_bindgen_build(

let wasm_path = target_directory
.join("wasm32-unknown-unknown")
.join(release_or_debug)
.join(profile_name)
.join(data.crate_name())
.with_extension("wasm");

Expand Down
3 changes: 3 additions & 0 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub fn cargo_build_wasm(
// Plain cargo builds use the dev cargo profile, which includes
// debug info by default.
}
BuildProfile::Custom(arg) => {
cmd.arg("--profile").arg(arg);
}
}

cmd.arg("--target").arg("wasm32-unknown-unknown");
Expand Down
31 changes: 22 additions & 9 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ impl FromStr for Target {

/// The build profile controls whether optimizations, debug info, and assertions
/// are enabled or disabled.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub enum BuildProfile {
/// Enable assertions and debug info. Disable optimizations.
Dev,
/// Enable optimizations. Disable assertions and debug info.
Release,
/// Enable optimizations and debug info. Disable assertions.
Profiling,
/// User-defined profile with --profile flag
Custom(String),
}

/// Everything required to configure and run the `wasm-pack build` command.
Expand Down Expand Up @@ -160,6 +162,10 @@ pub struct BuildOptions {
/// Create a profiling build. Enable optimizations and debug info.
pub profiling: bool,

#[clap(long = "profile")]
/// User-defined profile with --profile flag
pub profile: Option<String>,

#[clap(long = "out-dir", short = 'd', default_value = "pkg")]
/// Sets the output directory with a relative path.
pub out_dir: String,
Expand Down Expand Up @@ -196,6 +202,7 @@ impl Default for BuildOptions {
no_opt: false,
release: false,
profiling: false,
profile: None,
out_dir: String::new(),
out_name: None,
extra_options: Vec::new(),
Expand All @@ -221,13 +228,19 @@ impl Build {
let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir)).clean();

let dev = build_opts.dev || build_opts.debug;
let profile = match (dev, build_opts.release, build_opts.profiling) {
(false, false, false) | (false, true, false) => BuildProfile::Release,
(true, false, false) => BuildProfile::Dev,
(false, false, true) => BuildProfile::Profiling,
let profile = match (
dev,
build_opts.release,
build_opts.profiling,
build_opts.profile,
) {
(false, false, false, None) | (false, true, false, None) => BuildProfile::Release,
(true, false, false, None) => BuildProfile::Dev,
(false, false, true, None) => BuildProfile::Profiling,
(false, false, false, Some(profile)) => BuildProfile::Custom(profile),
// Unfortunately, `clap` doesn't expose clap's `conflicts_with`
// functionality yet, so we have to implement it ourselves.
_ => bail!("Can only supply one of the --dev, --release, or --profiling flags"),
_ => bail!("Can only supply one of the --dev, --release, --profiling, or --profile 'name' flags"),
};

Ok(Build {
Expand Down Expand Up @@ -355,7 +368,7 @@ impl Build {

fn step_build_wasm(&mut self) -> Result<()> {
info!("Building wasm...");
build::cargo_build_wasm(&self.crate_path, self.profile, &self.extra_options)?;
build::cargo_build_wasm(&self.crate_path, self.profile.clone(), &self.extra_options)?;

info!(
"wasm built at {:#?}.",
Expand Down Expand Up @@ -430,7 +443,7 @@ impl Build {
self.weak_refs,
self.reference_types,
self.target,
self.profile,
self.profile.clone(),
&self.extra_options,
)?;
info!("wasm bindings were built at {:#?}.", &self.out_dir);
Expand All @@ -440,7 +453,7 @@ impl Build {
fn step_run_wasm_opt(&mut self) -> Result<()> {
let mut args = match self
.crate_data
.configured_profile(self.profile)
.configured_profile(self.profile.clone())
.wasm_opt_args()
{
Some(args) => args,
Expand Down
29 changes: 29 additions & 0 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ struct CargoWasmPackProfiles {
deserialize_with = "CargoWasmPackProfile::deserialize_profiling"
)]
profiling: CargoWasmPackProfile,

#[serde(
default = "CargoWasmPackProfile::default_custom",
deserialize_with = "CargoWasmPackProfile::deserialize_custom"
)]
custom: CargoWasmPackProfile,
}

impl Default for CargoWasmPackProfiles {
Expand All @@ -93,6 +99,7 @@ impl Default for CargoWasmPackProfiles {
dev: CargoWasmPackProfile::default_dev(),
release: CargoWasmPackProfile::default_release(),
profiling: CargoWasmPackProfile::default_profiling(),
custom: CargoWasmPackProfile::default_custom(),
}
}
}
Expand Down Expand Up @@ -305,6 +312,18 @@ impl CargoWasmPackProfile {
}
}

fn default_custom() -> Self {
CargoWasmPackProfile {
wasm_bindgen: CargoWasmPackProfileWasmBindgen {
debug_js_glue: Some(false),
demangle_name_section: Some(true),
dwarf_debug_info: Some(false),
omit_default_module_path: Some(false),
},
wasm_opt: Some(CargoWasmPackProfileWasmOpt::Enabled(true)),
}
}

fn deserialize_dev<'de, D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down Expand Up @@ -332,6 +351,15 @@ impl CargoWasmPackProfile {
Ok(profile)
}

fn deserialize_custom<'de, D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(&Self::default_custom());
Ok(profile)
}

fn update_with_defaults(&mut self, defaults: &Self) {
macro_rules! d {
( $( $path:ident ).* ) => {
Expand Down Expand Up @@ -490,6 +518,7 @@ impl CrateData {
BuildProfile::Dev => &self.manifest.package.metadata.wasm_pack.profile.dev,
BuildProfile::Profiling => &self.manifest.package.metadata.wasm_pack.profile.profiling,
BuildProfile::Release => &self.manifest.package.metadata.wasm_pack.profile.release,
BuildProfile::Custom(_) => &self.manifest.package.metadata.wasm_pack.profile.custom,
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/all/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ fn build_different_profiles() {
}
}

#[test]
fn build_custom_profile() {
let profile_name = "my-custom-profile";
let fixture = utils::fixture::js_hello_world_with_custom_profile(profile_name);
fixture.install_local_wasm_bindgen();

fixture
.wasm_pack()
.arg("build")
.arg("--profile")
.arg(profile_name)
.assert()
.success();
}

#[test]
fn build_with_and_without_wasm_bindgen_debug() {
for debug in [true, false].iter().cloned() {
Expand Down
52 changes: 52 additions & 0 deletions tests/all/utils/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,49 @@ impl Fixture {
)
}

/// Add a `Cargo.toml` with a correctly configured `wasm-bindgen`
/// dependency, `wasm-bindgen-test` dev-dependency, and `crate-type =
/// ["cdylib"]`.
///
/// `name` is the crate's name.
/// `profile` is the custom profile name.
pub fn cargo_toml_with_custom_profile(&self, name: &str, profile_name: &str) -> &Self {
self.file(
"Cargo.toml",
&format!(
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "{}"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
# Note that this uses and `=` dependency because there are
# various tests which assert that the version of wasm
# bindgen downloaded is what we expect, and if `=` is
# removed then it will download whatever the newest version
# of wasm-bindgen is which may not be what's listed here.
wasm-bindgen = "=0.2.74"
[dev-dependencies]
wasm-bindgen-test = "0.3"
[profile.{}]
inherits = "release"
opt-level = 'z'
lto = true
"#,
name, profile_name
),
)
}

/// Add a `Cargo.toml` with a correctly configured `wasm-bindgen`
/// dependency, `wasm-bindgen-test` dev-dependency, and `crate-type =
/// ["cdylib"]`.
Expand Down Expand Up @@ -404,6 +447,15 @@ pub fn js_hello_world() -> Fixture {
fixture
}

pub fn js_hello_world_with_custom_profile(profile_name: &str) -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml_with_custom_profile("js-hello-world", profile_name)
.hello_world_src_lib();
fixture
}

pub fn no_cdylib() -> Fixture {
let fixture = Fixture::new();
fixture.readme().hello_world_src_lib().file(
Expand Down

0 comments on commit 513feba

Please sign in to comment.