3030#include < reflect/reflect_scope.h>
3131#include < reflect/reflect_type.h>
3232
33+ #include < dynlink/dynlink.h>
34+
3335#include < log/log.h>
3436
3537#include < filesystem>
@@ -65,8 +67,9 @@ union loader_impl_function_cast
6567 int (*fn)(void *, void *, void *);
6668};
6769
68- dynlink ext_loader_impl_load_from_file_dynlink (loader_impl_ext ext_impl, const loader_path path);
69- int ext_loader_impl_load_from_file_handle (loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path);
70+ static dynlink ext_loader_impl_load_from_file_dynlink (const char *path, const char *library_name);
71+ static dynlink ext_loader_impl_load_from_file_dynlink (loader_impl_ext ext_impl, const loader_path path);
72+ static int ext_loader_impl_load_from_file_handle (loader_impl_ext ext_impl, loader_impl_ext_handle ext_handle, const loader_path path);
7073static void ext_loader_impl_destroy_handle (loader_impl_ext_handle ext_handle);
7174
7275int ext_loader_impl_initialize_types (loader_impl impl)
@@ -122,6 +125,26 @@ int ext_loader_impl_execution_path(loader_impl impl, const loader_path path)
122125 return 0 ;
123126}
124127
128+ dynlink ext_loader_impl_load_from_file_dynlink (const char *path, const char *library_name)
129+ {
130+ /* This function will try to check if the library exists before loading it,
131+ so we avoid error messages from dynlink when guessing the file path for relative load from file */
132+ dynlink_name_impl platform_name;
133+
134+ dynlink_platform_name (library_name, platform_name);
135+
136+ std::filesystem::path lib_path (path);
137+
138+ lib_path /= platform_name;
139+
140+ if (std::filesystem::exists (lib_path) == false )
141+ {
142+ return NULL ;
143+ }
144+
145+ return dynlink_load (path, library_name, DYNLINK_FLAGS_BIND_LAZY | DYNLINK_FLAGS_BIND_GLOBAL);
146+ }
147+
125148dynlink ext_loader_impl_load_from_file_dynlink (loader_impl_ext ext_impl, const loader_path path)
126149{
127150 std::string lib_path_str (path);
@@ -136,18 +159,14 @@ dynlink ext_loader_impl_load_from_file_dynlink(loader_impl_ext ext_impl, const l
136159 if (lib_path.is_absolute ())
137160 {
138161 std::filesystem::path lib_dir = lib_path.parent_path ();
139- dynlink lib = dynlink_load (lib_dir.string ().c_str (), lib_name.c_str (), DYNLINK_FLAGS_BIND_LAZY | DYNLINK_FLAGS_BIND_GLOBAL);
140162
141- if (lib != NULL )
142- {
143- return lib;
144- }
163+ return ext_loader_impl_load_from_file_dynlink (lib_dir.string ().c_str (), lib_name.c_str ());
145164 }
146165 else
147166 {
148167 for (auto exec_path : ext_impl->paths )
149168 {
150- dynlink lib = dynlink_load (exec_path.string ().c_str (), lib_name.c_str (), DYNLINK_FLAGS_BIND_LAZY | DYNLINK_FLAGS_BIND_GLOBAL );
169+ dynlink lib = ext_loader_impl_load_from_file_dynlink (exec_path.string ().c_str (), lib_name.c_str ());
151170
152171 if (lib != NULL )
153172 {
0 commit comments