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