Added cross-platform support for plugin-paths

This commit is contained in:
Benedikt Tutzer 2019-04-03 13:21:40 +02:00
parent bbfb43006d
commit fd7fb1377d
2 changed files with 12 additions and 8 deletions

View File

@ -270,9 +270,9 @@ endif
ifeq ($(ENABLE_PYOSYS),1) ifeq ($(ENABLE_PYOSYS),1)
ifeq ($(PYTHON_MAJOR_VERSION),3) ifeq ($(PYTHON_MAJOR_VERSION),3)
LDLIBS += -lpython$(PYTHON_VERSION)m -lboost_python-py$(subst .,,$(PYTHON_VERSION)) -lboost_system LDLIBS += -lpython$(PYTHON_VERSION)m -lboost_python-py$(subst .,,$(PYTHON_VERSION)) -lboost_system -lstdc++fs
else else
LDLIBS += -lpython$(PYTHON_VERSION) -lboost_python-py$(subst .,,$(PYTHON_VERSION)) -lboost_system LDLIBS += -lpython$(PYTHON_VERSION) -lboost_python-py$(subst .,,$(PYTHON_VERSION)) -lboost_system -lstdc++fs
endif endif
CXXFLAGS += -I/usr/include/python$(PYTHON_VERSION) -D WITH_PYTHON CXXFLAGS += -I/usr/include/python$(PYTHON_VERSION) -D WITH_PYTHON
PY_WRAPPER_FILE = kernel/python_wrappers PY_WRAPPER_FILE = kernel/python_wrappers

View File

@ -26,6 +26,7 @@
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
# include <boost/algorithm/string/predicate.hpp> # include <boost/algorithm/string/predicate.hpp>
# include <Python.h> # include <Python.h>
# include <experimental/filesystem>
#endif #endif
YOSYS_NAMESPACE_BEGIN YOSYS_NAMESPACE_BEGIN
@ -51,25 +52,28 @@ void load_plugin(std::string filename, std::vector<std::string> aliases)
#endif #endif
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
if(boost::algorithm::ends_with(filename, ".py"))
std::experimental::filesystem::path full_path(filename);
if(strcmp(full_path.extension().c_str(), ".py") == 0)
{ {
int last_slash = filename.find_last_of('/'); std::string path(full_path.parent_path().c_str());
std::string path = filename.substr(0, last_slash); filename = full_path.filename().c_str();
filename = filename.substr(last_slash+1, filename.size());
filename = filename.substr(0,filename.size()-3); filename = filename.substr(0,filename.size()-3);
PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str()); PyRun_SimpleString(("sys.path.insert(0,\""+path+"\")").c_str());
PyErr_Print();
PyObject *filename_p = PyUnicode_FromString(filename.c_str()); PyObject *filename_p = PyUnicode_FromString(filename.c_str());
if(filename_p == NULL) if(filename_p == NULL)
{ {
PyErr_Print(); PyErr_Print();
log_cmd_error("Issues converting `%s' to Python\n", filename.c_str()); log_cmd_error("Issues converting `%s' to Python\n", full_path.filename().c_str());
return; return;
} }
PyObject *module_p = PyImport_Import(filename_p); PyObject *module_p = PyImport_Import(filename_p);
if(module_p == NULL) if(module_p == NULL)
{ {
PyErr_Print(); PyErr_Print();
log_cmd_error("Can't load python module `%s'\n", filename.c_str()); log_cmd_error("Can't load python module `%s'\n", full_path.filename().c_str());
return; return;
} }
loaded_python_plugins[orig_filename] = module_p; loaded_python_plugins[orig_filename] = module_p;