Skip to content

Commit 10b342e

Browse files
committed
Work around Rust passing incorrectly Meson-style library names under Windows
1 parent c888d16 commit 10b342e

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

src/lib.rs

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,33 @@ pub struct Dependencies {
296296
libs: HashMap<String, Library>,
297297
}
298298

299+
fn library_name(l: &String, link_paths: &Vec<PathBuf>) -> Result<(String, PathBuf), String> {
300+
let libnames = {
301+
let mut names = vec![format!("lib{}.a", l)];
302+
303+
if cfg!(target_os = "windows") {
304+
names.push(format!("{}.lib", l));
305+
}
306+
307+
names
308+
};
309+
310+
for dir in link_paths {
311+
for libname in &libnames {
312+
let libpath: PathBuf = dir.join(libname);
313+
if libpath.exists() {
314+
if cfg!(target_os = "windows") {
315+
return Ok((libname.clone(), libpath));
316+
} else {
317+
return Ok((l.to_owned(), libpath));
318+
}
319+
}
320+
}
321+
}
322+
323+
Err(l.to_owned())
324+
}
325+
299326
impl Dependencies {
300327
/// Retrieve details about a system dependency.
301328
///
@@ -405,10 +432,12 @@ impl Dependencies {
405432
// available and let the linking fail if the user is wrong.
406433
let is_static_lib_available = should_be_linked_statically;
407434

408-
lib.libs = split_string(&value)
409-
.into_iter()
410-
.map(|l| InternalLib::new(l, is_static_lib_available))
411-
.collect();
435+
let library = |l: String| match library_name(&l, &lib.link_paths) {
436+
Ok(l) => InternalLib::new(l.0, is_static_lib_available),
437+
Err(l) => InternalLib::new(l, is_static_lib_available),
438+
};
439+
440+
lib.libs = split_string(&value).into_iter().map(library).collect();
412441
}
413442
if let Some(value) = env.get(&EnvVariable::new_lib_framework(name)) {
414443
lib.frameworks = split_string(&value);
@@ -783,7 +812,6 @@ impl Config {
783812
let mut config = pkg_config::Config::new();
784813
config
785814
.print_system_libs(false)
786-
.cargo_metadata(false)
787815
.range_version(metadata::parse_version(version))
788816
.statik(statik);
789817

@@ -1007,31 +1035,20 @@ impl Library {
10071035
}
10081036
};
10091037

1010-
let is_static_available = |name: &String| -> bool {
1011-
let libnames = {
1012-
let mut names = vec![format!("lib{}.a", name)];
1013-
1014-
if cfg!(target_os = "windows") {
1015-
names.push(format!("{}.lib", name));
1016-
}
1017-
1018-
names
1019-
};
1020-
1021-
l.link_paths.iter().any(|dir| {
1022-
let library_exists = libnames.iter().any(|libname| dir.join(libname).exists());
1023-
library_exists && !system_roots.iter().any(|sys| dir.starts_with(sys))
1024-
})
1038+
let library = |name: &String| match library_name(name, &l.link_paths) {
1039+
Ok((libname, libpath)) => InternalLib::new(
1040+
libname,
1041+
!system_roots
1042+
.iter()
1043+
.any(|sys| libpath.parent().unwrap().starts_with(sys)),
1044+
),
1045+
Err(l) => InternalLib::new(l, false),
10251046
};
10261047

10271048
Self {
10281049
name: name.to_string(),
10291050
source: Source::PkgConfig,
1030-
libs: l
1031-
.libs
1032-
.iter()
1033-
.map(|lib| InternalLib::new(lib.to_owned(), is_static_available(lib)))
1034-
.collect(),
1051+
libs: l.libs.iter().map(library).collect(),
10351052
link_paths: l.link_paths,
10361053
include_paths: l.include_paths,
10371054
frameworks: l.frameworks,
@@ -1103,7 +1120,6 @@ impl Library {
11031120
let pkg_lib = pkg_config::Config::new()
11041121
.atleast_version(version)
11051122
.print_system_libs(false)
1106-
.cargo_metadata(false)
11071123
.statik(true)
11081124
.probe(lib);
11091125

@@ -1178,7 +1194,11 @@ impl fmt::Display for BuildFlag {
11781194
BuildFlag::SearchFramework(lib) => write!(f, "rustc-link-search=framework={}", lib),
11791195
BuildFlag::Lib(lib, statik) => {
11801196
if *statik {
1181-
write!(f, "rustc-link-lib=static={}", lib)
1197+
if cfg!(target_os = "windows") {
1198+
write!(f, "rustc-link-lib=static:+verbatim={}", lib)
1199+
} else {
1200+
write!(f, "rustc-link-lib=static={}", lib)
1201+
}
11821202
} else {
11831203
write!(f, "rustc-link-lib={}", lib)
11841204
}

0 commit comments

Comments
 (0)