1
- #include " icicle/runtime.h"
1
+ #include < iostream>
2
+ #include < dlfcn.h>
3
+ #include < dirent.h>
4
+ #include < sys/stat.h>
5
+ #include < string>
6
+
2
7
#include " icicle/runtime.h"
3
8
#include " icicle/device_api.h"
4
9
#include " icicle/errors.h"
@@ -68,3 +73,62 @@ extern "C" eIcicleError icicle_destroy_stream(icicleStreamHandle stream)
68
73
{
69
74
return DeviceAPI::get_thread_local_deviceAPI ()->destroy_stream (stream);
70
75
}
76
+
77
+ // Determine the shared library extension based on the operating system
78
+ #ifdef __linux__
79
+ const std::string SHARED_LIB_EXTENSION = " .so" ;
80
+ #elif __APPLE__
81
+ const std::string SHARED_LIB_EXTENSION = " .dylib" ;
82
+ #else
83
+ #error "Unsupported operating system"
84
+ #endif
85
+
86
+ extern " C" eIcicleError icicle_load_backend (const std::string& path)
87
+ {
88
+ auto is_shared_library = [](const std::string& filename) {
89
+ return filename.size () >= SHARED_LIB_EXTENSION.size () &&
90
+ filename.compare (
91
+ filename.size () - SHARED_LIB_EXTENSION.size (), SHARED_LIB_EXTENSION.size (), SHARED_LIB_EXTENSION) == 0 ;
92
+ };
93
+
94
+ auto load_library = [](const std::string& filePath) {
95
+ std::cout << " Attempting load: " << filePath << std::endl;
96
+ void * handle = dlopen (filePath.c_str (), RTLD_LAZY | RTLD_GLOBAL);
97
+ if (!handle) { std::cerr << " Failed to load " << filePath << " : " << dlerror () << std::endl; }
98
+ };
99
+
100
+ struct stat pathStat;
101
+ if (stat (path.c_str (), &pathStat) != 0 ) {
102
+ std::cerr << " Cannot access path: " << path << std::endl;
103
+ return eIcicleError::INVALID_ARGUMENT;
104
+ }
105
+
106
+ if (S_ISDIR (pathStat.st_mode )) {
107
+ // Path is a directory, recursively search for libraries
108
+ DIR* dir = opendir (path.c_str ());
109
+ if (!dir) {
110
+ std::cerr << " Cannot open directory: " << path << std::endl;
111
+ return eIcicleError::INVALID_ARGUMENT;
112
+ }
113
+
114
+ struct dirent * entry;
115
+ while ((entry = readdir (dir)) != nullptr ) {
116
+ std::string entryPath = path + " /" + entry->d_name ;
117
+
118
+ // Skip "." and ".." entries
119
+ if (std::string (entry->d_name ) == " ." || std::string (entry->d_name ) == " .." ) { continue ; }
120
+
121
+ // Recurse into subdirectories and load libraries in files
122
+ icicle_load_backend (entryPath);
123
+ }
124
+
125
+ closedir (dir);
126
+ } else if (S_ISREG (pathStat.st_mode )) {
127
+ // Path is a regular file, check if it is a shared library and load it
128
+ if (is_shared_library (path)) { load_library (path); }
129
+ } else {
130
+ std::cerr << " Unsupported file type: " << path << std::endl;
131
+ }
132
+
133
+ return eIcicleError::SUCCESS;
134
+ }
0 commit comments