Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 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 0 (6→7) taken 8 times.
✗ Branch 1 (6→32) not taken.
|
16 | app.footer("(c) Marc Auberer 2021-2025"); |
21 | |||
22 | // Add version flag | ||
23 |
4/8✓ Branch 0 (12→13) taken 8 times.
✗ Branch 1 (12→47) not taken.
✓ Branch 2 (13→14) taken 8 times.
✗ Branch 3 (13→44) not taken.
✓ Branch 4 (16→17) taken 8 times.
✗ Branch 5 (16→38) not taken.
✓ Branch 6 (17→18) taken 8 times.
✗ Branch 7 (17→36) not taken.
|
32 | app.set_version_flag("--version,-v", CommonUtil::buildVersionInfo()); |
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 0 (2→3) taken 8 times.
✗ Branch 1 (2→127) not taken.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→15) taken 8 times.
|
8 | if (app.get_subcommands().empty()) { |
35 | ✗ | std::cout << app.help(); | |
36 | ✗ | return; | |
37 | } | ||
38 | |||
39 |
4/4✓ Branch 0 (15→16) taken 7 times.
✓ Branch 1 (15→17) taken 1 times.
✓ Branch 2 (16→17) taken 1 times.
✓ Branch 3 (16→46) taken 6 times.
|
8 | if (shouldInstall || shouldUninstall) { |
40 | // Prepare the installation path | ||
41 |
1/2✓ Branch 0 (17→18) taken 2 times.
✗ Branch 1 (17→151) not taken.
|
2 | std::filesystem::path installPath = FileUtil::getSpiceBinDir(); |
42 |
2/4✓ Branch 0 (18→19) taken 2 times.
✗ Branch 1 (18→139) not taken.
✓ Branch 2 (19→20) taken 2 times.
✗ Branch 3 (19→137) not taken.
|
2 | installPath /= cliOptions.mainSourceFile.stem(); |
43 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) 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 (23→24) taken 1 times.
✓ Branch 1 (23→25) taken 1 times.
|
2 | if (shouldInstall) |
51 |
1/2✓ Branch 0 (24→25) taken 1 times.
✗ Branch 1 (24→149) 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 (25→26) taken 1 times.
✓ Branch 1 (25→44) taken 1 times.
✗ Branch 2 (26→27) not taken.
✓ Branch 3 (26→44) 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 (46→47) taken 1 times.
✓ Branch 1 (46→48) taken 7 times.
|
8 | if (!shouldCompile) |
64 | 1 | return; | |
65 | |||
66 | // Set output path and dir | ||
67 |
2/2✓ Branch 0 (48→49) taken 4 times.
✓ Branch 1 (48→76) taken 3 times.
|
7 | if (shouldExecute) { |
68 | 4 | cliOptions.execute = true; | |
69 |
1/2✓ Branch 0 (51→52) taken 4 times.
✗ Branch 1 (51→152) not taken.
|
4 | const long millis = duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
70 |
8/16✓ Branch 0 (53→54) taken 4 times.
✗ Branch 1 (53→174) not taken.
✓ Branch 2 (54→55) taken 4 times.
✗ Branch 3 (54→172) not taken.
✓ Branch 4 (55→56) taken 4 times.
✗ Branch 5 (55→168) not taken.
✓ Branch 6 (56→57) taken 4 times.
✗ Branch 7 (56→164) not taken.
✓ Branch 8 (57→58) taken 4 times.
✗ Branch 9 (57→161) not taken.
✓ Branch 10 (58→59) taken 4 times.
✗ Branch 11 (58→159) not taken.
✓ Branch 12 (59→60) taken 4 times.
✗ Branch 13 (59→157) not taken.
✓ Branch 14 (60→61) taken 4 times.
✗ Branch 15 (60→155) not taken.
|
4 | cliOptions.outputDir = std::filesystem::temp_directory_path() / "spice" / "output" / std::to_string(millis); |
71 |
2/4✓ Branch 0 (70→71) taken 4 times.
✗ Branch 1 (70→179) not taken.
✓ Branch 2 (71→72) taken 4 times.
✗ Branch 3 (71→177) not taken.
|
4 | cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename(); |
72 |
2/2✓ Branch 0 (77→78) taken 1 times.
✓ Branch 1 (77→91) taken 2 times.
|
3 | } else if (!cliOptions.outputPath.empty()) { |
73 |
1/2✗ Branch 0 (79→80) not taken.
✓ Branch 1 (79→87) 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 0 (87→88) taken 1 times.
✗ Branch 1 (87→185) not taken.
|
1 | cliOptions.outputDir = cliOptions.outputPath.parent_path(); |
78 | } | ||
79 | } else { | ||
80 | 2 | cliOptions.outputDir = "./"; | |
81 |
2/4✓ Branch 0 (92→93) taken 2 times.
✗ Branch 1 (92→188) not taken.
✓ Branch 2 (93→94) taken 2 times.
✗ Branch 3 (93→186) not taken.
|
2 | cliOptions.outputPath = cliOptions.outputDir / cliOptions.mainSourceFile.filename(); |
82 | } | ||
83 | |||
84 | // Set output file extension | ||
85 |
3/6✓ Branch 0 (99→100) taken 7 times.
✗ Branch 1 (99→102) not taken.
✗ Branch 2 (101→102) not taken.
✓ Branch 3 (101→103) taken 7 times.
✗ Branch 4 (104→105) not taken.
✓ Branch 5 (104→109) 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 0 (109→110) taken 7 times.
✗ Branch 1 (109→195) not taken.
✓ Branch 2 (110→111) taken 7 times.
✗ Branch 3 (110→193) not taken.
|
7 | cliOptions.outputPath.replace_extension(""); |
92 | #endif | ||
93 | } | ||
94 | |||
95 | // Set cache dir | ||
96 |
5/10✓ Branch 0 (113→114) taken 7 times.
✗ Branch 1 (113→207) not taken.
✓ Branch 2 (114→115) taken 7 times.
✗ Branch 3 (114→203) not taken.
✓ Branch 4 (115→116) taken 7 times.
✗ Branch 5 (115→200) not taken.
✓ Branch 6 (116→117) taken 7 times.
✗ Branch 7 (116→198) not taken.
✓ Branch 8 (117→118) taken 7 times.
✗ Branch 9 (117→196) 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 0 (2→3) taken 8 times.
✗ Branch 1 (2→5) 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 0 (2→3) taken 8 times.
✗ Branch 1 (2→80) not taken.
✓ Branch 2 (3→4) taken 8 times.
✗ Branch 3 (3→78) not taken.
|
8 | cliOptions.mainSourceFile = relative(cliOptions.mainSourceFile); |
126 | |||
127 | // Propagate llvm args to llvm | ||
128 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→29) 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 0 (29→30) taken 8 times.
✗ Branch 1 (29→97) not taken.
✓ Branch 2 (31→32) taken 8 times.
✗ Branch 3 (31→95) not taken.
✓ Branch 4 (32→33) taken 8 times.
✗ Branch 5 (32→93) not taken.
✓ Branch 6 (33→34) taken 8 times.
✗ Branch 7 (33→93) not taken.
|
8 | const llvm::Triple defaultTriple(llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple())); |
139 |
1/2✓ Branch 0 (37→38) taken 8 times.
✗ Branch 1 (37→58) not taken.
|
8 | if (cliOptions.targetTriple.empty()) { |
140 |
2/4✓ Branch 0 (38→39) taken 8 times.
✗ Branch 1 (38→121) not taken.
✓ Branch 2 (39→40) taken 8 times.
✗ Branch 3 (39→49) not taken.
|
8 | if (cliOptions.targetArch == TARGET_UNKNOWN) { // We have nothing -> obtain native triplet |
141 |
1/2✓ Branch 0 (41→42) taken 8 times.
✗ Branch 1 (41→121) not taken.
|
8 | cliOptions.targetTriple = defaultTriple.getTriple(); |
142 |
2/4✓ Branch 0 (42→43) taken 8 times.
✗ Branch 1 (42→101) not taken.
✓ Branch 2 (43→44) taken 8 times.
✗ Branch 3 (43→101) not taken.
|
8 | cliOptions.targetArch = defaultTriple.getArchName(); |
143 |
2/4✓ Branch 0 (44→45) taken 8 times.
✗ Branch 1 (44→102) not taken.
✓ Branch 2 (45→46) taken 8 times.
✗ Branch 3 (45→102) not taken.
|
8 | cliOptions.targetVendor = defaultTriple.getVendorName(); |
144 |
2/4✓ Branch 0 (46→47) taken 8 times.
✗ Branch 1 (46→103) not taken.
✓ Branch 2 (47→48) taken 8 times.
✗ Branch 3 (47→103) 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 (72→73) taken 1 times.
✓ Branch 1 (72→74) 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 (74→75) not taken.
✓ Branch 1 (74→76) 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 0 (4→5) taken 8 times.
✗ Branch 1 (4→155) not taken.
✓ Branch 2 (7→8) taken 8 times.
✗ Branch 3 (7→149) not taken.
✓ Branch 4 (8→9) taken 8 times.
✗ Branch 5 (8→147) not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("build", "Builds your Spice program and emits an executable"); |
194 |
2/4✓ Branch 0 (15→16) taken 8 times.
✗ Branch 1 (15→161) not taken.
✓ Branch 2 (16→17) taken 8 times.
✗ Branch 3 (16→159) 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 0 (27→28) taken 8 times.
✗ Branch 1 (27→173) not taken.
✓ Branch 2 (30→31) taken 8 times.
✗ Branch 3 (30→167) not taken.
✓ Branch 4 (31→32) taken 8 times.
✗ Branch 5 (31→165) 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 0 (38→39) taken 8 times.
✗ Branch 1 (38→185) not taken.
✓ Branch 2 (41→42) taken 8 times.
✗ Branch 3 (41→179) not taken.
✓ Branch 4 (42→43) taken 8 times.
✗ Branch 5 (42→177) 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 0 (49→50) taken 8 times.
✗ Branch 1 (49→197) not taken.
✓ Branch 2 (52→53) taken 8 times.
✗ Branch 3 (52→191) not taken.
✓ Branch 4 (53→54) taken 8 times.
✗ Branch 5 (53→189) 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 0 (60→61) taken 8 times.
✗ Branch 1 (60→209) not taken.
✓ Branch 2 (63→64) taken 8 times.
✗ Branch 3 (63→203) not taken.
✓ Branch 4 (64→65) taken 8 times.
✗ Branch 5 (64→201) 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 0 (71→72) taken 8 times.
✗ Branch 1 (71→221) not taken.
✓ Branch 2 (74→75) taken 8 times.
✗ Branch 3 (74→215) not taken.
✓ Branch 4 (75→76) taken 8 times.
✗ Branch 5 (75→213) 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 0 (82→83) taken 8 times.
✗ Branch 1 (82→233) not taken.
✓ Branch 2 (85→86) taken 8 times.
✗ Branch 3 (85→227) not taken.
✓ Branch 4 (86→87) taken 8 times.
✗ Branch 5 (86→225) not taken.
|
32 | subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info"); |
219 | // --disable-verifier | ||
220 |
3/6✓ Branch 0 (93→94) taken 8 times.
✗ Branch 1 (93→245) not taken.
✓ Branch 2 (96→97) taken 8 times.
✗ Branch 3 (96→239) not taken.
✓ Branch 4 (97→98) taken 8 times.
✗ Branch 5 (97→237) not taken.
|
32 | subCmd->add_flag<bool>("--disable-verifier", cliOptions.disableVerifier, "Disable LLVM module and function verification"); |
221 | // --no-entry | ||
222 |
3/6✓ Branch 0 (104→105) taken 8 times.
✗ Branch 1 (104→257) not taken.
✓ Branch 2 (107→108) taken 8 times.
✗ Branch 3 (107→251) not taken.
✓ Branch 4 (108→109) taken 8 times.
✗ Branch 5 (108→249) not taken.
|
32 | subCmd->add_flag<bool>("--no-entry", cliOptions.noEntryFct, "Do not generate main function"); |
223 | // --static | ||
224 |
3/6✓ Branch 0 (115→116) taken 8 times.
✗ Branch 1 (115→269) not taken.
✓ Branch 2 (118→119) taken 8 times.
✗ Branch 3 (118→263) not taken.
✓ Branch 4 (119→120) taken 8 times.
✗ Branch 5 (119→261) not taken.
|
32 | subCmd->add_flag<bool>("--static", cliOptions.staticLinking, "Link statically"); |
225 | // --dump-to-files | ||
226 |
3/6✓ Branch 0 (126→127) taken 8 times.
✗ Branch 1 (126→281) not taken.
✓ Branch 2 (129→130) taken 8 times.
✗ Branch 3 (129→275) not taken.
✓ Branch 4 (130→131) taken 8 times.
✗ Branch 5 (130→273) 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 0 (137→138) taken 8 times.
✗ Branch 1 (137→293) not taken.
✓ Branch 2 (140→141) taken 8 times.
✗ Branch 3 (140→287) not taken.
✓ Branch 4 (141→142) taken 8 times.
✗ Branch 5 (141→285) 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 0 (4→5) taken 8 times.
✗ Branch 1 (4→55) not taken.
✓ Branch 2 (7→8) taken 8 times.
✗ Branch 3 (7→49) not taken.
✓ Branch 4 (8→9) taken 8 times.
✗ Branch 5 (8→47) not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("run", "Builds your Spice program and runs it immediately"); |
238 |
2/4✓ Branch 0 (15→16) taken 8 times.
✗ Branch 1 (15→61) not taken.
✓ Branch 2 (16→17) taken 8 times.
✗ Branch 3 (16→59) 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 0 (26→27) taken 8 times.
✗ Branch 1 (26→73) not taken.
✓ Branch 2 (29→30) taken 8 times.
✗ Branch 3 (29→67) not taken.
✓ Branch 4 (30→31) taken 8 times.
✗ Branch 5 (30→65) not taken.
|
32 | subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info"); |
248 | // --disable-verifier | ||
249 |
3/6✓ Branch 0 (37→38) taken 8 times.
✗ Branch 1 (37→85) not taken.
✓ Branch 2 (40→41) taken 8 times.
✗ Branch 3 (40→79) not taken.
✓ Branch 4 (41→42) taken 8 times.
✗ Branch 5 (41→77) 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 0 (4→5) taken 8 times.
✗ Branch 1 (4→55) not taken.
✓ Branch 2 (7→8) taken 8 times.
✗ Branch 3 (7→49) not taken.
✓ Branch 4 (8→9) taken 8 times.
✗ Branch 5 (8→47) not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("test", "Builds your Spice program and runs all enclosed tests"); |
258 |
2/4✓ Branch 0 (15→16) taken 8 times.
✗ Branch 1 (15→61) not taken.
✓ Branch 2 (16→17) taken 8 times.
✗ Branch 3 (16→59) 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 0 (26→27) taken 8 times.
✗ Branch 1 (26→73) not taken.
✓ Branch 2 (29→30) taken 8 times.
✗ Branch 3 (29→67) not taken.
✓ Branch 4 (30→31) taken 8 times.
✗ Branch 5 (30→65) not taken.
|
32 | subCmd->add_flag<bool>("--debug-info,-g", cliOptions.generateDebugInfo, "Generate debug info"); |
271 | // --disable-verifier | ||
272 |
3/6✓ Branch 0 (37→38) taken 8 times.
✗ Branch 1 (37→85) not taken.
✓ Branch 2 (40→41) taken 8 times.
✗ Branch 3 (40→79) not taken.
✓ Branch 4 (41→42) taken 8 times.
✗ Branch 5 (41→77) 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 0 (4→5) taken 8 times.
✗ Branch 1 (4→33) not taken.
✓ Branch 2 (7→8) taken 8 times.
✗ Branch 3 (7→27) not taken.
✓ Branch 4 (8→9) taken 8 times.
✗ Branch 5 (8→25) 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 0 (15→16) taken 8 times.
✗ Branch 1 (15→39) not taken.
✓ Branch 2 (16→17) taken 8 times.
✗ Branch 3 (16→37) 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 0 (4→5) taken 8 times.
✗ Branch 1 (4→52) not taken.
✓ Branch 2 (7→8) taken 8 times.
✗ Branch 3 (7→46) not taken.
✓ Branch 4 (8→9) taken 8 times.
✗ Branch 5 (8→44) not taken.
|
32 | CLI::App *subCmd = app.add_subcommand("uninstall", "Builds your Spice program and runs it immediately"); |
299 |
2/4✓ Branch 0 (15→16) taken 8 times.
✗ Branch 1 (15→58) not taken.
✓ Branch 2 (16→17) taken 8 times.
✗ Branch 3 (16→56) 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 0 (25→26) taken 8 times.
✗ Branch 1 (25→79) not taken.
✓ Branch 2 (28→29) taken 8 times.
✗ Branch 3 (28→73) not taken.
|
24 | subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file") |
308 |
4/8✓ Branch 0 (29→30) taken 8 times.
✗ Branch 1 (29→71) not taken.
✓ Branch 2 (32→33) taken 8 times.
✗ Branch 3 (32→67) not taken.
✓ Branch 4 (33→34) taken 8 times.
✗ Branch 5 (33→64) not taken.
✓ Branch 6 (34→35) taken 8 times.
✗ Branch 7 (34→62) not taken.
|
40 | ->check(CLI::ExistingFile) |
309 | 8 | ->required(); | |
310 | 8 | } | |
311 | |||
312 | 32 | void Driver::addCompileSubcommandOptions(CLI::App *subCmd) { | |
313 | 1 | const auto buildModeCallback = [&](const CLI::results_t &results) { | |
314 |
1/2✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→31) not taken.
|
1 | std::string inputString = results.front(); |
315 |
1/2✓ Branch 0 (5→6) taken 1 times.
✗ Branch 1 (5→29) not taken.
|
1 | std::ranges::transform(inputString, inputString.begin(), tolower); |
316 | |||
317 |
2/4✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→29) not taken.
✗ Branch 2 (7→8) not taken.
✓ Branch 3 (7→9) taken 1 times.
|
1 | if (inputString == BUILD_MODE_DEBUG) |
318 | ✗ | cliOptions.buildMode = DEBUG; | |
319 |
2/4✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→29) not taken.
✓ Branch 2 (10→11) taken 1 times.
✗ Branch 3 (10→12) 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 | 1 | }; | |
328 | |||
329 | // --build-mode | ||
330 |
3/6✓ Branch 0 (5→6) taken 32 times.
✗ Branch 1 (5→282) not taken.
✓ Branch 2 (9→10) taken 32 times.
✗ Branch 3 (9→273) not taken.
✓ Branch 4 (10→11) taken 32 times.
✗ Branch 5 (10→271) not taken.
|
160 | subCmd->add_option("--build-mode,-m", buildModeCallback, "Build mode (debug, release, test)"); |
331 | // --llvm-args | ||
332 |
3/6✓ Branch 0 (19→20) taken 32 times.
✗ Branch 1 (19→297) not taken.
✓ Branch 2 (22→23) taken 32 times.
✗ Branch 3 (22→291) not taken.
✓ Branch 4 (23→24) taken 32 times.
✗ Branch 5 (23→289) not taken.
|
128 | subCmd->add_option<std::string>("--llvm-args,-llvm", cliOptions.llvmArgs, "Additional arguments for LLVM")->join(' '); |
333 | // --jobs | ||
334 |
3/6✓ Branch 0 (31→32) taken 32 times.
✗ Branch 1 (31→309) not taken.
✓ Branch 2 (34→35) taken 32 times.
✗ Branch 3 (34→303) not taken.
✓ Branch 4 (35→36) taken 32 times.
✗ Branch 5 (35→301) 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 0 (42→43) taken 32 times.
✗ Branch 1 (42→321) not taken.
✓ Branch 2 (45→46) taken 32 times.
✗ Branch 3 (45→315) not taken.
✓ Branch 4 (46→47) taken 32 times.
✗ Branch 5 (46→313) 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 0 (53→54) taken 32 times.
✗ Branch 1 (53→333) not taken.
✓ Branch 2 (56→57) taken 32 times.
✗ Branch 3 (56→327) not taken.
✓ Branch 4 (57→58) taken 32 times.
✗ Branch 5 (57→325) 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 0 (64→65) taken 32 times.
✗ Branch 1 (64→349) not taken.
✓ Branch 2 (68→69) taken 32 times.
✗ Branch 3 (68→339) not taken.
✓ Branch 4 (69→70) taken 32 times.
✗ Branch 5 (69→337) not taken.
|
128 | subCmd->add_flag_callback("-O0", [&] { cliOptions.optLevel = O0; }, "Disable optimization for the output executable."); |
343 |
3/6✓ Branch 0 (77→78) taken 32 times.
✗ Branch 1 (77→365) not taken.
✓ Branch 2 (81→82) taken 32 times.
✗ Branch 3 (81→355) not taken.
✓ Branch 4 (82→83) taken 32 times.
✗ Branch 5 (82→353) not taken.
|
128 | subCmd->add_flag_callback("-O1", [&] { cliOptions.optLevel = O1; }, "Only basic optimization is executed."); |
344 |
3/6✓ Branch 0 (90→91) taken 32 times.
✗ Branch 1 (90→381) not taken.
✓ Branch 2 (94→95) taken 32 times.
✗ Branch 3 (94→371) not taken.
✓ Branch 4 (95→96) taken 32 times.
✗ Branch 5 (95→369) not taken.
|
128 | subCmd->add_flag_callback("-O2", [&] { cliOptions.optLevel = O2; }, "More advanced optimization is applied."); |
345 |
3/6✓ Branch 0 (103→104) taken 32 times.
✗ Branch 1 (103→397) not taken.
✓ Branch 2 (107→108) taken 32 times.
✗ Branch 3 (107→387) not taken.
✓ Branch 4 (108→109) taken 32 times.
✗ Branch 5 (108→385) not taken.
|
128 | subCmd->add_flag_callback("-O3", [&] { cliOptions.optLevel = O3; }, "Aggressive optimization for best performance."); |
346 |
3/6✓ Branch 0 (116→117) taken 32 times.
✗ Branch 1 (116→413) not taken.
✓ Branch 2 (120→121) taken 32 times.
✗ Branch 3 (120→403) not taken.
✓ Branch 4 (121→122) taken 32 times.
✗ Branch 5 (121→401) not taken.
|
128 | subCmd->add_flag_callback("-Os", [&] { cliOptions.optLevel = Os; }, "Size optimization for output executable."); |
347 |
3/6✓ Branch 0 (129→130) taken 32 times.
✗ Branch 1 (129→429) not taken.
✓ Branch 2 (133→134) taken 32 times.
✗ Branch 3 (133→419) not taken.
✓ Branch 4 (134→135) taken 32 times.
✗ Branch 5 (134→417) not taken.
|
128 | subCmd->add_flag_callback("-Oz", [&] { cliOptions.optLevel = Oz; }, "Aggressive optimization for best size."); |
348 |
3/6✓ Branch 0 (142→143) taken 32 times.
✗ Branch 1 (142→441) not taken.
✓ Branch 2 (145→146) taken 32 times.
✗ Branch 3 (145→435) not taken.
✓ Branch 4 (146→147) taken 32 times.
✗ Branch 5 (146→433) not taken.
|
128 | subCmd->add_flag<bool>("-lto", cliOptions.useLTO, "Enable link time optimization (LTO)"); |
349 | |||
350 | // --debug-output | ||
351 |
3/6✓ Branch 0 (153→154) taken 32 times.
✗ Branch 1 (153→453) not taken.
✓ Branch 2 (156→157) taken 32 times.
✗ Branch 3 (156→447) not taken.
✓ Branch 4 (157→158) taken 32 times.
✗ Branch 5 (157→445) not taken.
|
128 | subCmd->add_flag<bool>("--debug-output,-d", cliOptions.printDebugOutput, "Enable debug output"); |
352 | // --dump-cst | ||
353 |
3/6✓ Branch 0 (164→165) taken 32 times.
✗ Branch 1 (164→465) not taken.
✓ Branch 2 (167→168) taken 32 times.
✗ Branch 3 (167→459) not taken.
✓ Branch 4 (168→169) taken 32 times.
✗ Branch 5 (168→457) 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 0 (175→176) taken 32 times.
✗ Branch 1 (175→477) not taken.
✓ Branch 2 (178→179) taken 32 times.
✗ Branch 3 (178→471) not taken.
✓ Branch 4 (179→180) taken 32 times.
✗ Branch 5 (179→469) 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 0 (186→187) taken 32 times.
✗ Branch 1 (186→489) not taken.
✓ Branch 2 (189→190) taken 32 times.
✗ Branch 3 (189→483) not taken.
✓ Branch 4 (190→191) taken 32 times.
✗ Branch 5 (190→481) not taken.
|
128 | subCmd->add_flag<bool>("--dump-symtab,-symtab", cliOptions.dumpSettings.dumpSymbolTable, "Dump serialized symbol tables"); |
358 | // --dump-types | ||
359 |
3/6✓ Branch 0 (197→198) taken 32 times.
✗ Branch 1 (197→501) not taken.
✓ Branch 2 (200→201) taken 32 times.
✗ Branch 3 (200→495) not taken.
✓ Branch 4 (201→202) taken 32 times.
✗ Branch 5 (201→493) not taken.
|
128 | subCmd->add_flag<bool>("--dump-types,-types", cliOptions.dumpSettings.dumpTypes, "Dump all used types"); |
360 | // --dump-ir | ||
361 |
3/6✓ Branch 0 (208→209) taken 32 times.
✗ Branch 1 (208→513) not taken.
✓ Branch 2 (211→212) taken 32 times.
✗ Branch 3 (211→507) not taken.
✓ Branch 4 (212→213) taken 32 times.
✗ Branch 5 (212→505) not taken.
|
128 | subCmd->add_flag<bool>("--dump-ir,-ir", cliOptions.dumpSettings.dumpIR, "Dump LLVM-IR"); |
362 | // --dump-assembly | ||
363 |
3/6✓ Branch 0 (219→220) taken 32 times.
✗ Branch 1 (219→525) not taken.
✓ Branch 2 (222→223) taken 32 times.
✗ Branch 3 (222→519) not taken.
✓ Branch 4 (223→224) taken 32 times.
✗ Branch 5 (223→517) 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 0 (230→231) taken 32 times.
✗ Branch 1 (230→537) not taken.
✓ Branch 2 (233→234) taken 32 times.
✗ Branch 3 (233→531) not taken.
✓ Branch 4 (234→235) taken 32 times.
✗ Branch 5 (234→529) not taken.
|
128 | subCmd->add_flag<bool>("--dump-object-file,-obj", cliOptions.dumpSettings.dumpObjectFile, "Dump object file"); |
366 | // --dump-dependency-graph | ||
367 |
3/6✓ Branch 0 (241→242) taken 32 times.
✗ Branch 1 (241→549) not taken.
✓ Branch 2 (244→245) taken 32 times.
✗ Branch 3 (244→543) not taken.
✓ Branch 4 (245→246) taken 32 times.
✗ Branch 5 (245→541) not taken.
|
128 | subCmd->add_flag<bool>("--dump-dependency-graph,-dep", cliOptions.dumpSettings.dumpDependencyGraph, "Dump compile unit dependency graph"); |
368 | |||
369 | // Source file | ||
370 |
2/4✓ Branch 0 (252→253) taken 32 times.
✗ Branch 1 (252→570) not taken.
✓ Branch 2 (255→256) taken 32 times.
✗ Branch 3 (255→564) not taken.
|
96 | subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file") |
371 |
4/8✓ Branch 0 (256→257) taken 32 times.
✗ Branch 1 (256→562) not taken.
✓ Branch 2 (259→260) taken 32 times.
✗ Branch 3 (259→558) not taken.
✓ Branch 4 (260→261) taken 32 times.
✗ Branch 5 (260→555) not taken.
✓ Branch 6 (261→262) taken 32 times.
✗ Branch 7 (261→553) not taken.
|
160 | ->check(CLI::ExistingFile) |
372 | 32 | ->required(); | |
373 | 32 | } | |
374 | |||
375 | /** | ||
376 | * Ensure that the compiler is not running in a Docker container | ||
377 | */ | ||
378 | 2 | void Driver::ensureNotDockerized() { | |
379 | 2 | const char *envValue = std::getenv(ENV_VAR_DOCKERIZED); | |
380 |
1/4✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→13) taken 2 times.
✗ Branch 2 (4→5) not taken.
✗ Branch 3 (4→13) not taken.
|
2 | if (envValue != nullptr && std::strcmp(envValue, "true") == 0) |
381 | ✗ | throw CliError(FEATURE_NOT_SUPPORTED_WHEN_DOCKERIZED, | |
382 | ✗ | "This feature is not supported in a containerized environment. Please use the standalone version of Spice."); | |
383 | 2 | } | |
384 | |||
385 | } // namespace spice::compiler | ||
386 |