]> git.cworth.org Git - glaze/blobdiff - glaze.c
Add a simple helper program to find libGL.so.1
[glaze] / glaze.c
diff --git a/glaze.c b/glaze.c
index f52463d82d49cc818467f94eda865c2349b18521..72070256fb4365d6874ba58a3d28f94495c5f9e9 100644 (file)
--- a/glaze.c
+++ b/glaze.c
 #define _GNU_SOURCE
 #include <dlfcn.h>
 
+#include "glaze.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 
-void *libgl_handle;
-
-static void
-open_libgl_handle (void)
+void *
+glaze_lookup (char *function)
 {
-       const char *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);
-       }
+       static void *libgl_handle = NULL;
+       void *ret;
 
-       libgl_handle = dlopen (path, RTLD_LAZY | RTLD_GLOBAL);
        if (libgl_handle == NULL) {
-               fprintf (stderr, "Error: Failed to dlopen %s\n", path);
-               exit (1);
+               const char *path;
+
+               path = getenv ("GLAZE_LIBGL");
+               if (path == NULL) {
+                       fprintf (stderr, "GLAZE_LIBGL unset. "
+                                "Please set to path of libGL.so under glaze.\n"
+                               );
+                       exit (1);
+               }
+
+               libgl_handle = dlopen (path, RTLD_LAZY | RTLD_GLOBAL);
+               if (libgl_handle == NULL) {
+                       fprintf (stderr, "Error: Failed to dlopen %s\n", path);
+                       exit (1);
+               }
        }
-}
 
-static void
-glaze_init (void) __attribute__((constructor));
+       ret = dlsym (libgl_handle, function);
 
-static void
-glaze_init (void)
-{
-       open_libgl_handle ();
-}
+       if (ret == NULL) {
+               fprintf (stderr, "Error: glaze_lookup failed to dlsym %s\n",
+                        function);
+               exit (1);
+       }
 
-static void *
-resolve (const char *name)
-{
-       return dlsym (libgl_handle, name);
+       return ret;
 }
-
-#define GLAZE_API(name)                                                        \
-void * name() __attribute__((ifunc(#name "_resolver")));               \
-static void *                                                          \
-name ## _resolver (void) { return resolve (#name); }
-
-#include "specs/gl.def"