2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
22 #ifdef YOSYS_ENABLE_PLUGINS
27 # include <boost/algorithm/string/predicate.hpp>
33 std::map
<std::string
, void*> loaded_plugins
;
35 std::map
<std::string
, void*> loaded_python_plugins
;
37 std::map
<std::string
, std::string
> loaded_plugin_aliases
;
39 #ifdef YOSYS_ENABLE_PLUGINS
40 void load_plugin(std::string filename
, std::vector
<std::string
> aliases
)
42 std::string orig_filename
= filename
;
44 if (filename
.find('/') == std::string::npos
)
45 filename
= "./" + filename
;
47 if (!loaded_plugins
.count(filename
) && !loaded_python_plugins
.count(filename
)) {
50 if(boost::algorithm::ends_with(filename
, ".py"))
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
)
60 log_cmd_error("Issues converting `%s' to Python\n", filename
.c_str());
63 PyObject
*module_p
= PyImport_Import(filename_p
);
66 log_cmd_error("Can't load python module `%s'\n", filename
.c_str());
69 loaded_python_plugins
[orig_filename
] = module_p
;
70 Pass::init_register();
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
);
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();
87 for (auto &alias
: aliases
)
88 loaded_plugin_aliases
[alias
] = orig_filename
;
91 void load_plugin(std::string
, std::vector
<std::string
>)
93 log_error("This version of yosys is built without plugin support.\n");
97 struct PluginPass
: public Pass
{
98 PluginPass() : Pass("plugin", "load and list loaded plugins") { }
101 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
103 log(" plugin [options]\n");
105 log("Load and list loaded plugins.\n");
107 log(" -i <plugin_filename>\n");
108 log(" Load (install) the specified plugin.\n");
110 log(" -a <alias_name>\n");
111 log(" Register the specified alias name for the loaded plugin\n");
114 log(" List loaded plugins\n");
117 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
)
119 std::string plugin_filename
;
120 std::vector
<std::string
> plugin_aliases
;
121 bool list_mode
= false;
124 for (argidx
= 1; argidx
< args
.size(); argidx
++)
126 if ((args
[argidx
] == "-i") && argidx
+1 < args
.size() && plugin_filename
.empty()) {
127 plugin_filename
= args
[++argidx
];
130 if ((args
[argidx
] == "-a") && argidx
+1 < args
.size()) {
131 plugin_aliases
.push_back(args
[++argidx
]);
134 if (args
[argidx
] == "-l") {
140 extra_args(args
, argidx
, design
, false);
142 if (!plugin_filename
.empty())
143 load_plugin(plugin_filename
, plugin_aliases
);
149 if (loaded_plugins
.empty() and loaded_python_plugins
.empty())
151 if (loaded_plugins
.empty())
153 log("No plugins loaded.\n");
155 log("Loaded plugins:\n");
157 for (auto &it
: loaded_plugins
)
158 log(" %s\n", it
.first
.c_str());
161 for (auto &it
: loaded_python_plugins
)
162 log(" %s\n", it
.first
.c_str());
165 if (!loaded_plugin_aliases
.empty()) {
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());