#include <string.h>
#include <unistd.h>
#include <libgen.h>
-#include <dlfcn.h>
#include <limits.h>
#include <errno.h>
std::string frontend_command = "auto";
std::string backend_command = "auto";
std::vector<std::string> passes_commands;
- std::vector<void*> loaded_modules;
+ std::vector<std::string> plugin_filenames;
std::string output_filename = "";
std::string scriptfile = "";
bool scriptfile_tcl = false;
passes_commands.push_back("opt");
break;
case 'm':
- loaded_modules.push_back(dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL));
- if (loaded_modules.back() == NULL) {
- fprintf(stderr, "Can't load module `%s': %s\n", optarg, dlerror());
- exit(1);
- }
+ plugin_filenames.push_back(optarg);
break;
case 'f':
frontend_command = optarg;
yosys_setup();
+ for (auto &fn : plugin_filenames)
+ load_plugin(fn, {});
+
if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
if (!got_output_filename)
backend_command = "";
yosys_shutdown();
- for (auto mod : loaded_modules)
- dlclose(mod);
-
return 0;
}
--- /dev/null
+/*
+ * 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"
+#include <dlfcn.h>
+
+YOSYS_NAMESPACE_BEGIN
+
+std::map<std::string, void*> loaded_plugins;
+std::map<std::string, std::string> loaded_plugin_aliases;
+
+void load_plugin(std::string filename, std::vector<std::string> aliases)
+{
+ if (filename.find('/') == std::string::npos)
+ filename = "./" + filename;
+
+ if (!loaded_plugins.count(filename)) {
+ void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
+ if (hdl == NULL)
+ log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror());
+ loaded_plugins[filename] = hdl;
+ Pass::init_register();
+ }
+
+ for (auto &alias : aliases)
+ loaded_plugin_aliases[alias] = filename;
+}
+
+struct PluginPass : public Pass {
+ PluginPass() : Pass("plugin", "load and list loaded plugins") { }
+ virtual void help()
+ {
+ // |---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");
+ }
+ virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
+ {
+ 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");
+ if (loaded_plugins.empty())
+ log("No plugins loaded.\n");
+ else
+ log("Loaded plugins:\n");
+
+ for (auto &it : loaded_plugins)
+ log(" %s\n", it.first.c_str());
+
+ if (!loaded_plugin_aliases.empty()) {
+ log("\n");
+ int max_alias_len = 1;
+ for (auto &it : loaded_plugin_aliases)
+ max_alias_len = std::max(max_alias_len, SIZE(it.first));
+ 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
+