Merge remote-tracking branch 'origin/master' into feature/python_bindings
[yosys.git] / passes / cmds / plugin.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/yosys.h"
21
22 #ifdef YOSYS_ENABLE_PLUGINS
23 # include <dlfcn.h>
24 #endif
25
26 #ifdef WITH_PYTHON
27 # include <boost/algorithm/string/predicate.hpp>
28 # include <Python.h>
29 #endif
30
31 YOSYS_NAMESPACE_BEGIN
32
33 std::map<std::string, void*> loaded_plugins;
34 #ifdef WITH_PYTHON
35 std::map<std::string, void*> loaded_python_plugins;
36 #endif
37 std::map<std::string, std::string> loaded_plugin_aliases;
38
39 #ifdef YOSYS_ENABLE_PLUGINS
40 void load_plugin(std::string filename, std::vector<std::string> aliases)
41 {
42 std::string orig_filename = filename;
43
44 if (filename.find('/') == std::string::npos)
45 filename = "./" + filename;
46
47 #ifdef WITH_PYTHON
48 if (!loaded_plugins.count(filename) && !loaded_python_plugins.count(filename)) {
49 #else
50 if (!loaded_plugins.count(filename)) {
51 #endif
52
53 #ifdef WITH_PYTHON
54 if(boost::algorithm::ends_with(filename, ".py"))
55 {
56 int last_slash = filename.find_last_of('/');
57 std::string path = filename.substr(0, last_slash);
58 filename = filename.substr(last_slash+1, filename.size());
59 filename = filename.substr(0,filename.size()-3);
60 PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str());
61 PyObject *filename_p = PyUnicode_FromString(filename.c_str());
62 if(filename_p == NULL)
63 {
64 log_cmd_error("Issues converting `%s' to Python\n", filename.c_str());
65 return;
66 }
67 PyObject *module_p = PyImport_Import(filename_p);
68 if(module_p == NULL)
69 {
70 log_cmd_error("Can't load python module `%s'\n", filename.c_str());
71 return;
72 }
73 loaded_python_plugins[orig_filename] = module_p;
74 Pass::init_register();
75 } else {
76 #endif
77
78 void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
79 if (hdl == NULL && orig_filename.find('/') == std::string::npos)
80 hdl = dlopen((proc_share_dirname() + "plugins/" + orig_filename + ".so").c_str(), RTLD_LAZY|RTLD_LOCAL);
81 if (hdl == NULL)
82 log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror());
83 loaded_plugins[orig_filename] = hdl;
84 Pass::init_register();
85
86 #ifdef WITH_PYTHON
87 }
88 #endif
89 }
90
91 for (auto &alias : aliases)
92 loaded_plugin_aliases[alias] = orig_filename;
93 }
94 #else
95 void load_plugin(std::string, std::vector<std::string>)
96 {
97 log_error("This version of yosys is built without plugin support.\n");
98 }
99 #endif
100
101 struct PluginPass : public Pass {
102 PluginPass() : Pass("plugin", "load and list loaded plugins") { }
103 void help() YS_OVERRIDE
104 {
105 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
106 log("\n");
107 log(" plugin [options]\n");
108 log("\n");
109 log("Load and list loaded plugins.\n");
110 log("\n");
111 log(" -i <plugin_filename>\n");
112 log(" Load (install) the specified plugin.\n");
113 log("\n");
114 log(" -a <alias_name>\n");
115 log(" Register the specified alias name for the loaded plugin\n");
116 log("\n");
117 log(" -l\n");
118 log(" List loaded plugins\n");
119 log("\n");
120 }
121 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
122 {
123 std::string plugin_filename;
124 std::vector<std::string> plugin_aliases;
125 bool list_mode = false;
126
127 size_t argidx;
128 for (argidx = 1; argidx < args.size(); argidx++)
129 {
130 if ((args[argidx] == "-i") && argidx+1 < args.size() && plugin_filename.empty()) {
131 plugin_filename = args[++argidx];
132 continue;
133 }
134 if ((args[argidx] == "-a") && argidx+1 < args.size()) {
135 plugin_aliases.push_back(args[++argidx]);
136 continue;
137 }
138 if (args[argidx] == "-l") {
139 list_mode = true;
140 continue;
141 }
142 break;
143 }
144 extra_args(args, argidx, design, false);
145
146 if (!plugin_filename.empty())
147 load_plugin(plugin_filename, plugin_aliases);
148
149 if (list_mode)
150 {
151 log("\n");
152 #ifdef WITH_PYTHON
153 if (loaded_plugins.empty() and loaded_python_plugins.empty())
154 #else
155 if (loaded_plugins.empty())
156 #endif
157 log("No plugins loaded.\n");
158 else
159 log("Loaded plugins:\n");
160
161 for (auto &it : loaded_plugins)
162 log(" %s\n", it.first.c_str());
163
164 #ifdef WITH_PYTHON
165 for (auto &it : loaded_python_plugins)
166 log(" %s\n", it.first.c_str());
167 #endif
168
169 if (!loaded_plugin_aliases.empty()) {
170 log("\n");
171 int max_alias_len = 1;
172 for (auto &it : loaded_plugin_aliases)
173 max_alias_len = max(max_alias_len, GetSize(it.first));
174 for (auto &it : loaded_plugin_aliases)
175 log("Alias: %-*s %s\n", max_alias_len, it.first.c_str(), it.second.c_str());
176 }
177 }
178 }
179 } PluginPass;
180
181 YOSYS_NAMESPACE_END
182