diff --git a/src/xinspect.hpp b/src/xinspect.hpp index 17c0cdcc..b83fba95 100644 --- a/src/xinspect.hpp +++ b/src/xinspect.hpp @@ -15,8 +15,6 @@ #include -#include - #include "xeus-cpp/xbuffer.hpp" #include "xeus-cpp/xpreamble.hpp" diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 686e7652..a366dbf5 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -11,12 +11,35 @@ #include // required before including llvm/ExecutionEngine/Orc/LLJIT.h because missing llvm/Object/SymbolicFile.h #include #include +#include #include #include #include #include -#include +#if defined(__linux__) +# include +#endif +#if defined(_WIN32) +# if defined(NOMINMAX) +# include +# else +# define NOMINMAX +# include +# undef NOMINMAX +# endif +#endif +#ifdef __APPLE__ +# include +# include +#endif +#if defined(__sun) +# include +#endif +#ifdef __FreeBSD__ +# include +# include +#endif #include @@ -105,6 +128,79 @@ __get_cxx_version () return std::to_string(cxx_version); } + inline std::string executable_path() + { + std::string path; +#if defined(UNICODE) + wchar_t buffer[1024]; +#else + char buffer[1024]; +#endif + std::memset(buffer, '\0', sizeof(buffer)); +#if defined(__linux__) + if (readlink("/proc/self/exe", buffer, sizeof(buffer)) != -1) + { + path = buffer; + } + else + { + // failed to determine run path + } +#elif defined (_WIN32) + #if defined(UNICODE) + if (GetModuleFileNameW(nullptr, buffer, sizeof(buffer)) != 0) + { + // Convert wchar_t to std::string + std::wstring wideString(buffer); + std::string narrowString(wideString.begin(), wideString.end()); + path = narrowString; + } + #else + if (GetModuleFileNameA(nullptr, buffer, sizeof(buffer)) != 0) + { + path = buffer; + } + #endif + // failed to determine run path +#elif defined (__APPLE__) + std::uint32_t size = sizeof(buffer); + if(_NSGetExecutablePath(buffer, &size) == 0) + { + path = buffer; + } + else + { + // failed to determine run path + } +#elif defined (__FreeBSD__) + int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; + size_t buffer_size = sizeof(buffer); + if (sysctl(mib, 4, buffer, &buffer_size, NULL, 0) != -1) + { + path = buffer; + } + else + { + // failed to determine run path + } +#elif defined(__sun) + path = getexecname(); +#endif + return path; + } + + inline std::string prefix_path() + { + std::string path = executable_path(); +#if defined (_WIN32) + char separator = '\\'; +#else + char separator = '/'; +#endif + std::string bin_folder = path.substr(0, path.find_last_of(separator)); + std::string prefix = bin_folder.substr(0, bin_folder.find_last_of(separator)) + separator; + return prefix; + } interpreter::interpreter(int argc, const char* const* argv) : xmagics() @@ -387,7 +483,7 @@ __get_cxx_version () void interpreter::init_includes() { - Cpp::AddIncludePath((xtl::prefix_path() + "/include/").c_str()); + Cpp::AddIncludePath((prefix_path() + "/include/").c_str()); } void interpreter::init_preamble() diff --git a/src/xutils.cpp b/src/xutils.cpp index 08169a4f..62a84d0f 100644 --- a/src/xutils.cpp +++ b/src/xutils.cpp @@ -22,9 +22,7 @@ #include #endif - #include "xeus-cpp/xutils.hpp" -#include "xeus-cpp/xinterpreter.hpp" namespace xcpp {