Skip to content

Commit

Permalink
Gather native libraries in EnsoLibraryFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Dec 23, 2024
1 parent 3d9a377 commit 7bc8768
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
58 changes: 21 additions & 37 deletions engine/runner/src/main/java/org/enso/runner/EnsoLibraryFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@

import static scala.jdk.javaapi.CollectionConverters.asJava;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import org.enso.compiler.core.EnsoParser;
import org.enso.compiler.core.ir.module.scope.imports.Polyglot;
import org.enso.filesystem.FileSystem$;
import org.enso.pkg.NativeLibraryFinder;
import org.enso.pkg.PackageManager$;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeProxyCreation;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import org.graalvm.nativeimage.hosted.RuntimeResourceAccess;
import org.graalvm.nativeimage.hosted.RuntimeSystemProperties;

public final class EnsoLibraryFeature implements Feature {
@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
try {
registerOpenCV(access.getApplicationClassLoader());
} catch (ReflectiveOperationException ex) {
ex.printStackTrace();
throw new IllegalStateException(ex);
}

var libs = new LinkedHashSet<Path>();
for (var p : access.getApplicationClassPath()) {
var p1 = p.getParent();
Expand Down Expand Up @@ -53,6 +51,7 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
*/

var classes = new TreeSet<String>();
var nativeLibPaths = new TreeSet<String>();
try {
for (var p : libs) {
var result = PackageManager$.MODULE$.Default().loadPackage(p.toFile());
Expand Down Expand Up @@ -90,6 +89,19 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
}
}
}
if (pkg.nativeLibraryDir().exists()) {
var nativeLibs =
NativeLibraryFinder.listAllNativeLibraries(pkg, FileSystem$.MODULE$.defaultFs());
for (var nativeLib : nativeLibs) {
assert nativeLib.exists() && nativeLib.isFile();
var dir = nativeLib.toPath().getParent().toFile();
assert dir.exists() && dir.isDirectory();
nativeLibPaths.add(dir.getAbsolutePath());
var current = System.getProperty("java.library.path");
RuntimeSystemProperties.register(
"java.library.path", current + File.pathSeparator + dir.getAbsolutePath());
}
}
}
}
} catch (Exception ex) {
Expand All @@ -101,35 +113,7 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
System.err.println(" " + className);
}
System.err.println("Registered " + classes.size() + " classes for reflection");
}

private static void registerOpenCV(ClassLoader cl) throws ReflectiveOperationException {
var moduleOpenCV = cl.getUnnamedModule();
var currentOS = System.getProperty("os.name").toUpperCase().replaceAll(" .*$", "");

var libOpenCV =
switch (currentOS) {
case "LINUX" -> "nu/pattern/opencv/linux/x86_64/libopencv_java470.so";
case "WINDOWS" -> "nu/pattern/opencv/windows/x86_64/opencv_java470.dll";
case "MAC" -> {
var arch = System.getProperty("os.arch").toUpperCase();
yield switch (arch) {
case "X86_64" -> "nu/pattern/opencv/osx/x86_64/libopencv_java470.dylib";
case "AARCH64" -> "nu/pattern/opencv/osx/ARMv8/libopencv_java470.dylib";
default -> null;
};
}
default -> null;
};

if (libOpenCV != null) {
var verify = cl.getResource(libOpenCV);
if (verify == null) {
throw new IllegalStateException("Cannot find " + libOpenCV + " resource in " + cl);
}
RuntimeResourceAccess.addResource(moduleOpenCV, libOpenCV);
} else {
throw new IllegalStateException("No resource suggested for " + currentOS);
}
System.err.println(
"Registered native libraries into runtime's java.library.path: " + nativeLibPaths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ trait FileSystem[F] {

object FileSystem {

val defaultFs = Default

/** Exposes [[FileSystem]] operations through method call syntax.
* All methods have the same semantics as the corresponding [[FileSystem]]
* methods.
Expand Down

0 comments on commit 7bc8768

Please sign in to comment.