diff --git a/CMakeLists.txt b/CMakeLists.txt index 8857466..7c5ea5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,12 @@ else() option(MULTITHREAD_SUPPORT_ENABLED "Multithreaded Support Enabled" TRUE) 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_SAMPLES "Build Samples Folder" FALSE) @@ -70,17 +76,24 @@ else(READLINE_LIBRARY) set (READLINE_FLAG ) endif(READLINE_LIBRARY) +SET(EXTRA_LINKER_FLAGS "") if (CMAKE_COMPILER_IS_GNUCC) - execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion - OUTPUT_VARIABLE GCC_VERSION) - + execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) if (GCC_VERSION VERSION_LESS 4.8) SET(CPP11_FLAG "-std=c++0x") else() SET(CPP11_FLAG "-std=c++11") 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() SET(CPP11_FLAG "-std=c++11") endif() @@ -105,17 +118,16 @@ else() endif() if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - option(USE_LIBCXX "Use clang's libcxx" TRUE) if (USE_LIBCXX) add_definitions(-stdlib=libc++) - set (EXTRA_LINKER_FLAGS ${CPP11_FLAG} -stdlib=libc++) + set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG} -stdlib=libc++) else () - set (EXTRA_LINKER_FLAGS ${CPP11_FLAG}) + set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG}) endif() elseif(CMAKE_COMPILER_IS_GNUCC) - set (EXTRA_LINKER_FLAGS ${CPP11_FLAG}) + set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG}) endif() # limitations in MinGW require us to make an optimized build diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index ff37d56..58d48f0 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -337,7 +337,7 @@ namespace chaiscript 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&); m_engine.add(fun(static_cast(&ChaiScript::load_module), this), "load_module"); @@ -449,7 +449,7 @@ namespace chaiscript dllpath = std::string(&buf.front(), pathlen); } - m_modulepaths.push_back(dllpath+"/"); + m_modulepaths.insert(m_modulepaths.begin(), dllpath+"/"); } #endif @@ -652,7 +652,7 @@ namespace chaiscript /// (the symbol mentioned above), an exception is thrown. /// /// \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 errors; @@ -665,23 +665,25 @@ namespace chaiscript postfixes.push_back(".so"); postfixes.push_back(""); - for (size_t i = 0; i < m_modulepaths.size(); ++i) + for (size_t i = 0; i < m_modulepaths.size(); ++i) + { + for (size_t j = 0; j < prefixes.size(); ++j) { - for (size_t j = 0; j < prefixes.size(); ++j) - { - for (size_t k = 0; k < postfixes.size(); ++k) - { - try { - std::string name = m_modulepaths[i] + prefixes[j] + t_module_name + postfixes[k]; - load_module(t_module_name, name); - return; - } catch (const chaiscript::exception::load_module_error &e) { - errors.push_back(e); - // Try next set - } - } + for (size_t k = 0; k < postfixes.size(); ++k) + { + try { + 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); + return name; + } catch (const chaiscript::exception::load_module_error &e) { + // std::cerr << "error: " << e.what() << std::endl; + errors.push_back(e); + // Try next set } + } } + } std::string errstring; diff --git a/src/main.cpp b/src/main.cpp index d633c76..c8d98e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,11 +43,11 @@ void using_history(){} -void *cast_module_symbol(std::string (*t_path)()) +void *cast_module_symbol(std::vector (*t_path)()) { union cast_union { - std::string (*in_ptr)(); + std::vector (*in_ptr)(); void *out_ptr; }; @@ -56,22 +56,27 @@ void *cast_module_symbol(std::string (*t_path)()) return c.out_ptr; } -std::string default_search_path() +std::vector default_search_paths() { + std::vector paths; + #ifdef CHAISCRIPT_WINDOWS // force no unicode CHAR path[4096]; int size = GetModuleFileNameA(0, path, sizeof(path)-1); std::string exepath(path, size); - size_t secondtolastslash = exepath.rfind('\\', exepath.rfind('\\') - 1); - if (secondtolastslash != std::string::npos) + size_t lastslash = exepath.rfind('\\'); + size_t secondtolastslash = exepath.rfind('\\', lastslash - 1); + if (lastslash != std::string::npos) { - return exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\"; - } else { - return ""; + paths.push_back(exepath.substr(0, lastslash)); } + if (secondtolastslash != std::string::npos) + { + return {exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\"}; + } #else std::string exepath; @@ -102,23 +107,30 @@ std::string default_search_path() if (exepath.empty()) { - Dl_info rInfo; + Dl_info rInfo; memset( &rInfo, 0, sizeof(rInfo) ); - if ( !dladdr(cast_module_symbol(&default_search_path), &rInfo) || !rInfo.dli_fname ) { - return ""; + if ( !dladdr(cast_module_symbol(&default_search_paths), &rInfo) || !rInfo.dli_fname ) { + return paths; } 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) { - return exepath.substr(0, secondtolastslash) + "/lib/chaiscript/"; - } else { - return ""; + paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/"); } #endif + + return paths; } void help(int n) { @@ -239,8 +251,6 @@ void interactive(chaiscript::ChaiScript& chai) int main(int argc, char *argv[]) { - std::vector usepaths; - std::vector modulepaths; // Disable deprecation warning for getenv call. #ifdef CHAISCRIPT_MSVC @@ -255,14 +265,16 @@ int main(int argc, char *argv[]) #pragma warning(pop) #endif + std::vector usepaths; usepaths.push_back(""); if (usepath) { usepaths.push_back(usepath); } - std::string searchpath = default_search_path(); - modulepaths.push_back(searchpath); + std::vector modulepaths; + std::vector searchpaths = default_search_paths(); + modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end()); modulepaths.push_back(""); if (modulepath) {