14 search_paths_.emplace_front(fs::path{});
17 if (
auto env_path = std::getenv(
"MIM_PLUGIN_PATH")) {
18 std::stringstream env_path_stream{env_path};
20 while (std::getline(env_path_stream, sub_path,
':'))
28 if (
auto install_path = fs::path{
MIM_INSTALL_PREFIX} /
"lib" /
"mim"; fs::exists(install_path)) {
34 insert_ = ++search_paths_.begin();
39 if (fs::equivalent(p, path))
return nullptr;
41 import_path2sym_.emplace_back(std::pair(std::move(path), sym));
42 return &import_path2sym_.back().first;
46 ILOG(
"loading plugin: '{}'", name);
49 WLOG(
"mim/plugin '{}' already loaded", name);
54 if (
auto path = fs::path{name.view()}; path.is_absolute() && fs::is_regular_file(path))
55 handle.reset(
dl::open(name.c_str()));
59 std::error_code ignore;
60 if (
bool reg_file = fs::is_regular_file(full_path, ignore); reg_file && !ignore) {
61 auto path_str = full_path.string();
62 if (handle.reset(
dl::open(path_str.c_str())); handle)
break;
68 if (!handle)
error(
"cannot open plugin '{}'", name);
70 if (
auto get_info =
reinterpret_cast<decltype(&
mim_get_plugin)
>(
dl::get(handle.get(),
"mim_get_plugin"))) {
72 auto info = get_info();
74 if (
auto reg = info.register_normalizers) reg(normalizers_);
75 if (
auto reg = info.register_stages) reg(phases_, passes_);
76 if (
auto reg = info.register_backends) reg(backends_);
79 error(
"mim/plugin has no 'mim_get_plugin()'");
84 if (
auto handle =
lookup(plugins_, plugin))
return dl::get(handle->get(), name);
void add_search_path(fs::path path)
bool is_loaded(Sym sym) const
const fs::path * add_import(fs::path, Sym)
Yields a fs::path* if not already added that you can use in Location; returns nullptr otherwise.
void * get_fun_ptr(Sym plugin, const char *name)
const auto & search_paths() const
#define MIM_INSTALL_PREFIX
void * get(void *handle, const char *symbol_name)
void * open(const char *filename)
static constexpr auto extension
std::optional< fs::path > path_to_curr_exe()
Yields std::nullopt if an error occurred.
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
auto lookup(const C &container, const K &key)
Yields pointer to element (or the element itself if it is already a pointer), if found and nullptr ot...
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
void error(Loc loc, const char *f, Args &&... args)
mim::Plugin mim_get_plugin()
std::unique_ptr< void, void(*)(void *)> Handle