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
134 lines (117 loc) · 3.49 KB
/
utils.cpp
File metadata and controls
134 lines (117 loc) · 3.49 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
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <windows.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()
{
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 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/runtime";
} else if (endswith(dirname, "src/lpython/tests") ||
endswith(to_lower(dirname), "src\\lpython\\tests")) {
// CTest Tests
return dirname + "/../../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;
}
}
// 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);
}
}