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*/
13
14#include <cstdlib>
15#include <filesystem>
16#include <sstream>
17
18namespace eonc {
19
21 static PluginLoader loader;
22 return loader;
23}
24
27
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
42 for (auto &[name, handle] : m_handles) {
43 dynlib::close(handle);
44 }
45}
46
47void 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
69void 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
103std::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
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
167bool 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
180void 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 }
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
void throw_not_found(const char *lib_base, const char *description) const override
Throw a descriptive error for a missing potential library.
std::vector< std::string > m_search_paths
void add_config_paths(const std::string &colon_paths) override
Inject search paths from the eOn config file.
dynlib::Handle open_lib(const char *lib_base) override
static PluginLoader & instance()
Thread-safe singleton accessor (Meyer's pattern).
void append_paths(const std::string &path_str, char sep)
std::string m_last_load_error
Last dynlib::error() from a failed open (for throw_not_found).
PluginLoader(const PluginLoader &)=delete
std::vector< std::string > lib_names(const char *lib_base) const
bool lib_present(const char *lib_base) const override
Filesystem probe: a matching library file is on the configured search path.
std::unordered_map< std::string, dynlib::Handle > m_handles
~PluginLoader() override
void * Handle
Definition DynLib.h:60
std::string error()
Definition DynLib.h:82
Handle open(const char *name) noexcept
Definition DynLib.h:62
void close(Handle h) noexcept
Definition DynLib.h:77
RAII resource manager for the ARTn C library with global synchronization.