forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
190 lines (166 loc) · 5.06 KB
/
utils.cpp
File metadata and controls
190 lines (166 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <fstream>
#include <bin/tpl/whereami/whereami.h>
#include <libasr/exception.h>
#include <lpython/utils.h>
#include <libasr/string_utils.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#define stat _stat
#if !defined S_ISDIR
#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
#endif
#endif
namespace LCompilers::LPython {
void get_executable_path(std::string &executable_path, int &dirname_length)
{
#ifdef HAVE_WHEREAMI
int length;
length = wai_getExecutablePath(NULL, 0, &dirname_length);
if (length > 0) {
std::string path(length+1, '\0');
wai_getExecutablePath(&path[0], length, &dirname_length);
executable_path = path;
if (executable_path[executable_path.size()-1] == '\0') {
executable_path = executable_path.substr(0,executable_path.size()-1);
}
} else {
throw LCompilersException("Cannot determine executable path.");
}
#else
executable_path = "src/bin/lpython.js";
dirname_length = 7;
#endif
}
std::string get_runtime_library_dir()
{
#ifdef HAVE_BUILD_TO_WASM
return "asset_dir";
#endif
char *env_p = std::getenv("LFORTRAN_RUNTIME_LIBRARY_DIR");
if (env_p) return env_p;
std::string path;
int dirname_length;
get_executable_path(path, dirname_length);
std::string dirname = path.substr(0,dirname_length);
if ( endswith(dirname, "src/bin")
|| endswith(dirname, "src\\bin")
|| endswith(dirname, "SRC\\BIN")) {
// Development version
return dirname + "/../runtime";
} else if (endswith(dirname, "src/lpython/tests") ||
endswith(to_lower(dirname), "src\\lpython\\tests")) {
// CTest Tests
return dirname + "/../../runtime";
} else {
// Installed version
return dirname + "/../share/lpython/lib";
}
}
std::string get_runtime_library_header_dir()
{
char *env_p = std::getenv("LFORTRAN_RUNTIME_LIBRARY_HEADER_DIR");
if (env_p) return env_p;
// The header file is in libasr/src/libasr/runtime for development, but in impure
// in installed version
std::string path;
int dirname_length;
get_executable_path(path, dirname_length);
std::string dirname = path.substr(0,dirname_length);
if ( endswith(dirname, "src/bin")
|| endswith(dirname, "src\\bin")
|| endswith(dirname, "SRC\\BIN")) {
// Development version
return dirname + "/../../libasr/src/libasr/runtime";
} else if (endswith(dirname, "src/lpython/tests") ||
endswith(to_lower(dirname), "src\\lpython\\tests")) {
// CTest Tests
return dirname + "/../../../libasr/src/libasr/runtime";
} else {
// Installed version
return dirname + "/../share/lpython/lib/impure";
}
return path;
}
bool is_directory(std::string path) {
struct stat buffer;
if (stat(path.c_str(), &buffer) == 0) {
if (S_ISDIR(buffer.st_mode)) {
return true;
} else {
return false;
}
}
return false;
}
bool path_exists(std::string path) {
struct stat buffer;
if (stat(path.c_str(), &buffer) == 0) {
return true;
} else {
return false;
}
}
#ifdef HAVE_LFORTRAN_LLVM
void open_cpython_library(DynamicLibrary &l) {
std::string conda_prefix = std::getenv("CONDA_PREFIX");
#if defined (__linux__)
l.l = dlopen((conda_prefix + "/lib/libpython3.so").c_str(), RTLD_DEEPBIND | RTLD_GLOBAL | RTLD_NOW);
#elif defined (__APPLE__)
l.l = dlopen((conda_prefix + "/lib/libpython3.dylib").c_str(), RTLD_GLOBAL | RTLD_NOW);
#else
l.l = LoadLibrary((conda_prefix + "\\python3.dll").c_str());
#endif
if (l.l == nullptr)
throw "Could not open CPython library";
}
void close_cpython_library(DynamicLibrary &l) {
#if (defined (__linux__)) or (defined (__APPLE__))
dlclose(l.l);
l.l = nullptr;
#else
FreeLibrary((HMODULE)l.l);
l.l = nullptr;
#endif
}
void open_symengine_library(DynamicLibrary &l) {
std::string conda_prefix = std::getenv("CONDA_PREFIX");
#if defined (__linux__)
l.l = dlopen((conda_prefix + "/lib/libsymengine.so").c_str(), RTLD_DEEPBIND | RTLD_GLOBAL | RTLD_NOW);
#elif defined (__APPLE__)
l.l = dlopen((conda_prefix + "/lib/libsymengine.dylib").c_str(), RTLD_GLOBAL | RTLD_NOW);
#else
l.l = LoadLibrary((conda_prefix + "\\Library\\bin\\symengine-0.11.dll").c_str());
#endif
if (l.l == nullptr)
throw "Could not open SymEngine library";
}
void close_symengine_library(DynamicLibrary &l) {
#if (defined (__linux__)) or (defined (__APPLE__))
dlclose(l.l);
l.l = nullptr;
#else
FreeLibrary((HMODULE)l.l);
l.l = nullptr;
#endif
}
#endif
// Decodes the exit status code of the process (in Unix)
// See `WEXITSTATUS` for more information.
// https://stackoverflow.com/a/27117435/15913193
// https://linux.die.net/man/3/system
int32_t get_exit_status(int32_t err) {
return (((err) >> 8) & 0x000000ff);
}
}