Skip to content

Commit

Permalink
Redefined implementation for library path finding mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Dec 14, 2024
1 parent 75bbf81 commit 67b7217
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
13 changes: 4 additions & 9 deletions src/n8/ast/expression/VariableDeclarationExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ DynamicObject VariableDeclarationExpression::visit(SymbolTable& symbols) {

return {};
}
#include <iostream>

NativeFunction VariableDeclarationExpression::loadNativeFunction(
std::string& libName,
std::string& funcName,
Expand All @@ -69,22 +69,17 @@ NativeFunction VariableDeclarationExpression::loadNativeFunction(
std::string(libName.c_str())
);

std::filesystem::path path(library);
std::cout << "Loading module: " << library << std::endl;
std::cout << "Loading DLLs from: " << path.parent_path().string() << std::endl;

std::filesystem::path searchPath(library);
if(Runtime::hasLoadedLibrary(library))
#if defined(__unix__) || defined(__linux__) || defined(__APPLE__)
handle = Runtime::getLoadedLibrary(library);
#elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
handle = static_cast<HMODULE>(
Runtime::getLoadedLibrary(library)
);
handle = static_cast<HMODULE>(Runtime::getLoadedLibrary(library));
#endif
else {
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)

AddDllDirectory(path.parent_path().wstring().c_str());
AddDllDirectory(searchPath.parent_path().wstring().c_str());
handle = LoadLibraryA(library.c_str());

#elif defined(__APPLE__)
Expand Down
62 changes: 37 additions & 25 deletions src/n8/util/PathHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <string>

namespace N8Util {

Expand All @@ -43,37 +45,47 @@ bool PathHelper::isLibraryInstalled(
}

std::string PathHelper::findSharedLibrary(std::string name) {
if(std::filesystem::exists(name))
return name;
std::string
#if defined(__APPLE__)
extension = ".dylib";
#elif defined(__unix__) || defined(__linux__) || defined(__APPLE__)
extension = ".so";
#elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
extension = ".dll";
#endif

std::string rawName = name + extension;
if(std::filesystem::exists(rawName))
return rawName;

namespace fs = std::filesystem;
std::string directoryPath = PathHelper::installationPath() +
std::string libPath = PathHelper::installationPath() +
FS_FILE_SEPARATOR + "modules";

try {
if(fs::exists(directoryPath) &&
fs::is_directory(directoryPath)
) {
std::string libPath = directoryPath +
FS_FILE_SEPARATOR + name +
FS_FILE_SEPARATOR + "lib" +
FS_FILE_SEPARATOR + name;

#if defined(__APPLE__)
libPath += ".dylib";
#elif defined(__unix__) || defined(__linux__) || defined(__APPLE__)
libPath += ".so";
#elif defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
libPath += ".dll";
#endif

if(fs::exists(libPath))
return libPath;
}
for(const auto& modules : std::filesystem::directory_iterator(libPath)) {
std::string modPath = modules.path().string() +
FS_FILE_SEPARATOR + "lib";
std::string folderName = fs::path(modules)
.filename()
.string();

if(folderName.rfind(name, 0) == 0)
for(const auto& entry : std::filesystem::directory_iterator(modPath))
if(entry.is_regular_file()) {
std::string filename = entry
.path()
.filename()
.string();
std::string ext = fs::path(filename)
.extension()
.string();

if(filename.rfind(name, 0) == 0 && ext == extension)
return entry.path().string();
}
}
catch(const std::exception& ex) {}

return "";
throw std::runtime_error("Cannot find shared library: " + name);
}

std::vector<std::string> PathHelper::getLibraryFiles(
Expand Down

0 comments on commit 67b7217

Please sign in to comment.