mirror of
https://github.com/YosysHQ/yosys
synced 2025-10-22 07:10:35 +00:00
plugins: add search path
This uses the environment variable `YOSYS_PLUGIN_PATH` to provide multiple colon-delimited search paths for native plugins in a similar manner to `PATH` for executables and `PYTHONPATH` for Python modules. This addresses https://github.com/YosysHQ/yosys/issues/2545, allowing Yosys to be better packaged in non-FHS environments such as Nix.
This commit is contained in:
parent
4970ad5a18
commit
e86797f029
3 changed files with 46 additions and 20 deletions
|
@ -22,6 +22,8 @@
|
||||||
|
|
||||||
#ifdef YOSYS_ENABLE_PLUGINS
|
#ifdef YOSYS_ENABLE_PLUGINS
|
||||||
# include <dlfcn.h>
|
# include <dlfcn.h>
|
||||||
|
# include <filesystem>
|
||||||
|
namespace fs = std::filesystem;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef YOSYS_ENABLE_PYTHON
|
#ifdef YOSYS_ENABLE_PYTHON
|
||||||
|
@ -39,6 +41,23 @@ std::map<std::string, void*> loaded_python_plugins;
|
||||||
std::map<std::string, std::string> loaded_plugin_aliases;
|
std::map<std::string, std::string> loaded_plugin_aliases;
|
||||||
|
|
||||||
#ifdef YOSYS_ENABLE_PLUGINS
|
#ifdef YOSYS_ENABLE_PLUGINS
|
||||||
|
inline const std::vector<fs::path> get_plugin_search_paths() {
|
||||||
|
std::vector<fs::path> result;
|
||||||
|
const char *yosys_plugin_path = std::getenv("YOSYS_PLUGIN_PATH");
|
||||||
|
if (yosys_plugin_path != nullptr && strlen(yosys_plugin_path)) {
|
||||||
|
// make mutable. std::string also manages allocation as a bonus
|
||||||
|
// guaranteed contiguous in c++>=11
|
||||||
|
std::string copy{yosys_plugin_path};
|
||||||
|
char *token = nullptr;
|
||||||
|
char *rest = ©[0];
|
||||||
|
while ((token = strtok_r(rest, ":", &rest))) {
|
||||||
|
result.push_back(fs::path(token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.push_back(fs::path(proc_share_dirname()) / "plugins"); // lowest priority
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void load_plugin(std::string filename, std::vector<std::string> aliases)
|
void load_plugin(std::string filename, std::vector<std::string> aliases)
|
||||||
{
|
{
|
||||||
std::string orig_filename = filename;
|
std::string orig_filename = filename;
|
||||||
|
@ -55,17 +74,17 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
|
||||||
const bool is_loaded = loaded_plugins.count(orig_filename);
|
const bool is_loaded = loaded_plugins.count(orig_filename);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
fs::path full_path = fs::absolute(filename);
|
||||||
|
|
||||||
if (!is_loaded) {
|
if (!is_loaded) {
|
||||||
// Check if we're loading a python script
|
// Check if we're loading a python script
|
||||||
if (filename.rfind(".py") != std::string::npos) {
|
if (full_path.extension() == ".py") {
|
||||||
#ifdef YOSYS_ENABLE_PYTHON
|
#ifdef YOSYS_ENABLE_PYTHON
|
||||||
py::object Path = py::module_::import("pathlib").attr("Path");
|
fs::path plugin_python_path = full_path.parent_path();
|
||||||
py::object full_path = Path(py::cast(filename));
|
fs::path basename = full_path.stem();
|
||||||
py::object plugin_python_path = full_path.attr("parent");
|
|
||||||
auto basename = py::cast<std::string>(full_path.attr("stem"));
|
|
||||||
|
|
||||||
py::object sys = py::module_::import("sys");
|
py::object sys = py::module_::import("sys");
|
||||||
sys.attr("path").attr("insert")(0, py::str(plugin_python_path));
|
sys.attr("path").attr("insert")(0, py::str(plugin_python_path.c_str()));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto module_container = py::module_::import(basename.c_str());
|
auto module_container = py::module_::import(basename.c_str());
|
||||||
|
@ -83,23 +102,25 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
// Otherwise we assume it's a native plugin
|
// Otherwise we assume it's a native plugin
|
||||||
|
|
||||||
void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
|
void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL);
|
||||||
|
|
||||||
// We were unable to open the file, try to do so from the plugin directory
|
// We were unable to open the file, try to do so from plugin search
|
||||||
if (hdl == NULL && orig_filename.find('/') == std::string::npos) {
|
// paths
|
||||||
hdl = dlopen([orig_filename]() {
|
if (hdl == nullptr && orig_filename.find('/') == std::string::npos) {
|
||||||
std::string new_path = proc_share_dirname() + "plugins/" + orig_filename;
|
const std::vector<fs::path> search_paths = get_plugin_search_paths();
|
||||||
|
for (const auto &search_path: search_paths) {
|
||||||
// Check if we need to append .so
|
fs::path potential_path = search_path / orig_filename;
|
||||||
if (new_path.find(".so") == std::string::npos)
|
if (potential_path.extension() != ".so") {
|
||||||
new_path.append(".so");
|
potential_path = search_path / (orig_filename + ".so");
|
||||||
|
}
|
||||||
return new_path;
|
hdl = dlopen(potential_path.c_str(), RTLD_LAZY | RTLD_LOCAL);
|
||||||
}().c_str(), RTLD_LAZY|RTLD_LOCAL);
|
if (hdl != nullptr) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hdl == NULL)
|
if (hdl == nullptr)
|
||||||
log_cmd_error("Can't load module `%s': %s\n", filename, dlerror());
|
log_cmd_error("Can't load module `%s': %s\n", filename, dlerror());
|
||||||
|
|
||||||
loaded_plugins[orig_filename] = hdl;
|
loaded_plugins[orig_filename] = hdl;
|
||||||
|
@ -116,7 +137,7 @@ void load_plugin(std::string, std::vector<std::string>)
|
||||||
log_error(
|
log_error(
|
||||||
"\n This version of Yosys cannot load plugins at runtime.\n"
|
"\n This version of Yosys cannot load plugins at runtime.\n"
|
||||||
" Some plugins may have been included at build time.\n"
|
" Some plugins may have been included at build time.\n"
|
||||||
" Use option `-H' to see the available built-in and plugin commands.\n"
|
" Use `yosys -H' to see the available built-in and plugin commands.\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
1
tests/various/.gitignore
vendored
1
tests/various/.gitignore
vendored
|
@ -2,6 +2,7 @@
|
||||||
/write_gzip.v
|
/write_gzip.v
|
||||||
/write_gzip.v.gz
|
/write_gzip.v.gz
|
||||||
/plugin.so
|
/plugin.so
|
||||||
|
/plugin_search
|
||||||
/plugin.so.dSYM
|
/plugin.so.dSYM
|
||||||
/temp
|
/temp
|
||||||
/smtlib2_module.smt2
|
/smtlib2_module.smt2
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
set -e
|
set -e
|
||||||
rm -f plugin.so
|
rm -f plugin.so
|
||||||
|
rm -rf plugin_search
|
||||||
CXXFLAGS=$(../../yosys-config --cxxflags)
|
CXXFLAGS=$(../../yosys-config --cxxflags)
|
||||||
DATDIR=$(../../yosys-config --datdir)
|
DATDIR=$(../../yosys-config --datdir)
|
||||||
DATDIR=${DATDIR//\//\\\/}
|
DATDIR=${DATDIR//\//\\\/}
|
||||||
CXXFLAGS=${CXXFLAGS//$DATDIR/..\/..\/share}
|
CXXFLAGS=${CXXFLAGS//$DATDIR/..\/..\/share}
|
||||||
../../yosys-config --exec --cxx ${CXXFLAGS} --ldflags -shared -o plugin.so plugin.cc
|
../../yosys-config --exec --cxx ${CXXFLAGS} --ldflags -shared -o plugin.so plugin.cc
|
||||||
../../yosys -m ./plugin.so -p "test" | grep -q "Plugin test passed!"
|
../../yosys -m ./plugin.so -p "test" | grep -q "Plugin test passed!"
|
||||||
|
mkdir -p plugin_search
|
||||||
|
mv plugin.so plugin_search/plugin.so
|
||||||
|
YOSYS_PLUGIN_PATH=$PWD/plugin_search ../../yosys -m plugin.so -p "test" | grep -q "Plugin test passed!"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue