23 #include "casadi_os.hpp"
24 #include "exception.hpp"
25 #include "global_options.hpp"
30 #define STRING(ITEMS) \
31 ((dynamic_cast<std::ostringstream &>(std::ostringstream() \
32 . seekp(0, std::ios_base::cur) << ITEMS)) . str())
52 std::vector<std::string> search_paths;
56 std::string casadipath;
57 while (std::getline(casadipaths, casadipath,
pathsep())) {
58 search_paths.push_back(casadipath);
63 pLIBDIR = getenv(
"CASADIPATH");
65 if (pLIBDIR!=
nullptr) {
66 std::stringstream casadipaths(pLIBDIR);
67 std::string casadipath;
68 while (std::getline(casadipaths, casadipath,
pathsep())) {
69 search_paths.push_back(casadipath);
74 search_paths.push_back(
"");
77 #ifdef PLUGIN_EXTRA_SEARCH_PATH
78 search_paths.push_back(
79 std::string(
"") + PLUGIN_EXTRA_SEARCH_PATH);
83 search_paths.push_back(
".");
90 handle_t open_shared_library(
const std::string& lib,
const std::vector<std::string> &search_paths,
91 const std::string& caller,
bool global) {
92 std::string resultpath;
93 return open_shared_library(lib, search_paths, resultpath, caller, global);
96 int close_shared_library(handle_t handle) {
98 return !FreeLibrary(handle);
100 return dlclose(handle);
104 handle_t open_shared_library(
const std::string& lib,
const std::vector<std::string> &search_paths,
105 std::string& resultpath,
const std::string& caller,
bool global) {
113 flag = RTLD_NOW | RTLD_GLOBAL;
115 flag = RTLD_LAZY | RTLD_LOCAL;
118 #if !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
119 flag |= RTLD_DEEPBIND;
126 std::stringstream errors;
127 errors << caller <<
": Cannot load shared library '"
128 << lib <<
"': " << std::endl;
130 <<
" Searched directories: 1. casadipath from GlobalOptions\n"
131 <<
" 2. CASADIPATH env var\n"
132 <<
" 3. PATH env var (Windows)\n"
133 <<
" 4. LD_LIBRARY_PATH env var (Linux)\n"
134 <<
" 5. DYLD_LIBRARY_PATH env var (osx)\n"
135 <<
" A library may be 'not found' even if the file exists:\n"
136 <<
" * library is not compatible (different compiler/bitness)\n"
137 <<
" * the dependencies are not found\n"
140 std::string searchpath;
143 for (casadi_int i=0;i<search_paths.size();++i) {
144 searchpath = search_paths[i];
146 SetDllDirectory(TEXT(searchpath.c_str()));
147 handle = LoadLibrary(TEXT(lib.c_str()));
148 SetDllDirectory(NULL);
150 std::string libname = searchpath.empty() ? lib : searchpath +
filesep() + lib;
151 handle = dlopen(libname.c_str(), flag);
154 resultpath = searchpath;
157 errors << std::endl <<
" Tried '" << searchpath <<
"' :";
159 errors << std::endl <<
" Error code (WIN32): " << STRING(GetLastError());
161 errors << std::endl <<
" Error code: " << dlerror();
166 casadi_assert(handle!=
nullptr, errors.str());
static std::string getCasadiPath()
std::vector< std::string > get_search_paths()