Skip to content

Commit 59e7b91

Browse files
committed
Solve more issues in build.rs.
1 parent 4e91b70 commit 59e7b91

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

source/ports/rs_port/build.rs

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ struct InstallPath {
1515
names: Vec<&'static str>,
1616
}
1717

18+
/// Represents the match of a library when it's found
19+
struct LibraryPath {
20+
path: PathBuf,
21+
library: String,
22+
}
23+
1824
/// Find files recursively in a directory matching a pattern
1925
fn find_files_recursively<P: AsRef<Path>>(
2026
root_dir: P,
@@ -70,7 +76,7 @@ fn platform_install_paths() -> Result<InstallPath, Box<dyn std::error::Error>> {
7076
PathBuf::from("/opt/homebrew/lib/"),
7177
PathBuf::from("/usr/local/lib/"),
7278
],
73-
names: vec!["metacall.dylib"],
79+
names: vec!["libmetacall.dylib"],
7480
})
7581
} else if cfg!(target_os = "linux") {
7682
Ok(InstallPath {
@@ -89,39 +95,63 @@ fn get_search_config() -> Result<InstallPath, Box<dyn std::error::Error>> {
8995
// For custom paths, we need to search for any metacall library variant
9096
return Ok(InstallPath {
9197
paths: vec![PathBuf::from(custom_path)],
92-
names: vec!["libmetacall.so", "libmetacalld.so", "libmetacall.dylib", "libmetacalld.dylib", "metacall.dll", "metacalld.dll"]
98+
names: vec![
99+
"libmetacall.so",
100+
"libmetacalld.so",
101+
"libmetacall.dylib",
102+
"libmetacalld.dylib",
103+
"metacall.dll",
104+
"metacalld.dll",
105+
],
93106
});
94107
}
95108

96109
// Fall back to platform-specific paths
97110
platform_install_paths()
98111
}
99112

113+
/// Get the parent path and library name
114+
fn get_parent_and_library(path: &Path) -> Option<(PathBuf, String)> {
115+
let parent = path.parent()?.to_path_buf();
116+
117+
// Get the file stem (filename without extension)
118+
let stem = path.file_stem()?.to_str()?;
119+
120+
// Remove "lib" prefix if present
121+
let cleaned_stem = stem.strip_prefix("lib").unwrap_or(stem).to_string();
122+
123+
Some((parent, cleaned_stem))
124+
}
125+
100126
/// Find the MetaCall library
101127
/// This orchestrates the search process
102-
fn find_metacall_library() -> Result<PathBuf, Box<dyn std::error::Error>> {
128+
fn find_metacall_library() -> Result<LibraryPath, Box<dyn std::error::Error>> {
103129
let search_config = get_search_config()?;
104130

105131
// Search in each configured path
106132
for search_path in &search_config.paths {
107133
for name in &search_config.names {
108134
// Search with no limit in depth
109-
match find_files_recursively(search_path, &name.to_string(), None) {
135+
match find_files_recursively(search_path, name, None) {
110136
Ok(files) if !files.is_empty() => {
111137
let found_lib = fs::canonicalize(&files[0])?;
112138

113-
return Ok(found_lib.clone());
139+
match get_parent_and_library(&found_lib) {
140+
Some((parent, name)) => {
141+
return Ok(LibraryPath {
142+
path: parent,
143+
library: name,
144+
})
145+
}
146+
None => continue,
147+
};
114148
}
115149
Ok(_) => {
116150
// No files found in this path, continue searching
117151
continue;
118152
}
119153
Err(e) => {
120-
eprintln!(
121-
"Error searching in {}: {}",
122-
search_path.display(),
123-
e
124-
);
154+
eprintln!("Error searching in {}: {}", search_path.display(), e);
125155
continue;
126156
}
127157
}
@@ -167,16 +197,15 @@ fn main() {
167197
// When building from Cargo, try to find MetaCall
168198
match find_metacall_library() {
169199
Ok(lib_path) => {
170-
// Extract the directory containing the library
171-
if let Some(lib_dir) = lib_path.parent() {
172-
println!("cargo:rustc-link-search=native={}", lib_dir.display());
173-
println!("cargo:rustc-link-lib=dylib=metacall");
174-
}
200+
println!("cargo:rustc-link-search=native={}", lib_path.path.display());
201+
println!("cargo:rustc-link-lib=dylib={}", lib_path.library);
175202
}
176203
Err(e) => {
177204
// Print the error
178-
eprintln!("Failed to find MetaCall library with: {e} \
179-
Still trying to link in case the library is in system paths");
205+
eprintln!(
206+
"Failed to find MetaCall library with: {e} \
207+
Still trying to link in case the library is in system paths"
208+
);
180209

181210
// Still try to link in case the library is in system paths
182211
println!("cargo:rustc-link-lib=dylib=metacall")

0 commit comments

Comments
 (0)