MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / getDefaultPluginPaths

Method getDefaultPluginPaths

lib/plugin/plugin.cpp:242–335  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

240}
241
242std::vector<std::filesystem::path> Plugin::getDefaultPluginPaths() noexcept {
243 using namespace std::literals::string_view_literals;
244 std::vector<std::filesystem::path> Result;
245 std::error_code Error;
246
247 // Extra directories from environ variable
248 if (const auto ExtraEnv = ::getenv("WASMEDGE_PLUGIN_PATH")) {
249 std::string_view ExtraEnvStr = ExtraEnv;
250 for (auto Sep = ExtraEnvStr.find(':'); Sep != std::string_view::npos;
251 Sep = ExtraEnvStr.find(':')) {
252 Result.push_back(std::filesystem::u8path(ExtraEnvStr.substr(0, Sep)));
253 const auto Next = ExtraEnvStr.find_first_not_of(':', Sep);
254 ExtraEnvStr = ExtraEnvStr.substr(Next);
255 }
256 Result.push_back(std::filesystem::u8path(ExtraEnvStr));
257 }
258
259 // Plugin directory for the WasmEdge installation.
260#if WASMEDGE_OS_LINUX || WASMEDGE_OS_MACOS
261 Dl_info DLInfo;
262 int Status =
263 dladdr(reinterpret_cast<void *>(Plugin::getDefaultPluginPaths), &DLInfo);
264 if (Status != 0) {
265 if (DLInfo.dli_fname == nullptr) {
266 spdlog::error(
267 "Address matched to a shared object but not to any symbol "sv
268 "within the object. dli_fname is null."sv);
269 return std::vector<std::filesystem::path>();
270 }
271 auto LibPath = std::filesystem::u8path(DLInfo.dli_fname)
272 .parent_path()
273 .lexically_normal();
274 const auto UsrStr = "/usr"sv;
275 const auto LibStr = "/lib"sv;
276 const auto &PathStr = LibPath.native();
277 if ((PathStr.size() >= UsrStr.size() &&
278 std::equal(UsrStr.begin(), UsrStr.end(), PathStr.begin())) ||
279 (PathStr.size() >= LibStr.size() &&
280 std::equal(LibStr.begin(), LibStr.end(), PathStr.begin()))) {
281 // The installation path of the WasmEdge library is under "/usr".
282 // Plug-in path will be in "LIB_PATH/wasmedge".
283 // If the installation path is under "/usr/lib" or "/usr/lib64", the
284 // traced library path will be "/lib" or "/lib64".
285 Result.push_back(LibPath / std::filesystem::u8path("wasmedge"sv));
286 } else {
287 // The installation path of the WasmEdge library is not under "/usr", such
288 // as "$HOME/.wasmedge". Plug-in path will be in "LIB_PATH/../plugin".
289 Result.push_back(LibPath / std::filesystem::u8path(".."sv) /
290 std::filesystem::u8path("plugin"sv));
291 }
292 } else {
293 spdlog::error(ErrCode::Value::NonNullRequired);
294 spdlog::error("Address could not be matched to any shared object. "sv
295 "Detailed error information is not available."sv);
296 return std::vector<std::filesystem::path>();
297 }
298#elif WASMEDGE_OS_WINDOWS
299 // Global plugin directory.

Callers

nothing calls this directly

Calls 8

SUCCEEDED_Function · 0.85
substrMethod · 0.80
errorFunction · 0.50
findMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected