Skip to content

Commit 51e6351

Browse files
authored
Merge pull request #440 from fitzgen/build-profiles-toml-config
Add three build profiles and infrastructure for their toml config
2 parents 91e3293 + 64d52d8 commit 51e6351

File tree

11 files changed

+464
-65
lines changed

11 files changed

+464
-65
lines changed

Cargo.lock

+41-44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
- [`src/utils.rs`](./tutorial/template-deep-dive/src-utils-rs.md)
1919
- [Packaging and Publishing](./tutorial/packaging-and-publishing.md)
2020
- [Using your Library](./tutorial/using-your-library.md)
21+
- [`Cargo.toml` Configuration](./cargo-toml-configuration.md)
2122
- [Contributing](./contributing.md)

docs/src/cargo-toml-configuration.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# `Cargo.toml` Configuration
2+
3+
`wasm-pack` can be configured via the `package.metadata.wasm-pack` key in
4+
`Cargo.toml`. Every option has a default, and is not required.
5+
6+
There are three profiles: `dev`, `profiling`, and `release`. These correspond to
7+
the `--dev`, `--profiling`, and `--release` flags passed to `wasm-pack build`.
8+
9+
The available configuration options and their default values are shown below:
10+
11+
```toml
12+
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
13+
# Should we enable wasm-bindgen's debug assertions in its generated JS glue?
14+
debug-js-glue = true
15+
# Should wasm-bindgen demangle the symbols in the "name" custom section?
16+
demangle-name-section = true
17+
# Should we emit the DWARF debug info custom sections?
18+
dwarf-debug-info = false
19+
20+
[package.metadata.wasm-pack.profile.profiling.wasm-bindgen]
21+
debug-js-glue = false
22+
demangle-name-section = true
23+
dwarf-debug-info = false
24+
25+
[package.metadata.wasm-pack.profile.release.wasm-bindgen]
26+
debug-js-glue = false
27+
demangle-name-section = true
28+
dwarf-debug-info = false
29+
```

docs/src/commands/build.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,28 @@ wasm-pack build examples/js-hello-world
1616
This path should point to a directory that contains a `Cargo.toml` file. If no
1717
path is given, the `build` command will run in the current directory.
1818

19-
## Debug
19+
## Profile
2020

21-
The init command accepts an optional `--debug` argument. This will build the
22-
output package using cargo's
23-
[default non-release profile][cargo-profile-sections-documentation]. Building
24-
this way is faster but applies few optimizations to the output, and enables
25-
debug assertions and other runtime correctness checks.
21+
The `build` command accepts an optional profile argument: one of `--dev`,
22+
`--profiling`, or `--release`. If none is supplied, then `--release` is used.
2623

27-
The exact meaning of this flag may evolve as the platform matures.
24+
Th controls whether debug assertions are enabled, debug info is generated, and
25+
which (if any) optimizations are enabled.
26+
27+
| Profile | Debug Assertions | Debug Info | Optimizations | Notes |
28+
|---------------|------------------|------------|---------------|---------------------------------------|
29+
| `--dev` | Yes | Yes | No | Useful for development and debugging. |
30+
| `--profiling` | No | Yes | Yes | Useful when profiling and investigating performance issues. |
31+
| `--release` | No | No | Yes | Useful for shipping to production. |
32+
33+
The `--dev` profile will build the output package using cargo's [default
34+
non-release profile][cargo-profile-sections-documentation]. Building this way is
35+
faster but applies few optimizations to the output, and enables debug assertions
36+
and other runtime correctness checks. The `--profiling` and `--release` profiles
37+
use cargo's release profile, but the former enables debug info as well, which
38+
helps when investigating performance issues in a profiler.
39+
40+
The exact meaning of the profile flags may evolve as the platform matures.
2841

2942
[cargo-profile-sections-documentation]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections
3043

src/bindgen.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use binaries::{Cache, Download};
44
use child;
5+
use command::build::BuildProfile;
56
use emoji;
67
use failure::{self, ResultExt};
78
use manifest::CrateData;
@@ -145,14 +146,17 @@ pub fn wasm_bindgen_build(
145146
out_dir: &Path,
146147
disable_dts: bool,
147148
target: &str,
148-
debug: bool,
149+
profile: BuildProfile,
149150
step: &Step,
150151
log: &Logger,
151152
) -> Result<(), failure::Error> {
152153
let msg = format!("{}Running WASM-bindgen...", emoji::RUNNER);
153154
PBAR.step(step, &msg);
154155

155-
let release_or_debug = if debug { "debug" } else { "release" };
156+
let release_or_debug = match profile {
157+
BuildProfile::Release | BuildProfile::Profiling => "release",
158+
BuildProfile::Dev => "debug",
159+
};
156160

157161
let out_dir = out_dir.to_str().unwrap();
158162

@@ -181,9 +185,16 @@ pub fn wasm_bindgen_build(
181185
.arg(dts_arg)
182186
.arg(target_arg);
183187

184-
if debug {
188+
let profile = data.configured_profile(profile);
189+
if profile.wasm_bindgen_debug_js_glue() {
185190
cmd.arg("--debug");
186191
}
192+
if !profile.wasm_bindgen_demangle_name_section() {
193+
cmd.arg("--no-demangle");
194+
}
195+
if profile.wasm_bindgen_dwarf_debug_info() {
196+
cmd.arg("--keep-debug");
197+
}
187198

188199
child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
189200
Ok(())

src/build.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Building a Rust crate into a `.wasm` binary.
22
33
use child;
4+
use command::build::BuildProfile;
45
use emoji;
56
use failure::{Error, ResultExt};
67
use progressbar::Step;
@@ -62,13 +63,32 @@ pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), Error> {
6263
}
6364

6465
/// Run `cargo build` targetting `wasm32-unknown-unknown`.
65-
pub fn cargo_build_wasm(log: &Logger, path: &Path, debug: bool, step: &Step) -> Result<(), Error> {
66+
pub fn cargo_build_wasm(
67+
log: &Logger,
68+
path: &Path,
69+
profile: BuildProfile,
70+
step: &Step,
71+
) -> Result<(), Error> {
6672
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
6773
PBAR.step(step, &msg);
6874
let mut cmd = Command::new("cargo");
6975
cmd.current_dir(path).arg("build").arg("--lib");
70-
if !debug {
71-
cmd.arg("--release");
76+
match profile {
77+
BuildProfile::Profiling => {
78+
// Once there are DWARF debug info consumers, force enable debug
79+
// info, because builds that use the release cargo profile disables
80+
// debug info.
81+
//
82+
// cmd.env("RUSTFLAGS", "-g");
83+
cmd.arg("--release");
84+
}
85+
BuildProfile::Release => {
86+
cmd.arg("--release");
87+
}
88+
BuildProfile::Dev => {
89+
// Plain cargo builds use the dev cargo profile, which includes
90+
// debug info by default.
91+
}
7292
}
7393
cmd.arg("--target").arg("wasm32-unknown-unknown");
7494
child::run(log, cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;

src/command/build.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ pub struct Build {
2525
pub scope: Option<String>,
2626
pub disable_dts: bool,
2727
pub target: String,
28-
pub debug: bool,
28+
pub profile: BuildProfile,
2929
pub mode: BuildMode,
30-
// build_config: Option<BuildConfig>,
3130
pub out_dir: PathBuf,
3231
pub bindgen: Option<Download>,
3332
pub cache: Cache,
@@ -64,6 +63,18 @@ impl FromStr for BuildMode {
6463
}
6564
}
6665

66+
/// The build profile controls whether optimizations, debug info, and assertions
67+
/// are enabled or disabled.
68+
#[derive(Clone, Copy, Debug)]
69+
pub enum BuildProfile {
70+
/// Enable assertions and debug info. Disable optimizations.
71+
Dev,
72+
/// Enable optimizations. Disable assertions and debug info.
73+
Release,
74+
/// Enable optimizations and debug info. Disable assertions.
75+
Profiling,
76+
}
77+
6778
/// Everything required to configure and run the `wasm-pack build` command.
6879
#[derive(Debug, StructOpt)]
6980
pub struct BuildOptions {
@@ -97,6 +108,14 @@ pub struct BuildOptions {
97108
/// optimizations.
98109
dev: bool,
99110

111+
#[structopt(long = "release")]
112+
/// Create a release build. Enable optimizations and disable debug info.
113+
release: bool,
114+
115+
#[structopt(long = "profiling")]
116+
/// Create a profiling build. Enable optimizations and debug info.
117+
profiling: bool,
118+
100119
#[structopt(long = "out-dir", short = "d", default_value = "pkg")]
101120
/// Sets the output directory with a relative path.
102121
pub out_dir: String,
@@ -110,16 +129,25 @@ impl Build {
110129
let crate_path = set_crate_path(build_opts.path)?;
111130
let crate_data = manifest::CrateData::new(&crate_path)?;
112131
let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir));
113-
// let build_config = manifest::xxx(&crate_path).xxx();
132+
133+
let dev = build_opts.dev || build_opts.debug;
134+
let profile = match (dev, build_opts.release, build_opts.profiling) {
135+
(false, false, false) | (false, true, false) => BuildProfile::Release,
136+
(true, false, false) => BuildProfile::Dev,
137+
(false, false, true) => BuildProfile::Profiling,
138+
// Unfortunately, `structopt` doesn't expose clap's `conflicts_with`
139+
// functionality yet, so we have to implement it ourselves.
140+
_ => bail!("Can only supply one of the --dev, --release, or --profiling flags"),
141+
};
142+
114143
Ok(Build {
115144
crate_path,
116145
crate_data,
117146
scope: build_opts.scope,
118147
disable_dts: build_opts.disable_dts,
119148
target: build_opts.target,
120-
debug: build_opts.dev || build_opts.debug,
149+
profile,
121150
mode: build_opts.mode,
122-
// build_config,
123151
out_dir,
124152
bindgen: None,
125153
cache: Cache::new()?,
@@ -227,7 +255,7 @@ impl Build {
227255

228256
fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
229257
info!(&log, "Building wasm...");
230-
build::cargo_build_wasm(log, &self.crate_path, self.debug, step)?;
258+
build::cargo_build_wasm(log, &self.crate_path, self.profile, step)?;
231259

232260
info!(
233261
&log,
@@ -302,7 +330,7 @@ impl Build {
302330
&self.out_dir,
303331
self.disable_dts,
304332
&self.target,
305-
self.debug,
333+
self.profile,
306334
step,
307335
log,
308336
)?;

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern crate indicatif;
1414
#[macro_use]
1515
extern crate lazy_static;
1616
extern crate parking_lot;
17+
extern crate serde;
1718
#[macro_use]
1819
extern crate serde_derive;
1920
extern crate serde_json;

0 commit comments

Comments
 (0)