yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
plugin.cc
Go to the documentation of this file.
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 
27 
28 std::map<std::string, void*> loaded_plugins;
29 std::map<std::string, std::string> loaded_plugin_aliases;
30 
31 #ifdef YOSYS_ENABLE_PLUGINS
32 void load_plugin(std::string filename, std::vector<std::string> aliases)
33 {
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;
43  }
44 
45  for (auto &alias : aliases)
46  loaded_plugin_aliases[alias] = filename;
47 }
48 #else
49 void load_plugin(std::string, std::vector<std::string>)
50 {
51  log_error("This version of yosys is built without plugin support.\n");
52 }
53 #endif
54 
55 struct PluginPass : public Pass {
56  PluginPass() : Pass("plugin", "load and list loaded plugins") { }
57  virtual void help()
58  {
59  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
60  log("\n");
61  log(" plugin [options]\n");
62  log("\n");
63  log("Load and list loaded plugins.\n");
64  log("\n");
65  log(" -i <plugin_filename>\n");
66  log(" Load (install) the specified plugin.\n");
67  log("\n");
68  log(" -a <alias_name>\n");
69  log(" Register the specified alias name for the loaded plugin\n");
70  log("\n");
71  log(" -l\n");
72  log(" List loaded plugins\n");
73  log("\n");
74  }
75  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
76  {
77  std::string plugin_filename;
78  std::vector<std::string> plugin_aliases;
79  bool list_mode = false;
80 
81  size_t argidx;
82  for (argidx = 1; argidx < args.size(); argidx++)
83  {
84  if ((args[argidx] == "-i") && argidx+1 < args.size() && plugin_filename.empty()) {
85  plugin_filename = args[++argidx];
86  continue;
87  }
88  if ((args[argidx] == "-a") && argidx+1 < args.size()) {
89  plugin_aliases.push_back(args[++argidx]);
90  continue;
91  }
92  if (args[argidx] == "-l") {
93  list_mode = true;
94  continue;
95  }
96  break;
97  }
98  extra_args(args, argidx, design, false);
99 
100  if (!plugin_filename.empty())
101  load_plugin(plugin_filename, plugin_aliases);
102 
103  if (list_mode)
104  {
105  log("\n");
106  if (loaded_plugins.empty())
107  log("No plugins loaded.\n");
108  else
109  log("Loaded plugins:\n");
110 
111  for (auto &it : loaded_plugins)
112  log(" %s\n", it.first.c_str());
113 
114  if (!loaded_plugin_aliases.empty()) {
115  log("\n");
116  int max_alias_len = 1;
117  for (auto &it : loaded_plugin_aliases)
118  max_alias_len = std::max(max_alias_len, GetSize(it.first));
119  for (auto &it : loaded_plugin_aliases)
120  log("Alias: %-*s %s\n", max_alias_len, it.first.c_str(), it.second.c_str());
121  }
122  }
123  }
124 } PluginPass;
125 
127 
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: plugin.cc:75
#define YOSYS_NAMESPACE_END
Definition: yosys.h:100
YOSYS_NAMESPACE_BEGIN std::map< std::string, void * > loaded_plugins
Definition: plugin.cc:28
void log_error(const char *format,...)
Definition: log.cc:204
void load_plugin(std::string, std::vector< std::string >)
Definition: plugin.cc:49
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
PluginPass()
Definition: plugin.cc:56
Definition: register.h:27
void log_cmd_error(const char *format,...)
Definition: log.cc:211
std::map< std::string, std::string > loaded_plugin_aliases
Definition: plugin.cc:29
#define NULL
#define YOSYS_NAMESPACE_BEGIN
Definition: yosys.h:99
void log(const char *format,...)
Definition: log.cc:180
PluginPass PluginPass
static void init_register()
Definition: register.cc:54
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
virtual void help()
Definition: plugin.cc:57