36class DynamicLibrariesManagerPrivate
39 DynamicLibrariesManagerPrivate();
41 bool isSomethingSimilarLoaded(
const std::string& libName)
const;
43 std::map<std::string, std::shared_ptr< QLibrary > > m_handles;
46 std::vector<std::string> m_supplementarySearchPath;
51static std::string ownLibraryDirectory()
54 if (dladdr(
reinterpret_cast<const void*
>(&ownLibraryDirectory), &info) == 0
55 || info.dli_fname ==
nullptr)
57 return boost::filesystem::path(info.dli_fname).parent_path().string();
63static std::string ownLibraryDirectory()
65 HMODULE module =
nullptr;
66 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
67 | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
68 reinterpret_cast<LPCWSTR
>(&ownLibraryDirectory),
72 auto length = GetModuleFileNameW(module, path,
sizeof(path) /
sizeof(path[0]));
73 if (length == 0 || length >=
sizeof(path) /
sizeof(path[0]))
75 return boost::filesystem::path(std::wstring(path, length)).parent_path().string();
79DynamicLibrariesManagerPrivate::DynamicLibrariesManagerPrivate() :
81 m_supplementarySearchPath()
89 auto dir = ownLibraryDirectory();
91 m_supplementarySearchPath.push_back(dir);
92#elif defined(__APPLE__)
96 auto dir = ownLibraryDirectory();
98 m_supplementarySearchPath.push_back(dir);
102bool DynamicLibrariesManagerPrivate::isSomethingSimilarLoaded(
const std::string& libName)
const
104 bool alreadyLoaded =
false;
107 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
110 hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
111 if (hModuleSnap == INVALID_HANDLE_VALUE)
112 throw LimaException(
"CreateToolhelp32Snapshoot returned INVALID_HANDLE_VALUE.");
114 me32.dwSize =
sizeof(MODULEENTRY32);
116 if (!Module32First(hModuleSnap, &me32))
118 CloseHandle(hModuleSnap);
119 throw LimaException(
"Module32First failed.");
127 boost::filesystem::path p(me32.szModule);
130 CloseHandle(hModuleSnap);
134 std::string fn = p.filename().string();
135 if (fn.find(libName, 0) == 0)
138 CloseHandle(hModuleSnap);
142 }
while (Module32Next(hModuleSnap, &me32));
144 CloseHandle(hModuleSnap);
148 std::pair<bool&, const std::string&> data(alreadyLoaded, libName);
150 dl_iterate_phdr([](dl_phdr_info *info,
size_t size,
void* data) ->
int {
154 throw LimaException(
"Callback passed to dl_iterate_phdr unexpectedly got NULL in \"data\" argument.");
156 auto& d = *((std::pair<bool&, const std::string&>*)data);
160 if (info ==
nullptr || info->dlpi_name ==
nullptr)
161 throw LimaException(
"Callback passed to dl_iterate_phdr unexpectedly got NULL in \"info\" argument.");
163 std::string s(info->dlpi_name);
167 boost::filesystem::path p(info->dlpi_name);
168 std::string fn = p.filename().string();
169 if (fn.find(d.second, 0) == 3)
179 return alreadyLoaded;
182DynamicLibrariesManager::DynamicLibrariesManager() :
193 m_d->isSomethingSimilarLoaded(libName);
194 auto it = m_d->m_handles.find(libName);
195 return (it != m_d->m_handles.end());
201 LDEBUG <<
"DynamicLibrariesManager::loadLibrary() -- libName=" << libName;
203 if (m_d->isSomethingSimilarLoaded(libName))
205 LTRACE <<
"DynamicLibrariesManager::loadLibrary trying to reload dynamic library " << libName;
206 LTRACE <<
"DynamicLibrariesManager::loadLibrary this is not an error, silently ignoring.";
210 auto it = m_d->m_handles.find(libName);
211 if (it != m_d->m_handles.end())
213 LTRACE <<
"DynamicLibrariesManager::loadLibrary trying to reload dynamic library." << libName;
214 LTRACE <<
"DynamicLibrariesManager::loadLibrary this is not an error, silently ignoring.";
218 std::shared_ptr< QLibrary > libhandle;
219 std::vector< std::pair< std::string, std::string> > triedPaths;
221 for (
const auto& path: m_d->m_supplementarySearchPath)
223 LDEBUG <<
"Trying supplementary " << (path+
"/"+libName).c_str();
231 auto dllPath = boost::filesystem::path(path) / (libName +
".dll");
232 if (boost::filesystem::exists(dllPath))
234 if (LoadLibraryExW(dllPath.wstring().c_str(),
nullptr,
235 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
236 | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) ==
nullptr)
239 LERROR <<
"DynamicLibrariesManager::loadLibrary() -- LoadLibraryExW"
240 << dllPath.string() <<
"failed with error" << GetLastError();
245 auto candidate = path +
"/" + libName;
251 if (!boost::filesystem::exists(path +
"/lib" + libName +
".dylib")
252 && boost::filesystem::is_directory(path))
254 for (
const auto& entry: boost::filesystem::directory_iterator(path))
256 auto name = entry.path().filename().string();
257 if (name.rfind(
"lib" + libName +
".", 0) == 0
258 && name.size() > 6 && name.substr(name.size() - 6) ==
".dylib")
260 candidate = entry.path().string();
266 libhandle = std::make_shared<QLibrary>(candidate.c_str());
268 libhandle->setLoadHints(QLibrary::ResolveAllSymbolsHint
269 | QLibrary::ExportExternalSymbolsHint);
270 if (libhandle->load())
272 m_d->m_handles.insert(std::make_pair(libName,libhandle));
273 LDEBUG <<
"the library " << libName.c_str()
274 <<
" was loaded from supplementary search path";
275 LDEBUG <<
"the library fully-qualified name: " << libhandle->fileName();
281 LDEBUG <<
"DynamicLibrariesManager::loadLibrary() -- "
282 <<
"Failed to open supplementary lib " << libhandle->errorString();
283 triedPaths.emplace_back( path, libhandle->errorString().toStdString() );
291 LDEBUG <<
"Trying " << libName;
292 libhandle = std::make_shared<QLibrary>( libName.c_str() );
293 libhandle->setLoadHints(QLibrary::ResolveAllSymbolsHint
294 | QLibrary::ExportExternalSymbolsHint);
295 if (libhandle->load())
297 m_d->m_handles.insert(std::make_pair(libName,libhandle));
298 LDEBUG <<
"the library " << libName
299 <<
" was loaded from system default search path";
300 LDEBUG <<
"the library fully-qualified name: " << libhandle->fileName();
306 for (
const auto& pathErr: triedPaths)
308 LERROR <<
"Failed to load library " << libName <<
"from " << pathErr.first <<
" due to " << pathErr.second;
310 LERROR <<
"Failed to load library " << libName <<
"also from system paths";
311 LERROR <<
"DynamicLibrariesManager::loadLibrary() -- "
312 <<
"Failed to open system lib " << libhandle->errorString();
318 m_d->m_handles[libName] = libhandle;
319 LDEBUG <<
"the library " << libName <<
" was loaded";
327 m_d->m_supplementarySearchPath.begin(),
328 m_d->m_supplementarySearchPath.end(), searchPath)
329 != m_d->m_supplementarySearchPath.end())
335 LINFO <<
"adding search path '" << searchPath.c_str() <<
"'";
338 m_d->m_supplementarySearchPath.push_back(searchPath);
348#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
349 auto list = searchPathes.replace(
"\\",
"/").split(QRegularExpression(
"[;]"),
350 QString::SkipEmptyParts);
352 auto list = searchPathes.replace(
"\\",
"/").split(QRegularExpression(
"[;]"),
356#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
357 auto list = searchPathes.replace(
"\\",
"/").split(QRegularExpression(
"[:;]"),
358 QString::SkipEmptyParts);
360 auto list = searchPathes.replace(
"\\",
"/").split(QRegularExpression(
"[:;]"),
365 for(
const auto& searchPath: list)
368 LINFO <<
"DynamicLibrariesManager::addSearchPathes() adding:" << searchPath;