-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
engine/runtime/src/main/java/org/enso/interpreter/runtime/NativeLibraryFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |