Commit db5918bf authored by Tim Janik's avatar Tim Janik

initial import of gmodule.

parent df6a9afd
## Process this file with automake to produce Makefile.in
SUBDIRS = gmodule
configincludedir = $(pkglibdir)/include
bin_SCRIPTS=glib-config
BUILT_SOURCES=glib-config
glib-config: glib-config.in
EXTRA_DIST = \
glib.m4 \
......
......@@ -222,4 +222,87 @@ if test $gtk_ok = no; then
fi
AC_MSG_RESULT($gtk_ok)
AC_OUTPUT([Makefile glib-config],[chmod +x glib-config])
dnl ***********************
dnl *** g_module checks ***
dnl ***********************
G_MODULE_LIBS=
G_MODULE_LDFLAGS=
G_MODULE_IMPL=
G_MODULE_NEED_USCORE=0
G_MODULE_HAVE_DLERROR=0
dnl *** dlopen() in system libraries
if test -z "$G_MODULE_IMPL"; then
AC_CHECK_FUNC(dlopen,
G_MODULE_IMPL=G_MODULE_IMPL_DL
,)
fi
dnl *** dlopen() in libdl
if test -z "$G_MODULE_IMPL"; then
AC_CHECK_LIB(dl, dlopen,
G_MODULE_LIBS=-ldl
G_MODULE_IMPL=G_MODULE_IMPL_DL
,)
fi
dnl *** shl_load() in libdld (HP-UX)
if test -z "$G_MODULE_IMPL"; then
G_MODULE_LDFLAGS='-Wl,-E'
AC_CHECK_LIB(dld, shl_load,
G_MODULE_LIBS=-ldld
G_MODULE_IMPL=G_MODULE_IMPL_DLD
,)
fi
dnl *** additional checks for G_MODULE_IMPL_DL
if test "$G_MODULE_IMPL" = "G_MODULE_IMPL_DL"; then
if test -d /usr/include/linux; then
G_MODULE_LDFLAGS='-rdynamic'
fi
LIBS_orig="$LIBS"
LDFLAGS_orig="$LDFLAGS"
LIBS="$LIBS $G_MODULE_LIBS"
LDFLAGS="$LDFLAGS $G_MODULE_LDFLAGS"
dnl *** check whether we need preceeding underscores
AC_MSG_CHECKING(for preceeding underscore in symbols)
AC_CACHE_VAL(glib_cv_uscore,[
AC_TRY_RUN([
#include <dlfcn.h>
int glib_underscore_test (void) { return 42; }
int main() {
void *f1 = (void*)0, *f2 = (void*)0, *handle;
handle = dlopen ((void*)0, 0);
if (handle) {
f1 = dlsym (handle, "glib_underscore_test");
f2 = dlsym (handle, "_glib_underscore_test");
} return !(f2 && !f1);
}],
glib_cv_uscore=yes
G_MODULE_NEED_USCORE=1
,
glib_cv_uscore=no
G_MODULE_NEED_USCORE=0
,)
])
AC_MSG_RESULT($glib_cv_uscore)
LDFLAGS="$LDFLAGS_orig"
dnl *** check for having dlerror()
AC_CHECK_FUNC(dlerror,
G_MODULE_HAVE_DLERROR=1,
G_MODULE_HAVE_DLERROR=0)
LIBS="$LIBS_orig"
fi
dnl *** done, have e got an implementation?
if test -z "$G_MODULE_IMPL"; then
G_MODULE_IMPL=0
fi
AC_SUBST(G_MODULE_IMPL)
AC_SUBST(G_MODULE_LIBS)
AC_SUBST(G_MODULE_LDFLAGS)
AC_SUBST(G_MODULE_HAVE_DLERROR)
AC_SUBST(G_MODULE_NEED_USCORE)
AC_OUTPUT([
Makefile
glib-config
gmodule/gmoduleconf.h
gmodule/Makefile
],[chmod +x glib-config])
......@@ -4,14 +4,29 @@ prefix=@prefix@
exec_prefix=@exec_prefix@
exec_prefix_set=no
usage="\
Usage: glib-config [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--libs] [--cflags]"
usage()
{
cat <<EOF
Usage: glib-config [OPTIONS] [LIBRARIES]
Options:
[--prefix[=DIR]]
[--exec-prefix[=DIR]]
[--version]
[--libs]
[--cflags]
Libraries:
glib
gmodule
EOF
exit $1
}
if test $# -eq 0; then
echo "${usage}" 1>&2
exit 1
usage 1 1>&2
fi
lib_glib=yes
while test $# -gt 0; do
case "$1" in
-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
......@@ -26,32 +41,57 @@ while test $# -gt 0; do
fi
;;
--prefix)
echo $prefix
echo_prefix=yes
;;
--exec-prefix=*)
exec_prefix=$optarg
exec_prefix_set=yes
;;
--exec-prefix)
echo $exec_prefix
echo_exec_prefix=yes
;;
--version)
echo @GLIB_VERSION@
exit 0
;;
--cflags)
if test @includedir@ != /usr/include ; then
includes=-I@includedir@
if test "@includedir@" != /usr/include ; then
includes="-I@includedir@"
fi
echo -I@libdir@/glib/include $includes
echo_cflags=yes
;;
--libs)
echo -L@libdir@ -lglib-@LT_RELEASE@
echo_libs=yes
;;
glib)
lib_glib=yes
;;
gmodule)
lib_gmodule=yes
;;
*)
echo "${usage}" 1>&2
exit 1
usage 1 1>&2
;;
esac
shift
done
if test "$echo_prefix" = "yes"; then
echo $prefix
fi
if test "$echo_exec_prefix" = "yes"; then
echo $exec_prefix
fi
if test "$echo_cflags" = "yes"; then
echo -I@libdir@/glib/include $includes
fi
if test "$echo_libs" = "yes"; then
libs=""
if test "$lib_glib" = "yes"; then
libs="$libs -lglib-@LT_RELEASE@"
fi
if test "$lib_gmodule" = "yes"; then
libs="@G_MODULE_LDFLAGS@ -lgmodule-@LT_RELEASE@ $libs @G_MODULE_LIBS@"
fi
echo "-L@libdir@ $libs"
fi
## Process this file with automake to produce Makefile.in
SUBDIRS = gmodule
configincludedir = $(pkglibdir)/include
bin_SCRIPTS=glib-config
BUILT_SOURCES=glib-config
glib-config: glib-config.in
EXTRA_DIST = \
glib.m4 \
......
Tim Janik <timj@gtk.org>
This diff is collapsed.
Sun Aug 9 10:31:05 1998 Tim Janik <timj@gtk.org>
* ChangeLog start for gmodule.
## Process this file with automake to produce Makefile.in
INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/gmodule
EXTRA_DIST = \
gmoduleconf.h.in
BUILT_SOURCES = gmoduleconf.h
gmoduleconf.h: gmoduleconf.h.in
include_HEADERS = \
gmodule.h
lib_LTLIBRARIES = libgmodule-1.1.la libgplugin_a.la libgplugin_b.la
libgmodule_1_1_la_SOURCES = gmodule.c
libgmodule_1_1_la_LDFLAGS = @G_MODULE_LDFLAGS@ -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE)
libgmodule_1_1_la_LIBADD = @G_MODULE_LIBS@ -lglib-@LT_RELEASE@
libgplugin_a_la_SOURCES = libgplugin_a.c
libgplugin_a_la_LDFLAGS = @G_MODULE_LDFLAGS@
libgplugin_a_la_LIBADD = @G_MODULE_LIBS@ -lglib-@LT_RELEASE@
libgplugin_b_la_SOURCES = libgplugin_b.c
libgplugin_b_la_LDFLAGS = @G_MODULE_LDFLAGS@
libgplugin_b_la_LIBADD = @G_MODULE_LIBS@ -lglib-@LT_RELEASE@
noinst_PROGRAMS = testgmodule
testgmodule_LDFLAGS += @G_MODULE_LDFLAGS@
testgmodule_LDADD = libgmodule-@LT_RELEASE@.la -lglib-@LT_RELEASE@ @G_MODULE_LIBS@
.PHONY: files release
files:
@files=`ls $(DISTFILES) 2> /dev/null `; for p in $$files; do \
echo $$p; \
done
install-libLTLIBRARIES: libgmodule-1.1.la
@$(NORMAL_INSTALL)
$(mkinstalldirs) $(DESTDIR)$(libdir)
@list='libgmodule-1.1.la'; for p in $$list; do \
if test -f $$p; then \
echo "$(LIBTOOL) --mode=install $(INSTALL_DATA) $$p $(DESTDIR)$(libdir)/$$p"; \
$(LIBTOOL) --mode=install $(INSTALL_DATA) $$p $(DESTDIR)$(libdir)/$$p; \
else :; fi; \
done
release:
$(MAKE) dist distdir=$(PACKAGE)`date +"%y%m%d"`
/* GMODULE - GLIB wrapper code for dynamic module loading
* Copyright (C) 1998 Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <dlfcn.h>
/* Perl includes <nlist.h> and <link.h> instead of <dlfcn.h> on some systmes? */
/* dlerror() is not implemented on all systems
*/
#ifndef G_MODULE_HAVE_DLERROR
# ifdef __NetBSD__
# define dlerror() g_strerror (errno)
# else /* !__NetBSD__ */
/* could we rely on errno's state here? */
# define dlerror() "unknown dl-error"
# endif /* !__NetBSD__ */
#endif /* G_MODULE_HAVE_DLERROR */
/* some flags are missing on some systems, so we provide
* harmless defaults.
* The Perl sources say, RTLD_LAZY needs to be defined as (1),
* at least for Solaris 1.
*/
#ifndef RTLD_GLOBAL
#define RTLD_GLOBAL 0
#endif /* RTLD_GLOBAL */
#ifndef RTLD_LAZY
#define RTLD_LAZY 1
#endif /* RTLD_LAZY */
#ifndef RTLD_NOW
#define RTLD_NOW 0
#endif /* RTLD_NOW */
/* --- functions --- */
static gpointer
_g_module_open (const gchar *file_name,
gboolean bind_lazy)
{
gpointer handle;
handle = dlopen (file_name, RTLD_GLOBAL | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
if (!handle)
g_module_set_error (dlerror ());
return handle;
}
static gpointer
_g_module_self (void)
{
gpointer handle;
/* to query symbols from the program itself, special link options
* are required on some systems.
*/
handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
if (!handle)
g_module_set_error (dlerror ());
return handle;
}
static void
_g_module_close (gpointer *handle_p,
gboolean is_unref)
{
/* are there any systems out there that have dlopen()/dlclose()
* without a reference count implementation?
*/
is_unref |= 1;
if (is_unref)
{
if (dlclose (*handle_p) != 0)
g_module_set_error (dlerror ());
}
}
static gpointer
_g_module_symbol (gpointer *handle_p,
const gchar *symbol_name)
{
gpointer p;
p = dlsym (*handle_p, symbol_name);
if (!p)
g_module_set_error (dlerror ());
return p;
}
/* GMODULE - GLIB wrapper code for dynamic module loading
* Copyright (C) 1998 Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <dl.h>
/* some flags are missing on some systems, so we provide
* harmless defaults.
*/
#ifndef DYNAMIC_PATH
#define DYNAMIC_PATH 0
#endif /* DYNAMIC_PATH */
/* should we have BIND_TOGETHER here as well? */
#define CONST_BIND_FLAGS (BIND_NONFATAL | BIND_VERBOSE)
/* --- functions --- */
static gpointer
_g_module_open (const gchar *file_name,
gboolean bind_lazy)
{
gpointer handle;
handle = shl_load (file_name, CONST_BIND_FLAGS | (bind_lazy ? BIND_DEFERRED : BIND_IMMEDIATE), 0);
if (!handle)
g_module_set_error (g_strerror (errno));
return handle;
}
static gpointer
_g_module_self (void)
{
gpointer handle;
handle = PROG_HANDLE;
if (!handle)
g_module_set_error (g_strerror (errno));
return handle;
}
static void
_g_module_close (gpointer *handle_p,
gboolean is_unref)
{
if (!is_unref)
{
if (shl_unload (*handle_p) != 0)
g_module_set_error (g_strerror (errno));
}
}
static gpointer
_g_module_symbol (gpointer *handle_p,
const gchar *symbol_name)
{
gpointer p = NULL;
/* should we restrict lookups to TYPE_PROCEDURE?
*/
if (shl_findsym (handle_p, symbol_name, TYPE_UNDEFINED, &p) != 0 || p == NULL)
g_module_set_error (g_strerror (errno));
return p;
}
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "gmodule.h"
#include "gmoduleconf.h"
#include <errno.h>
#include <string.h>
/* We maintain a list of modules, so we can reference count them.
* That's needed because some platforms don't support refernce counts on
* modules (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
* Also, the module for the program itself is kept seperatedly for
* faster access.
*/
/* --- structures --- */
struct _GModule
{
gchar *file_name;
gpointer handle;
guint ref_count;
GModule *next;
};
/* --- prototypes --- */
static gpointer _g_module_open (const gchar *file_name,
gboolean bind_lazy);
static void _g_module_close (gpointer *handle_p,
gboolean is_unref);
static gpointer _g_module_self (void);
static gpointer _g_module_symbol (gpointer *handle_p,
const gchar *symbol_name);
static inline void g_module_set_error (const gchar *error);
static inline GModule* g_module_find_by_handle (gpointer handle);
static inline GModule* g_module_find_by_name (const gchar *name);
/* --- variables --- */
static GModule *modules = NULL;
static GModule *main_module = NULL;
static gchar *module_error = NULL;
/* --- inline functions --- */
static inline GModule*
g_module_find_by_handle (gpointer handle)
{
GModule *module;
for (module = modules; module; module = module->next)
if (handle == module->handle)
return module;
return NULL;
}
static inline GModule*
g_module_find_by_name (const gchar *name)
{
GModule *module;
for (module = modules; module; module = module->next)
if (strcmp (name, module->file_name) == 0)
return module;
return NULL;
}
static inline void
g_module_set_error (const gchar *error)
{
if (module_error)
g_free (module_error);
if (error)
module_error = g_strdup (error);
else
module_error = NULL;
errno = 0;
}
/* --- include platform specifc code --- */
#define CHECK_ERROR(rv) { g_module_set_error (NULL); }
#if (G_MODULE_IMPL == G_MODULE_IMPL_DL)
#include "gmodule-dl.c"
#elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
#include "gmodule-dld.c"
#else
#undef CHECK_ERROR
#define CHECK_ERROR(rv) { g_module_set_error ("unsupported"); return rv; }
#endif /* no implementation */
/* --- functions --- */
gboolean
g_module_supported (void)
{
CHECK_ERROR (FALSE);
return TRUE;
}
GModule*
g_module_open (const gchar *file_name,
GModuleFlags flags)
{
GModule *module;
gpointer handle;
CHECK_ERROR (NULL);
if (!file_name)
{
if (!main_module)
{
handle = _g_module_self ();
if (handle)
{
main_module = g_new (GModule, 1);
main_module->file_name = NULL;
main_module->ref_count = 1;
main_module->next = NULL;
main_module->handle = handle;
}
}
return main_module;
}
/* we first search the module list by name */
module = g_module_find_by_name (file_name);
if (module)
{
module->ref_count++;
return module;
}
/* open the module */
handle = _g_module_open (file_name, (flags & G_MODULE_BIND_LAZY) != 0);
if (handle)
{
GModule *module;
/* search the module list by handle, since file names are not unique */
module = g_module_find_by_handle (handle);
if (module)
{
_g_module_close (&module->handle, TRUE);
module->ref_count++;
g_module_set_error (NULL);
return module;
}
module = g_new (GModule, 1);
module->file_name = g_strdup (file_name);
module->handle = handle;
module->ref_count = 1;
module->next = modules;
modules = module;
return module;
}
return NULL;
}
gboolean
g_module_close (GModule *module)
{
CHECK_ERROR (FALSE);
g_return_val_if_fail (module != NULL, FALSE);
g_return_val_if_fail (module->ref_count > 0, FALSE);
if (module != main_module)
module->ref_count--;
if (!module->ref_count)
{
GModule *last;
GModule *node;
_g_module_close (&module->handle, FALSE);
g_free (module->file_name);
last = NULL;
node = modules;
while (node)
{
if (node == module)
{
if (last)
last->next = node->next;
else
modules = node->next;
break;
}
last = node;
node = last->next;
}
g_free (module);
}
return module_error == NULL;
}
gchar*
g_module_error (void)
{
return module_error;
}
gboolean
g_module_symbol (GModule *module,
const gchar *symbol_name,
gconstpointer *symbol)
{
if (symbol)
*symbol = NULL;
CHECK_ERROR (FALSE);
g_return_val_if_fail (module != NULL, FALSE);
g_return_val_if_fail (symbol_name != NULL, FALSE);
g_return_val_if_fail (symbol != NULL, FALSE);
#ifdef G_MODULE_NEED_USCORE
symbol_name = g_strconcat ("_", symbol_name, NULL);
*symbol = _g_module_symbol (&module->handle, symbol_name);
g_free (symbol_name);
#else /* !G_MODULE_NEED_USCORE */
*symbol = _g_module_symbol (&module->handle, symbol_name);
#endif /* !G_MODULE_NEED_USCORE */
if (module_error)
{
*symbol = NULL;
return FALSE;
}
return TRUE;
}
gchar*
g_module_name (GModule *module)
{
g_return_val_if_fail (module != NULL, NULL);
if (module == main_module)
return "main";
return module->file_name;
}
/* GMODULE - GLIB wrapper code for dynamic module loading
* Copyright (C) 1998 Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*