From e879a274fdd31eba1db32d2c62b27fa7fa133546 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Tue, 10 Dec 2024 06:21:25 +0100 Subject: [PATCH] Including opencv native libraries as a resource in enso runner binary (#11807) --- build.sbt | 1 + build/build/src/engine/context.rs | 8 +++- .../org/enso/runner/EnsoLibraryFeature.java | 37 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 08020ac19c..1e40428b55 100644 --- a/build.sbt +++ b/build.sbt @@ -3621,6 +3621,7 @@ lazy val `engine-runner` = project ).distinct val stdLibsJars = `base-polyglot-root`.listFiles("*.jar").map(_.getAbsolutePath()) ++ + `image-polyglot-root`.listFiles("*.jar").map(_.getAbsolutePath()) ++ `table-polyglot-root`.listFiles("*.jar").map(_.getAbsolutePath()) core ++ stdLibsJars }, diff --git a/build/build/src/engine/context.rs b/build/build/src/engine/context.rs index 250d042a66..896783ae3e 100644 --- a/build/build/src/engine/context.rs +++ b/build/build/src/engine/context.rs @@ -690,7 +690,13 @@ pub async fn runner_sanity_test( .run_ok() .await; - let all_cmds = test_base.and(test_internal_base).and(test_geo); + let test_image = Command::new(&enso) + .args(["--run", repo_root.test.join("Image_Tests").as_str()]) + .set_env(ENSO_DATA_DIRECTORY, engine_package)? + .run_ok() + .await; + + let all_cmds = test_base.and(test_internal_base).and(test_geo).and(test_image); // The following test does not actually run anything, it just checks if the engine // can accept `--jvm` argument and evaluates something. diff --git a/engine/runner/src/main/java/org/enso/runner/EnsoLibraryFeature.java b/engine/runner/src/main/java/org/enso/runner/EnsoLibraryFeature.java index 433271da46..7b25fd8a11 100644 --- a/engine/runner/src/main/java/org/enso/runner/EnsoLibraryFeature.java +++ b/engine/runner/src/main/java/org/enso/runner/EnsoLibraryFeature.java @@ -12,10 +12,17 @@ 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; 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(); for (var p : access.getApplicationClassPath()) { var p1 = p.getParent(); @@ -95,4 +102,34 @@ public final class EnsoLibraryFeature implements Feature { } 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); + } + } }