3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-10-18 21:40:28 +00:00

Merge pull request #5427 from donn/plugin_search_paths

plugins: add search paths
This commit is contained in:
Miodrag Milanović 2025-10-15 20:02:05 +02:00 committed by GitHub
commit 759996b968
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 20 deletions

View file

@ -22,6 +22,8 @@
#ifdef YOSYS_ENABLE_PLUGINS
# include <dlfcn.h>
# include <filesystem>
namespace fs = std::filesystem;
#endif
#ifdef YOSYS_ENABLE_PYTHON
@ -39,6 +41,26 @@ std::map<std::string, void*> loaded_python_plugins;
std::map<std::string, std::string> loaded_plugin_aliases;
#ifdef YOSYS_ENABLE_PLUGINS
static constexpr const char *path_delimiters = fs::path::preferred_separator == '\\' ? ";" : ":" ;
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 = &copy[0];
while ((token = strtok_r(rest, path_delimiters, &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)
{
std::string orig_filename = filename;
@ -55,17 +77,17 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
const bool is_loaded = loaded_plugins.count(orig_filename);
#endif
fs::path full_path = fs::absolute(filename);
if (!is_loaded) {
// Check if we're loading a python script
if (filename.rfind(".py") != std::string::npos) {
if (full_path.extension() == ".py") {
#ifdef YOSYS_ENABLE_PYTHON
py::object Path = py::module_::import("pathlib").attr("Path");
py::object full_path = Path(py::cast(filename));
py::object plugin_python_path = full_path.attr("parent");
auto basename = py::cast<std::string>(full_path.attr("stem"));
fs::path plugin_python_path = full_path.parent_path();
fs::path basename = full_path.stem();
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 {
auto module_container = py::module_::import(basename.c_str());
@ -83,23 +105,25 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
#endif
} else {
// Otherwise we assume it's a native plugin
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
if (hdl == NULL && orig_filename.find('/') == std::string::npos) {
hdl = dlopen([orig_filename]() {
std::string new_path = proc_share_dirname() + "plugins/" + orig_filename;
// Check if we need to append .so
if (new_path.find(".so") == std::string::npos)
new_path.append(".so");
return new_path;
}().c_str(), RTLD_LAZY|RTLD_LOCAL);
// We were unable to open the file, try to do so from plugin search
// paths
if (hdl == nullptr && orig_filename.find('/') == std::string::npos) {
const std::vector<fs::path> search_paths = get_plugin_search_paths();
for (const auto &search_path: search_paths) {
fs::path potential_path = search_path / orig_filename;
if (potential_path.extension() != ".so") {
potential_path = search_path / (orig_filename + ".so");
}
hdl = dlopen(potential_path.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());
loaded_plugins[orig_filename] = hdl;
@ -116,7 +140,7 @@ void load_plugin(std::string, std::vector<std::string>)
log_error(
"\n This version of Yosys cannot load plugins at runtime.\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

View file

@ -2,6 +2,7 @@
/write_gzip.v
/write_gzip.v.gz
/plugin.so
/plugin_search
/plugin.so.dSYM
/temp
/smtlib2_module.smt2

View file

@ -1,8 +1,12 @@
set -e
rm -f plugin.so
rm -rf plugin_search
CXXFLAGS=$(../../yosys-config --cxxflags)
DATDIR=$(../../yosys-config --datdir)
DATDIR=${DATDIR//\//\\\/}
CXXFLAGS=${CXXFLAGS//$DATDIR/..\/..\/share}
../../yosys-config --exec --cxx ${CXXFLAGS} --ldflags -shared -o plugin.so plugin.cc
../../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!"