Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/xinspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

#include <pugixml.hpp>

#include <xtl/xsystem.hpp>

#include "xeus-cpp/xbuffer.hpp"
#include "xeus-cpp/xpreamble.hpp"

Expand Down
100 changes: 98 additions & 2 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,35 @@
#include <cinttypes> // required before including llvm/ExecutionEngine/Orc/LLJIT.h because missing llvm/Object/SymbolicFile.h
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include <xtl/xsystem.hpp>
#if defined(__linux__)
# include <unistd.h>
#endif
#if defined(_WIN32)
# if defined(NOMINMAX)
# include <windows.h>
# else
# define NOMINMAX
# include <windows.h>
# undef NOMINMAX
# endif
#endif
#ifdef __APPLE__
# include <cstdint>
# include <mach-o/dyld.h>
#endif
#if defined(__sun)
# include <stdlib.h>
#endif
#ifdef __FreeBSD__
# include <sys/types.h>
# include <sys/sysctl.h>
#endif

#include <xeus/xhelper.hpp>

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions src/xutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
#include <unistd.h>
#endif


#include "xeus-cpp/xutils.hpp"
#include "xeus-cpp/xinterpreter.hpp"

namespace xcpp
{
Expand Down