GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 90.6% 58 / 12 / 76
Functions: 90.9% 10 / 0 / 11
Branches: 47.7% 63 / 26 / 158

src/util/SystemUtil.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "SystemUtil.h"
4
5 #include <array>
6 #include <iostream> // IWYU pragma: keep (usage in Windows-only code)
7 #include <vector>
8 #if OS_UNIX
9 #include <unistd.h>
10 #elif OS_WINDOWS
11 #include <windows.h>
12 #else
13 #error "Unsupported platform"
14 #endif
15
16 #include <driver/Driver.h>
17 #include <exception/CompilerError.h>
18 #include <exception/LinkerError.h>
19
20 #include <llvm/TargetParser/Triple.h>
21
22 namespace spice::compiler {
23
24 /**
25 * Execute external command. Used to execute compiled binaries
26 *
27 * @param command Command to execute
28 * @param redirectStdErrToStdOut Redirect StdErr to StdOut
29 * @return Result struct
30 */
31 410 ExecResult SystemUtil::exec(const std::string &command, bool redirectStdErrToStdOut) {
32 #if OS_UNIX
33
1/2
✓ Branch 2 → 3 taken 410 times.
✗ Branch 2 → 41 not taken.
410 std::string redirectedCommand = command;
34
2/2
✓ Branch 3 → 4 taken 205 times.
✓ Branch 3 → 5 taken 205 times.
410 if (redirectStdErrToStdOut)
35
1/2
✓ Branch 4 → 5 taken 205 times.
✗ Branch 4 → 39 not taken.
205 redirectedCommand += " 2>&1"; // Redirect stderr to stdout
36
1/2
✓ Branch 6 → 7 taken 410 times.
✗ Branch 6 → 39 not taken.
410 FILE *pipe = popen(redirectedCommand.c_str(), "r");
37 #elif OS_WINDOWS
38 std::string redirectedCommand = command;
39 if (redirectStdErrToStdOut)
40 redirectedCommand = "\"" + command + " 2>&1\""; // Redirect stderr to stdout
41 FILE *pipe = _popen(redirectedCommand.c_str(), "r");
42 #else
43 #error "Unsupported platform"
44 #endif
45
46 if (!pipe) // GCOV_EXCL_LINE
47 throw CompilerError(IO_ERROR, "Failed to execute command: " + command); // GCOV_EXCL_LINE
48
49 410 std::array<char, 128> buffer{};
50
1/2
✓ Branch 13 → 14 taken 410 times.
✗ Branch 13 → 39 not taken.
410 std::stringstream result;
51
3/4
✓ Branch 22 → 23 taken 6850 times.
✗ Branch 22 → 37 not taken.
✓ Branch 23 → 15 taken 6440 times.
✓ Branch 23 → 24 taken 410 times.
14110 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
52
1/2
✓ Branch 17 → 18 taken 6440 times.
✗ Branch 17 → 37 not taken.
6440 result << buffer.data();
53
54 #if OS_UNIX
55
1/2
✓ Branch 24 → 25 taken 410 times.
✗ Branch 24 → 37 not taken.
410 const int exitCode = pclose(pipe) / 256;
56 #elif OS_WINDOWS
57 const int exitCode = _pclose(pipe);
58 #else
59 #error "Unsupported platform"
60 #endif
61 410 return {result.str(), exitCode};
62
1/2
✓ Branch 25 → 26 taken 410 times.
✗ Branch 25 → 37 not taken.
410 }
63
64 /**
65 * Checks if a certain command is available on the computer
66 *
67 * @param cmd Command to search for
68 * @return Present or not
69 */
70 3 bool SystemUtil::isCommandAvailable(const std::string &cmd) {
71 #if OS_UNIX
72
2/4
✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 13 not taken.
✓ Branch 3 → 4 taken 3 times.
✗ Branch 3 → 11 not taken.
3 const std::string checkCmd = "which " + cmd + " > /dev/null 2>&1";
73 #elif OS_WINDOWS
74 const std::string checkCmd = "where " + cmd + " > nul 2>&1";
75 #else
76 #error "Unsupported platform"
77 #endif
78
1/2
✓ Branch 6 → 7 taken 3 times.
✗ Branch 6 → 14 not taken.
6 return std::system(checkCmd.c_str()) == 0;
79 3 }
80
81 /**
82 * Checks if Graphviz is installed on the system
83 *
84 * @return Present or not
85 */
86
2/4
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 13 not taken.
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 11 not taken.
3 bool SystemUtil::isGraphvizInstalled() { return isCommandAvailable("dot"); }
87
88 /**
89 * Search for a supported linker invoker on the system and return the executable name or path.
90 * This function may throw a LinkerError if no linker invoker is found.
91 *
92 * @return Name and path to the linker invoker executable
93 */
94 205 ExternalBinaryFinderResult SystemUtil::findLinkerInvoker() {
95 #if OS_UNIX
96
1/2
✓ Branch 26 → 4 taken 205 times.
✗ Branch 26 → 27 not taken.
205 for (const char *linkerInvokerName : {"clang", "gcc"})
97
2/4
✓ Branch 8 → 9 taken 205 times.
✗ Branch 8 → 36 not taken.
✓ Branch 23 → 6 taken 205 times.
✗ Branch 23 → 24 not taken.
410 for (const std::string path : {"/usr/bin/", "/usr/local/bin/", "/bin/"})
98
4/8
✓ Branch 10 → 11 taken 205 times.
✗ Branch 10 → 43 not taken.
✓ Branch 11 → 12 taken 205 times.
✗ Branch 11 → 41 not taken.
✓ Branch 12 → 13 taken 205 times.
✗ Branch 12 → 39 not taken.
✓ Branch 15 → 16 taken 205 times.
✗ Branch 15 → 18 not taken.
205 if (std::filesystem::exists(path + linkerInvokerName))
99
2/4
✓ Branch 16 → 17 taken 205 times.
✗ Branch 16 → 45 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 25 taken 205 times.
410 return ExternalBinaryFinderResult{linkerInvokerName, path + linkerInvokerName};
100 #elif OS_WINDOWS
101 for (const char *linkerInvokerName : {"clang", "gcc"})
102 if (isCommandAvailable(std::string(linkerInvokerName) + " -v"))
103 return ExternalBinaryFinderResult{linkerInvokerName, linkerInvokerName};
104 #else
105 #error "Unsupported platform"
106 #endif
107 const auto msg = "No supported linker invoker was found on the system. Supported are: clang and gcc"; // LCOV_EXCL_LINE
108 throw LinkerError(LINKER_INVOKER_NOT_FOUND, msg); // LCOV_EXCL_LINE
109 }
110
111 /**
112 * Search for a supported linker on the system and return the executable name or path.
113 * This function may throw a LinkerError if no linker is found.
114 *
115 * @param cliOptions Command line options
116 * @return Name and path to the linker executable
117 */
118 205 ExternalBinaryFinderResult SystemUtil::findLinker([[maybe_unused]] const CliOptions &cliOptions) {
119 #if OS_UNIX
120 205 std::vector<const char *> linkerList;
121
1/2
✓ Branch 2 → 3 taken 205 times.
✗ Branch 2 → 75 not taken.
205 linkerList.reserve(5);
122 // mold does only support linking for unix and darwin
123
1/2
✓ Branch 4 → 5 taken 205 times.
✗ Branch 4 → 7 not taken.
205 if (!cliOptions.targetTriple.isOSWindows())
124
1/2
✓ Branch 5 → 6 taken 205 times.
✗ Branch 5 → 48 not taken.
205 linkerList.push_back("mold");
125
1/2
✓ Branch 7 → 8 taken 205 times.
✗ Branch 7 → 49 not taken.
205 linkerList.push_back("ld.lld");
126
1/2
✓ Branch 8 → 9 taken 205 times.
✗ Branch 8 → 50 not taken.
205 linkerList.push_back("ld64.ddl");
127
1/2
✓ Branch 9 → 10 taken 205 times.
✗ Branch 9 → 51 not taken.
205 linkerList.push_back("gold");
128
1/2
✓ Branch 10 → 11 taken 205 times.
✗ Branch 10 → 52 not taken.
205 linkerList.push_back("ld");
129
130
1/2
✓ Branch 38 → 13 taken 205 times.
✗ Branch 38 → 39 not taken.
205 for (const char *linkerName : linkerList)
131
2/4
✓ Branch 18 → 19 taken 410 times.
✗ Branch 18 → 53 not taken.
✓ Branch 33 → 16 taken 410 times.
✗ Branch 33 → 34 not taken.
820 for (const std::string path : {"/usr/bin/", "/usr/local/bin/", "/bin/"})
132
5/8
✓ Branch 20 → 21 taken 410 times.
✗ Branch 20 → 60 not taken.
✓ Branch 21 → 22 taken 410 times.
✗ Branch 21 → 58 not taken.
✓ Branch 22 → 23 taken 410 times.
✗ Branch 22 → 56 not taken.
✓ Branch 25 → 26 taken 205 times.
✓ Branch 25 → 28 taken 205 times.
410 if (std::filesystem::exists(path + linkerName))
133
3/4
✓ Branch 26 → 27 taken 205 times.
✗ Branch 26 → 62 not taken.
✓ Branch 30 → 31 taken 205 times.
✓ Branch 30 → 35 taken 205 times.
615 return ExternalBinaryFinderResult{linkerName, path + linkerName};
134 #elif OS_WINDOWS
135 for (const char *linkerName : {"lld", "ld"})
136 if (isCommandAvailable(std::string(linkerName) + " -v"))
137 return ExternalBinaryFinderResult{linkerName, linkerName};
138 #else
139 #error "Unsupported platform"
140 #endif
141 const auto msg = "No supported linker was found on the system. Supported are: mold, lld, gold and ld"; // LCOV_EXCL_LINE
142 throw LinkerError(LINKER_NOT_FOUND, msg); // LCOV_EXCL_LINE
143 205 }
144
145 /**
146 * Search for a supported archiver on the system and return the executable name or path.
147 * This function may throw a LinkerError if no archiver is found.
148 *
149 * @return Name and path to the archiver executable
150 */
151 ExternalBinaryFinderResult SystemUtil::findArchiver() {
152 #if OS_UNIX
153 for (const char *archiverName : {"llvm-ar", "gcc-ar", "ar"})
154 for (const std::string path : {"/usr/bin/", "/usr/local/bin/", "/bin/"})
155 if (std::filesystem::exists(path + archiverName))
156 return ExternalBinaryFinderResult{archiverName, path + archiverName};
157 #elif OS_WINDOWS
158 for (const char *archiverName : {"llvm-lib", "lib"})
159 if (isCommandAvailable(std::string(archiverName) + " -v"))
160 return ExternalBinaryFinderResult{archiverName, archiverName};
161 #else
162 #error "Unsupported platform"
163 #endif
164 const auto msg = "No supported archiver was found on the system. Supported are: llvm-ar and ar"; // LCOV_EXCL_LINE
165 throw LinkerError(ARCHIVER_NOT_FOUND, msg); // LCOV_EXCL_LINE
166 }
167
168 /**
169 * Retrieve the file extension of the produced output file, depending on target container format and target OS
170 *
171 * @param cliOptions Command line options
172 * @param outputContainer Output container
173 * @return File extension
174 */
175 458 const char *SystemUtil::getOutputFileExtension(const CliOptions &cliOptions, OutputContainer outputContainer) {
176 static constexpr auto OUTPUT_CONTAINER_COUNT = static_cast<size_t>(OutputContainer::MAX);
177 static constexpr std::array<const char *, OUTPUT_CONTAINER_COUNT> OC_EXT_MAP_WASM = {"wasm", "o", "a", "so"};
178 static constexpr std::array<const char *, OUTPUT_CONTAINER_COUNT> OC_EXT_MAP_MACOS = {"", "o", "a", "dylib"};
179 static constexpr std::array<const char *, OUTPUT_CONTAINER_COUNT> OC_EXT_MAP_WINDOWS = {"exe", "obj", "lib", "dll"};
180 static constexpr std::array<const char *, OUTPUT_CONTAINER_COUNT> OC_EXT_MAP_LINUX = {"", "o", "a", "so"};
181
182 458 const auto outputContainerCasted = static_cast<uint8_t>(outputContainer);
183
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 457 times.
458 if (cliOptions.targetTriple.isWasm())
184 1 return OC_EXT_MAP_WASM[outputContainerCasted];
185
2/2
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 10 taken 456 times.
457 if (cliOptions.targetTriple.isOSDarwin())
186 1 return OC_EXT_MAP_MACOS[outputContainerCasted];
187
2/2
✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 14 taken 455 times.
456 if (cliOptions.targetTriple.isOSWindows())
188 1 return OC_EXT_MAP_WINDOWS[outputContainerCasted];
189 455 return OC_EXT_MAP_LINUX[outputContainerCasted];
190 }
191
192 /**
193 * Retrieve the dir, where the standard library lives.
194 * Returns an empty string if the std was not found.
195 *
196 * @return Std directory
197 */
198 962 std::filesystem::path SystemUtil::getStdDir() {
199 #if OS_UNIX
200
3/6
✓ Branch 2 → 3 taken 962 times.
✗ Branch 2 → 32 not taken.
✓ Branch 3 → 4 taken 962 times.
✗ Branch 3 → 30 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 962 times.
962 if (exists(std::filesystem::path("/usr/lib/spice/std/")))
201 return "/usr/lib/spice/std/";
202 #endif
203
1/2
✓ Branch 8 → 9 taken 962 times.
✗ Branch 8 → 21 not taken.
962 if (std::getenv("SPICE_STD_DIR"))
204
3/6
✓ Branch 10 → 11 taken 962 times.
✗ Branch 10 → 33 not taken.
✓ Branch 11 → 12 taken 962 times.
✗ Branch 11 → 34 not taken.
✓ Branch 12 → 13 taken 962 times.
✗ Branch 12 → 15 not taken.
962 if (const std::filesystem::path stdPath(std::getenv("SPICE_STD_DIR")); exists(stdPath))
205
2/4
✓ Branch 13 → 14 taken 962 times.
✗ Branch 13 → 34 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 962 times.
962 return stdPath;
206 const auto errMsg = "Standard library could not be found. Check if the env var SPICE_STD_DIR exists"; // GCOV_EXCL_LINE
207 throw CompilerError(STD_NOT_FOUND, errMsg); // GCOV_EXCL_LINE
208 }
209
210 /**
211 * Retrieve the dir, where the bootstrap compiler lives.
212 * Returns an empty string if the bootstrap compiler was not found.
213 *
214 * @return
215 */
216 18 std::filesystem::path SystemUtil::getBootstrapDir() {
217
1/2
✓ Branch 3 → 4 taken 18 times.
✗ Branch 3 → 13 not taken.
18 if (std::getenv("SPICE_BOOTSTRAP_DIR")) {
218
3/8
✓ Branch 5 → 6 taken 18 times.
✗ Branch 5 → 22 not taken.
✓ Branch 6 → 7 taken 18 times.
✗ Branch 6 → 23 not taken.
✓ Branch 7 → 8 taken 18 times.
✗ Branch 7 → 9 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
18 if (const std::filesystem::path stdPath(std::getenv("SPICE_BOOTSTRAP_DIR")); exists(stdPath))
219 36 return stdPath;
220 }
221 const auto errMsg = "Bootstrap compiler could not be found. Check if the env var SPICE_BOOTSTRAP_DIR exists"; // GCOV_EXCL_LINE
222 throw CompilerError(BOOTSTRAP_NOT_FOUND, errMsg); // GCOV_EXCL_LINE
223 }
224
225 /**
226 * Retrieve the dir, where output binaries should go when installing them
227 *
228 * @return Installation directory
229 */
230 2 std::filesystem::path SystemUtil::getSpiceBinDir() {
231 #if OS_UNIX
232 2 return "/usr/local/bin/";
233 #elif OS_WINDOWS
234 const char *userProfile = std::getenv("USERPROFILE");
235 assert(userProfile != nullptr && strlen(userProfile) > 0);
236 return std::filesystem::path(userProfile) / "spice" / "bin";
237 #else
238 #error "Unsupported platform"
239 #endif
240 }
241
242 /**
243 * Get the memory page size of the current system
244 *
245 * @return Page size in bytes
246 */
247 448 size_t SystemUtil::getSystemPageSize() {
248 #if OS_UNIX
249 448 return static_cast<size_t>(sysconf(_SC_PAGESIZE));
250 #elif OS_WINDOWS
251 SYSTEM_INFO si;
252 GetSystemInfo(&si);
253 return static_cast<size_t>(si.dwPageSize);
254 #else
255 #error "Unsupported platform"
256 #endif
257 }
258
259 } // namespace spice::compiler
260