Skip to content

Commit c17b62d

Browse files
committed
Add NativeLibraryFinder
1 parent 05851f5 commit c17b62d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

engine/runtime/src/main/java/org/enso/interpreter/runtime/HostClassLoader.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,31 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
8181
}
8282
}
8383

84+
/**
85+
* Find the library with the specified name inside the {@code polyglot/lib} directory of caller's
86+
* project. The search inside the {@code polyglot/lib} directory hierarchy is specified by <a
87+
* href="https://bits.netbeans.org/23/javadoc/org-openide-modules/org/openide/modules/doc-files/api.html#jni">NetBeans
88+
* JNI specification</a>.
89+
*
90+
* <p>Note: The current implementation iterates all the {@code polyglot/lib} directories of all
91+
* the packages.
92+
*
93+
* @param libname The library name. Without platform-specific suffix or prefix.
94+
* @return Absolute path to the library if found, or null.
95+
*/
96+
@Override
97+
protected String findLibrary(String libname) {
98+
var pkgRepo = EnsoContext.get(null).getPackageRepository();
99+
for (var pkg : pkgRepo.getLoadedPackagesJava()) {
100+
var libPath = NativeLibraryFinder.findNativeLibrary(libname, pkg);
101+
if (libPath != null) {
102+
return libPath;
103+
}
104+
}
105+
logger.trace("Native library {} not found in any package", libname);
106+
return null;
107+
}
108+
84109
@Override
85110
public void close() {
86111
loadedClasses.clear();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.enso.interpreter.runtime;
2+
3+
import com.oracle.truffle.api.TruffleFile;
4+
import java.util.Locale;
5+
import org.enso.pkg.Package;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
/**
10+
* Helper class to find native libraries in packages. The search algorithm complies to the <a
11+
* href="https://bits.netbeans.org/23/javadoc/org-openide-modules/org/openide/modules/doc-files/api.html#jni">NetBeans
12+
* JNI specification</a>.
13+
*/
14+
final class NativeLibraryFinder {
15+
16+
private static final Logger logger = LoggerFactory.getLogger(NativeLibraryFinder.class);
17+
18+
private NativeLibraryFinder() {}
19+
20+
/**
21+
* Tries to find native library in the given package.
22+
*
23+
* @param libName the name of the library to find, without platform specific prefix or suffix.
24+
* @param pkg the package to search in.
25+
* @return null if not found, absolute path otherwise.
26+
*/
27+
static String findNativeLibrary(String libName, Package<TruffleFile> pkg) {
28+
var arch = System.getProperty("os.arch");
29+
var osName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
30+
var libNameWithSuffix = System.mapLibraryName(libName);
31+
var libDir = pkg.polyglotDir().resolve("lib");
32+
if (!libDir.exists()) {
33+
logger.trace("Native library directory {} does not exist", libDir);
34+
return null;
35+
}
36+
var nativeLib = libDir.resolve(libNameWithSuffix);
37+
if (nativeLib.exists()) {
38+
logger.trace("Found native library {}", nativeLib);
39+
return nativeLib.getAbsoluteFile().getPath();
40+
}
41+
nativeLib = libDir.resolve(arch).resolve(libNameWithSuffix);
42+
if (nativeLib.exists()) {
43+
logger.trace("Found native library {}", nativeLib);
44+
return nativeLib.getAbsoluteFile().getPath();
45+
}
46+
nativeLib = libDir.resolve(arch).resolve(osName).resolve(libNameWithSuffix);
47+
if (nativeLib.exists()) {
48+
logger.trace("Found native library {}", nativeLib);
49+
return nativeLib.getAbsoluteFile().getPath();
50+
}
51+
return null;
52+
}
53+
}

0 commit comments

Comments
 (0)