>From 59d529388d062ddad581bcd7f0c56690a84bf7df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AE=8B=E6=96=87=E6=AD=A6?=
Date: Fri, 16 Sep 2016 10:30:36 +0800
Subject: [PATCH 1/3] Use libltdl to dlopen backends.
* include/guile-dbi/guile-dbi.h (gdbi_db_handle_t): Use 'lt_dlhandle'.
* src/guile-dbi.c: Replace libdl calls with their libltdl equivalences.
* Makefile.am (SUBDIRS): Add libltdl.
* configure.ac: Use LTDL_INIT. Add LTDLINCL and LIBLTDL to flags.
* src/Makefile.am (libguile_dbi_la_LIBDD): Remove '-ldl'.
---
guile-dbi/Makefile.am | 2 +-
guile-dbi/configure.ac | 8 +++++---
guile-dbi/include/guile-dbi/guile-dbi.h | 3 ++-
guile-dbi/src/Makefile.am | 2 +-
guile-dbi/src/guile-dbi.c | 25 ++++++++++---------------
5 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/guile-dbi/Makefile.am b/guile-dbi/Makefile.am
index e4cae61..011740b 100644
--- a/guile-dbi/Makefile.am
+++ b/guile-dbi/Makefile.am
@@ -24,7 +24,7 @@
ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = gnu
-SUBDIRS = src include doc
+SUBDIRS = libltdl src include doc
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = guile-dbi.pc
diff --git a/guile-dbi/configure.ac b/guile-dbi/configure.ac
index cc45703..d902cb0 100644
--- a/guile-dbi/configure.ac
+++ b/guile-dbi/configure.ac
@@ -55,14 +55,16 @@ AC_CHECK_FUNCS([strerror])
# ltmain needs these:
AC_CONFIG_MACRO_DIR([m4])
-LT_INIT
+LT_INIT([dlopen])
+LT_CONFIG_LTDL_DIR([libltdl])
+LTDL_INIT
# AC_PROG_INSTALL
# AC_PROG_MAKE_SET
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])], [AC_SUBST([AM_DEFAULT_VERBOSITY],1)])
-CFLAGS=`$GUILECONFIG compile`
-LIBS=`$GUILECONFIG link`
+CFLAGS="$LTDLINCL `$GUILECONFIG compile`"
+LIBS="$LIBLTDL `$GUILECONFIG link`"
GUILE_SITE=`$GUILECONFIG info sitedir`
AC_ARG_WITH([guile-site-dir],
diff --git a/guile-dbi/include/guile-dbi/guile-dbi.h b/guile-dbi/include/guile-dbi/guile-dbi.h
index b75d3dc..00449ee 100644
--- a/guile-dbi/include/guile-dbi/guile-dbi.h
+++ b/guile-dbi/include/guile-dbi/guile-dbi.h
@@ -24,6 +24,7 @@
#define __GUILE_DBI_H__
#include
+#include
/* guile smob struct */
typedef struct
@@ -32,7 +33,7 @@ typedef struct
SCM constr;
SCM status; /* pair: car = error numeric code, cdr = status message */
SCM closed; /* boolean, TRUE if closed otherwise FALSE */
- void* handle;
+ lt_dlhandle handle;
void* db_info;
int in_free; /* boolean, used to avoid alloc during garbage collection */
const char * bcknd_str;
diff --git a/guile-dbi/src/Makefile.am b/guile-dbi/src/Makefile.am
index a3001db..0795899 100644
--- a/guile-dbi/src/Makefile.am
+++ b/guile-dbi/src/Makefile.am
@@ -35,7 +35,7 @@ SUFFIXES = .x
lib_LTLIBRARIES = libguile-dbi.la
libguile_dbi_la_SOURCES = guile-dbi.c guile-dbi.x
-libguile_dbi_la_LIBADD = -ldl $(LIBS)
+libguile_dbi_la_LIBADD = $(LIBS)
libguile_dbi_la_LDFLAGS = -export-dynamic -version-info @DBI_INTERFACE@
libguile_dbi_la_DEPENDENCIES = $(LTLIBOBJS)
diff --git a/guile-dbi/src/guile-dbi.c b/guile-dbi/src/guile-dbi.c
index 5d797d9..d4ae040 100644
--- a/guile-dbi/src/guile-dbi.c
+++ b/guile-dbi/src/guile-dbi.c
@@ -24,7 +24,6 @@
#include
#include
#include
-#include
#include
@@ -59,9 +58,8 @@ SCM_DEFINE (make_g_db_handle, "dbi-open", 2, 0, 0,
g_db_handle->bcknd_str = scm_to_locale_string (bcknd);
g_db_handle->bcknd_strlen = strlen(g_db_handle->bcknd_str);
- /* The +20 allos for .so or .dylib on MacOS */
sodbd_len = sizeof(char) * (strlen("libguile-dbd-") +
- g_db_handle->bcknd_strlen + 20);
+ g_db_handle->bcknd_strlen + 1);
sodbd = (char*) malloc (sodbd_len);
if (sodbd == NULL)
{
@@ -70,18 +68,14 @@ SCM_DEFINE (make_g_db_handle, "dbi-open", 2, 0, 0,
SCM_RETURN_NEWSMOB (g_db_handle_tag, g_db_handle);
}
-#ifdef __APPLE__
- snprintf(sodbd, sodbd_len, "libguile-dbd-%s.dylib", g_db_handle->bcknd_str);
-#else
- snprintf(sodbd, sodbd_len, "libguile-dbd-%s.so", g_db_handle->bcknd_str);
-#endif
+ snprintf(sodbd, sodbd_len, "libguile-dbd-%s", g_db_handle->bcknd_str);
- g_db_handle->handle = dlopen(sodbd, RTLD_NOW);
+ g_db_handle->handle = lt_dlopenext(sodbd);
if (g_db_handle->handle == NULL)
{
free(sodbd);
g_db_handle->status = scm_cons(scm_from_int(1),
- scm_from_locale_string(dlerror()));
+ scm_from_locale_string(lt_dlerror()));
SCM_RETURN_NEWSMOB (g_db_handle_tag, g_db_handle);
}
@@ -174,7 +168,7 @@ SCM_DEFINE (close_g_db_handle, "dbi-close", 1, 0, 0,
(*dbd_close)(g_db_handle);
if (g_db_handle->handle)
{
- dlclose(g_db_handle->handle);
+ lt_dlclose(g_db_handle->handle);
g_db_handle->handle = NULL;
}
scm_remember_upto_here_1(db_handle);
@@ -311,6 +305,7 @@ init_dbi(void)
if (is_inited) return;
is_inited = 1;
init_db_handle_type();
+ lt_dlinit();
#ifndef SCM_MAGIC_SNARFER
#include "guile-dbi.x"
@@ -324,7 +319,7 @@ void
__gdbi_dbd_wrap(gdbi_db_handle_t* dbh, const char* function_name,
void** function_pointer)
{
- char *ret = NULL;
+ const char *ret = NULL;
char *func = NULL;
size_t func_len;
@@ -340,9 +335,9 @@ __gdbi_dbd_wrap(gdbi_db_handle_t* dbh, const char* function_name,
/* I assume this is correct for all OS'es */
snprintf(func, func_len, "__%s_%s", dbh->bcknd_str, function_name);
- dlerror(); /* Clear any existing error -- Solaris needs this */
- *function_pointer = dlsym(dbh->handle, func);
- if ((ret = dlerror()) != NULL)
+ lt_dlerror(); /* Clear any existing error -- Solaris needs this */
+ *function_pointer = lt_dlsym(dbh->handle, func);
+ if ((ret = lt_dlerror()) != NULL)
{
free(func);
if (dbh->in_free) return; /* do not SCM anything while in GC */
--
2.10.0