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
10const fs::path* Driver::Imports::add(fs::path path, Sym sym) {
11 if (!fs::exists(path)) {
12 driver_.WLOG("import path '{}' does not exist", path.string());
13 return nullptr;
14 }
15 for (auto p : paths())
16 if (fs::equivalent(p, path)) return nullptr;
17
18 path2sym_.emplace_back(std::pair(std::move(path), sym));
19 return &path2sym_.back().first;
20}
21
23 : log_(flags_)
24 , world_(this)
25 , imports_(*this) {
26 // prepend empty path
27 search_paths_.emplace_front(fs::path{});
28
29 // paths from env
30 if (auto env_path = std::getenv("MIM_PLUGIN_PATH")) {
31 std::stringstream env_path_stream{env_path};
32 std::string sub_path;
33 while (std::getline(env_path_stream, sub_path, ':'))
34 add_search_path(sub_path);
35 }
36
37 // add path/to/mim.exe/../../lib/mim
38 if (auto path = sys::path_to_curr_exe()) add_search_path(path->parent_path().parent_path() / MIM_LIBDIR / "mim");
39
40 // add install path if different from above
41 if (auto install_path = fs::path{MIM_INSTALL_PREFIX} / MIM_LIBDIR / "mim"; fs::exists(install_path)) {
42 if (search_paths().size() < 2 || !fs::equivalent(install_path, search_paths().back()))
43 add_search_path(std::move(install_path));
44 }
45
46 // all other user paths are placed just behind the first path (the empty path)
47 insert_ = ++search_paths_.begin();
48}
49
50void Driver::load(Sym name) {
51 ILOG("💾 loading plugin: '{}'", name);
52
53 if (is_loaded(name)) {
54 WLOG("mim/plugin '{}' already loaded", name);
55 return;
56 }
57
58 auto handle = Plugin::Handle{nullptr, dl::close};
59 if (auto path = fs::path{name.view()}; path.is_absolute() && fs::is_regular_file(path))
60 handle.reset(dl::open(name.c_str()));
61 if (!handle) {
62 for (const auto& path : search_paths()) {
63 auto full_path = path / fmt("libmim_{}.{}", name, dl::extension);
64 std::error_code ignore;
65 if (bool reg_file = fs::is_regular_file(full_path, ignore); reg_file && !ignore) {
66 auto path_str = full_path.string();
67 if (handle.reset(dl::open(path_str.c_str())); handle) break;
68 }
69 if (handle) break;
70 }
71 }
72
73 if (!handle) error("cannot open plugin '{}'", name);
74
75 if (auto get_info = reinterpret_cast<decltype(&mim_get_plugin)>(dl::get(handle.get(), "mim_get_plugin"))) {
76 assert_emplace(plugins_, name, std::move(handle));
77 auto info = get_info();
78 // clang-format off
79 if (auto reg = info.register_normalizers) reg(normalizers_);
80 if (auto reg = info.register_stages) reg(stages_);
81 if (auto reg = info.register_backends) reg(backends_);
82 // clang-format on
83 } else {
84 error("mim/plugin has no 'mim_get_plugin()'");
85 }
86}
87
88void* Driver::get_fun_ptr(Sym plugin, const char* name) {
89 if (auto handle = lookup(plugins_, plugin)) return dl::get(handle->get(), name);
90 return nullptr;
91}
92
93} // namespace mim
const fs::path * add(fs::path, Sym)
Yields a fs::path*, if not already added that you can use in Location; returns nullptr otherwise.
Definition driver.cpp:10
void load(Sym name)
Definition driver.cpp:50
void add_search_path(fs::path path)
Definition driver.h:38
bool is_loaded(Sym sym) const
Definition driver.h:86
void * get_fun_ptr(Sym plugin, const char *name)
Definition driver.cpp:88
const auto & search_paths() const
Definition driver.h:37
#define MIM_LIBDIR
Definition config.h:11
#define MIM_INSTALL_PREFIX
Definition config.h:10
#define ILOG(...)
Definition log.h:91
#define WLOG(...)
Definition log.h:90
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:28
Definition ast.h:14
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:118
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:100
std::string fmt(const char *s, Args &&... args)
Wraps mim::print to output a formatted std:string.
Definition print.h:163
void error(Loc loc, const char *f, Args &&... args)
Definition dbg.h:125
mim::Plugin mim_get_plugin()
std::unique_ptr< void, void(*)(void *)> Handle
Definition plugin.h:31