@@ -12,7 +12,7 @@ use std::{
1212/// Represents the install paths for a platform
1313struct InstallPath {
1414 paths : Vec < PathBuf > ,
15- name : String ,
15+ names : Vec < & ' static str > ,
1616}
1717
1818/// Find files recursively in a directory matching a pattern
@@ -54,32 +54,28 @@ fn find_files_recursively<P: AsRef<Path>>(
5454
5555fn platform_install_paths ( ) -> Result < InstallPath , Box < dyn std:: error:: Error > > {
5656 if cfg ! ( target_os = "windows" ) {
57- // defaults to path:
58- // C:\Users\Default\AppData\Local
57+ // Defaults to path: C:\Users\Default\AppData\Local
5958 let local_app_data = env:: var ( "LOCALAPPDATA" )
6059 . unwrap_or_else ( |_| String :: from ( "C:\\ Users\\ Default\\ AppData\\ Local" ) ) ;
6160
62- println ! ( "windows" ) ;
6361 Ok ( InstallPath {
6462 paths : vec ! [ PathBuf :: from( local_app_data)
6563 . join( "MetaCall" )
6664 . join( "metacall" ) ] ,
67- name : "metacall.dll" . to_string ( ) ,
65+ names : vec ! [ "metacall.dll" ] ,
6866 } )
6967 } else if cfg ! ( target_os = "macos" ) {
70- println ! ( "macos" ) ;
7168 Ok ( InstallPath {
7269 paths : vec ! [
7370 PathBuf :: from( "/opt/homebrew/lib/" ) ,
7471 PathBuf :: from( "/usr/local/lib/" ) ,
7572 ] ,
76- name : "metacall.dylib" . to_string ( ) ,
73+ names : vec ! [ "metacall.dylib" ] ,
7774 } )
7875 } else if cfg ! ( target_os = "linux" ) {
79- println ! ( "linux" ) ;
8076 Ok ( InstallPath {
8177 paths : vec ! [ PathBuf :: from( "/usr/local/lib/" ) , PathBuf :: from( "/gnu/lib/" ) ] ,
82- name : "libmetacall.so" . to_string ( ) ,
78+ names : vec ! [ "libmetacall.so" ] ,
8379 } )
8480 } else {
8581 Err ( format ! ( "Platform {} not supported" , env:: consts:: OS ) . into ( ) )
@@ -93,7 +89,7 @@ fn get_search_config() -> Result<InstallPath, Box<dyn std::error::Error>> {
9389 // For custom paths, we need to search for any metacall library variant
9490 return Ok ( InstallPath {
9591 paths : vec ! [ PathBuf :: from( custom_path) ] ,
96- name : r"^(lib)?metacall(d)?\.(so| dylib|dll)$" . to_string ( ) ,
92+ names : vec ! [ "libmetacall.so" , "libmetacalld.so" , "libmetacall. dylib" , "libmetacalld.dylib" , "metacall.dll" , "metacalld.dll" ]
9793 } ) ;
9894 }
9995
@@ -108,32 +104,26 @@ fn find_metacall_library() -> Result<PathBuf, Box<dyn std::error::Error>> {
108104
109105 // Search in each configured path
110106 for search_path in & search_config. paths {
111- println ! (
112- "cargo:warning=Searching for MetaCall in: {}" ,
113- search_path. display( )
114- ) ;
115-
116- // Only search at depth 0 (current directory)
117- match find_files_recursively ( search_path, & search_config. name , Some ( 0 ) ) {
118- Ok ( files) if !files. is_empty ( ) => {
119- let found_lib = fs:: canonicalize ( & files[ 0 ] ) ?;
120- println ! (
121- "cargo:warning=Found MetaCall library: {}" ,
122- found_lib. display( )
123- ) ;
124- return Ok ( found_lib. clone ( ) ) ;
125- }
126- Ok ( _) => {
127- // No files found in this path, continue searching
128- continue ;
129- }
130- Err ( e) => {
131- println ! (
132- "cargo:warning=Error searching in {}: {}" ,
133- search_path. display( ) ,
134- e
135- ) ;
136- continue ;
107+ for name in & search_config. names {
108+ // Search with no limit in depth
109+ match find_files_recursively ( search_path, & name. to_string ( ) , None ) {
110+ Ok ( files) if !files. is_empty ( ) => {
111+ let found_lib = fs:: canonicalize ( & files[ 0 ] ) ?;
112+
113+ return Ok ( found_lib. clone ( ) ) ;
114+ }
115+ Ok ( _) => {
116+ // No files found in this path, continue searching
117+ continue ;
118+ }
119+ Err ( e) => {
120+ eprintln ! (
121+ "Error searching in {}: {}" ,
122+ search_path. display( ) ,
123+ e
124+ ) ;
125+ continue ;
126+ }
137127 }
138128 }
139129 }
@@ -154,49 +144,43 @@ fn find_metacall_library() -> Result<PathBuf, Box<dyn std::error::Error>> {
154144}
155145
156146fn main ( ) {
157- println ! ( "------------ BEGIN ------------" ) ;
158-
159147 // When running tests from CMake
160148 if let Ok ( val) = env:: var ( "PROJECT_OUTPUT_DIR" ) {
161- println ! ( "cargo:warning=Using CMake build path: {}" , val ) ;
149+ // Link search path to build folder
162150 println ! ( "cargo:rustc-link-search=native={val}" ) ;
163151
152+ // Link against correct version of metacall
164153 match env:: var ( "CMAKE_BUILD_TYPE" ) {
165- Ok ( val) if val == "Debug" => {
166- println ! ( "cargo:rustc-link-lib=dylib=metacalld" ) ;
154+ Ok ( val) => {
155+ if val == "Debug" {
156+ // Try to link the debug version when running tests
157+ println ! ( "cargo:rustc-link-lib=dylib=metacalld" ) ;
158+ } else {
159+ println ! ( "cargo:rustc-link-lib=dylib=metacall" ) ;
160+ }
167161 }
168- _ => {
162+ Err ( _ ) => {
169163 println ! ( "cargo:rustc-link-lib=dylib=metacall" ) ;
170164 }
171165 }
172- return ;
173- }
174-
175- println ! ( "cargo:warning=Using pure Cargo build, searching for MetaCall..." ) ;
176-
177- // When building from Cargo - try to find MetaCall
178- match find_metacall_library ( ) {
179- Ok ( lib_path) => {
180- // Extract the directory containing the library
181- if let Some ( lib_dir) = lib_path. parent ( ) {
182- println ! ( "cargo:rustc-link-search=native={}" , lib_dir. display( ) ) ;
183- }
184-
185- // Link against the library
186- let profile = env:: var ( "PROFILE" ) . unwrap_or_else ( |_| "release" . to_string ( ) ) ;
187- match profile. as_str ( ) {
188- "debug" | "release" => {
189- println ! ( "cargo:rustc-link-lib=dylib=metacall" ) ;
190- }
191- _ => {
166+ } else {
167+ // When building from Cargo, try to find MetaCall
168+ match find_metacall_library ( ) {
169+ 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( ) ) ;
192173 println ! ( "cargo:rustc-link-lib=dylib=metacall" ) ;
193174 }
194175 }
195- }
196- Err ( e) => {
197- println ! ( "cargo:warning={e}" ) ;
198- // Still try to link in case the library is in system paths
199- println ! ( "cargo:rustc-link-lib=dylib=metacall" ) ;
176+ Err ( e) => {
177+ // 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") ;
180+
181+ // Still try to link in case the library is in system paths
182+ println ! ( "cargo:rustc-link-lib=dylib=metacall" )
183+ }
200184 }
201185 }
202186}
0 commit comments