2014-08-22 06:58:36 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/yosys.h"
|
2014-08-22 09:09:13 -05:00
|
|
|
|
|
|
|
#ifdef YOSYS_ENABLE_PLUGINS
|
|
|
|
# include <dlfcn.h>
|
|
|
|
#endif
|
2014-08-22 06:58:36 -05:00
|
|
|
|
2018-08-16 09:00:11 -05:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
# include <boost/algorithm/string/predicate.hpp>
|
|
|
|
# include <Python.h>
|
2019-04-04 02:24:50 -05:00
|
|
|
# include <boost/filesystem.hpp>
|
2018-08-16 09:00:11 -05:00
|
|
|
#endif
|
|
|
|
|
2014-08-22 06:58:36 -05:00
|
|
|
YOSYS_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
std::map<std::string, void*> loaded_plugins;
|
2018-08-16 09:00:11 -05:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
std::map<std::string, void*> loaded_python_plugins;
|
|
|
|
#endif
|
2014-08-22 06:58:36 -05:00
|
|
|
std::map<std::string, std::string> loaded_plugin_aliases;
|
|
|
|
|
2014-10-17 05:04:40 -05:00
|
|
|
#ifdef YOSYS_ENABLE_PLUGINS
|
2014-08-22 06:58:36 -05:00
|
|
|
void load_plugin(std::string filename, std::vector<std::string> aliases)
|
|
|
|
{
|
2016-03-30 03:02:03 -05:00
|
|
|
std::string orig_filename = filename;
|
|
|
|
|
2014-08-22 06:58:36 -05:00
|
|
|
if (filename.find('/') == std::string::npos)
|
|
|
|
filename = "./" + filename;
|
|
|
|
|
2018-08-20 09:04:43 -05:00
|
|
|
#ifdef WITH_PYTHON
|
2018-08-20 08:28:09 -05:00
|
|
|
if (!loaded_plugins.count(filename) && !loaded_python_plugins.count(filename)) {
|
2018-08-20 09:04:43 -05:00
|
|
|
#else
|
|
|
|
if (!loaded_plugins.count(filename)) {
|
|
|
|
#endif
|
2018-08-16 09:00:11 -05:00
|
|
|
|
|
|
|
#ifdef WITH_PYTHON
|
2019-04-03 06:21:40 -05:00
|
|
|
|
2019-04-04 02:24:50 -05:00
|
|
|
boost::filesystem::path full_path(filename);
|
2019-04-03 06:21:40 -05:00
|
|
|
|
|
|
|
if(strcmp(full_path.extension().c_str(), ".py") == 0)
|
2018-08-16 09:00:11 -05:00
|
|
|
{
|
2019-04-03 06:21:40 -05:00
|
|
|
std::string path(full_path.parent_path().c_str());
|
|
|
|
filename = full_path.filename().c_str();
|
2018-08-16 09:00:11 -05:00
|
|
|
filename = filename.substr(0,filename.size()-3);
|
2019-04-04 03:35:01 -05:00
|
|
|
PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str());
|
2019-04-03 06:21:40 -05:00
|
|
|
PyErr_Print();
|
2019-04-04 03:35:01 -05:00
|
|
|
PyObject *module_p = PyImport_ImportModule(filename.c_str());
|
2018-08-16 09:00:11 -05:00
|
|
|
if(module_p == NULL)
|
|
|
|
{
|
2019-04-03 05:21:56 -05:00
|
|
|
PyErr_Print();
|
2019-04-03 06:21:40 -05:00
|
|
|
log_cmd_error("Can't load python module `%s'\n", full_path.filename().c_str());
|
2018-08-16 09:00:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
loaded_python_plugins[orig_filename] = module_p;
|
|
|
|
Pass::init_register();
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
|
2014-08-22 06:58:36 -05:00
|
|
|
void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
|
2016-03-30 03:02:03 -05:00
|
|
|
if (hdl == NULL && orig_filename.find('/') == std::string::npos)
|
|
|
|
hdl = dlopen((proc_share_dirname() + "plugins/" + orig_filename + ".so").c_str(), RTLD_LAZY|RTLD_LOCAL);
|
2014-08-22 06:58:36 -05:00
|
|
|
if (hdl == NULL)
|
|
|
|
log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror());
|
2016-03-30 03:02:03 -05:00
|
|
|
loaded_plugins[orig_filename] = hdl;
|
2014-08-22 06:58:36 -05:00
|
|
|
Pass::init_register();
|
2018-08-16 09:00:11 -05:00
|
|
|
|
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-22 06:58:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &alias : aliases)
|
2016-03-30 03:02:03 -05:00
|
|
|
loaded_plugin_aliases[alias] = orig_filename;
|
2014-10-17 05:04:40 -05:00
|
|
|
}
|
2014-08-22 09:09:13 -05:00
|
|
|
#else
|
2014-10-17 05:04:40 -05:00
|
|
|
void load_plugin(std::string, std::vector<std::string>)
|
|
|
|
{
|
2014-08-22 09:09:13 -05:00
|
|
|
log_error("This version of yosys is built without plugin support.\n");
|
2014-08-22 06:58:36 -05:00
|
|
|
}
|
2014-10-17 05:04:40 -05:00
|
|
|
#endif
|
2014-08-22 06:58:36 -05:00
|
|
|
|
|
|
|
struct PluginPass : public Pass {
|
|
|
|
PluginPass() : Pass("plugin", "load and list loaded plugins") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2014-08-22 06:58:36 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" plugin [options]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Load and list loaded plugins.\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -i <plugin_filename>\n");
|
|
|
|
log(" Load (install) the specified plugin.\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -a <alias_name>\n");
|
|
|
|
log(" Register the specified alias name for the loaded plugin\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -l\n");
|
|
|
|
log(" List loaded plugins\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2014-08-22 06:58:36 -05:00
|
|
|
{
|
|
|
|
std::string plugin_filename;
|
|
|
|
std::vector<std::string> plugin_aliases;
|
|
|
|
bool list_mode = false;
|
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if ((args[argidx] == "-i") && argidx+1 < args.size() && plugin_filename.empty()) {
|
|
|
|
plugin_filename = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((args[argidx] == "-a") && argidx+1 < args.size()) {
|
|
|
|
plugin_aliases.push_back(args[++argidx]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (args[argidx] == "-l") {
|
|
|
|
list_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design, false);
|
|
|
|
|
|
|
|
if (!plugin_filename.empty())
|
|
|
|
load_plugin(plugin_filename, plugin_aliases);
|
|
|
|
|
|
|
|
if (list_mode)
|
|
|
|
{
|
|
|
|
log("\n");
|
2018-08-16 09:00:11 -05:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
if (loaded_plugins.empty() and loaded_python_plugins.empty())
|
|
|
|
#else
|
2014-08-22 06:58:36 -05:00
|
|
|
if (loaded_plugins.empty())
|
2018-08-16 09:00:11 -05:00
|
|
|
#endif
|
2014-08-22 06:58:36 -05:00
|
|
|
log("No plugins loaded.\n");
|
|
|
|
else
|
|
|
|
log("Loaded plugins:\n");
|
|
|
|
|
|
|
|
for (auto &it : loaded_plugins)
|
|
|
|
log(" %s\n", it.first.c_str());
|
|
|
|
|
2018-08-16 09:00:11 -05:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
for (auto &it : loaded_python_plugins)
|
|
|
|
log(" %s\n", it.first.c_str());
|
|
|
|
#endif
|
|
|
|
|
2014-08-22 06:58:36 -05:00
|
|
|
if (!loaded_plugin_aliases.empty()) {
|
|
|
|
log("\n");
|
|
|
|
int max_alias_len = 1;
|
|
|
|
for (auto &it : loaded_plugin_aliases)
|
2015-10-25 13:30:49 -05:00
|
|
|
max_alias_len = max(max_alias_len, GetSize(it.first));
|
2014-08-22 06:58:36 -05:00
|
|
|
for (auto &it : loaded_plugin_aliases)
|
|
|
|
log("Alias: %-*s %s\n", max_alias_len, it.first.c_str(), it.second.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} PluginPass;
|
|
|
|
|
|
|
|
YOSYS_NAMESPACE_END
|
|
|
|
|