diff options
author | Reuben Thomas <rrt@sc3d.org> | 2017-03-07 13:02:37 +0000 |
---|---|---|
committer | Reuben Thomas <rrt@sc3d.org> | 2017-04-04 22:33:33 +0100 |
commit | a8e771a58a996d210dc9d12a3896d04b27465cc1 (patch) | |
tree | 8775efa8a44f8472050706571e82d588f4a183ad /src | |
parent | 75d31e7c59cb10c269b30f9c9ff500a39e9d6fd5 (diff) | |
download | enchant-a8e771a58a996d210dc9d12a3896d04b27465cc1.tar.gz |
Make library relocatable using gnulib’s relocatable-lib-lgpl module
This adds a new API enchant_set_prefix_dir, which necessitated changing the
C++ API so that rather than constructing a broker instance statically, which
would pre-empt any setting of the prefix dir, the Broker object must now be
explicitly created. This also gives an easier way to have multiple Brokers,
if desired.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/enchant++.h | 33 | ||||
-rw-r--r-- | src/enchant.h | 3 | ||||
-rw-r--r-- | src/lib.c | 89 |
4 files changed, 59 insertions, 71 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index f21fb8a..4e1f4fb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,10 @@ AM_CPPFLAGS = -I$(top_srcdir) $(ISYSTEM)$(top_builddir)/lib $(ISYSTEM)$(top_srcdir)/lib $(ENCHANT_CFLAGS) $(WARN_CFLAGS) +DEFS = -DENABLE_COSTLY_RELOCATABLE -DIN_LIBRARY -DINSTALLDIR=\"$(libdir)\" lib_LTLIBRARIES = libenchant.la -libenchant_la_CPPFLAGS = $(AM_CPPFLAGS) -DENCHANT_SYSTEM_ORDERING=\"$(sysconfdir)/enchant\" -DPKGLIBDIR=\"$(pkglibdir)\" -D_ENCHANT_BUILD=1 -DENCHANT_VERSION_STRING=\"@ENCHANT_MAJOR_VERSION@.@ENCHANT_MINOR_VERSION@.@ENCHANT_MICRO_VERSION@\" -libenchant_la_LIBADD = $(top_builddir)/lib/libgnu.la $(ENCHANT_LIBS) +libenchant_la_CPPFLAGS = $(AM_CPPFLAGS) -D_ENCHANT_BUILD=1 -DENCHANT_VERSION_STRING=\"@ENCHANT_MAJOR_VERSION@.@ENCHANT_MINOR_VERSION@.@ENCHANT_MICRO_VERSION@\" +libenchant_la_LIBADD = $(top_builddir)/lib/libgnu.la $(ENCHANT_LIBS) $(LTLIBOBJS) libenchant_la_LDFLAGS = -no-undefined -export-symbols-regex '^enchant_.*' if OS_WIN32 diff --git a/src/enchant++.h b/src/enchant++.h index 741c9db..089a90c 100644 --- a/src/enchant++.h +++ b/src/enchant++.h @@ -37,6 +37,10 @@ namespace enchant { + void set_prefix_dir (const std::string prefix) { + enchant_set_prefix_dir (prefix.c_str ()); + } + class Broker; class Exception : public std::exception @@ -205,10 +209,15 @@ namespace enchant public: - static Broker * instance () { - return &m_instance; + Broker () + : m_broker (enchant_broker_init ()) + { + } + + ~Broker () { + enchant_broker_free (m_broker); } - + Dict * request_dict (const std::string & lang) { EnchantDict * dict = enchant_broker_request_dict (m_broker, lang.c_str()); @@ -251,30 +260,12 @@ namespace enchant private: - // space reserved for API/ABI expansion - void * _private[5]; - - Broker () - : m_broker (enchant_broker_init ()) - { - } - - ~Broker () { - enchant_broker_free (m_broker); - } - // not implemented Broker (const Broker & rhs); Broker& operator=(const Broker & rhs); - static Broker m_instance; - EnchantBroker * m_broker; }; // class enchant::Broker - - // define the broker instance - Broker Broker::m_instance; - } // enchant namespace #endif /* ENCHANT_PLUS_PLUS_H */ diff --git a/src/enchant.h b/src/enchant.h index fa41799..ef517a7 100644 --- a/src/enchant.h +++ b/src/enchant.h @@ -154,6 +154,9 @@ ENCHANT_MODULE_EXPORT (void) EnchantDictDescribeFn fn, void * user_data); +ENCHANT_MODULE_EXPORT(void) + enchant_set_prefix_dir(const char *); + #ifdef __cplusplus } #endif @@ -43,6 +43,8 @@ #include "enchant-provider.h" #include "pwl.h" #include "unused-parameter.h" +#include "relocatable.h" +#include "configmake.h" /********************************************************************************/ @@ -85,6 +87,16 @@ typedef void (*EnchantPreConfigureFunc) (EnchantProvider * provider, /********************************************************************************/ /********************************************************************************/ +/* Relocate a path and ensure the result is allocated on the heap */ +static char * +enchant_relocate (const char *path) +{ + char *newpath = (char *) relocate (path); + if (path == newpath) + newpath = strdup (newpath); + return newpath; +} + static void enchant_ensure_dir_exists (const char* dir) { @@ -110,16 +122,12 @@ enchant_get_conf_dirs (void) { GSList *conf_dirs = NULL; - char *prefix = enchant_get_prefix_dir (); - if (prefix) - { - conf_dirs = g_slist_append (conf_dirs, g_build_filename (prefix, "share", "enchant", NULL)); - g_free (prefix); - } + conf_dirs = g_slist_append (conf_dirs, enchant_relocate (PKGDATADIR)); -#if defined(ENCHANT_SYSTEM_ORDERING) - conf_dirs = g_slist_append (conf_dirs, strdup (ENCHANT_SYSTEM_ORDERING)); -#endif + char *sysconfdir = enchant_relocate (SYSCONFDIR); + char *pkgconfdir = g_build_filename (sysconfdir, "enchant", NULL); + conf_dirs = g_slist_append (conf_dirs, pkgconfdir); + free (sysconfdir); conf_dirs = g_slist_append (conf_dirs, enchant_get_user_config_dir ()); @@ -1117,43 +1125,30 @@ enchant_load_providers_in_dir (EnchantBroker * broker, const char *dir_name) static void enchant_load_providers (EnchantBroker * broker) { - char *prefix = enchant_get_prefix_dir (); - const char *pkglibdir = PKGLIBDIR; - - enchant_load_providers_in_dir (broker, pkglibdir); - - if (prefix) - { - char *module_dir = g_build_filename (prefix, "lib", "enchant", NULL); - if (strcmp (module_dir, pkglibdir) != 0) - enchant_load_providers_in_dir (broker, module_dir); - g_free (module_dir); - g_free (prefix); - } + char *module_dir = enchant_relocate (PKGLIBDIR); + enchant_load_providers_in_dir (broker, module_dir); + free (module_dir); } static void enchant_load_ordering_from_file (EnchantBroker * broker, const char * file) { - char line [1024]; - char * tag, * ordering; + char line[1024]; - size_t i, len; - - FILE * f; - - f = g_fopen (file, "r"); + FILE * f = g_fopen (file, "r"); if (!f) return; while (NULL != fgets (line, sizeof(line), f)) { + size_t i, len; + for (i = 0, len = strlen(line); i < len && line[i] != ':'; i++) ; if (i < len) { - tag = g_strndup (line, i); - ordering = g_strndup (line+(i+1), len - i); + char * tag = g_strndup (line, i); + char * ordering = g_strndup (line + (i + 1), len - i); enchant_broker_set_ordering (broker, tag, ordering); @@ -1873,7 +1868,7 @@ enchant_get_user_language(void) * compiled, except it is determined at runtime based on the location * of the enchant library. * - * Returns: the prefix dir if it can be determined, or %null otherwise. Must be g_free'd. + * Returns: the prefix dir. Must be free'd. * * This API is private to the providers. * @@ -1881,23 +1876,21 @@ enchant_get_user_language(void) ENCHANT_MODULE_EXPORT (char *) enchant_get_prefix_dir(void) { - char * prefix = NULL; - - { - /* Use ENCHANT_PREFIX_DIR env var */ - const gchar* env = g_getenv("ENCHANT_PREFIX_DIR"); - if (env) { - prefix = g_filename_to_utf8(env, -1, NULL, NULL, NULL); - } - } - -#if defined(ENCHANT_PREFIX_DIR) - if (!prefix) { - prefix = g_strdup (ENCHANT_PREFIX_DIR); - } -#endif + return enchant_relocate (INSTALLPREFIX); +} - return prefix; +/** + * enchant_set_prefix_dir + * + * Set the prefix dir. This overrides any auto-detected value, + * and can also be used on systems or installations where + * auto-detection does not work. + * + */ +ENCHANT_MODULE_EXPORT (void) +enchant_set_prefix_dir(const char *new_prefix) +{ + set_relocation_prefix (INSTALLPREFIX, new_prefix); } ENCHANT_MODULE_EXPORT(const char *) _GL_ATTRIBUTE_CONST |