summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJosh Blum2013-07-07 20:06:44 -0700
committerJosh Blum2013-07-07 20:06:44 -0700
commit746b27ece7ae9cd60cab5945a195751ec9f9199f (patch)
treed89b7b66318fd799223af2358a74a7b270ad31d2 /lib
parent0793d5b373f0d6ed54b08765be09cc8250fe7f6a (diff)
downloadsandhi-746b27ece7ae9cd60cab5945a195751ec9f9199f.tar.gz
sandhi-746b27ece7ae9cd60cab5945a195751ec9f9199f.tar.bz2
sandhi-746b27ece7ae9cd60cab5945a195751ec9f9199f.zip
gras: added module loader
Diffstat (limited to 'lib')
-rw-r--r--lib/CMakeLists.txt16
-rw-r--r--lib/module_loader.cpp61
2 files changed, 76 insertions, 1 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 93ef63c..265020e 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -7,7 +7,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
########################################################################
# Setup Boost
########################################################################
-find_package(Boost COMPONENTS thread date_time regex system)
+find_package(Boost COMPONENTS thread date_time regex system filesystem)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
list(APPEND GRAS_LIBRARIES ${Boost_LIBRARIES})
@@ -37,6 +37,20 @@ file(GLOB apology_sources "${GRAS_SOURCE_DIR}/Apology/lib/*.cpp")
list(APPEND GRAS_SOURCES ${apology_sources})
########################################################################
+# Setup Module Loader
+########################################################################
+file(TO_NATIVE_PATH "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/gras/modules" GRAS_MODULE_PATH)
+STRING(REPLACE "\\" "\\\\" GRAS_MODULE_PATH ${GRAS_MODULE_PATH})
+configure_file(
+ ${CMAKE_CURRENT_SOURCE_DIR}/module_loader.cpp
+ ${CMAKE_CURRENT_BINARY_DIR}/module_loader.cpp
+@ONLY)
+list(APPEND GRAS_SOURCES
+ ${CMAKE_CURRENT_BINARY_DIR}/module_loader.cpp
+)
+list(APPEND GRAS_LIBRARIES ${CMAKE_DL_LIBS})
+
+########################################################################
# Append gras-core library sources
########################################################################
list(APPEND GRAS_SOURCES
diff --git a/lib/module_loader.cpp b/lib/module_loader.cpp
new file mode 100644
index 0000000..ce59502
--- /dev/null
+++ b/lib/module_loader.cpp
@@ -0,0 +1,61 @@
+// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+#include <gras/gras.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/tokenizer.hpp>
+#include <boost/foreach.hpp>
+#include <stdexcept>
+#include <iostream>
+#include <cstdlib>
+
+namespace fs = boost::filesystem;
+
+#ifdef BOOST_WINDOWS_API
+ #include <windows.h>
+ static const char *SEP = ";";
+ static bool load_module_in_path(const char *path)
+ {
+ return LoadLibrary(path) != NULL;
+ }
+#else
+ #include <dlfcn.h>
+ static const char *SEP = ":";
+ static bool load_module_in_path(const char *path)
+ {
+ return dlopen(path, RTLD_LAZY) != NULL;
+ }
+#endif
+
+static void load_all_modules_in_path(const fs::path &path)
+{
+ if (not fs::exists(fs::path(path))) return;
+ if (not fs::is_directory(fs::path(path))) return;
+ for(
+ fs::directory_iterator dir_itr(path);
+ dir_itr != fs::directory_iterator();
+ ++dir_itr
+ ){
+ const std::string mod_path = dir_itr->path().string();
+ if (not load_module_in_path(mod_path.c_str()))
+ {
+ std::cerr << "GRAS Module loader fail: " << mod_path << std::endl;
+ }
+ }
+}
+
+GRAS_STATIC_BLOCK(gras_module_loader)
+{
+ std::string search_paths = "@GRAS_MODULE_PATH@";
+ const char *module_path_env = std::getenv("GRAS_MODULE_PATH");
+ if (module_path_env != NULL)
+ {
+ search_paths += SEP;
+ search_paths += module_path_env;
+ }
+ if (search_paths.empty()) return;
+ BOOST_FOREACH(const std::string &path, boost::tokenizer<boost::char_separator<char> > (search_paths, boost::char_separator<char>(SEP)))
+ {
+ if (path.empty()) continue;
+ load_all_modules_in_path(fs::path(path));
+ }
+}