Skip to content

Commit 33a2145

Browse files
committed
fix(windows): fixup lib format
Original `cfg!(windows)` don't work, and MinGW toolchain .a for static link, .dll for dynamic link. Otherwise it reports error: /usr/bin/x86_64-w64-mingw32-ld: cannot find -lxxx: No such file or directory Signed-off-by: Fu Lin <[email protected]>
1 parent fa5dbc2 commit 33a2145

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,25 +303,32 @@ impl Build {
303303
}
304304

305305
fn format_lib_name(&self, output: &str) -> PathBuf {
306+
let target_os = goos_from_env().unwrap();
306307
let mut lib = String::with_capacity(output.len() + 7);
307308
lib.push_str("lib");
308309
lib.push_str(output);
309310
lib.push_str(match self.build_mode {
311+
// It's odd here. neither `if cfg!(windows)` nor
312+
// `#[cfg( target_os = "windows" )]` works here.
310313
BuildMode::CArchive => {
311-
if cfg!(windows) {
314+
// Only msvc toolchain will use `.lib` suffix.
315+
// mingw toolchain will use `.a`.
316+
let target_env = get_env_var("CARGO_CFG_TARGET_ENV").unwrap();
317+
if target_os.eq("windows") && target_env.eq("msvc") {
312318
".lib"
313319
} else {
314320
".a"
315321
}
316322
}
317323
BuildMode::CShared => {
318-
if cfg!(windows) {
324+
if target_os.eq("windows") {
319325
".dll"
320326
} else {
321327
".so"
322328
}
323329
}
324330
});
331+
325332
lib.into()
326333
}
327334
}

0 commit comments

Comments
 (0)