eOn 3.2.0
Long-timescale dynamics: aKMC, NEB, parallel replica
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
PluginLoader.cpp
Go to the documentation of this file.
1
/*
2
** This file is part of eOn.
3
**
4
** SPDX-License-Identifier: BSD-3-Clause
5
**
6
** Copyright (c) 2010--present, eOn Development Team
7
** All rights reserved.
8
**
9
** Repo:
10
** https://github.com/TheochemUI/eOn
11
*/
12
#include "
eon/potentials/PluginLoader.h
"
13
14
#include <cstdlib>
15
#include <filesystem>
16
#include <sstream>
17
18
namespace
eonc
{
19
20
PluginLoader
&
PluginLoader::instance
() {
21
static
PluginLoader
loader;
22
return
loader;
23
}
24
25
PluginLoader::PluginLoader
(
Key
)
26
:
PluginLoader
() {}
27
28
PluginLoader::PluginLoader
() {
29
// Seed from EON_POTENTIALS_PATH env var (colon-separated on POSIX).
30
// Config-file paths are prepended later via add_config_paths().
31
const
char
*env = std::getenv(
"EON_POTENTIALS_PATH"
);
32
if
(env && *env) {
33
#ifdef _WIN32
34
append_paths
(env,
';'
);
35
#else
36
append_paths
(env,
':'
);
37
#endif
38
}
39
}
40
41
PluginLoader::~PluginLoader
() {
42
for
(
auto
&[name, handle] :
m_handles
) {
43
dynlib::close
(handle);
44
}
45
}
46
47
void
PluginLoader::append_paths
(
const
std::string &path_str,
char
sep) {
48
std::string::size_type start = 0;
49
while
(start < path_str.size()) {
50
auto
pos = path_str.find(sep, start);
51
if
(pos == std::string::npos)
52
pos = path_str.size();
53
if
(pos > start) {
54
std::string p = path_str.substr(start, pos - start);
55
bool
dup =
false
;
56
for
(
const
auto
&existing :
m_search_paths
) {
57
if
(existing == p) {
58
dup =
true
;
59
break
;
60
}
61
}
62
if
(!dup)
63
m_search_paths
.push_back(std::move(p));
64
}
65
start = pos + 1;
66
}
67
}
68
69
void
PluginLoader::add_config_paths
(
const
std::string &colon_paths) {
70
if
(colon_paths.empty())
71
return
;
72
std::lock_guard<std::mutex> lock(
m_mutex
);
73
// Config paths have higher priority than env-var paths: insert at front.
74
std::vector<std::string> new_paths;
75
#ifdef _WIN32
76
const
char
sep =
';'
;
77
#else
78
const
char
sep =
':'
;
79
#endif
80
std::string::size_type start = 0;
81
while
(start < colon_paths.size()) {
82
auto
pos = colon_paths.find(sep, start);
83
if
(pos == std::string::npos)
84
pos = colon_paths.size();
85
if
(pos > start) {
86
std::string p = colon_paths.substr(start, pos - start);
87
bool
dup =
false
;
88
for
(
const
auto
&existing :
m_search_paths
) {
89
if
(existing == p) {
90
dup =
true
;
91
break
;
92
}
93
}
94
if
(!dup)
95
new_paths.push_back(std::move(p));
96
}
97
start = pos + 1;
98
}
99
m_search_paths
.insert(
m_search_paths
.begin(), new_paths.begin(),
100
new_paths.end());
101
}
102
103
std::vector<std::string>
PluginLoader::lib_names
(
const
char
*lib_base)
const
{
104
std::vector<std::string> names;
105
#ifdef _WIN32
106
names.push_back(std::string(lib_base) +
".dll"
);
107
names.push_back(std::string(
"lib"
) + lib_base +
".dll"
);
108
#elif defined(__APPLE__)
109
names.push_back(std::string(
"lib"
) + lib_base +
".dylib"
);
110
#else
111
names.push_back(std::string(
"lib"
) + lib_base +
".so"
);
112
#endif
113
return
names;
114
}
115
116
dynlib::Handle
PluginLoader::open_lib
(
const
char
*lib_base) {
117
std::lock_guard<std::mutex> lock(
m_mutex
);
118
119
auto
it =
m_handles
.find(lib_base);
120
if
(it !=
m_handles
.end()) {
121
return
it->second;
122
}
123
124
auto
names =
lib_names
(lib_base);
125
dynlib::Handle
h{};
126
std::string last_err;
127
bool
any_file =
false
;
128
129
// Probe the filesystem first. dlopen of a present plugin runs that
130
// library's static initializers (Fortran / LAMMPS banners), so skip
131
// names that are not on disk.
132
for
(
const
auto
&dir :
m_search_paths
) {
133
for
(
const
auto
&name : names) {
134
std::string full = (std::filesystem::path(dir) / name).
string
();
135
std::error_code ec;
136
if
(!std::filesystem::exists(full, ec))
137
continue
;
138
any_file =
true
;
139
h =
dynlib::open
(full.c_str());
140
if
(h) {
141
m_handles
[lib_base] = h;
142
return
h;
143
}
144
// Capture immediately after LoadLibrary/dlopen (GetLastError/dlerror).
145
last_err =
dynlib::error
();
146
}
147
}
148
149
// Fall back to system search (LD_LIBRARY_PATH, etc.)
150
for
(
const
auto
&name : names) {
151
h =
dynlib::open
(name.c_str());
152
if
(h) {
153
m_handles
[lib_base] = h;
154
return
h;
155
}
156
last_err =
dynlib::error
();
157
}
158
159
// Stash diagnostics for throw_not_found (thread-local; open is
160
// mutex-guarded).
161
m_last_load_error
= last_err;
162
m_last_load_saw_file
= any_file;
163
m_handles
[lib_base] =
nullptr
;
164
return
nullptr
;
165
}
166
167
bool
PluginLoader::lib_present
(
const
char
*lib_base)
const
{
168
std::lock_guard<std::mutex> lock(
m_mutex
);
169
auto
names =
lib_names
(lib_base);
170
for
(
const
auto
&dir :
m_search_paths
) {
171
for
(
const
auto
&name : names) {
172
std::error_code ec;
173
if
(std::filesystem::exists(std::filesystem::path(dir) / name, ec))
174
return
true
;
175
}
176
}
177
return
false
;
178
}
179
180
void
PluginLoader::throw_not_found
(
const
char
*lib_base,
181
const
char
*description)
const
{
182
auto
names =
lib_names
(lib_base);
183
std::ostringstream oss;
184
oss << description <<
" requested but library not found.\n"
185
<<
"Looked for: "
;
186
for
(
size_t
i = 0; i < names.size(); ++i) {
187
if
(i > 0)
188
oss <<
", "
;
189
oss << names[i];
190
}
191
oss <<
"\n"
;
192
if
(!
m_search_paths
.empty()) {
193
oss <<
"Search paths (config potentials_path + EON_POTENTIALS_PATH):\n"
;
194
for
(
const
auto
&p :
m_search_paths
) {
195
oss <<
" "
<< p <<
"\n"
;
196
}
197
}
198
if
(
m_last_load_saw_file
) {
199
oss <<
"A matching file was present but LoadLibrary/dlopen failed"
;
200
if
(!
m_last_load_error
.empty())
201
oss <<
": "
<<
m_last_load_error
;
202
oss <<
"\n(often a missing flang_rt / FortranRuntime dependency on "
203
"PATH).\n"
;
204
}
else
if
(!
m_last_load_error
.empty()) {
205
oss <<
"Loader error: "
<<
m_last_load_error
<<
"\n"
;
206
}
207
oss <<
"Set [Potential] potentials_path in config.ini, "
208
"or set EON_POTENTIALS_PATH,\n"
209
"or ensure the libraries are in LD_LIBRARY_PATH."
;
210
throw
std::runtime_error(oss.str());
211
}
212
213
}
// namespace eonc
PluginLoader.h
eonc::PluginLoader::Key
Definition
PluginLoader.h:116
eonc::PluginLoader::throw_not_found
void throw_not_found(const char *lib_base, const char *description) const override
Throw a descriptive error for a missing potential library.
Definition
PluginLoader.cpp:180
eonc::PluginLoader::m_search_paths
std::vector< std::string > m_search_paths
Definition
PluginLoader.h:127
eonc::PluginLoader::m_last_load_saw_file
bool m_last_load_saw_file
Definition
PluginLoader.h:132
eonc::PluginLoader::add_config_paths
void add_config_paths(const std::string &colon_paths) override
Inject search paths from the eOn config file.
Definition
PluginLoader.cpp:69
eonc::PluginLoader::open_lib
dynlib::Handle open_lib(const char *lib_base) override
Definition
PluginLoader.cpp:116
eonc::PluginLoader::PluginLoader
PluginLoader()
Definition
PluginLoader.cpp:28
eonc::PluginLoader::instance
static PluginLoader & instance()
Thread-safe singleton accessor (Meyer's pattern).
Definition
PluginLoader.cpp:20
eonc::PluginLoader::append_paths
void append_paths(const std::string &path_str, char sep)
Definition
PluginLoader.cpp:47
eonc::PluginLoader::m_last_load_error
std::string m_last_load_error
Last dynlib::error() from a failed open (for throw_not_found).
Definition
PluginLoader.h:131
eonc::PluginLoader::PluginLoader
PluginLoader(const PluginLoader &)=delete
eonc::PluginLoader::lib_names
std::vector< std::string > lib_names(const char *lib_base) const
Definition
PluginLoader.cpp:103
eonc::PluginLoader::m_mutex
std::mutex m_mutex
Definition
PluginLoader.h:128
eonc::PluginLoader::lib_present
bool lib_present(const char *lib_base) const override
Filesystem probe: a matching library file is on the configured search path.
Definition
PluginLoader.cpp:167
eonc::PluginLoader::m_handles
std::unordered_map< std::string, dynlib::Handle > m_handles
Definition
PluginLoader.h:129
eonc::PluginLoader::~PluginLoader
~PluginLoader() override
Definition
PluginLoader.cpp:41
eonc::dynlib::Handle
void * Handle
Definition
DynLib.h:60
eonc::dynlib::error
std::string error()
Definition
DynLib.h:82
eonc::dynlib::open
Handle open(const char *name) noexcept
Definition
DynLib.h:62
eonc::dynlib::close
void close(Handle h) noexcept
Definition
DynLib.h:77
eonc
RAII resource manager for the ARTn C library with global synchronization.
Definition
ARTnSaddleSearch.cpp:23
client
potentials
PluginLoader.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf