Skip to content

Commit

Permalink
Add NativeLibraryFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Dec 17, 2024
1 parent 05851f5 commit c17b62d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
}
}

/**
* Find the library with the specified name inside the {@code polyglot/lib} directory of caller's
* project. The search inside the {@code polyglot/lib} directory hierarchy is specified by <a
* href="https://bits.netbeans.org/23/javadoc/org-openide-modules/org/openide/modules/doc-files/api.html#jni">NetBeans
* JNI specification</a>.
*
* <p>Note: The current implementation iterates all the {@code polyglot/lib} directories of all
* the packages.
*
* @param libname The library name. Without platform-specific suffix or prefix.
* @return Absolute path to the library if found, or null.
*/
@Override
protected String findLibrary(String libname) {
var pkgRepo = EnsoContext.get(null).getPackageRepository();
for (var pkg : pkgRepo.getLoadedPackagesJava()) {
var libPath = NativeLibraryFinder.findNativeLibrary(libname, pkg);
if (libPath != null) {
return libPath;
}
}
logger.trace("Native library {} not found in any package", libname);
return null;
}

@Override
public void close() {
loadedClasses.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.enso.interpreter.runtime;

import com.oracle.truffle.api.TruffleFile;
import java.util.Locale;
import org.enso.pkg.Package;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Helper class to find native libraries in packages. The search algorithm complies to the <a
* href="https://bits.netbeans.org/23/javadoc/org-openide-modules/org/openide/modules/doc-files/api.html#jni">NetBeans
* JNI specification</a>.
*/
final class NativeLibraryFinder {

private static final Logger logger = LoggerFactory.getLogger(NativeLibraryFinder.class);

private NativeLibraryFinder() {}

/**
* Tries to find native library in the given package.
*
* @param libName the name of the library to find, without platform specific prefix or suffix.
* @param pkg the package to search in.
* @return null if not found, absolute path otherwise.
*/
static String findNativeLibrary(String libName, Package<TruffleFile> pkg) {
var arch = System.getProperty("os.arch");
var osName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
var libNameWithSuffix = System.mapLibraryName(libName);
var libDir = pkg.polyglotDir().resolve("lib");
if (!libDir.exists()) {
logger.trace("Native library directory {} does not exist", libDir);
return null;
}
var nativeLib = libDir.resolve(libNameWithSuffix);
if (nativeLib.exists()) {
logger.trace("Found native library {}", nativeLib);
return nativeLib.getAbsoluteFile().getPath();
}
nativeLib = libDir.resolve(arch).resolve(libNameWithSuffix);
if (nativeLib.exists()) {
logger.trace("Found native library {}", nativeLib);
return nativeLib.getAbsoluteFile().getPath();
}
nativeLib = libDir.resolve(arch).resolve(osName).resolve(libNameWithSuffix);
if (nativeLib.exists()) {
logger.trace("Found native library {}", nativeLib);
return nativeLib.getAbsoluteFile().getPath();
}
return null;
}
}

0 comments on commit c17b62d

Please sign in to comment.