13#include "arccore/base/String.h"
14#include "arccore/base/PlatformUtils.h"
15#include "arccore/base/FatalErrorException.h"
16#include "arccore/base/internal/IDynamicLibraryLoader.h"
23#ifdef ARCCORE_OS_WIN32
32namespace Arcane::NativeLoading
39#ifdef ARCCORE_OS_WIN32
40using NativeHandle = HMODULE;
42using NativeHandle =
void*;
49_toStdString(
const String& value)
51 return std::string(value.toStdStringView());
58_fileName(
const std::string& name)
60#ifdef ARCCORE_OS_WIN32
61 if (name.size() >= 4 && name.substr(name.size() - 4) ==
".dll")
64#elif defined(ARCCORE_OS_MACOS)
65 std::string result = name;
66 if (result.rfind(
"lib", 0) != 0)
67 result =
"lib" + result;
68 if (result.size() < 6 || result.substr(result.size() - 6) !=
".dylib")
72 std::string result = name;
73 if (result.rfind(
"lib", 0) != 0)
74 result =
"lib" + result;
75 if (result.size() < 3 || result.substr(result.size() - 3) !=
".so")
85_path(
const std::string& directory,
const std::string& file)
87 if (directory.empty() || directory ==
".")
89 if (directory.back() ==
'/' || directory.back() ==
'\\')
90 return directory + file;
91#ifdef ARCCORE_OS_WIN32
92 return directory +
"\\" + file;
94 return directory +
"/" + file;
101#ifdef ARCCORE_OS_WIN32
103_toWideString(
const std::string& value)
105 int n = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(),
106 static_cast<int>(value.size()),
nullptr, 0);
109 std::wstring result(
static_cast<size_t>(n), L
'\0');
110 if (!::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(),
111 static_cast<int>(value.size()), result.data(), n))
128 m_is_verbose = (value ==
"1" || value ==
"true");
131 void build()
override {}
143 bool m_is_verbose =
false;
144 std::set<NativeLibrary*> m_opened_libraries;
155 NativeLibrary(
NativeLoader* manager, NativeHandle handle)
159 void close()
override;
165 NativeHandle m_handle;
172_tryOpen(
const std::string& path)
175 std::cout <<
"** Load Dynamic Library '" << path <<
"'...";
176#ifdef ARCCORE_OS_WIN32
177 std::wstring wide_path = _toWideString(path);
178 NativeHandle handle = wide_path.empty() ? nullptr : ::LoadLibraryW(wide_path.c_str());
180 NativeHandle handle = ::dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
183 std::cout << (handle ?
" OK\n" :
" NOT FOUND\n");
187 m_opened_libraries.insert(library);
197 std::string requested_name = _toStdString(name);
198 std::string built_name = _fileName(requested_name);
199 std::vector<std::string> names{ built_name };
200 if (built_name != requested_name)
201 names.push_back(requested_name);
202 std::string requested_directory = _toStdString(directory);
203 std::vector<std::string> directories{ requested_directory };
204 if (requested_directory !=
".")
205 directories.emplace_back(
".");
206 for (
const auto& dir : directories)
207 for (
const auto& file : names)
208 if (
auto* library = _tryOpen(_path(dir, file)))
219 std::vector<NativeLibrary*> libraries(m_opened_libraries.begin(), m_opened_libraries.end());
220 for (
auto* library : libraries) {
232 m_opened_libraries.erase(library);
243#ifdef ARCCORE_OS_WIN32
244 bool is_ok = ::FreeLibrary(m_handle) != 0;
246 bool is_ok = ::dlclose(m_handle) == 0;
250 std::cerr <<
"WARNING: can not unload module\n";
251 m_manager->removeInstance(
this);
264 std::string symbol = _toStdString(symbol_name);
265#ifdef ARCCORE_OS_WIN32
266 void* address =
reinterpret_cast<void*
>(::GetProcAddress(m_handle, symbol.c_str()));
267 bool found = (address !=
nullptr);
270 void* address = ::dlsym(m_handle, symbol.c_str());
271 bool found = (::dlerror() ==
nullptr);
294extern "C++" IDynamicLibraryLoader*
295getDefaultGlibDynamicLibraryLoader();
304 bool use_glib =
true;
308#if !defined(ARCCORE_HAS_GLIB)
312 return getDefaultGlibDynamicLibraryLoader();
313 return &NativeLoading::native_loader;
Interface of a dynamic library loader.
static IDynamicLibraryLoader * getDefault()
Service used for dynamically loading libraries.
Interface of a dynamic library.
void close() override
Closes the dynamic library.
void * getSymbolAddress(const String &symbol_name, bool *is_found) override
Returns the address of the symbol named symbol_name.
IDynamicLibrary * open(const String &directory, const String &name) override
Loads a dynamic library.
void closeLibraries() override
Closes all libraries opened via open().
Unicode character string.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --