|
| 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