Skip to content

Commit

Permalink
feat(core): report mismatching native jars. Close LWJGL#587
Browse files Browse the repository at this point in the history
  • Loading branch information
Spasi authored and theofficialgman committed Feb 25, 2022
1 parent 986d3c8 commit c22e138
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
7 changes: 6 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,12 @@
<macrodef name="lwjgl-jar">
<attribute name="destfile"/>
<attribute name="title"/>
<attribute name="platform" default=""/>
<element name="content" optional="true" implicit="true"/>
<sequential>
<local name="platform-attrib"/>
<condition property="platform-attrib" value="true" else="false"><not><length string="@{platform}" length="0"/></not></condition>

<jar destfile="@{destfile}" level="9" index="true" indexmetainf="true" whenmanifestonly="skip">
<manifest>
<attribute name="Specification-Title" value="Lightweight Java Game Library - @{title}"/>
Expand All @@ -891,6 +895,7 @@
<attribute name="Implementation-Title" value="${module}"/>
<attribute name="Implementation-Version" value="${revision}"/>
<attribute name="Implementation-Vendor" value="lwjgl.org"/>
<attribute name="LWJGL-Platform" value="@{platform}" if:true="${platform-attrib}"/>
<attribute name="Multi-Release" value="true" if:set="jdk9"/>
</manifest>
<content/>
Expand All @@ -913,7 +918,7 @@
</resourcecount>
</condition>

<lwjgl-jar destfile="${release}/${module}/${module}-natives-@{platform}.jar" title="@{title}" unless:set="platform.skip">
<lwjgl-jar destfile="${release}/${module}/${module}-natives-@{platform}.jar" platform="@{path}" title="@{title}" unless:set="platform.skip">
<fileset dir="${release}/${module}/native" includes="@{path}/${module.path}/*.@{type}*"/>
<fileset dir="${release}/${module}/native/@{path}" includes="META-INF/versions/9/module-info.class" if:set="jdk9"/>
<content/>
Expand Down
50 changes: 49 additions & 1 deletion modules/lwjgl/core/src/main/java/org/lwjgl/system/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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")
);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> platforms = new ArrayList<>(8);
try {
Enumeration<URL> 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
Expand Down

0 comments on commit c22e138

Please sign in to comment.