Renamed SIZE() to GetSize() because of name collision on Win32
[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 YOSYS_NAMESPACE_BEGIN
27
28 std::map<std::string, void*> loaded_plugins;
29 std::map<std::string, std::string> loaded_plugin_aliases;
30
31 void load_plugin(std::string filename, std::vector<std::string> aliases)
32 {
33 #ifdef YOSYS_ENABLE_PLUGINS
34 if (filename.find('/') == std::string::npos)
35 filename = "./" + filename;
36
37 if (!loaded_plugins.count(filename)) {
38 void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
39 if (hdl == NULL)
40 log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror());
41 loaded_plugins[filename] = hdl;
42 Pass::init_register();
43 }
44
45 for (auto &alias : aliases)
46 loaded_plugin_aliases[alias] = filename;
47 #else
48 log_error("This version of yosys is built without plugin support.\n");
49 #endif
50 }
51
52 struct PluginPass : public Pass {
53 PluginPass() : Pass("plugin", "load and list loaded plugins") { }
54 virtual void help()
55 {
56 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
57 log("\n");
58 log(" plugin [options]\n");
59 log("\n");
60 log("Load and list loaded plugins.\n");
61 log("\n");
62 log(" -i <plugin_filename>\n");
63 log(" Load (install) the specified plugin.\n");
64 log("\n");
65 log(" -a <alias_name>\n");
66 log(" Register the specified alias name for the loaded plugin\n");
67 log("\n");
68 log(" -l\n");
69 log(" List loaded plugins\n");
70 log("\n");
71 }
72 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
73 {
74 std::string plugin_filename;
75 std::vector<std::string> plugin_aliases;
76 bool list_mode = false;
77
78 size_t argidx;
79 for (argidx = 1; argidx < args.size(); argidx++)
80 {
81 if ((args[argidx] == "-i") && argidx+1 < args.size() && plugin_filename.empty()) {
82 plugin_filename = args[++argidx];
83 continue;
84 }
85 if ((args[argidx] == "-a") && argidx+1 < args.size()) {
86 plugin_aliases.push_back(args[++argidx]);
87 continue;
88 }
89 if (args[argidx] == "-l") {
90 list_mode = true;
91 continue;
92 }
93 break;
94 }
95 extra_args(args, argidx, design, false);
96
97 if (!plugin_filename.empty())
98 load_plugin(plugin_filename, plugin_aliases);
99
100 if (list_mode)
101 {
102 log("\n");
103 if (loaded_plugins.empty())
104 log("No plugins loaded.\n");
105 else
106 log("Loaded plugins:\n");
107
108 for (auto &it : loaded_plugins)
109 log(" %s\n", it.first.c_str());
110
111 if (!loaded_plugin_aliases.empty()) {
112 log("\n");
113 int max_alias_len = 1;
114 for (auto &it : loaded_plugin_aliases)
115 max_alias_len = std::max(max_alias_len, GetSize(it.first));
116 for (auto &it : loaded_plugin_aliases)
117 log("Alias: %-*s %s\n", max_alias_len, it.first.c_str(), it.second.c_str());
118 }
119 }
120 }
121 } PluginPass;
122
123 YOSYS_NAMESPACE_END
124