From cec0694a7f219bd3590e6a04beba34ca45539540 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Mon, 12 Aug 2013 14:09:58 -0700 Subject: [PATCH] Add a new libglaze library 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 | 1 + Makefile | 57 ++++++++++++++++++++++++++---------------------- configure | 34 +++++++++++++++++++++++++---- glaze.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ glaze.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 30 deletions(-) create mode 100644 glaze.c create mode 100644 glaze.h diff --git a/.gitignore b/.gitignore index 52ed726..4b1f017 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +glaze.pc lib32 lib64 libglaze.so* diff --git a/Makefile b/Makefile index 3ccb7fa..069192a 100644 --- 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) diff --git a/configure b/configure index 6195562..d115790 100755 --- 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 < + +#include "glaze.h" + +#include +#include + +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 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_. + * + * 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 */ -- 2.43.0