Skip to content

Commit 60e18c1

Browse files
committed
implements custom build profile
solves rustwasm#1111
1 parent 62ab39c commit 60e18c1

File tree

6 files changed

+127
-11
lines changed

6 files changed

+127
-11
lines changed

src/bindgen.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ pub fn wasm_bindgen_build(
2323
profile: BuildProfile,
2424
extra_options: &Vec<String>,
2525
) -> Result<()> {
26-
let release_or_debug = match profile {
26+
let profile_name = match profile.clone() {
2727
BuildProfile::Release | BuildProfile::Profiling => "release",
2828
BuildProfile::Dev => "debug",
29+
BuildProfile::Custom(profile_name) => &profile_name.clone(),
2930
};
3031

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

4243
let wasm_path = target_directory
4344
.join("wasm32-unknown-unknown")
44-
.join(release_or_debug)
45+
.join(profile_name)
4546
.join(data.crate_name())
4647
.with_extension("wasm");
4748

src/build/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ pub fn cargo_build_wasm(
104104
// Plain cargo builds use the dev cargo profile, which includes
105105
// debug info by default.
106106
}
107+
BuildProfile::Custom(arg) => {
108+
cmd.arg("--profile").arg(arg);
109+
}
107110
}
108111

109112
cmd.arg("--target").arg("wasm32-unknown-unknown");

src/command/build.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ impl FromStr for Target {
100100

101101
/// The build profile controls whether optimizations, debug info, and assertions
102102
/// are enabled or disabled.
103-
#[derive(Clone, Copy, Debug)]
103+
#[derive(Clone, Debug)]
104104
pub enum BuildProfile {
105105
/// Enable assertions and debug info. Disable optimizations.
106106
Dev,
107107
/// Enable optimizations. Disable assertions and debug info.
108108
Release,
109109
/// Enable optimizations and debug info. Disable assertions.
110110
Profiling,
111+
/// User-defined profile with --profile flag
112+
Custom(String),
111113
}
112114

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

165+
#[clap(long = "profile")]
166+
/// User-defined profile with --profile flag
167+
pub profile: Option<String>,
168+
163169
#[clap(long = "out-dir", short = 'd', default_value = "pkg")]
164170
/// Sets the output directory with a relative path.
165171
pub out_dir: String,
@@ -196,6 +202,7 @@ impl Default for BuildOptions {
196202
no_opt: false,
197203
release: false,
198204
profiling: false,
205+
profile: None,
199206
out_dir: String::new(),
200207
out_name: None,
201208
extra_options: Vec::new(),
@@ -221,13 +228,19 @@ impl Build {
221228
let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir)).clean();
222229

223230
let dev = build_opts.dev || build_opts.debug;
224-
let profile = match (dev, build_opts.release, build_opts.profiling) {
225-
(false, false, false) | (false, true, false) => BuildProfile::Release,
226-
(true, false, false) => BuildProfile::Dev,
227-
(false, false, true) => BuildProfile::Profiling,
231+
let profile = match (
232+
dev,
233+
build_opts.release,
234+
build_opts.profiling,
235+
build_opts.profile,
236+
) {
237+
(false, false, false, None) | (false, true, false, None) => BuildProfile::Release,
238+
(true, false, false, None) => BuildProfile::Dev,
239+
(false, false, true, None) => BuildProfile::Profiling,
240+
(false, false, false, Some(profile)) => BuildProfile::Custom(profile),
228241
// Unfortunately, `clap` doesn't expose clap's `conflicts_with`
229242
// functionality yet, so we have to implement it ourselves.
230-
_ => bail!("Can only supply one of the --dev, --release, or --profiling flags"),
243+
_ => bail!("Can only supply one of the --dev, --release, --profiling, or --profile 'name' flags"),
231244
};
232245

233246
Ok(Build {
@@ -355,7 +368,7 @@ impl Build {
355368

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

360373
info!(
361374
"wasm built at {:#?}.",
@@ -430,7 +443,7 @@ impl Build {
430443
self.weak_refs,
431444
self.reference_types,
432445
self.target,
433-
self.profile,
446+
self.profile.clone(),
434447
&self.extra_options,
435448
)?;
436449
info!("wasm bindings were built at {:#?}.", &self.out_dir);
@@ -440,7 +453,7 @@ impl Build {
440453
fn step_run_wasm_opt(&mut self) -> Result<()> {
441454
let mut args = match self
442455
.crate_data
443-
.configured_profile(self.profile)
456+
.configured_profile(self.profile.clone())
444457
.wasm_opt_args()
445458
{
446459
Some(args) => args,

src/manifest/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ struct CargoWasmPackProfiles {
8585
deserialize_with = "CargoWasmPackProfile::deserialize_profiling"
8686
)]
8787
profiling: CargoWasmPackProfile,
88+
89+
#[serde(
90+
default = "CargoWasmPackProfile::default_custom",
91+
deserialize_with = "CargoWasmPackProfile::deserialize_custom"
92+
)]
93+
custom: CargoWasmPackProfile,
8894
}
8995

9096
impl Default for CargoWasmPackProfiles {
@@ -93,6 +99,7 @@ impl Default for CargoWasmPackProfiles {
9399
dev: CargoWasmPackProfile::default_dev(),
94100
release: CargoWasmPackProfile::default_release(),
95101
profiling: CargoWasmPackProfile::default_profiling(),
102+
custom: CargoWasmPackProfile::default_custom(),
96103
}
97104
}
98105
}
@@ -304,6 +311,18 @@ impl CargoWasmPackProfile {
304311
}
305312
}
306313

314+
fn default_custom() -> Self {
315+
CargoWasmPackProfile {
316+
wasm_bindgen: CargoWasmPackProfileWasmBindgen {
317+
debug_js_glue: Some(false),
318+
demangle_name_section: Some(true),
319+
dwarf_debug_info: Some(false),
320+
omit_default_module_path: Some(false),
321+
},
322+
wasm_opt: Some(CargoWasmPackProfileWasmOpt::Enabled(true)),
323+
}
324+
}
325+
307326
fn deserialize_dev<'de, D>(deserializer: D) -> Result<Self, D::Error>
308327
where
309328
D: serde::Deserializer<'de>,
@@ -331,6 +350,15 @@ impl CargoWasmPackProfile {
331350
Ok(profile)
332351
}
333352

353+
fn deserialize_custom<'de, D>(deserializer: D) -> Result<Self, D::Error>
354+
where
355+
D: serde::Deserializer<'de>,
356+
{
357+
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
358+
profile.update_with_defaults(&Self::default_custom());
359+
Ok(profile)
360+
}
361+
334362
fn update_with_defaults(&mut self, defaults: &Self) {
335363
macro_rules! d {
336364
( $( $path:ident ).* ) => {
@@ -489,6 +517,7 @@ impl CrateData {
489517
BuildProfile::Dev => &self.manifest.package.metadata.wasm_pack.profile.dev,
490518
BuildProfile::Profiling => &self.manifest.package.metadata.wasm_pack.profile.profiling,
491519
BuildProfile::Release => &self.manifest.package.metadata.wasm_pack.profile.release,
520+
BuildProfile::Custom(_) => &self.manifest.package.metadata.wasm_pack.profile.custom,
492521
}
493522
}
494523

tests/all/build.rs

+15
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ fn build_different_profiles() {
212212
}
213213
}
214214

215+
#[test]
216+
fn build_custom_profile() {
217+
let profile_name = "my-custom-profile";
218+
let fixture = utils::fixture::js_hello_world_with_custom_profile(profile_name);
219+
fixture.install_local_wasm_bindgen();
220+
221+
fixture
222+
.wasm_pack()
223+
.arg("build")
224+
.arg("--profile")
225+
.arg(profile_name)
226+
.assert()
227+
.success();
228+
}
229+
215230
#[test]
216231
fn build_with_and_without_wasm_bindgen_debug() {
217232
for debug in [true, false].iter().cloned() {

tests/all/utils/fixture.rs

+55
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,58 @@ impl Fixture {
148148
149149
[dev-dependencies]
150150
wasm-bindgen-test = "0.3"
151+
152+
[profile.my-custom-profile]
153+
inherits = "release"
151154
"#,
152155
name
153156
),
154157
)
155158
}
156159

160+
/// Add a `Cargo.toml` with a correctly configured `wasm-bindgen`
161+
/// dependency, `wasm-bindgen-test` dev-dependency, and `crate-type =
162+
/// ["cdylib"]`.
163+
///
164+
/// `name` is the crate's name.
165+
/// `profile` is the custom profile name.
166+
pub fn cargo_toml_with_custom_profile(&self, name: &str, profile_name: &str) -> &Self {
167+
self.file(
168+
"Cargo.toml",
169+
&format!(
170+
r#"
171+
[package]
172+
authors = ["The wasm-pack developers"]
173+
description = "so awesome rust+wasm package"
174+
license = "WTFPL"
175+
name = "{}"
176+
repository = "https://github.com/rustwasm/wasm-pack.git"
177+
version = "0.1.0"
178+
179+
[lib]
180+
crate-type = ["cdylib"]
181+
182+
[dependencies]
183+
# Note that this uses and `=` dependency because there are
184+
# various tests which assert that the version of wasm
185+
# bindgen downloaded is what we expect, and if `=` is
186+
# removed then it will download whatever the newest version
187+
# of wasm-bindgen is which may not be what's listed here.
188+
wasm-bindgen = "=0.2.74"
189+
190+
[dev-dependencies]
191+
wasm-bindgen-test = "0.3"
192+
193+
[profile.{}]
194+
inherits = "release"
195+
opt-level = 'z'
196+
lto = true
197+
"#,
198+
name, profile_name
199+
),
200+
)
201+
}
202+
157203
/// Add a `Cargo.toml` with a correctly configured `wasm-bindgen`
158204
/// dependency, `wasm-bindgen-test` dev-dependency, and `crate-type =
159205
/// ["cdylib"]`.
@@ -404,6 +450,15 @@ pub fn js_hello_world() -> Fixture {
404450
fixture
405451
}
406452

453+
pub fn js_hello_world_with_custom_profile(profile_name: &str) -> Fixture {
454+
let fixture = Fixture::new();
455+
fixture
456+
.readme()
457+
.cargo_toml_with_custom_profile("js-hello-world", profile_name)
458+
.hello_world_src_lib();
459+
fixture
460+
}
461+
407462
pub fn no_cdylib() -> Fixture {
408463
let fixture = Fixture::new();
409464
fixture.readme().hello_world_src_lib().file(

0 commit comments

Comments
 (0)