Python passes are now looked for in share/plugins and can be added by specifying...
[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 if (!loaded_plugins.count(filename)) {
48
49 #ifdef WITH_PYTHON
50 if(boost::algorithm::ends_with(filename, ".py"))
51 {
52 int last_slash = filename.find_last_of('/');
53 std::string path = filename.substr(0, last_slash);
54 filename = filename.substr(last_slash+1, filename.size());
55 filename = filename.substr(0,filename.size()-3);
56 PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str());
57 PyObject *filename_p = PyUnicode_FromString(filename.c_str());
58 if(filename_p == NULL)
59 {
60 log_cmd_error("Issues converting `%s' to Python\n", filename.c_str());
61 return;
62 }
63 PyObject *module_p = PyImport_Import(filename_p);
64 if(module_p == NULL)
65 {
66 log_cmd_error("Can't load python module `%s'\n", filename.c_str());
67 return;
68 }
69 loaded_python_plugins[orig_filename] = module_p;
70 Pass::init_register();
71 } else {
72 #endif
73
74 void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
75 if (hdl == NULL && orig_filename.find('/') == std::string::npos)
76 hdl = dlopen((proc_share_dirname() + "plugins/" + orig_filename + ".so").c_str(), RTLD_LAZY|RTLD_LOCAL);
77 if (hdl == NULL)
78 log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror());
79 loaded_plugins[orig_filename] = hdl;
80 Pass::init_register();
81
82 #ifdef WITH_PYTHON
83 }
84 #endif
85 }
86
87 for (auto &alias : aliases)
88 loaded_plugin_aliases[alias] = orig_filename;
89 }
90 #else
91 void load_plugin(std::string, std::vector<std::string>)
92 {
93 log_error("This version of yosys is built without plugin support.\n");
94 }
95 #endif
96
97 struct PluginPass : public Pass {
98 PluginPass() : Pass("plugin", "load and list loaded plugins") { }
99 virtual void help()
100 {
101 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
102 log("\n");
103 log(" plugin [options]\n");
104 log("\n");
105 log("Load and list loaded plugins.\n");
106 log("\n");
107 log(" -i <plugin_filename>\n");
108 log(" Load (install) the specified plugin.\n");
109 log("\n");
110 log(" -a <alias_name>\n");
111 log(" Register the specified alias name for the loaded plugin\n");
112 log("\n");
113 log(" -l\n");
114 log(" List loaded plugins\n");
115 log("\n");
116 }
117 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
118 {
119 std::string plugin_filename;
120 std::vector<std::string> plugin_aliases;
121 bool list_mode = false;
122
123 size_t argidx;
124 for (argidx = 1; argidx < args.size(); argidx++)
125 {
126 if ((args[argidx] == "-i") && argidx+1 < args.size() && plugin_filename.empty()) {
127 plugin_filename = args[++argidx];
128 continue;
129 }
130 if ((args[argidx] == "-a") && argidx+1 < args.size()) {
131 plugin_aliases.push_back(args[++argidx]);
132 continue;
133 }
134 if (args[argidx] == "-l") {
135 list_mode = true;
136 continue;
137 }
138 break;
139 }
140 extra_args(args, argidx, design, false);
141
142 if (!plugin_filename.empty())
143 load_plugin(plugin_filename, plugin_aliases);
144
145 if (list_mode)
146 {
147 log("\n");
148 #ifdef WITH_PYTHON
149 if (loaded_plugins.empty() and loaded_python_plugins.empty())
150 #else
151 if (loaded_plugins.empty())
152 #endif
153 log("No plugins loaded.\n");
154 else
155 log("Loaded plugins:\n");
156
157 for (auto &it : loaded_plugins)
158 log(" %s\n", it.first.c_str());
159
160 #ifdef WITH_PYTHON
161 for (auto &it : loaded_python_plugins)
162 log(" %s\n", it.first.c_str());
163 #endif
164
165 if (!loaded_plugin_aliases.empty()) {
166 log("\n");
167 int max_alias_len = 1;
168 for (auto &it : loaded_plugin_aliases)
169 max_alias_len = max(max_alias_len, GetSize(it.first));
170 for (auto &it : loaded_plugin_aliases)
171 log("Alias: %-*s %s\n", max_alias_len, it.first.c_str(), it.second.c_str());
172 }
173 }
174 }
175 } PluginPass;
176
177 YOSYS_NAMESPACE_END
178