61{
62 if (
get(backend) !=
nullptr)
63 {
64 return;
65 }
66
67 const std::string backend_so = "libbackend_" + backend + SHARED_LIB_EXT;
68 void *handle = dlopen(backend_so.c_str(), RTLD_LAZY | RTLD_LOCAL);
69
70 if (handle == nullptr)
71 {
72 VERBOSE(BackendManager) <<
"Failed to load backend '" << backend <<
"' - " << dlerror() <<
"\n";
73 return;
74 }
75
76 VERBOSE(BackendManager) <<
"Successfully loaded '" << backend <<
"'(" << backend_so <<
")\n";
77
78 {
79
80 auto backend_create = (
backend_create_t)dlsym(handle,
"onert_backend_create");
81 if (backend_create == nullptr)
82 {
83
84 fprintf(stderr, "BackendManager: unable to find function `onert_backend_create` : %s\n",
85 dlerror());
86 dlclose(handle);
87 return;
88 }
89
90
92 if (backend_destroy == nullptr)
93 {
94
95 fprintf(stderr, "BackendManager: unable to find `function onert_backend_destroy` : %s\n",
96 dlerror());
97 dlclose(handle);
98 return;
99 }
100
101 auto backend_object =
102 std::unique_ptr<backend::Backend, backend_destroy_t>(backend_create(), backend_destroy);
103 bool initialized = backend_object->config()->initialize();
104 if (!initialized)
105 {
106 VERBOSE(BackendManager) << backend.c_str()
107 << " backend initialization failed. Don't use this backend"
108 << std::endl;
109 dlclose(handle);
110 return;
111 }
112 _gen_map.emplace(backend_object->config()->id(), std::move(backend_object));
113 }
114
115
116 auto u_handle = std::unique_ptr<void, dlhandle_destroy_t>{
117 handle, [
id = backend, filename = backend_so](
void *
h) {
118 if (dlclose(h) == 0)
119 {
120 VERBOSE(BackendManager) <<
"Successfully unloaded '" <<
id <<
"'(" << filename <<
")\n";
121 }
122 else
123 {
124 VERBOSE(BackendManager) <<
"Failed to unload backend '" <<
id <<
"'- " << dlerror() <<
"\n";
125 }
126 }};
127 _handle_map.emplace(backend, std::move(u_handle));
128}
void(*)(backend::Backend *) backend_destroy_t
backend::Backend *(*)() backend_create_t
#define VERBOSE(name, lv)