diff --git a/modules/core/src/main/java/org/lwjgl/egl/EGL.java b/modules/core/src/main/java/org/lwjgl/egl/EGL.java index f146ed4f58..a825bcf136 100644 --- a/modules/core/src/main/java/org/lwjgl/egl/EGL.java +++ b/modules/core/src/main/java/org/lwjgl/egl/EGL.java @@ -51,22 +51,21 @@ private EGL() {} /** Loads the EGL native library, using the default library name. */ public static void create() { - String libName = Configuration.LIBRARY_NAME_EGL.get(); - if ( libName == null ) { - switch ( Platform.get() ) { - case LINUX: - libName = "libEGL.so.1"; - break; - case MACOSX: - throw new UnsupportedOperationException("MacOS X does not support EGL"); - case WINDOWS: - libName = "libEGL"; - break; - default: - throw new IllegalStateException(); - } + SharedLibrary EGL; + switch ( Platform.get() ) { + case LINUX: + EGL = Library.loadNative(Configuration.LIBRARY_NAME_EGL, "libEGL.so.1"); + break; + case MACOSX: + EGL = Library.loadNative(Configuration.LIBRARY_NAME_EGL, "EGL"); + break; + case WINDOWS: + EGL = Library.loadNative(Configuration.LIBRARY_NAME_EGL, "libEGL", "EGL"); + break; + default: + throw new IllegalStateException(); } - create(libName); + create(EGL); } /** @@ -75,8 +74,10 @@ public static void create() { * @param libName the native library name */ public static void create(String libName) { - final SharedLibrary EGL = Library.loadNative(libName); + create(Library.loadNative(libName)); + } + private static void create(final SharedLibrary EGL) { try { FunctionProvider functionProvider = new FunctionProvider.Default() { private final long eglGetProcAddress = EGL.getFunctionAddress("eglGetProcAddress"); diff --git a/modules/core/src/main/java/org/lwjgl/opencl/CL.java b/modules/core/src/main/java/org/lwjgl/opencl/CL.java index eb75533926..4d30c1d96e 100644 --- a/modules/core/src/main/java/org/lwjgl/opencl/CL.java +++ b/modules/core/src/main/java/org/lwjgl/opencl/CL.java @@ -29,25 +29,26 @@ public final class CL { private CL() {} public static void create() { - create(Configuration.LIBRARY_NAME_OPENCL.get("OpenCL")); + SharedLibrary CL; + switch ( Platform.get() ) { + case LINUX: + case WINDOWS: + CL = Library.loadNative(Configuration.LIBRARY_NAME_OPENCL, "OpenCL"); + break; + case MACOSX: + CL = Library.loadNative(Configuration.LIBRARY_NAME_OPENCL, "OpenCL", "/System/Library/Frameworks/OpenCL.framework"); + break; + default: + throw new IllegalStateException(); + } + create(CL); } public static void create(String libName) { - final SharedLibrary OPENCL; - - { - SharedLibrary dll; - try { - dll = Library.loadNative(libName); - } catch (Throwable t) { - if ( Platform.get() == Platform.MACOSX ) - dll = apiCreateLibrary("/System/Library/Frameworks/OpenCL.framework"); - else - throw new RuntimeException(t); - } - OPENCL = dll; - } + create(Library.loadNative(libName)); + } + public static void create(final SharedLibrary OPENCL) { try { FunctionProviderLocal functionProvider = new FunctionProviderLocal.Default() { diff --git a/modules/core/src/main/java/org/lwjgl/opengl/GL.java b/modules/core/src/main/java/org/lwjgl/opengl/GL.java index daf53923cb..12fcdeb60c 100644 --- a/modules/core/src/main/java/org/lwjgl/opengl/GL.java +++ b/modules/core/src/main/java/org/lwjgl/opengl/GL.java @@ -65,22 +65,21 @@ private GL() {} /** Loads the OpenGL native library, using the default library name. */ public static void create() { - String libName; + SharedLibrary GL; switch ( Platform.get() ) { - case WINDOWS: - libName = "opengl32"; - break; case LINUX: - libName = "libGL.so.1"; + GL = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "libGL.so.1", "libGL.so"); break; case MACOSX: - libName = "/System/Library/Frameworks/OpenGL.framework"; + GL = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "/System/Library/Frameworks/OpenGL.framework"); + break; + case WINDOWS: + GL = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "opengl32"); break; default: throw new IllegalStateException(); } - - create(Configuration.LIBRARY_NAME_OPENGL.get(libName)); + create(GL); } /** @@ -89,8 +88,10 @@ public static void create() { * @param libName the native library name */ public static void create(String libName) { - final SharedLibrary OPENGL = Library.loadNative(libName); + create(Library.loadNative(libName)); + } + private static void create(final SharedLibrary OPENGL) { abstract class FunctionProviderGL extends FunctionProvider.Default { abstract long getExtensionAddress(long name); diff --git a/modules/core/src/main/java/org/lwjgl/opengles/GLES.java b/modules/core/src/main/java/org/lwjgl/opengles/GLES.java index 297da95549..3628713dc5 100644 --- a/modules/core/src/main/java/org/lwjgl/opengles/GLES.java +++ b/modules/core/src/main/java/org/lwjgl/opengles/GLES.java @@ -65,21 +65,21 @@ private GLES() {} /** Loads the OpenGL ES native library, using the default library name. */ public static void create() { - String libName; + SharedLibrary GLES; switch ( Platform.get() ) { case LINUX: - libName = "libGLESv2.so.2"; + GLES = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "libGLESv2.so.2"); break; case MACOSX: - throw new UnsupportedOperationException("MacOS X does not support OpenGL ES"); + GLES = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "GLESv2"); + break; case WINDOWS: - libName = "libGLESv2"; + GLES = Library.loadNative(Configuration.LIBRARY_NAME_OPENGL, "libGLESv2", "GLESv2"); break; default: throw new IllegalStateException(); } - - create(Configuration.LIBRARY_NAME_OPENGLES.get(libName)); + create(GLES); } /** @@ -88,8 +88,10 @@ public static void create() { * @param libName the native library name */ public static void create(String libName) { - final SharedLibrary GLES = Library.loadNative(libName); + create(Library.loadNative(libName)); + } + private static void create(final SharedLibrary GLES) { try { FunctionProvider functionProvider = new FunctionProvider.Default() { @Override diff --git a/modules/core/src/main/java/org/lwjgl/system/Library.java b/modules/core/src/main/java/org/lwjgl/system/Library.java index 3a0d432f63..f837b7ba07 100644 --- a/modules/core/src/main/java/org/lwjgl/system/Library.java +++ b/modules/core/src/main/java/org/lwjgl/system/Library.java @@ -11,10 +11,10 @@ import static org.lwjgl.system.APIUtil.*; -/** Initializes the LWJGL native library and handles loading additional shared libraries. */ +/** Initializes the LWJGL shared library and handles loading additional shared libraries. */ public final class Library { - /** The LWJGL native library name */ + /** The LWJGL shared library name. */ public static final String JNI_LIBRARY_NAME = Configuration.LIBRARY_NAME_LWJGL.get(System.getProperty("os.arch").contains("64") ? "lwjgl" : "lwjgl32"); private static final String JAVA_LIBRARY_PATH = "java.library.path"; @@ -39,7 +39,9 @@ public SharedLibrary load(File library) { 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("\tJVM: " + System.getProperty("java.vm.name") + " v" + System.getProperty("java.vm.version") + " by " + System.getProperty("java.vm.vendor")); + apiLog( + "\tJVM: " + System.getProperty("java.vm.name") + " v" + System.getProperty("java.vm.version") + " by " + System.getProperty("java.vm.vendor") + ); } try { @@ -60,13 +62,13 @@ public SharedLibrary load(File library) { private Library() {} - /** Ensures that the LWJGL native library has been loaded. */ + /** Ensures that the LWJGL shared library has been loaded. */ public static void initialize() { // intentionally empty to trigger static initializer } /** - * Loads a native library using {@link System}. + * Loads a shared library using {@link System}. * *
If {@code name} is an absolute path or {@link Configuration#LIBRARY_PATH} is set, {@link System#load} will be used. Otherwise, * {@link System#loadLibrary} will be used.
@@ -106,16 +108,15 @@ public static void loadSystem(String name) throws UnsatisfiedLinkError { } /** - * Loads a native library using OS-specific APIs (e.g. {@link org.lwjgl.system.windows.WinBase#LoadLibrary LoadLibrary} or + * Loads a shared library using OS-specific APIs (e.g. {@link org.lwjgl.system.windows.WinBase#LoadLibrary LoadLibrary} or * {@link org.lwjgl.system.linux.DynamicLinkLoader#dlopen dlopen}). * - * @param name the library name, without an OS specific prefix or file extension (e.g. GL, not libGL.so) + * @param name the library name. OS-specific prefixes and file extensions are optional (e.g. both {@code "GL"} and {@code "libGL.so.1"} are valid on Linux) * - * @return the native library + * @return the shared library * - * @throws UnsatisfiedLinkError if the library could not be loaded + * @throws RuntimeException if the library could not be loaded */ - public static SharedLibrary loadNative(String name) { if ( new File(name).isAbsolute() ) return apiCreateLibrary(name); @@ -148,6 +149,42 @@ public static SharedLibrary loadNative(String name) { } } + /** + * Loads a shared library using {@link #loadNative(String)} with the name specified by {@code name}. If {@code name} is not set, + * {@link #loadNative(String)} will be called with the names specified by {@code defaultNames}. The first successful will be returned. + * + * @param name a {@link Configuration} that specifies the library name + * @param defaultNames the default library name + * + * @return the shared library + * + * @throws RuntimeException if the library could not be loaded + */ + public static SharedLibrary loadNative(Configuration name, String... defaultNames) { + if ( defaultNames.length == 1 || name.get() != null ) + return loadNative(name.get(defaultNames[0])); + else { + if ( defaultNames.length == 0 ) + throw new RuntimeException("No default names specified."); + + SharedLibrary library = null; + try { + library = Library.loadNative(defaultNames[0]); // try first + } catch (Exception e) { + for ( int i = 1; i < defaultNames.length; i++ ) { // try alternatives + try { + library = Library.loadNative(defaultNames[i]); + break; + } catch (Exception ignored) { + } + } + if ( library == null ) + throw new RuntimeException("Failed to load library.", e); // original exception + } + return library; + } + } + private interface LibraryLoader