mirror of
https://github.com/nomic-ai/gpt4all.git
synced 2025-10-23 17:09:06 +00:00
* backend: refactor dlhandle.h into oscompat.{cpp,h} Signed-off-by: Jared Van Bortel <jared@nomic.ai> * llmodel: alias std::filesystem Signed-off-by: Jared Van Bortel <jared@nomic.ai> * llmodel: use wide strings for paths on Windows Using the native path representation allows us to manipulate paths and call LoadLibraryEx without mangling non-ASCII characters. Signed-off-by: Jared Van Bortel <jared@nomic.ai> * llmodel: prefer built-in std::filesystem functionality Signed-off-by: Jared Van Bortel <jared@nomic.ai> * oscompat: fix string type error Signed-off-by: Jared Van Bortel <jared@nomic.ai> * backend: rename oscompat back to dlhandle Signed-off-by: Jared Van Bortel <jared@nomic.ai> * dlhandle: fix #includes Signed-off-by: Jared Van Bortel <jared@nomic.ai> * dlhandle: remove another #include Signed-off-by: Jared Van Bortel <jared@nomic.ai> * dlhandle: move dlhandle #include Signed-off-by: Jared Van Bortel <jared@nomic.ai> * dlhandle: remove #includes that are covered by dlhandle.h Signed-off-by: Jared Van Bortel <jared@nomic.ai> * llmodel: fix #include order Signed-off-by: Jared Van Bortel <jared@nomic.ai> --------- Signed-off-by: Jared Van Bortel <jared@nomic.ai>
57 lines
1.3 KiB
C++
57 lines
1.3 KiB
C++
#include "dlhandle.h"
|
|
|
|
#ifndef _WIN32
|
|
# include <dlfcn.h>
|
|
#else
|
|
# include <sstream>
|
|
# define WIN32_LEAN_AND_MEAN
|
|
# ifndef NOMINMAX
|
|
# define NOMINMAX
|
|
# endif
|
|
# include <windows.h>
|
|
#endif
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
Dlhandle::Dlhandle(const fs::path &fpath) {
|
|
chandle = dlopen(fpath.c_str(), RTLD_LAZY | RTLD_LOCAL);
|
|
if (!chandle) {
|
|
throw Exception("dlopen(\"" + fpath.filename().string() + "\"): " + dlerror());
|
|
}
|
|
}
|
|
|
|
Dlhandle::~Dlhandle() {
|
|
if (chandle) dlclose(chandle);
|
|
}
|
|
|
|
void *Dlhandle::get_internal(const char *symbol) const {
|
|
return dlsym(chandle, symbol);
|
|
}
|
|
|
|
#else // defined(_WIN32)
|
|
|
|
Dlhandle::Dlhandle(const fs::path &fpath) {
|
|
auto afpath = fs::absolute(fpath);
|
|
chandle = LoadLibraryExW(afpath.c_str(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
|
|
|
|
if (!chandle) {
|
|
auto err = GetLastError();
|
|
std::ostringstream ss;
|
|
ss << "LoadLibraryExW(\"" << fpath.filename().string() << "\") failed with error 0x" << std::hex << err;
|
|
throw Exception(ss.str());
|
|
}
|
|
}
|
|
|
|
Dlhandle::~Dlhandle() {
|
|
if (chandle) FreeLibrary(HMODULE(chandle));
|
|
}
|
|
|
|
void *Dlhandle::get_internal(const char *symbol) const {
|
|
return GetProcAddress(HMODULE(chandle), symbol);
|
|
}
|
|
|
|
#endif // defined(_WIN32)
|