GCC Code Coverage Report


Directory: ../
File: src/util/FileUtil.cpp
Date: 2025-10-27 22:48:14
Coverage Exec Excl Total
Lines: 65.8% 50 8 84
Functions: 63.6% 7 0 11
Branches: 36.6% 60 14 178

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "FileUtil.h"
4
5 #include <array>
6 #include <filesystem>
7 #include <iostream> // IWYU pragma: keep (usage in Windows-only code)
8
9 #include <driver/Driver.h>
10 #include <exception/CompilerError.h>
11 #include <exception/LinkerError.h>
12 #include <util/CommonUtil.h>
13
14 namespace spice::compiler {
15
16 /**
17 * Creates a file and writes fileContent to it.
18 *
19 * @param filePath File path
20 * @param fileContent String to write into the file
21 */
22 void FileUtil::writeToFile(const std::filesystem::path &filePath, const std::string &fileContent) {
23 std::ofstream file(filePath);
24 if (!file)
25 throw CompilerError(IO_ERROR, "Failed to open file: " + filePath.string());
26 file << fileContent;
27 file.flush();
28 file.close();
29 }
30
31 /**
32 * Retrieve the contents of a file as a string
33 *
34 * @param filePath File path
35 * @return File contents as a string
36 */
37 600 std::string FileUtil::getFileContent(const std::filesystem::path &filePath) {
38
1/2
✓ Branch 2 → 3 taken 600 times.
✗ Branch 2 → 35 not taken.
600 std::ifstream file(filePath);
39
2/4
✓ Branch 3 → 4 taken 600 times.
✗ Branch 3 → 33 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 12 taken 600 times.
600 if (!file)
40 throw CompilerError(IO_ERROR, "Failed to open file: " + filePath.string());
41
1/2
✓ Branch 12 → 13 taken 600 times.
✗ Branch 12 → 33 not taken.
600 std::stringstream stringStream;
42
1/2
✓ Branch 14 → 15 taken 600 times.
✗ Branch 14 → 31 not taken.
600 stringStream << file.rdbuf();
43
1/2
✓ Branch 15 → 16 taken 600 times.
✗ Branch 15 → 31 not taken.
600 file.close();
44
1/2
✓ Branch 16 → 17 taken 600 times.
✗ Branch 16 → 31 not taken.
1200 return stringStream.str();
45 600 }
46
47 /**
48 * Retrieve the number of lines in a file
49 *
50 * @param filePath File path
51 * @return Number of lines
52 */
53 size_t FileUtil::getLineCount(const std::filesystem::path &filePath) {
54 std::ifstream file(filePath);
55 if (!file)
56 throw CompilerError(IO_ERROR, "Failed to open file: " + filePath.string());
57 size_t lineCount = 0;
58 std::string line;
59 while (std::getline(file, line))
60 lineCount++;
61 file.close();
62 return lineCount;
63 }
64
65 /**
66 * Execute external command. Used to execute compiled binaries
67 *
68 * @param command Command to execute
69 * @param redirectStdErrToStdOut Redirect StdErr to StdOut
70 * @return Result struct
71 */
72 394 ExecResult FileUtil::exec(const std::string &command, bool redirectStdErrToStdOut) {
73 #if OS_UNIX
74
1/2
✓ Branch 2 → 3 taken 394 times.
✗ Branch 2 → 41 not taken.
394 std::string redirectedCommand = command;
75
2/2
✓ Branch 3 → 4 taken 197 times.
✓ Branch 3 → 5 taken 197 times.
394 if (redirectStdErrToStdOut)
76
1/2
✓ Branch 4 → 5 taken 197 times.
✗ Branch 4 → 39 not taken.
197 redirectedCommand += " 2>&1"; // Redirect stderr to stdout
77
1/2
✓ Branch 6 → 7 taken 394 times.
✗ Branch 6 → 39 not taken.
394 FILE *pipe = popen(redirectedCommand.c_str(), "r");
78 #elif OS_WINDOWS
79 std::string redirectedCommand = command;
80 if (redirectStdErrToStdOut)
81 redirectedCommand = "\"" + command + " 2>&1\""; // Redirect stderr to stdout
82 FILE *pipe = _popen(redirectedCommand.c_str(), "r");
83 #else
84 #error "Unsupported platform"
85 #endif
86
87 if (!pipe) // GCOV_EXCL_LINE
88 throw CompilerError(IO_ERROR, "Failed to execute command: " + command); // GCOV_EXCL_LINE
89
90 394 std::array<char, 128> buffer{};
91
1/2
✓ Branch 13 → 14 taken 394 times.
✗ Branch 13 → 39 not taken.
394 std::stringstream result;
92
3/4
✓ Branch 22 → 23 taken 6818 times.
✗ Branch 22 → 37 not taken.
✓ Branch 23 → 15 taken 6424 times.
✓ Branch 23 → 24 taken 394 times.
14030 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
93
1/2
✓ Branch 17 → 18 taken 6424 times.
✗ Branch 17 → 37 not taken.
6424 result << buffer.data();
94
95 #if OS_UNIX
96
1/2
✓ Branch 24 → 25 taken 394 times.
✗ Branch 24 → 37 not taken.
394 const int exitCode = pclose(pipe) / 256;
97 #elif OS_WINDOWS
98 const int exitCode = _pclose(pipe);
99 #else
100 #error "Unsupported platform"
101 #endif
102 394 return {result.str(), exitCode};
103
1/2
✓ Branch 25 → 26 taken 394 times.
✗ Branch 25 → 37 not taken.
394 }
104
105 /**
106 * Checks if a certain command is available on the computer
107 *
108 * @param cmd Command to search for
109 * @return Present or not
110 */
111 bool FileUtil::isCommandAvailable(const std::string &cmd) {
112 #if OS_UNIX
113 const std::string checkCmd = "which " + cmd + " > /dev/null 2>&1";
114 #elif OS_WINDOWS
115 const std::string checkCmd = "where " + cmd + " > nul 2>&1";
116 #else
117 #error "Unsupported platform"
118 #endif
119 return std::system(checkCmd.c_str()) == 0;
120 }
121
122 /**
123 * Checks if Graphviz is installed on the system
124 *
125 * @return Present or not
126 */
127 bool FileUtil::isGraphvizInstalled() { return std::system("dot -V") == 0; }
128
129 /**
130 * Search for a supported linker invoker on the system and return the executable name or path.
131 * This function may throw a LinkerError if no linker invoker is found.
132 *
133 * @return Name and path to the linker invoker executable
134 */
135 197 ExternalBinaryFinderResult FileUtil::findLinkerInvoker() {
136 #if OS_UNIX
137
1/2
✓ Branch 26 → 4 taken 197 times.
✗ Branch 26 → 27 not taken.
197 for (const char *linkerInvokerName : {"clang", "gcc"})
138
2/4
✓ Branch 8 → 9 taken 197 times.
✗ Branch 8 → 36 not taken.
✓ Branch 23 → 6 taken 197 times.
✗ Branch 23 → 24 not taken.
394 for (const std::string path : {"/usr/bin/", "/usr/local/bin/", "/bin/"})
139
4/8
✓ Branch 10 → 11 taken 197 times.
✗ Branch 10 → 43 not taken.
✓ Branch 11 → 12 taken 197 times.
✗ Branch 11 → 41 not taken.
✓ Branch 12 → 13 taken 197 times.
✗ Branch 12 → 39 not taken.
✓ Branch 15 → 16 taken 197 times.
✗ Branch 15 → 18 not taken.
197 if (std::filesystem::exists(path + linkerInvokerName))
140
2/4
✓ Branch 16 → 17 taken 197 times.
✗ Branch 16 → 45 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 25 taken 197 times.
394 return ExternalBinaryFinderResult{linkerInvokerName, path + linkerInvokerName};
141 #elif OS_WINDOWS
142 for (const char *linkerInvokerName : {"clang", "gcc"})
143 if (isCommandAvailable(std::string(linkerInvokerName) + " -v"))
144 return ExternalBinaryFinderResult{linkerInvokerName, linkerInvokerName};
145 #else
146 #error "Unsupported platform"
147 #endif
148 const auto msg = "No supported linker invoker was found on the system. Supported are: clang and gcc"; // LCOV_EXCL_LINE
149 throw LinkerError(LINKER_NOT_FOUND, msg); // LCOV_EXCL_LINE
150 }
151
152 /**
153 * Search for a supported linker on the system and return the executable name or path.
154 * This function may throw a LinkerError if no linker is found.
155 *
156 * @return Name and path to the linker executable
157 */
158 197 ExternalBinaryFinderResult FileUtil::findLinker([[maybe_unused]] const CliOptions &cliOptions) {
159 #if OS_UNIX
160 197 std::vector<const char *> linkerList;
161
1/2
✓ Branch 2 → 3 taken 197 times.
✗ Branch 2 → 75 not taken.
197 linkerList.reserve(5);
162 // mold does only support linking for unix and darwin
163
2/4
✓ Branch 3 → 4 taken 197 times.
✗ Branch 3 → 75 not taken.
✓ Branch 4 → 5 taken 197 times.
✗ Branch 4 → 7 not taken.
197 if (cliOptions.targetOs != "windows")
164
1/2
✓ Branch 5 → 6 taken 197 times.
✗ Branch 5 → 48 not taken.
197 linkerList.push_back("mold");
165
1/2
✓ Branch 7 → 8 taken 197 times.
✗ Branch 7 → 49 not taken.
197 linkerList.push_back("ld.lld");
166
1/2
✓ Branch 8 → 9 taken 197 times.
✗ Branch 8 → 50 not taken.
197 linkerList.push_back("ld64.ddl");
167
1/2
✓ Branch 9 → 10 taken 197 times.
✗ Branch 9 → 51 not taken.
197 linkerList.push_back("gold");
168
1/2
✓ Branch 10 → 11 taken 197 times.
✗ Branch 10 → 52 not taken.
197 linkerList.push_back("ld");
169
170
1/2
✓ Branch 38 → 13 taken 197 times.
✗ Branch 38 → 39 not taken.
197 for (const char *linkerName : linkerList)
171
2/4
✓ Branch 18 → 19 taken 394 times.
✗ Branch 18 → 53 not taken.
✓ Branch 33 → 16 taken 394 times.
✗ Branch 33 → 34 not taken.
788 for (const std::string path : {"/usr/bin/", "/usr/local/bin/", "/bin/"})
172
5/8
✓ Branch 20 → 21 taken 394 times.
✗ Branch 20 → 60 not taken.
✓ Branch 21 → 22 taken 394 times.
✗ Branch 21 → 58 not taken.
✓ Branch 22 → 23 taken 394 times.
✗ Branch 22 → 56 not taken.
✓ Branch 25 → 26 taken 197 times.
✓ Branch 25 → 28 taken 197 times.
394 if (std::filesystem::exists(path + linkerName))
173
3/4
✓ Branch 26 → 27 taken 197 times.
✗ Branch 26 → 62 not taken.
✓ Branch 30 → 31 taken 197 times.
✓ Branch 30 → 35 taken 197 times.
591 return ExternalBinaryFinderResult{linkerName, path + linkerName};
174 #elif OS_WINDOWS
175 for (const char *linkerName : {"lld", "ld"})
176 if (isCommandAvailable(std::string(linkerName) + " -v"))
177 return ExternalBinaryFinderResult{linkerName, linkerName};
178 #else
179 #error "Unsupported platform"
180 #endif
181 const auto msg = "No supported linker was found on the system. Supported are: mold, lld, gold and ld"; // LCOV_EXCL_LINE
182 throw LinkerError(LINKER_NOT_FOUND, msg); // LCOV_EXCL_LINE
183 197 }
184
185 /**
186 * Retrieve the dir, where the standard library lives.
187 * Returns an empty string if the std was not found.
188 *
189 * @return Std directory
190 */
191 880 std::filesystem::path FileUtil::getStdDir() {
192 #if OS_UNIX
193
3/6
✓ Branch 2 → 3 taken 880 times.
✗ Branch 2 → 25 not taken.
✓ Branch 3 → 4 taken 880 times.
✗ Branch 3 → 23 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 880 times.
880 if (exists(std::filesystem::path("/usr/lib/spice/std/")))
194 return {"/usr/lib/spice/std/"};
195 #endif
196
1/2
✓ Branch 8 → 9 taken 880 times.
✗ Branch 8 → 21 not taken.
880 if (std::getenv("SPICE_STD_DIR"))
197
3/6
✓ Branch 10 → 11 taken 880 times.
✗ Branch 10 → 26 not taken.
✓ Branch 11 → 12 taken 880 times.
✗ Branch 11 → 27 not taken.
✓ Branch 12 → 13 taken 880 times.
✗ Branch 12 → 15 not taken.
880 if (const std::filesystem::path stdPath(std::getenv("SPICE_STD_DIR")); exists(stdPath))
198
2/4
✓ Branch 13 → 14 taken 880 times.
✗ Branch 13 → 27 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 880 times.
880 return stdPath;
199 return ""; // GCOV_EXCL_LINE
200 }
201
202 /**
203 * Retrieve the dir, where the bootstrap compiler lives.
204 * Returns an empty string if the bootstrap compiler was not found.
205 *
206 * @return
207 */
208 18 std::filesystem::path FileUtil::getBootstrapDir() {
209
1/2
✓ Branch 3 → 4 taken 18 times.
✗ Branch 3 → 13 not taken.
18 if (std::getenv("SPICE_BOOTSTRAP_DIR")) {
210
3/8
✓ Branch 5 → 6 taken 18 times.
✗ Branch 5 → 16 not taken.
✓ Branch 6 → 7 taken 18 times.
✗ Branch 6 → 17 not taken.
✓ Branch 7 → 8 taken 18 times.
✗ Branch 7 → 9 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
18 if (const std::filesystem::path stdPath(std::getenv("SPICE_BOOTSTRAP_DIR")); exists(stdPath))
211 18 return stdPath;
212 }
213 return ""; // GCOV_EXCL_LINE
214 }
215
216 /**
217 * Retrieve the dir, where output binaries should go when installing them
218 *
219 * @return Installation directory
220 */
221 2 std::filesystem::path FileUtil::getSpiceBinDir() {
222 #if OS_UNIX
223 2 return "/usr/local/bin/";
224 #elif OS_WINDOWS
225 return std::filesystem::path(std::getenv("USERPROFILE")) / "spice" / "bin";
226 #else
227 #error "Unsupported platform"
228 #endif
229 }
230
231 } // namespace spice::compiler
232