Cleanup search for chaiscript_stdlib and fix some bugs

This commit is contained in:
Jason Turner 2014-03-23 16:42:04 -06:00
parent 304e34002b
commit bf0737a35c
3 changed files with 69 additions and 43 deletions

View File

@ -9,6 +9,12 @@ else()
option(MULTITHREAD_SUPPORT_ENABLED "Multithreaded Support Enabled" TRUE) option(MULTITHREAD_SUPPORT_ENABLED "Multithreaded Support Enabled" TRUE)
endif() endif()
if (CMAKE_COMPILER_IS_GNUCC)
option(ENABLE_COVERAGE "Enable Coverage Reporting in GCC" FALSE)
endif()
option(BUILD_MODULES "Build Extra Modules (stl, reflection)" TRUE) option(BUILD_MODULES "Build Extra Modules (stl, reflection)" TRUE)
option(BUILD_SAMPLES "Build Samples Folder" FALSE) option(BUILD_SAMPLES "Build Samples Folder" FALSE)
@ -70,17 +76,24 @@ else(READLINE_LIBRARY)
set (READLINE_FLAG ) set (READLINE_FLAG )
endif(READLINE_LIBRARY) endif(READLINE_LIBRARY)
SET(EXTRA_LINKER_FLAGS "")
if (CMAKE_COMPILER_IS_GNUCC) if (CMAKE_COMPILER_IS_GNUCC)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
OUTPUT_VARIABLE GCC_VERSION)
if (GCC_VERSION VERSION_LESS 4.8) if (GCC_VERSION VERSION_LESS 4.8)
SET(CPP11_FLAG "-std=c++0x") SET(CPP11_FLAG "-std=c++0x")
else() else()
SET(CPP11_FLAG "-std=c++11") SET(CPP11_FLAG "-std=c++11")
endif() endif()
if (ENABLE_COVERAGE)
add_definitions(-fprofile-arcs -ftest-coverage)
SET(EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} "-fprofile-arcs -ftest-coverage")
# SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "-fprofile-arcs -ftest-coverage")
endif()
else() else()
SET(CPP11_FLAG "-std=c++11") SET(CPP11_FLAG "-std=c++11")
endif() endif()
@ -105,17 +118,16 @@ else()
endif() endif()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
option(USE_LIBCXX "Use clang's libcxx" TRUE) option(USE_LIBCXX "Use clang's libcxx" TRUE)
if (USE_LIBCXX) if (USE_LIBCXX)
add_definitions(-stdlib=libc++) add_definitions(-stdlib=libc++)
set (EXTRA_LINKER_FLAGS ${CPP11_FLAG} -stdlib=libc++) set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG} -stdlib=libc++)
else () else ()
set (EXTRA_LINKER_FLAGS ${CPP11_FLAG}) set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG})
endif() endif()
elseif(CMAKE_COMPILER_IS_GNUCC) elseif(CMAKE_COMPILER_IS_GNUCC)
set (EXTRA_LINKER_FLAGS ${CPP11_FLAG}) set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG})
endif() endif()
# limitations in MinGW require us to make an optimized build # limitations in MinGW require us to make an optimized build

View File

@ -337,7 +337,7 @@ namespace chaiscript
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_type_name, std::ref(m_engine)), "name"); m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_type_name, std::ref(m_engine)), "name");
typedef void (ChaiScript::*load_mod_1)(const std::string&); typedef std::string (ChaiScript::*load_mod_1)(const std::string&);
typedef void (ChaiScript::*load_mod_2)(const std::string&, const std::string&); typedef void (ChaiScript::*load_mod_2)(const std::string&, const std::string&);
m_engine.add(fun(static_cast<load_mod_1>(&ChaiScript::load_module), this), "load_module"); m_engine.add(fun(static_cast<load_mod_1>(&ChaiScript::load_module), this), "load_module");
@ -449,7 +449,7 @@ namespace chaiscript
dllpath = std::string(&buf.front(), pathlen); dllpath = std::string(&buf.front(), pathlen);
} }
m_modulepaths.push_back(dllpath+"/"); m_modulepaths.insert(m_modulepaths.begin(), dllpath+"/");
} }
#endif #endif
@ -652,7 +652,7 @@ namespace chaiscript
/// (the symbol mentioned above), an exception is thrown. /// (the symbol mentioned above), an exception is thrown.
/// ///
/// \throw chaiscript::exception::load_module_error In the event that no matching module can be found. /// \throw chaiscript::exception::load_module_error In the event that no matching module can be found.
void load_module(const std::string &t_module_name) std::string load_module(const std::string &t_module_name)
{ {
std::vector<exception::load_module_error> errors; std::vector<exception::load_module_error> errors;
@ -673,9 +673,11 @@ namespace chaiscript
{ {
try { try {
std::string name = m_modulepaths[i] + prefixes[j] + t_module_name + postfixes[k]; std::string name = m_modulepaths[i] + prefixes[j] + t_module_name + postfixes[k];
// std::cerr << "trying location: " << name << std::endl;
load_module(t_module_name, name); load_module(t_module_name, name);
return; return name;
} catch (const chaiscript::exception::load_module_error &e) { } catch (const chaiscript::exception::load_module_error &e) {
// std::cerr << "error: " << e.what() << std::endl;
errors.push_back(e); errors.push_back(e);
// Try next set // Try next set
} }

View File

@ -43,11 +43,11 @@ void using_history(){}
void *cast_module_symbol(std::string (*t_path)()) void *cast_module_symbol(std::vector<std::string> (*t_path)())
{ {
union cast_union union cast_union
{ {
std::string (*in_ptr)(); std::vector<std::string> (*in_ptr)();
void *out_ptr; void *out_ptr;
}; };
@ -56,22 +56,27 @@ void *cast_module_symbol(std::string (*t_path)())
return c.out_ptr; return c.out_ptr;
} }
std::string default_search_path() std::vector<std::string> default_search_paths()
{ {
std::vector<std::string> paths;
#ifdef CHAISCRIPT_WINDOWS // force no unicode #ifdef CHAISCRIPT_WINDOWS // force no unicode
CHAR path[4096]; CHAR path[4096];
int size = GetModuleFileNameA(0, path, sizeof(path)-1); int size = GetModuleFileNameA(0, path, sizeof(path)-1);
std::string exepath(path, size); std::string exepath(path, size);
size_t secondtolastslash = exepath.rfind('\\', exepath.rfind('\\') - 1); size_t lastslash = exepath.rfind('\\');
if (secondtolastslash != std::string::npos) size_t secondtolastslash = exepath.rfind('\\', lastslash - 1);
if (lastslash != std::string::npos)
{ {
return exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\"; paths.push_back(exepath.substr(0, lastslash));
} else {
return "";
} }
if (secondtolastslash != std::string::npos)
{
return {exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\"};
}
#else #else
std::string exepath; std::string exepath;
@ -104,21 +109,28 @@ std::string default_search_path()
{ {
Dl_info rInfo; Dl_info rInfo;
memset( &rInfo, 0, sizeof(rInfo) ); memset( &rInfo, 0, sizeof(rInfo) );
if ( !dladdr(cast_module_symbol(&default_search_path), &rInfo) || !rInfo.dli_fname ) { if ( !dladdr(cast_module_symbol(&default_search_paths), &rInfo) || !rInfo.dli_fname ) {
return ""; return paths;
} }
exepath = std::string(rInfo.dli_fname); exepath = std::string(rInfo.dli_fname);
} }
size_t secondtolastslash = exepath.rfind('/', exepath.rfind('/') - 1); size_t lastslash = exepath.rfind('/');
size_t secondtolastslash = exepath.rfind('/', lastslash - 1);
if (lastslash != std::string::npos)
{
paths.push_back(exepath.substr(0, lastslash));
}
if (secondtolastslash != std::string::npos) if (secondtolastslash != std::string::npos)
{ {
return exepath.substr(0, secondtolastslash) + "/lib/chaiscript/"; paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
} else {
return "";
} }
#endif #endif
return paths;
} }
void help(int n) { void help(int n) {
@ -239,8 +251,6 @@ void interactive(chaiscript::ChaiScript& chai)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
std::vector<std::string> usepaths;
std::vector<std::string> modulepaths;
// Disable deprecation warning for getenv call. // Disable deprecation warning for getenv call.
#ifdef CHAISCRIPT_MSVC #ifdef CHAISCRIPT_MSVC
@ -255,14 +265,16 @@ int main(int argc, char *argv[])
#pragma warning(pop) #pragma warning(pop)
#endif #endif
std::vector<std::string> usepaths;
usepaths.push_back(""); usepaths.push_back("");
if (usepath) if (usepath)
{ {
usepaths.push_back(usepath); usepaths.push_back(usepath);
} }
std::string searchpath = default_search_path(); std::vector<std::string> modulepaths;
modulepaths.push_back(searchpath); std::vector<std::string> searchpaths = default_search_paths();
modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
modulepaths.push_back(""); modulepaths.push_back("");
if (modulepath) if (modulepath)
{ {