-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
file-util.spice
153 lines (144 loc) · 4.25 KB
/
file-util.spice
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
// Std imports
import "std/os/os";
import "std/os/cmd";
import "std/os/env";
import "std/io/file";
import "std/io/filepath";
import "std/iterator/array-iterator";
public type ExecResult struct {
String output
int exitCode
}
/**
* Execute external command. Used to execute compiled binaries
*
* @param cmd Command to execute
* @return Result struct
*/
public f<ExecResult> exec(const string cmd) {
// ToDo
return ExecResult{};
}
/**
* Checks if a certain command is available on the computer
*
* @param cmd Command to search for
* @return Present or not
*/
public f<bool> isCommandAvailable(const string cmd) {
String checkCmd;
if isWindows() {
checkCmd = "where " + cmd + " > nul 2>&1";
} else if isLinux() {
checkCmd = "which " + cmd + " > /dev/null 2>&1";
} else {
panic(Error("Unsupported platform"));
}
return execCmd(checkCmd.getRaw()) == 0;
}
/**
* Checks if Graphviz is installed on the system
*
* @return Present or not
*/
public f<bool> isGraphvizInstalled() {
return isCommandAvailable("dot");
}
/**
* Search for a supported linker invoker on the system and return the executable name or path.
* This function may throw a LinkerError if no linker invoker is found.
*
* @return Name of path to the linker invoker executable
*/
public f<String> findLinkerInvoker() {
foreach const string linkerInvoker : ["clang", "gcc"] {
if isLinux() {
foreach const string path : ["/usr/bin/", "/usr/local/bin/", "/bin/"] {
const String linkerInvokerPath = path + linkerInvoker;
if fileExists(linkerInvokerPath.getRaw()) {
return linkerInvokerPath;
}
}
} else if isWindows() {
String linkerInvokerLookupCommand = linkerInvoker + " -v";
if isCommandAvailable(linkerInvokerLookupCommand.getRaw()) {
return String(linkerInvoker);
}
}
}
panic(Error("No supported linker invoker found on the system. Supported are: clang and gcc"));
}
/**
* Search for a supported linker on the system and return the executable name or path.
* This function may throw a LinkerError if no linker is found.
*
* @return Name of path to the linker executable
*/
public f<String> findLinker() {
if isLinux() {
foreach const string linkerName : ["mold", "lld", "gold", "ld"] {
foreach const string path : ["/usr/bin/", "/usr/local/bin/", "/bin/"] {
String linker = path + linkerName;
if fileExists(linker.getRaw()) {
return linker;
}
}
}
} else if isWindows() {
foreach const string linkerName : ["lld", "ld"] {
String checkCmd = linkerName + " -v";
if isCommandAvailable(checkCmd.getRaw()) {
return String(linkerName);
}
}
}
panic(Error("No supported linker found on the system. Supported are: mold, lld, gold and ld"));
}
/**
* Retrieve the dir, where the standard library lives.
* Returns an empty string if the std was not found.
*
* @return Std directory
*/
public f<FilePath> getStdDir() {
if isLinux() {
result = FilePath("/usr/lib/spice/std/");
if result.exists() { return; }
}
string value = getEnv("SPICE_STD_DIR");
if value != "" {
result = FilePath(value);
if result.exists() { return; }
}
return FilePath();
}
/**
* Retrieve the dir, where the bootstrap compiler lives.
* Returns an empty string if the bootstrap compiler was not found.
*
* @return
*/
public f<FilePath> getBootstrapCompilerDir() {
string value = getEnv("SPICE_BOOTSTRAP_DIR");
if value != "" {
result = FilePath(value);
if result.exists() { return; }
}
return FilePath();
}
/**
* Retrieve the dir, where output binaries should go when installing them
*
* @return Installation directory
*/
public f<FilePath> getSpiceBinDir() {
if isWindows() {
string userprofile = getEnv("USERPROFILE");
assert userprofile != "";
return FilePath(userprofile) / "spice" / "bin";
} else if isLinux() {
return FilePath("/usr/local/bin");
} else {
panic(Error("Unsupported platform"));
}
}