Skip to content

Commit 2d770a8

Browse files
committed
More static fixes
1 parent 338be0b commit 2d770a8

File tree

3 files changed

+51
-57
lines changed

3 files changed

+51
-57
lines changed

build.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -94,43 +94,6 @@ fn files_with_extension<'e>(dir: &Path, extension: impl AsRef<OsStr> + 'e) -> Re
9494
})
9595
}
9696

97-
/// Returns Some(new_file_name) if some parts of the filename were removed, None otherwise
98-
fn cleanup_lib_filename(filename: &OsStr) -> Option<&OsStr> {
99-
let mut new_filename = filename;
100-
// used to check for the file extension (with dots stripped) and for the part of the filename
101-
const LIB_EXTS: [&str; 7] = [".so.", ".a.", ".dll.", ".lib.", ".dylib.", ".framework.", ".tbd."];
102-
let filename_path = Path::new(new_filename);
103-
// strip lib extension from the filename
104-
if let (Some(stem), Some(extension)) = (filename_path.file_stem(), filename_path.extension().and_then(OsStr::to_str)) {
105-
if LIB_EXTS.iter().any(|e| e.trim_matches('.').eq_ignore_ascii_case(extension)) {
106-
new_filename = stem;
107-
}
108-
}
109-
if let Some(mut file) = new_filename.to_str() {
110-
let orig_len = file.len();
111-
112-
// strip "lib" prefix from the filename unless targeting MSVC
113-
if !*TARGET_ENV_MSVC {
114-
file = file.strip_prefix("lib").unwrap_or(file);
115-
}
116-
117-
// strip lib extension + suffix (e.g. .so.4.6.0) from the filename
118-
LIB_EXTS.iter().for_each(|&inner_ext| {
119-
if let Some(inner_ext_idx) = file.find(inner_ext) {
120-
file = &file[..inner_ext_idx];
121-
}
122-
});
123-
if orig_len != file.len() {
124-
new_filename = OsStr::new(file);
125-
}
126-
}
127-
if new_filename.len() != filename.len() {
128-
Some(new_filename)
129-
} else {
130-
None
131-
}
132-
}
133-
13497
fn get_module_header_dir(header_dir: &Path) -> Option<PathBuf> {
13598
let mut out = header_dir.join("opencv2.framework/Headers");
13699
if out.exists() {

build/cmake_probe.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use semver::Version;
99
use shlex::Shlex;
1010

1111
use super::library::Linkage;
12-
use super::Result;
12+
use super::{Result, TARGET_ENV_MSVC};
1313

1414
#[derive(Debug, PartialEq, Eq)]
1515
pub struct LinkLib(pub Linkage, pub String);
@@ -25,6 +25,46 @@ impl LinkLib {
2525
};
2626
format!("cargo:rustc-link-lib={link_spec}{}", self.1)
2727
}
28+
29+
/// Returns Some(new_file_name) if some parts of the filename were removed, None otherwise
30+
pub fn with_cleaned_up_lib_filename(self) -> Option<Self> {
31+
if matches!(self.0, Linkage::Static) {
32+
Some(self)
33+
} else {
34+
let mut new_filename = Path::new(&self.1).as_os_str();
35+
// used to check for the file extension (with dots stripped) and for the part of the filename
36+
const LIB_EXTS: [&str; 7] = [".so.", ".a.", ".dll.", ".lib.", ".dylib.", ".framework.", ".tbd."];
37+
let filename_path = Path::new(new_filename);
38+
// strip lib extension from the filename
39+
if let (Some(stem), Some(extension)) = (filename_path.file_stem(), filename_path.extension().and_then(OsStr::to_str)) {
40+
if LIB_EXTS.iter().any(|e| e.trim_matches('.').eq_ignore_ascii_case(extension)) {
41+
new_filename = stem;
42+
}
43+
}
44+
if let Some(mut file) = new_filename.to_str() {
45+
let orig_len = file.len();
46+
47+
// strip "lib" prefix from the filename unless targeting MSVC
48+
if !*TARGET_ENV_MSVC {
49+
file = file.strip_prefix("lib").unwrap_or(file);
50+
}
51+
52+
// strip lib extension + suffix (e.g. .so.4.6.0) from the filename
53+
LIB_EXTS.iter().for_each(|&inner_ext| {
54+
if let Some(inner_ext_idx) = file.find(inner_ext) {
55+
file = &file[..inner_ext_idx];
56+
}
57+
});
58+
if orig_len != file.len() {
59+
new_filename = OsStr::new(file);
60+
}
61+
}
62+
new_filename
63+
.to_str()
64+
.filter(|new_filename| new_filename.len() != self.1.len())
65+
.map(|new_filename| Self(self.0, new_filename.to_string()))
66+
}
67+
}
2868
}
2969

3070
impl From<&str> for LinkLib {
@@ -203,30 +243,27 @@ impl<'r> CmakeProbe<'r> {
203243
link_libs.push(LinkLib(Linkage::Framework, name));
204244
} else if !arg.starts_with('-') {
205245
let path = Path::new(arg);
206-
let linkage = if Self::is_library_static(path) {
246+
let linkage = if Self::is_library_static_archive(path) {
207247
Linkage::Static
208248
} else {
209-
Linkage::Default
210-
};
211-
if let Some(file) = path.file_name().and_then(super::cleanup_lib_filename) {
212249
if let Some(parent) = path.parent().map(|p| p.to_owned()) {
213-
let search_path = LinkSearch(linkage, parent);
250+
let search_path = LinkSearch(Linkage::Default, parent);
214251
if !link_paths.contains(&search_path) {
215252
link_paths.push(search_path);
216253
}
217254
} else {
218255
panic!("{}", arg.to_string());
219256
}
220-
let file = file.to_str().expect("Non-UTF8 filename");
221-
link_libs.push(LinkLib(linkage, file.to_string()));
222-
}
257+
Linkage::Default
258+
};
259+
link_libs.push(LinkLib(linkage, path.to_str().expect("Non-UTF8 filename").to_string()));
223260
} else {
224261
eprintln!("=== Unexpected cmake compiler argument found: {arg}");
225262
}
226263
}
227264
}
228265

229-
fn is_library_static(path: &Path) -> bool {
266+
fn is_library_static_archive(path: &Path) -> bool {
230267
path.extension().map_or(false, |ext| ext.eq_ignore_ascii_case("a"))
231268
}
232269

build/library.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use dunce::canonicalize;
88
use semver::Version;
99

1010
use super::cmake_probe::{CmakeProbe, LinkLib, LinkSearch};
11-
use super::{cleanup_lib_filename, get_version_from_headers, Result, MANIFEST_DIR, OUT_DIR, TARGET_VENDOR_APPLE};
11+
use super::{get_version_from_headers, Result, MANIFEST_DIR, OUT_DIR, TARGET_VENDOR_APPLE};
1212

1313
struct PackageName;
1414

@@ -114,15 +114,9 @@ pub struct Library {
114114

115115
impl Library {
116116
fn process_library_list(libs: impl IntoIterator<Item = LinkLib>) -> impl Iterator<Item = LinkLib> {
117-
libs.into_iter().filter_map(|LinkLib(linkage, path)| {
118-
let path = Path::new(&path);
119-
if let Some(filename) = path.file_name() {
120-
let filename = cleanup_lib_filename(filename).unwrap_or(filename);
121-
filename.to_str().map(|f| LinkLib(linkage, f.to_owned()))
122-
} else {
123-
None
124-
}
125-
})
117+
libs
118+
.into_iter()
119+
.filter_map(|link_lib| link_lib.with_cleaned_up_lib_filename())
126120
}
127121

128122
fn version_from_include_paths(include_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> Option<Version> {

0 commit comments

Comments
 (0)