Skip to content

Commit f9a541b

Browse files
committed
Handle target specific outputs such as .wasm
Fixes #4535 - Do not add metadata to wasm32 bin target because generated .js will refer corresponding .wasm. - Handle different usages of "-" and "_" in .js and .wasm file names. (e.g "foo-bar.js" vs. "foo_bar.wasm") - Change file mode of cargo_rustc/context.rs (100755 -> 100644)
1 parent 84b3989 commit f9a541b

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/cargo/ops/cargo_rustc/context.rs

100755100644
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
463463
// - OSX encodes the dylib name in the executable
464464
// - Windows rustc multiple files of which we can't easily link all of them
465465
//
466+
// No metadata for bin because of an issue
467+
// - wasm32 rustc/emcc encodes the .wasm name in the .js (rust-lang/cargo#4535)
468+
//
466469
// Two exceptions
467470
// 1) Upstream dependencies (we aren't exporting + need to resolve name conflict)
468471
// 2) __CARGO_DEFAULT_LIB_METADATA env var
@@ -478,9 +481,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
478481
// doing this eventually.
479482
let __cargo_default_lib_metadata = env::var("__CARGO_DEFAULT_LIB_METADATA");
480483
if !unit.profile.test &&
481-
(unit.target.is_dylib() || unit.target.is_cdylib()) &&
484+
(unit.target.is_dylib() || unit.target.is_cdylib() ||
485+
(unit.target.is_bin() && self.target_triple().starts_with("wasm32-"))) &&
482486
unit.pkg.package_id().source_id().is_path() &&
483-
!__cargo_default_lib_metadata.is_ok() {
487+
!__cargo_default_lib_metadata.is_ok()
488+
{
484489
return None;
485490
}
486491

@@ -652,11 +657,21 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
652657
suffix,
653658
linkable,
654659
);
655-
for (suffix, linkable) in suffixes {
660+
for (suffix, linkable, should_replace_hyphens) in suffixes {
661+
// wasm bin target will generate two files in deps such as
662+
// "web-stuff.js" and "web_stuff.wasm". Note the different usages of
663+
// "-" and "_". should_replace_hyphens is a flag to indicate that
664+
// we need to convert the stem "web-stuff" to "web_stuff", so we
665+
// won't miss "web_stuff.wasm".
666+
let conv = |s: String| if should_replace_hyphens {
667+
s.replace("-", "_")
668+
} else {
669+
s
670+
};
656671
let filename =
657-
out_dir.join(format!("{}{}{}", prefix, stem, suffix));
672+
out_dir.join(format!("{}{}{}", prefix, conv(stem.clone()), suffix));
658673
let link_dst = link_stem.clone().map(|(ld, ls)| {
659-
ld.join(format!("{}{}{}", prefix, ls, suffix))
674+
ld.join(format!("{}{}{}", prefix, conv(ls), suffix))
660675
});
661676
ret.push((filename, link_dst, linkable));
662677
}
@@ -1174,27 +1189,32 @@ fn parse_crate_type(
11741189
Ok(Some((prefix.to_string(), suffix.to_string())))
11751190
}
11761191

1192+
// (not a rustdoc)
1193+
// Return a list of 3-tuples (suffix, linkable, should_replace_hyphens).
1194+
//
1195+
// should_replace_hyphens will be used by the caller to replace "-" with "_"
1196+
// in a bin_stem. See the caller side (calc_target_filenames()) for details.
11771197
fn add_target_specific_suffixes(
11781198
target_triple: &str,
11791199
crate_type: &str,
11801200
suffix: &str,
11811201
linkable: bool,
1182-
) -> Vec<(String, bool)> {
1183-
let mut suffixes = vec![(suffix.to_string(), linkable)];
1202+
) -> Vec<(String, bool, bool)> {
1203+
let mut ret = vec![(suffix.to_string(), linkable, false)];
11841204

11851205
// rust-lang/cargo#4500
11861206
if target_triple.ends_with("pc-windows-msvc") && crate_type.ends_with("dylib") &&
11871207
suffix == ".dll"
11881208
{
1189-
suffixes.push((format!("{}.lib", suffix), false));
1209+
ret.push((".dll.lib".to_string(), false, false));
11901210
}
11911211

11921212
// rust-lang/cargo#4535
1193-
if target_triple == "wasm32-unknown-emscripten" && crate_type == "bin" &&
1213+
if target_triple.starts_with("wasm32-") && crate_type == "bin" &&
11941214
suffix == ".js"
11951215
{
1196-
suffixes.push((".wasm".to_string(), false));
1216+
ret.push((".wasm".to_string(), false, true));
11971217
}
11981218

1199-
suffixes
1219+
ret
12001220
}

tests/build.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,7 +3371,7 @@ fn cdylib_final_outputs() {
33713371
let p = project("foo")
33723372
.file("Cargo.toml", r#"
33733373
[project]
3374-
name = "foo"
3374+
name = "foo-bar"
33753375
authors = []
33763376
version = "0.1.0"
33773377
@@ -3383,11 +3383,11 @@ fn cdylib_final_outputs() {
33833383
assert_that(p.cargo_process("build"), execs().with_status(0));
33843384

33853385
let files = if cfg!(windows) {
3386-
vec!["foo.dll.lib", "foo.dll"]
3386+
vec!["foo_bar.dll.lib", "foo_bar.dll"]
33873387
} else if cfg!(target_os = "macos") {
3388-
vec!["libfoo.dylib"]
3388+
vec!["libfoo_bar.dylib"]
33893389
} else {
3390-
vec!["libfoo.so"]
3390+
vec!["libfoo_bar.so"]
33913391
};
33923392

33933393
for file in files {
@@ -3408,7 +3408,7 @@ fn wasm32_final_outputs() {
34083408
let p = project("foo")
34093409
.file("Cargo.toml", r#"
34103410
[project]
3411-
name = "foo"
3411+
name = "foo-bar"
34123412
authors = []
34133413
version = "0.1.0"
34143414
"#)
@@ -3449,11 +3449,11 @@ fn wasm32_final_outputs() {
34493449

34503450
let pkgid = packages
34513451
.package_ids()
3452-
.filter(|id| id.name() == "foo")
3452+
.filter(|id| id.name() == "foo-bar")
34533453
.collect::<Vec<_>>();
34543454
let pkg = packages.get(pkgid[0]).expect("Can't get package");
34553455

3456-
let target = Target::bin_target("foo", p.root().join("src/main.rs"), None);
3456+
let target = Target::bin_target("foo-bar", p.root().join("src/main.rs"), None);
34573457

34583458
let unit = Unit {
34593459
pkg: &pkg,
@@ -3478,7 +3478,7 @@ fn wasm32_final_outputs() {
34783478
let target_filenames = ctx.target_filenames(&unit).expect("Can't get target file names");
34793479

34803480
// Verify the result.
3481-
let mut expected = vec!["debug/foo.js", "debug/foo.wasm"];
3481+
let mut expected = vec!["debug/foo-bar.js", "debug/foo_bar.wasm"];
34823482

34833483
assert_eq!(target_filenames.len(), expected.len());
34843484

0 commit comments

Comments
 (0)