]> git.cworth.org Git - glaze/commitdiff
Add a new libglaze library
authorCarl Worth <cworth@cworth.org>
Mon, 12 Aug 2013 21:09:58 +0000 (14:09 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 12 Aug 2013 21:21:58 +0000 (14:21 -0700)
So far this is simply a glaze_lookup function which uses GLAZE_LIBGL
to find libGL.so, then does a dlopen on that, and then a dlsym for the
function name of interest.

.gitignore
Makefile
configure
glaze.c [new file with mode: 0644]
glaze.h [new file with mode: 0644]

index 52ed726e1e5570cfc79e80b3f76f9561a8d7edaf..4b1f0179ec7bbf40bbaf5e1450b33a9b382b026e 100644 (file)
@@ -1,3 +1,4 @@
+glaze.pc
 lib32
 lib64
 libglaze.so*
index 3ccb7fa3d94772a059b42f0510e0085aea86e7f8..069192afe1f76bb048cdb858b8c23c0becfa69d6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,30 @@
-LIBGLAZE_VERSION_MAJOR   = 0
-LIBGLAZE_VERSION_MINOR   = 0
-LIBGLAZE_VERSION_RELEASE = 0
+all:
+
+# Get settings from the output of configure by running it to generate
+# Makefile.config if it doesn't exist yet.
+
+# If Makefile.config doesn't exist, then srcdir won't be
+# set. Conditionally set it (assuming a plain srcdir build) so that
+# the rule to generate Makefile.config can actually work.
+srcdir ?= .
+
+include Makefile.config
+Makefile.config: $(srcdir)/configure
+ifeq ($(configure_options),)
+       @echo ""
+       @echo "Note: Calling ./configure with no command-line arguments. This is often fine,"
+       @echo "      but if you want to specify any arguments (such as an alternate prefix"
+       @echo "      into which to install), call ./configure explicitly and then make again."
+       @echo "      See \"./configure --help\" for more details."
+       @echo ""
+endif
+       $(srcdir)/configure $(configure_options)
 
 LIBGLAZE_LINKER_NAME = libglaze.so
-LIBGLAZE_SONAME = $(LIBGLAZE_LINKER_NAME).$(LIBGLAZE_VERSION_MAJOR)
-LIBGLAZE_LIBNAME = $(LIBGLAZE_SONAME).$(LIBGLAZE_VERSION_MINOR).$(LIBGLAZE_VERSION_RELEASE)
+LIBGLAZE_SONAME = $(LIBGLAZE_LINKER_NAME).$(MAJOR)
+LIBGLAZE_LIBNAME = $(LIBGLAZE_SONAME).$(MINOR).$(RELEASE)
 
-TARGETS = lib64/libGL.so.1 lib32/libGL.so.1
+TARGETS = $(LIBGLAZE_LIBNAME) lib64/libGL.so.1 lib32/libGL.so.1
 
 all: $(TARGETS)
 
@@ -27,26 +45,13 @@ install: all
        install -m0644 lib64/libGL.so.1 $(DESTDIR)$(LIBDIR)/glaze/lib64
        mkdir -p $(DESTDIR)$(LIBDIR)/glaze/lib32
        install -m0644 lib32/libGL.so.1 $(DESTDIR)$(LIBDIR)/glaze/lib32
+       install -m0644 $(LIBGLAZE_LIBNAME) $(DESTDIR)$(LIBDIR)
+       ln -sf $(LIBGLAZE_LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBGLAZE_SONAME)
+       ln -sf $(LIBGLAZE_LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBGLAZE_LINKER_NAME)
+       mkdir -p $(DESTDIR)$(LIBDIR)/pkgconfig
+       install glaze.pc $(DESTDIR)$(LIBDIR)/pkgconfig
+       mkdir -p $(DESTDIR)$(INCLUDEDIR)/glaze
+       install -m0644 glaze.h $(DESTDIR)$(INCLUDEDIR)/glaze
 
 clean:
        rm -f $(TARGETS)
-
-# Get settings from the output of configure by running it to generate
-# Makefile.config if it doesn't exist yet.
-
-# If Makefile.config doesn't exist, then srcdir won't be
-# set. Conditionally set it (assuming a plain srcdir build) so that
-# the rule to generate Makefile.config can actually work.
-srcdir ?= .
-
-include Makefile.config
-Makefile.config: $(srcdir)/configure
-ifeq ($(configure_options),)
-       @echo ""
-       @echo "Note: Calling ./configure with no command-line arguments. This is often fine,"
-       @echo "      but if you want to specify any arguments (such as an alternate prefix"
-       @echo "      into which to install), call ./configure explicitly and then make again."
-       @echo "      See \"./configure --help\" for more details."
-       @echo ""
-endif
-       $(srcdir)/configure $(configure_options)
index 6195562fb3f63120bca09c5c4db5ecbf441b2baf..d115790d56296b6d99f9d732b525f226cf6c04fa 100755 (executable)
--- a/configure
+++ b/configure
@@ -3,6 +3,11 @@
 PROJECT=glaze
 PROJECT_BLURB="a shiny way to wrap OpenGL"
 
+MAJOR=0
+MINOR=0
+RELEASE=0
+VERSION=${MAJOR}.${MINOR}.${RELEASE}
+
 srcdir=$(dirname "$0")
 
 # For a non-srcdir configure invocation (such as ../configure), create
@@ -240,22 +245,43 @@ CFLAGS = ${CFLAGS}
 LDFLAGS = ${LDFLAGS}
 
 # Flags to enable warnings when using the C compiler
-WARN_CFLAGS=${WARN_CFLAGS}
+WARN_CFLAGS = ${WARN_CFLAGS}
 
 # The prefix to which ${PROJECT} should be installed
 PREFIX = ${PREFIX}
 
 # The directory to which executables should be installed
-BINDIR = ${BINDIR:=\$(PREFIX)/bin}
+BINDIR = ${BINDIR:-\$(PREFIX)/bin}
 
 # The directory to which libraries should be installed
-LIBDIR = ${LIBDIR:=\$(PREFIX)/lib}
+LIBDIR = ${LIBDIR:-\$(PREFIX)/lib}
 
 # The directory to which headers should be installed
-INCLUDEDIR = ${INCLUDEDIR:=\$(PREFIX)/include}
+INCLUDEDIR = ${INCLUDEDIR:-\$(PREFIX)/include}
 
 # Whether compiler can create 32 or 64-bit binaries
 COMPILER_SUPPORTS_32 = ${have_m32}
 COMPILER_SUPPORTS_64 = ${have_m64}
 
+# Version information for glaze library
+MAJOR   = ${MAJOR}
+MINOR   = ${MINOR}
+RELEASE = ${RELEASE}
+VERSION = ${VERSION}
+
+EOF
+
+# construct the glaze.pc file
+cat > glaze.pc <<EOF
+prefix=${PREFIX}
+exec_prefix=\${prefix}
+libdir=${LIBDIR:-\${exec_prefix\}/lib}
+includedir=${INCLUDEDIR:-\${prefix\}/include}
+
+Name: ${PROJECT}
+Description: ${PROJECT_BLURB}
+Version: ${VERSION}
+
+Libs: -L\${libdir} -lglaze
+Cflags: -I\${includedir}/glaze
 EOF
diff --git a/glaze.c b/glaze.c
new file mode 100644 (file)
index 0000000..7207025
--- /dev/null
+++ b/glaze.c
@@ -0,0 +1,63 @@
+/* Copyright © 2013, Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#define _GNU_SOURCE
+#include <dlfcn.h>
+
+#include "glaze.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void *
+glaze_lookup (char *function)
+{
+       static void *libgl_handle = NULL;
+       void *ret;
+
+       if (libgl_handle == NULL) {
+               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);
+               }
+       }
+
+       ret = dlsym (libgl_handle, function);
+
+       if (ret == NULL) {
+               fprintf (stderr, "Error: glaze_lookup failed to dlsym %s\n",
+                        function);
+               exit (1);
+       }
+
+       return ret;
+}
diff --git a/glaze.h b/glaze.h
new file mode 100644 (file)
index 0000000..9a9115a
--- /dev/null
+++ b/glaze.h
@@ -0,0 +1,58 @@
+/* Copyright © 2013, Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef GLAZE_H
+#define GLAZE_H
+
+/* Perform dynamic lookup of 'function' within the library specified
+ * by the GLAZE_LIBGL environment variable.
+ */
+void *
+glaze_lookup (char *function);
+
+/* A helper macro for glaze-using wrappers to defer an operation to
+ * the "real" underlying OpenGL function.
+ *
+ * The underlying function to be called will be obtained by calling
+ * glaze_lookup. This lookup will be cached in a local variable
+ * (defined by this macro) named real_<function>.
+ *
+ * NOTE: This macro can only be used a single time within any
+ * function, (due to the locally-defined variable). If this is
+ * inadequate, callers can instead call glaze_lookup manually.
+ */
+#define GLAZE_DEFER(function,...) do {                         \
+       static typeof(&function) real_ ## function;             \
+       if (! real_ ## function)                                \
+               real_ ## function = glaze_lookup (#function);   \
+       real_ ## function(__VA_ARGS__);                         \
+} while (0);
+
+/* As GLAZE_DEFER, but also set 'ret' to the value returned by the
+ * underlying "real" function. */
+#define GLAZE_DEFER_WITH_RETURN(ret, function,...) do {        \
+       static typeof(&function) real_ ## function;             \
+       if (! real_ ## function)                                \
+               real_ ## function = glaze_lookup (#function);   \
+       (ret) = real_ ## function(__VA_ARGS__);                 \
+} while (0);
+
+#endif /* GLAZE_H */