From c22e138c878eb9d8cbc41b6556fba1ec420ecfc0 Mon Sep 17 00:00:00 2001 From: Ioannis Tsakpinis Date: Sun, 13 Sep 2020 16:38:02 +0300 Subject: [PATCH] feat(core): report mismatching native jars. Close #587 --- build.xml | 7 ++- .../main/java/org/lwjgl/system/Library.java | 50 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/build.xml b/build.xml index 8be9c5ebbe..157951b5b1 100644 --- a/build.xml +++ b/build.xml @@ -881,8 +881,12 @@ + + + + @@ -891,6 +895,7 @@ + @@ -913,7 +918,7 @@ - + diff --git a/modules/lwjgl/core/src/main/java/org/lwjgl/system/Library.java b/modules/lwjgl/core/src/main/java/org/lwjgl/system/Library.java index c7798d9739..dd40c87644 100644 --- a/modules/lwjgl/core/src/main/java/org/lwjgl/system/Library.java +++ b/modules/lwjgl/core/src/main/java/org/lwjgl/system/Library.java @@ -15,6 +15,7 @@ import java.security.*; import java.util.*; import java.util.function.*; +import java.util.jar.*; import java.util.regex.*; import static org.lwjgl.system.APIUtil.*; @@ -41,7 +42,7 @@ public final class Library { if (DEBUG) { apiLog("Version: " + Version.getVersion()); apiLog("\t OS: " + System.getProperty("os.name") + " v" + System.getProperty("os.version")); - apiLog("\tJRE: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")); + apiLog("\tJRE: " + Platform.get().getName() + " " + System.getProperty("os.arch") + " " + System.getProperty("java.version")); apiLog( "\tJVM: " + System.getProperty("java.vm.name") + " v" + System.getProperty("java.vm.version") + " by " + System.getProperty("java.vm.vendor") ); @@ -158,6 +159,7 @@ public static void loadSystem( apiLog(String.format("\t%s not found in %s", libName, JAVA_LIBRARY_PATH)); } + detectPlatformMismatch(context, module); printError(true); throw new UnsatisfiedLinkError("Failed to locate library: " + libName); } @@ -319,6 +321,7 @@ private static SharedLibrary loadNative(Class context, String module, String } if (printError) { + detectPlatformMismatch(context, module); printError(bundledWithLWJGL); } throw new UnsatisfiedLinkError("Failed to locate library: " + libName); @@ -478,6 +481,51 @@ private static Path findFile(String path, String file) { return null; } + private static void detectPlatformMismatch(Class context, String module) { + String moduleTitle = module.equals("org.lwjgl") ? "lwjgl" : "lwjgl-" + module.substring("org.lwjgl.".length()); + + List platforms = new ArrayList<>(8); + try { + Enumeration manifests = context.getClassLoader().getResources(JarFile.MANIFEST_NAME); + while (manifests.hasMoreElements()) { + try (InputStream is = manifests.nextElement().openStream()) { + Manifest manifest = new Manifest(is); + Attributes attribs = manifest.getMainAttributes(); + + if (moduleTitle.equals(attribs.getValue("Implementation-Title"))) { + String jarPlatform = attribs.getValue("LWJGL-Platform"); + if (jarPlatform != null) { + platforms.add(jarPlatform); + } + } + } + } + } catch (IOException ignored) { + } + + if (!platforms.isEmpty()) { + DEBUG_STREAM.println("[LWJGL] Platform/architecture mismatch detected for module: " + module); + DEBUG_STREAM.println("\tJVM platform:"); + DEBUG_STREAM.format( + "\t\t%s %s %s\n", + Platform.get().getName(), + System.getProperty("os.arch"), + System.getProperty("java.version") + ); + DEBUG_STREAM.format( + "\t\t%s v%s by %s\n", + System.getProperty("java.vm.name"), + System.getProperty("java.vm.version"), + System.getProperty("java.vm.vendor") + ); + DEBUG_STREAM.format( + "\tPlatform%s available on classpath:\n\t\t%s\n", + (platforms.size() == 1 ? "" : "s"), + String.join("\n\t\t", platforms) + ); + } + } + private static void printError(boolean bundledWithLWJGL) { printError( "[LWJGL] Failed to load a library. Possible solutions:\n" + (bundledWithLWJGL