]> git.cworth.org Git - glaze/blobdiff - glaze-gl.c
Remove dependency of libglaze on libelf
[glaze] / glaze-gl.c
index 0c2eada8d458ea1ca672a1e9a90df365032909ce..453d4606f2be1320b009630bf1e041960b190afc 100644 (file)
@@ -24,6 +24,9 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+
+#include <fcntl.h>
 
 void *libgl_handle;
 void *wrapper_handle;
@@ -31,20 +34,36 @@ void *wrapper_handle;
 static void
 open_libgl_handle (void)
 {
-       const char *path;
+       const char *libgl_path;
 
        if (libgl_handle)
                return;
 
-       path = getenv ("GLAZE_LIBGL");
-       if (path == NULL) {
-               fprintf (stderr, "GLAZE_LIBGL unset. Please set to path of real libGL.so under glaze.\n");
-               exit (1);
+       libgl_path = getenv ("GLAZE_LIBGL");
+
+       if (libgl_path == NULL) {
+
+#if GLAZE_BITS == 32
+               libgl_path = getenv ("GLAZE_LIBGL_32_AUTO");
+#elif GLAZE_BITS == 64
+               libgl_path = getenv ("GLAZE_LIBGL_64_AUTO");
+#endif
+
+               if (libgl_path == NULL) {
+                       fprintf (stderr,
+                                "Error: Failed to detect OpenGL library.\n"
+                                "Please set GLAZE_LIBGL to path of real libGL.so\n");
+                       exit (1);
+               }
+
+               setenv ("GLAZE_LIBGL", libgl_path, 1);
        }
 
-       libgl_handle = dlopen (path, RTLD_LAZY | RTLD_GLOBAL);
+       dlerror();
+       libgl_handle = dlopen (libgl_path, RTLD_LAZY | RTLD_GLOBAL);
        if (libgl_handle == NULL) {
-               fprintf (stderr, "Error: Failed to dlopen %s\n", path);
+               fprintf (stderr, "glaze_init: Error: Failed to dlopen %s: %s\n",
+                        libgl_path, dlerror());
                exit (1);
        }
 }
@@ -63,9 +82,11 @@ open_wrapper_handle (void)
                exit (1);
        }
 
+       dlerror ();
        wrapper_handle = dlopen (path, RTLD_LAZY);
        if (wrapper_handle == NULL) {
-               fprintf (stderr, "Error: Failed to dlopen %s\n", path);
+               const char *error = dlerror();
+               fprintf (stderr, "Error: Failed to dlopen %s: %s\n", path, error);
                exit (1);
        }
 }