Skip to content

Commit 269fb7c

Browse files
authored
Use a unique name for libwit_bindgen_cabi_realloc.a per-crate-version (#928)
* Use a unique name for `libwit_bindgen_cabi_realloc.a` per-crate-version This commit updates how the weak `cabi_realloc` symbol is linked. Previously rustc was told to use `-lwit_bindgen_cabi_realloc` with a `-L` pointing to the source directory of the crate which has a precompiled copy. This caused issues, however, when two versions were present in a crate graph but only one was used. With two versions present in a crate graph rustc would use two `-L` flags with directories that both contain the same-named library. Which one was picked depended on the order that rustc passed flags. If only one crate was used, however, then rustc would not pass the rlib for the other crate. This could end up in a situation where with two wit-bindgen versions A and B: * During linking, A's `libwit_bindgen_cabi_realloc.a` file was used. * Rustc only passed B's rlib since A wasn't actually used anywhere. * The linker then tried to load A's version of the actual cabi_realloc symbol, but only B's was present. The fix in this commit is to uniquely name the library name based on the crate version. This matches how the symbol name is different per crate version, for example. * Remove copy/paste
1 parent 90a1e54 commit 269fb7c

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

crates/guest-rust/rt/build.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
14
fn main() {
2-
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or(String::new());
3-
let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or(String::new());
5+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or(String::new());
6+
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or(String::new());
7+
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
48

59
if target_family != "wasm" {
610
return;
@@ -10,8 +14,18 @@ fn main() {
1014
panic!("only wasm32 supports cabi-realloc right now");
1115
}
1216

13-
println!("cargo:rustc-link-lib=wit_bindgen_cabi_realloc");
14-
let cwd = std::env::current_dir().unwrap();
15-
let cwd = cwd.display();
16-
println!("cargo:rustc-link-search=native={cwd}/src");
17+
let mut src = env::current_dir().unwrap();
18+
src.push("src");
19+
src.push("libwit_bindgen_cabi_realloc.a");
20+
21+
let dst_name = format!(
22+
"wit_bindgen_cabi_realloc{}",
23+
env!("CARGO_PKG_VERSION").replace(".", "_")
24+
);
25+
let dst = out_dir.join(format!("lib{dst_name}.a"));
26+
27+
std::fs::copy(&src, &dst).unwrap();
28+
29+
println!("cargo:rustc-link-lib={dst_name}");
30+
println!("cargo:rustc-link-search=native={}", out_dir.display());
1731
}

0 commit comments

Comments
 (0)