MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
driver.cpp
Go to the documentation of this file.
1#include "mim/driver.h"
2
3#include "mim/plugin.h"
4
5#include "mim/util/dl.h"
6#include "mim/util/sys.h"
7
8namespace mim {
9
10namespace {
11std::vector<fs::path> get_plugin_name_variants(std::string_view name) {
12 std::vector<fs::path> names;
13 names.push_back(name); // if the user gives "libmim_foo.so"
14 names.push_back(fmt("libmim_{}.{}", name, dl::extension));
15 return names;
16}
17} // namespace
18
20 : log_(flags_)
21 , world_(this) {
22 // prepend empty path
23 search_paths_.emplace_front(fs::path{});
24
25 // paths from env
26 if (auto env_path = std::getenv("MIM_PLUGIN_PATH")) {
27 std::stringstream env_path_stream{env_path};
28 std::string sub_path;
29 while (std::getline(env_path_stream, sub_path, ':')) add_search_path(sub_path);
30 }
31
32 // add path/to/mim.exe/../../lib/mim
33 if (auto path = sys::path_to_curr_exe()) add_search_path(path->parent_path().parent_path() / "lib" / "mim");
34
35 // add install path if different from above
36 if (auto install_path = fs::path{MIM_INSTALL_PREFIX} / "lib" / "mim"; fs::exists(install_path)) {
37 if (search_paths().empty() || !fs::equivalent(install_path, search_paths().back()))
38 add_search_path(std::move(install_path));
39 }
40
41 // all other user paths are placed just behind the first path (the empty path)
42 insert_ = ++search_paths_.begin();
43}
44
45const fs::path* Driver::add_import(fs::path path, Sym sym) {
46 for (const auto& [p, _] : imports_)
47 if (fs::equivalent(p, path)) return nullptr;
48
49 imports_.emplace_back(std::pair(std::move(path), sym));
50 return &imports_.back().first;
51}
52
53void Driver::load(Sym name) {
54 ILOG("loading plugin: '{}'", name);
55
56 if (is_loaded(name)) {
57 WLOG("mim/plugin '{}' already loaded", name);
58 return;
59 }
60
61 auto handle = Plugin::Handle{nullptr, dl::close};
62 if (auto path = fs::path{name.view()}; path.is_absolute() && fs::is_regular_file(path))
63 handle.reset(dl::open(name));
64 if (!handle) {
65 for (const auto& path : search_paths()) {
66 for (auto name_variants = get_plugin_name_variants(name); const auto& name_variant : name_variants) {
67 auto full_path = path / name_variant;
68 std::error_code ignore;
69 if (bool reg_file = fs::is_regular_file(full_path, ignore); reg_file && !ignore) {
70 auto path_str = full_path.string();
71 if (handle.reset(dl::open(path_str.c_str())); handle) break;
72 }
73 }
74 if (handle) break;
75 }
76 }
77
78 if (!handle) error("cannot open plugin '{}'", name);
79
80 if (auto get_info = reinterpret_cast<decltype(&mim_get_plugin)>(dl::get(handle.get(), "mim_get_plugin"))) {
81 assert_emplace(plugins_, name, std::move(handle));
82 auto info = get_info();
83 if (auto reg = info.register_passes) reg(passes_);
84 if (auto reg = info.register_normalizers) reg(normalizers_);
85 if (auto reg = info.register_backends) reg(backends_);
86 } else {
87 error("mim/plugin has no 'mim_get_plugin()'");
88 }
89}
90
91void* Driver::get_fun_ptr(Sym plugin, const char* name) {
92 if (auto handle = lookup(plugins_, plugin)) return dl::get(handle->get(), name);
93 return nullptr;
94}
95
96} // namespace mim
void load(Sym name)
Definition driver.cpp:53
void add_search_path(fs::path path)
Definition driver.h:37
bool is_loaded(Sym sym) const
Definition driver.h:60
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.
Definition driver.cpp:45
void * get_fun_ptr(Sym plugin, const char *name)
Definition driver.cpp:91
const auto & search_paths() const
Definition driver.h:36
#define MIM_INSTALL_PREFIX
Definition config.h:10
#define ILOG(...)
Definition log.h:89
#define WLOG(...)
Definition log.h:88
void * get(void *handle, const char *symbol_name)
Definition dl.cpp:36
void close(void *handle)
Definition dl.cpp:55
void * open(const char *filename)
Definition dl.cpp:17
static constexpr auto extension
Definition dl.h:8
std::optional< fs::path > path_to_curr_exe()
Yields std::nullopt if an error occurred.
Definition sys.cpp:27
Definition cfg.h:11
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:102
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...
Definition util.h:93
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
Definition print.h:155
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:122
MIM_EXPORT mim::Plugin mim_get_plugin()
std::unique_ptr< void, void(*)(void *)> Handle
Definition plugin.h:30