Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "Driver.h" | ||
4 | |||
5 | #include <exception/CliError.h> | ||
6 | #include <util/CommonUtil.h> | ||
7 | #include <util/CompilerWarning.h> | ||
8 | #include <util/FileUtil.h> | ||
9 | |||
10 | #include <llvm/Support/CommandLine.h> | ||
11 | #include <llvm/TargetParser/Host.h> | ||
12 | #include <llvm/TargetParser/Triple.h> | ||
13 | |||
14 | namespace spice::compiler { | ||
15 | |||
16 | 8 | void Driver::init() { | |
17 | // Allow positional args | ||
18 | 8 | app.positionals_at_end(); | |
19 | 8 | app.allow_extras(false); | |
20 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | app.footer("(c) Marc Auberer 2021-2024"); |
21 | |||
22 | // Add version flag | ||
23 |
4/8✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
|
32 | app.set_version_flag("--version,-v", CommonUtil::getVersionInfo()); |
24 | |||
25 | // Create sub-commands | ||
26 | 8 | addBuildSubcommand(); | |
27 | 8 | addRunSubcommand(); | |
28 | 8 | addTestSubcommand(); | |
29 | 8 | addInstallSubcommand(); | |
30 | 8 | addUninstallSubcommand(); | |
31 | |||
32 | 8 | app.final_callback([&] { | |
33 | // Print help text for the root command if no sub-command was given | ||
34 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
|
8 | if (app.get_subcommands().empty()) { |
35 | ✗ | std::cout << app.help(); | |
36 | ✗ | return; | |
37 | } | ||
38 | |||
39 |
4/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
|
8 | if (shouldInstall || shouldUninstall) { |
40 | // Prepare the installation path | ||
41 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | std::filesystem::path installPath = FileUtil::getSpiceBinDir(); |
42 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | installPath /= cliOptions.mainSourceFile.stem(); |
43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!dryRun) |
44 | ✗ | create_directories(installPath); | |
45 | #if OS_WINDOWS | ||
46 | installPath.replace_extension("exe"); | ||
47 | #endif | ||
48 | |||
49 | // If the binary should be installed, set the output path to the Spice bin directory | ||
50 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (shouldInstall) |
51 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | cliOptions.outputPath = installPath; |
52 | |||
53 | // If the binary should be uninstalled, check if the executable exists and uninstall it | ||
54 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | if (shouldUninstall && !dryRun) { |
55 | ✗ | if (exists(installPath) && std::filesystem::remove(installPath)) | |
56 | ✗ | std::cout << "Successfully uninstalled.\n"; | |
57 | else | ||
58 | ✗ | CompilerWarning(UNINSTALL_FAILED, "The executable was not found at the expected location").print(); | |
59 | } | ||
60 | 2 | } | |
61 | |||
62 | // Abort here if we do not need to compile | ||
63 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (!shouldCompile) |
64 | 1 | return; | |
65 | |||
66 | // Set output path and dir | ||
67 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
|
7 | if (shouldExecute) { |
68 | 4 | cliOptions.execute = true; | |
69 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | const auto millis = duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
70 |
8/16✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 4 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 4 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 4 times.
✗ Branch 23 not taken.
|
4 | cliOptions.outputDir = std::filesystem::temp_directory_path() / "spice" / "output" / std::to_string(millis); |
71 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename(); |
72 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | } else if (!cliOptions.outputPath.empty()) { |
73 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (is_directory(cliOptions.outputPath)) { |
74 | ✗ | cliOptions.outputDir = cliOptions.outputPath; | |
75 | ✗ | cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename(); | |
76 | } else { | ||
77 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | cliOptions.outputDir = cliOptions.outputPath.parent_path(); |
78 | } | ||
79 | } else { | ||
80 | 2 | cliOptions.outputDir = "./"; | |
81 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename(); |
82 | } | ||
83 | |||
84 | // Set output file extension | ||
85 |
3/6✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 7 times.
|
7 | if (cliOptions.targetArch == TARGET_WASM32 || cliOptions.targetArch == TARGET_WASM64) { |
86 | ✗ | cliOptions.outputPath.replace_extension("wasm"); | |
87 | } else { | ||
88 | #if OS_WINDOWS | ||
89 | cliOptions.outputPath.replace_extension("exe"); | ||
90 | #else | ||
91 |
2/4✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
7 | cliOptions.outputPath.replace_extension(""); |
92 | #endif | ||
93 | } | ||
94 | |||
95 | // Set cache dir | ||
96 |
5/10✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 7 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 7 times.
✗ Branch 14 not taken.
|
7 | cliOptions.cacheDir = std::filesystem::temp_directory_path() / "spice" / "cache"; |
97 | |||
98 | // Create directories in case they not exist yet | ||
99 | 7 | create_directories(cliOptions.cacheDir); | |
100 | 7 | create_directories(cliOptions.outputDir); | |
101 | }); | ||
102 | 8 | } | |
103 | |||
104 | /** | ||
105 | * Start the parsing process | ||
106 | * | ||
107 | * @param argc Argument count | ||
108 | * @param argv Argument vector | ||
109 | * @return Return code | ||
110 | */ | ||
111 | 8 | int Driver::parse(int argc, const char *argv[]) { | |
112 | try { | ||
113 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | app.parse(argc, argv); |
114 | 8 | return EXIT_SUCCESS; | |
115 | ✗ | } catch (const CLI::ParseError &parseError) { | |
116 | ✗ | return app.exit(parseError); | |
117 | ✗ | } | |
118 | } | ||
119 | |||
120 | /** | ||
121 | * Initialize the cli options based on the input of the user | ||
122 | */ | ||
123 | 8 | void Driver::enrich() { | |
124 | // Make path of given main source file canonical and relative | ||
125 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | cliOptions.mainSourceFile = relative(cliOptions.mainSourceFile); |
126 | |||
127 | // Propagate llvm args to llvm | ||
128 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | if (!cliOptions.llvmArgs.empty()) { |
129 | ✗ | const std::vector<std::string> result = CommonUtil::split("llvm " + cliOptions.llvmArgs); | |
130 | ✗ | std::vector<const char *> resultCStr; | |
131 | ✗ | resultCStr.reserve(result.size()); | |
132 | ✗ | for (const std::string &str : result) | |
133 | ✗ | resultCStr.push_back(str.c_str()); | |
134 | ✗ | llvm::cl::ParseCommandLineOptions(static_cast<int>(result.size()), resultCStr.data()); | |
135 | ✗ | } | |
136 | |||
137 | // Propagate target information | ||
138 |
4/8✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 8 times.
✗ Branch 12 not taken.
|
8 | const llvm::Triple defaultTriple(llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple())); |
139 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | if (cliOptions.targetTriple.empty()) { |
140 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
|
8 | if (cliOptions.targetArch == TARGET_UNKNOWN) { // We have nothing -> obtain native triplet |
141 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | cliOptions.targetTriple = defaultTriple.getTriple(); |
142 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | cliOptions.targetArch = defaultTriple.getArchName(); |
143 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | cliOptions.targetVendor = defaultTriple.getVendorName(); |
144 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | cliOptions.targetOs = defaultTriple.getOSName(); |
145 | 8 | cliOptions.isNativeTarget = true; | |
146 | } else { // We have arch, vendor and os -> obtain triplet | ||
147 | ✗ | const llvm::Triple triple(cliOptions.targetArch, cliOptions.targetVendor, cliOptions.targetOs); | |
148 | ✗ | cliOptions.targetTriple = triple.getTriple(); | |
149 | ✗ | cliOptions.isNativeTarget = triple == defaultTriple; | |
150 | ✗ | } | |
151 | } else { // Obtain arch, vendor and os by the triplet | ||
152 | ✗ | const llvm::Triple triple(llvm::Triple::normalize(cliOptions.targetTriple)); | |
153 | ✗ | cliOptions.targetArch = triple.getArchName(); | |
154 | ✗ | cliOptions.targetVendor = triple.getVendorName(); | |
155 | ✗ | cliOptions.targetOs = triple.getOSName(); | |
156 | ✗ | cliOptions.isNativeTarget = triple == defaultTriple; | |
157 | ✗ | } | |
158 | |||
159 | // Always preserve IR value names when dumping IR | ||
160 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (cliOptions.dumpSettings.dumpIR) |
161 | 1 | cliOptions.namesForIRValues = true; | |
162 | |||
163 | // Enable test mode when test mode was selected | ||
164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (cliOptions.buildMode == TEST) { |
165 | ✗ | cliOptions.testMode = true; | |
166 | ✗ | cliOptions.noEntryFct = true; | |
167 | ✗ | cliOptions.generateTestMain = true; | |
168 | ✗ | cliOptions.buildMode = DEBUG; | |
169 | } | ||
170 | 8 | } | |
171 | |||
172 | /** | ||
173 | * Executes the built executable | ||
174 | */ | ||
175 | ✗ | void Driver::runBinary() const { | |
176 | // Print status message | ||
177 | ✗ | if (cliOptions.printDebugOutput) | |
178 | ✗ | std::cout << "Running executable ...\n\n"; | |
179 | |||
180 | // Run executable | ||
181 | ✗ | std::filesystem::path executablePath = cliOptions.outputPath; | |
182 | ✗ | executablePath.make_preferred(); | |
183 | ✗ | const int exitCode = std::system(executablePath.string().c_str()) / 256; | |
184 | ✗ | if (exitCode != 0) | |
185 | ✗ | throw CliError(NON_ZERO_EXIT_CODE, "Your Spice executable exited with non-zero exit code " + std::to_string(exitCode)); | |
186 | ✗ | } | |
187 | |||
188 | /** | ||
189 | * Add build subcommand to cli interface | ||
190 | */ | ||
191 | 8 | void Driver::addBuildSubcommand() { | |
192 | // Create sub-command itself | ||
193 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("build", "Builds your Spice program and emits an executable"); |
194 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | subCmd->alias("b"); |
195 | 8 | subCmd->ignore_case(); | |
196 | 8 | subCmd->configurable(); | |
197 | 8 | subCmd->callback([&] { | |
198 | 2 | shouldCompile = true; // Requires the source file to be compiled | |
199 | 2 | }); | |
200 | |||
201 | 8 | addCompileSubcommandOptions(subCmd); | |
202 | |||
203 | // --target-triple | ||
204 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_option<std::string>("--target,-t,--target-triple", cliOptions.targetTriple, |
205 | "Target triple for the emitted executable (for cross-compiling)"); | ||
206 | // --target-arch | ||
207 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_option<std::string>("--target-arch", cliOptions.targetArch, |
208 | "Target arch for emitted executable (for cross-compiling)"); | ||
209 | // --target-vendor | ||
210 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_option<std::string>("--target-vendor", cliOptions.targetVendor, |
211 | "Target vendor for emitted executable (for cross-compiling)"); | ||
212 | // --target-os | ||
213 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_option<std::string>("--target-os", cliOptions.targetOs, "Target os for emitted executable (for cross-compiling)"); |
214 | |||
215 | // --output | ||
216 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_option<std::filesystem::path>("--output,-o", cliOptions.outputPath, "Set the output file path"); |
217 | // --debug-info | ||
218 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info"); |
219 | // --disable-verifier | ||
220 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification"); |
221 | // --no-entry | ||
222 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--no-entry", cliOptions.noEntryFct, "Do not generate main function"); |
223 | // --static | ||
224 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--static", cliOptions.staticLinking, "Link statically"); |
225 | // --dump-to-files | ||
226 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--dump-to-files", cliOptions.dumpSettings.dumpToFiles, "Redirect dumps to files instead of printing"); |
227 | // --abort-after-dump | ||
228 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--abort-after-dump", cliOptions.dumpSettings.abortAfterDump, |
229 | "Abort the compilation process after dumping the first requested resource"); | ||
230 | 8 | } | |
231 | |||
232 | /** | ||
233 | * Add run subcommand to cli interface | ||
234 | */ | ||
235 | 8 | void Driver::addRunSubcommand() { | |
236 | // Create sub-command itself | ||
237 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("run", "Builds your Spice program and runs it immediately"); |
238 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | subCmd->alias("r"); |
239 | 8 | subCmd->ignore_case(); | |
240 | 8 | subCmd->callback([&] { | |
241 | 2 | shouldCompile = shouldExecute = true; // Requires the source file to be compiled | |
242 | 2 | }); | |
243 | |||
244 | 8 | addCompileSubcommandOptions(subCmd); | |
245 | |||
246 | // --debug-info | ||
247 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info"); |
248 | // --disable-verifier | ||
249 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification"); |
250 | 8 | } | |
251 | |||
252 | /** | ||
253 | * Add test subcommand to cli interface | ||
254 | */ | ||
255 | 8 | void Driver::addTestSubcommand() { | |
256 | // Create sub-command itself | ||
257 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("test", "Builds your Spice program and runs all enclosed tests"); |
258 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | subCmd->alias("t"); |
259 | 8 | subCmd->ignore_case(); | |
260 | 8 | subCmd->callback([&] { | |
261 | 2 | shouldCompile = shouldExecute = true; // Requires the source file to be compiled | |
262 | 2 | cliOptions.testMode = true; // Always enable assertions for tests, also in higher opt levels | |
263 | 2 | cliOptions.generateTestMain = true; // An alternative entry function is generated | |
264 | 2 | cliOptions.noEntryFct = true; // To not have two main functions, disable normal main | |
265 | 2 | }); | |
266 | |||
267 | 8 | addCompileSubcommandOptions(subCmd); | |
268 | |||
269 | // --debug-info | ||
270 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info"); |
271 | // --disable-verifier | ||
272 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification"); |
273 | 8 | } | |
274 | |||
275 | /** | ||
276 | * Add install subcommand to cli interface | ||
277 | */ | ||
278 | 8 | void Driver::addInstallSubcommand() { | |
279 | // Create sub-command itself | ||
280 | CLI::App *subCmd = | ||
281 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | app.add_subcommand("install", "Builds your Spice program and installs it to a directory in the PATH variable"); |
282 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | subCmd->alias("i"); |
283 | 8 | subCmd->ignore_case(); | |
284 | 8 | subCmd->callback([&] { | |
285 | 1 | shouldCompile = true; | |
286 | 1 | shouldInstall = true; | |
287 | 1 | ensureNotDockerized(); | |
288 | 1 | }); | |
289 | |||
290 | 8 | addCompileSubcommandOptions(subCmd); | |
291 | 8 | } | |
292 | |||
293 | /** | ||
294 | * Add uninstall subcommand to cli interface | ||
295 | */ | ||
296 | 8 | void Driver::addUninstallSubcommand() { | |
297 | // Create sub-command itself | ||
298 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("uninstall", "Builds your Spice program and runs it immediately"); |
299 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | subCmd->alias("u"); |
300 | 8 | subCmd->ignore_case(); | |
301 | 8 | subCmd->callback([&] { | |
302 | 1 | shouldUninstall = true; | |
303 | 1 | ensureNotDockerized(); | |
304 | 1 | }); | |
305 | |||
306 | // Source file | ||
307 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file") |
308 |
4/8✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
|
48 | ->check(CLI::ExistingFile) |
309 | 8 | ->required(); | |
310 | 8 | } | |
311 | |||
312 | 32 | void Driver::addCompileSubcommandOptions(CLI::App *subCmd) { | |
313 | 65 | const std::function buildModeCallback = [&](const CLI::results_t &results) { | |
314 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | std::string inputString = results.front(); |
315 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | std::ranges::transform(inputString, inputString.begin(), tolower); |
316 | |||
317 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (inputString == BUILD_MODE_DEBUG) |
318 | ✗ | cliOptions.buildMode = DEBUG; | |
319 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | else if (inputString == BUILD_MODE_RELEASE) |
320 | 1 | cliOptions.buildMode = RELEASE; | |
321 | ✗ | else if (inputString == BUILD_MODE_TEST) | |
322 | ✗ | cliOptions.buildMode = TEST; | |
323 | else | ||
324 | ✗ | throw CliError(INVALID_BUILD_MODE, "Invalid build mode: " + inputString); | |
325 | |||
326 | 1 | return true; | |
327 | 33 | }; | |
328 | |||
329 | // --build-mode | ||
330 |
4/8✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 32 times.
✗ Branch 12 not taken.
|
160 | subCmd->add_option("--build-mode,-m", buildModeCallback, "Build mode (debug, release, test)"); |
331 | // --llvm-args | ||
332 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_option<std::string>("--llvm-args,-llvm", cliOptions.llvmArgs, "Additional arguments for LLVM")->join(' '); |
333 | // --jobs | ||
334 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_option<unsigned short>("--jobs,-j", cliOptions.compileJobCount, "Compile jobs (threads), used for compilation"); |
335 | // --ignore-cache | ||
336 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--ignore-cache", cliOptions.ignoreCache, "Force re-compilation of all source files"); |
337 | // --use-lifetime-markers | ||
338 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--use-lifetime-markers", cliOptions.useLifetimeMarkers, |
339 | "Generate lifetime markers to enhance optimizations"); | ||
340 | |||
341 | // Opt levels | ||
342 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
|
128 | subCmd->add_flag_callback("-O0", [&] { cliOptions.optLevel = O0; }, "Disable optimization for the output executable."); |
343 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
|
128 | subCmd->add_flag_callback("-O1", [&] { cliOptions.optLevel = O1; }, "Only basic optimization is executed."); |
344 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
|
128 | subCmd->add_flag_callback("-O2", [&] { cliOptions.optLevel = O2; }, "More advanced optimization is applied."); |
345 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
|
128 | subCmd->add_flag_callback("-O3", [&] { cliOptions.optLevel = O3; }, "Aggressive optimization for best performance."); |
346 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
|
128 | subCmd->add_flag_callback("-Os", [&] { cliOptions.optLevel = Os; }, "Size optimization for output executable."); |
347 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
|
128 | subCmd->add_flag_callback("-Oz", [&] { cliOptions.optLevel = Oz; }, "Aggressive optimization for best size."); |
348 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("-lto", cliOptions.useLTO, "Enable link time optimization (LTO)"); |
349 | |||
350 | // --debug-output | ||
351 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--debug-output,-d", cliOptions.printDebugOutput, "Enable debug output"); |
352 | // --dump-cst | ||
353 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--dump-cst,-cst", cliOptions.dumpSettings.dumpCST, "Dump CST as serialized string and SVG image"); |
354 | // --dump-ast | ||
355 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--dump-ast,-ast", cliOptions.dumpSettings.dumpAST, "Dump AST as serialized string and SVG image"); |
356 | // --dump-symtab | ||
357 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--dump-symtab,-symtab", cliOptions.dumpSettings.dumpSymbolTable, "Dump serialized symbol tables"); |
358 | // --dump-types | ||
359 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--dump-types,-types", cliOptions.dumpSettings.dumpTypes, "Dump all used types"); |
360 | // --dump-ir | ||
361 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--dump-ir,-ir", cliOptions.dumpSettings.dumpIR, "Dump LLVM-IR"); |
362 | // --dump-assembly | ||
363 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--dump-assembly,-asm,-s", cliOptions.dumpSettings.dumpAssembly, "Dump Assembly code"); |
364 | // --dump-object-file | ||
365 |
3/6✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
|
128 | subCmd->add_flag<bool>("--dump-object-file,-obj", cliOptions.dumpSettings.dumpObjectFile, "Dump object file"); |
366 | |||
367 | // Source file | ||
368 |
2/4✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
|
64 | subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file") |
369 |
4/8✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 32 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 32 times.
✗ Branch 11 not taken.
|
192 | ->check(CLI::ExistingFile) |
370 | 32 | ->required(); | |
371 | 32 | } | |
372 | |||
373 | /** | ||
374 | * Ensure that the compiler is not running in a Docker container | ||
375 | */ | ||
376 | 2 | void Driver::ensureNotDockerized() { | |
377 | 2 | const char *envValue = std::getenv(ENV_VAR_DOCKERIZED); | |
378 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (envValue != nullptr && std::strcmp(envValue, "true") == 0) |
379 | ✗ | throw CliError(FEATURE_NOT_SUPPORTED_WHEN_DOCKERIZED, | |
380 | ✗ | "This feature is not supported in a containerized environment. Please use the standalone version of Spice."); | |
381 | 2 | } | |
382 | |||
383 | } // namespace spice::compiler | ||
384 |