Arcane  4.2.3.0
User documentation
Loading...
Searching...
No Matches
NativeDynamicLibraryLoader.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* NativeDynamicLibraryLoader.cc (C) 2000-2026 */
9/* */
10/* Dynamic library loader with Glib (uses gmodule). */
11/*---------------------------------------------------------------------------*/
12
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"
17
18#include <iostream>
19#include <set>
20#include <string>
21#include <vector>
22
23#ifdef ARCCORE_OS_WIN32
24#include <windows.h>
25#else
26#include <dlfcn.h>
27#endif
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane::NativeLoading
33{
34class NativeLibrary;
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39#ifdef ARCCORE_OS_WIN32
40using NativeHandle = HMODULE;
41#else
42using NativeHandle = void*;
43#endif
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
48std::string
49_toStdString(const String& value)
50{
51 return std::string(value.toStdStringView());
52}
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57std::string
58_fileName(const std::string& name)
59{
60#ifdef ARCCORE_OS_WIN32
61 if (name.size() >= 4 && name.substr(name.size() - 4) == ".dll")
62 return name;
63 return name + ".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")
69 result += ".dylib";
70 return result;
71#else
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")
76 result += ".so";
77 return result;
78#endif
79}
80
81/*---------------------------------------------------------------------------*/
82/*---------------------------------------------------------------------------*/
83
84std::string
85_path(const std::string& directory, const std::string& file)
86{
87 if (directory.empty() || directory == ".")
88 return "./" + file;
89 if (directory.back() == '/' || directory.back() == '\\')
90 return directory + file;
91#ifdef ARCCORE_OS_WIN32
92 return directory + "\\" + file;
93#else
94 return directory + "/" + file;
95#endif
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101#ifdef ARCCORE_OS_WIN32
102std::wstring
103_toWideString(const std::string& value)
104{
105 int n = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(),
106 static_cast<int>(value.size()), nullptr, 0);
107 if (n <= 0)
108 return {};
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))
112 return {};
113 return result;
114}
115#endif
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120class NativeLoader
121: public IDynamicLibraryLoader
122{
123 public:
124
125 NativeLoader()
126 {
127 String value = platform::getEnvironmentVariable("ARCANE_VERBOSE_DYNAMICLIBRARY");
128 m_is_verbose = (value == "1" || value == "true");
129 }
130
131 void build() override {}
132
133 IDynamicLibrary* open(const String& directory, const String& name) override;
134 void closeLibraries() override;
135 void removeInstance(NativeLibrary* library);
136
137 private:
138
139 IDynamicLibrary* _tryOpen(const std::string& path);
140
141 private:
142
143 bool m_is_verbose = false;
144 std::set<NativeLibrary*> m_opened_libraries;
145};
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
150class NativeLibrary
151: public IDynamicLibrary
152{
153 public:
154
155 NativeLibrary(NativeLoader* manager, NativeHandle handle)
156 : m_manager(manager)
157 , m_handle(handle)
158 {}
159 void close() override;
160 void* getSymbolAddress(const String& symbol_name, bool* is_found) override;
161
162 private:
163
164 NativeLoader* m_manager;
165 NativeHandle m_handle;
166};
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171IDynamicLibrary* NativeLoader::
172_tryOpen(const std::string& path)
173{
174 if (m_is_verbose)
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());
179#else
180 NativeHandle handle = ::dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
181#endif
182 if (m_is_verbose)
183 std::cout << (handle ? " OK\n" : " NOT FOUND\n");
184 if (!handle)
185 return nullptr;
186 auto* library = new NativeLibrary(this, handle);
187 m_opened_libraries.insert(library);
188 return library;
189}
190
191/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
193
194IDynamicLibrary* NativeLoader::
195open(const String& directory, const String& name)
196{
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)))
209 return library;
210 return nullptr;
211}
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
216void NativeLoader::
217closeLibraries()
218{
219 std::vector<NativeLibrary*> libraries(m_opened_libraries.begin(), m_opened_libraries.end());
220 for (auto* library : libraries) {
221 library->close();
222 delete library;
223 }
224}
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
228
229void NativeLoader::
230removeInstance(NativeLibrary* library)
231{
232 m_opened_libraries.erase(library);
233}
234
235/*---------------------------------------------------------------------------*/
236/*---------------------------------------------------------------------------*/
237
238void NativeLibrary::
239close()
240{
241 if (!m_handle)
242 return;
243#ifdef ARCCORE_OS_WIN32
244 bool is_ok = ::FreeLibrary(m_handle) != 0;
245#else
246 bool is_ok = ::dlclose(m_handle) == 0;
247#endif
248 m_handle = nullptr;
249 if (!is_ok)
250 std::cerr << "WARNING: can not unload module\n";
251 m_manager->removeInstance(this);
252}
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
256
257void* NativeLibrary::
258getSymbolAddress(const String& symbol_name, bool* is_found)
259{
260 if (is_found)
261 *is_found = false;
262 if (!m_handle)
263 return nullptr;
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);
268#else
269 (void)::dlerror();
270 void* address = ::dlsym(m_handle, symbol.c_str());
271 bool found = (::dlerror() == nullptr);
272#endif
273 if (is_found)
274 *is_found = found;
275 return address;
276}
277
278namespace
279{
280 NativeLoader native_loader;
281}
282
283/*---------------------------------------------------------------------------*/
284/*---------------------------------------------------------------------------*/
285
286} // namespace Arcane::NativeLoading
287
288/*---------------------------------------------------------------------------*/
289/*---------------------------------------------------------------------------*/
290
291namespace Arcane
292{
293
294extern "C++" IDynamicLibraryLoader*
295getDefaultGlibDynamicLibraryLoader();
296
297/*---------------------------------------------------------------------------*/
298/*---------------------------------------------------------------------------*/
299
300IDynamicLibraryLoader* IDynamicLibraryLoader::
301getDefault()
302{
303 // At the moment use Glib dynamic library loader by default
304 bool use_glib = true;
305 String value = platform::getEnvironmentVariable("ARCANE_USE_GLIB_DYNAMICLIBRARY_LOADER");
306 if (value == "0")
307 use_glib = false;
308#if !defined(ARCCORE_HAS_GLIB)
309 use_glib = false;
310#endif
311 if (use_glib)
312 return getDefaultGlibDynamicLibraryLoader();
313 return &NativeLoading::native_loader;
314}
315
316/*---------------------------------------------------------------------------*/
317/*---------------------------------------------------------------------------*/
318
319} // namespace Arcane
320
321/*---------------------------------------------------------------------------*/
322/*---------------------------------------------------------------------------*/
String getEnvironmentVariable(const String &name)
Environment variable named name.