Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
2 | |||
3 | #pragma once | ||
4 | |||
5 | #include "../../lib/cli11/CLI11.hpp" | ||
6 | |||
7 | // Undef conflicting macros (only problematic on Windows) | ||
8 | #undef TRUE | ||
9 | #undef FALSE | ||
10 | #undef CONST | ||
11 | |||
12 | namespace spice::compiler { | ||
13 | |||
14 | const char *const TARGET_UNKNOWN = "unknown"; | ||
15 | const char *const TARGET_WASM32 = "wasm32"; | ||
16 | const char *const TARGET_WASM64 = "wasm64"; | ||
17 | const char *const ENV_VAR_DOCKERIZED = "SPICE_DOCKERIZED"; | ||
18 | |||
19 | enum OptLevel : uint8_t { | ||
20 | O0 = 0, // No optimization | ||
21 | O1 = 1, // Only basic optimizations | ||
22 | O2 = 2, // Default optimization level | ||
23 | O3 = 3, // Aggressively optimize for performance | ||
24 | Os = 4, // Optimize for code size | ||
25 | Oz = 5, // Aggressively optimize for code size | ||
26 | }; | ||
27 | |||
28 | enum BuildMode : uint8_t { | ||
29 | DEBUG = 0, // Default build mode, uses -O0 per default | ||
30 | RELEASE = 1, // Build without debug information and with -O2 per default | ||
31 | TEST = 2, // Build with test main function and always emit assertions | ||
32 | }; | ||
33 | const char *const BUILD_MODE_DEBUG = "debug"; | ||
34 | const char *const BUILD_MODE_RELEASE = "release"; | ||
35 | const char *const BUILD_MODE_TEST = "test"; | ||
36 | |||
37 | /** | ||
38 | * Representation of the various cli options | ||
39 | */ | ||
40 | struct CliOptions { | ||
41 | std::filesystem::path mainSourceFile; // e.g. ./main.spice | ||
42 | std::string targetTriple; // In format: <arch><sub>-<vendor>-<sys>-<abi> | ||
43 | std::string targetArch = TARGET_UNKNOWN; | ||
44 | std::string targetVendor = TARGET_UNKNOWN; | ||
45 | std::string targetOs = TARGET_UNKNOWN; | ||
46 | bool isNativeTarget = true; | ||
47 | bool useCPUFeatures = true; | ||
48 | bool execute = false; | ||
49 | std::filesystem::path cacheDir; // Where the cache files go. Should always be a temp directory | ||
50 | std::filesystem::path outputDir = "./"; // Where the object files go. Should always be a temp directory | ||
51 | std::filesystem::path outputPath; // Where the output binary goes. | ||
52 | BuildMode buildMode = DEBUG; // Default build mode is debug | ||
53 | unsigned short compileJobCount = 0; // 0 for auto | ||
54 | bool ignoreCache = false; | ||
55 | std::string llvmArgs; | ||
56 | bool printDebugOutput = false; | ||
57 | struct DumpSettings { | ||
58 | bool dumpCST = false; | ||
59 | bool dumpAST = false; | ||
60 | bool dumpSymbolTable = false; | ||
61 | bool dumpTypes = false; | ||
62 | bool dumpCacheStats = false; | ||
63 | bool dumpDependencyGraph = false; | ||
64 | bool dumpIR = false; | ||
65 | bool dumpAssembly = false; | ||
66 | bool dumpObjectFile = false; | ||
67 | bool dumpToFiles = false; | ||
68 | bool abortAfterDump = false; | ||
69 | } dumpSettings; | ||
70 | bool namesForIRValues = false; | ||
71 | bool useLifetimeMarkers = false; | ||
72 | OptLevel optLevel = O0; // The default optimization level for debug build mode is O0 | ||
73 | bool useLTO = false; | ||
74 | bool noEntryFct = false; | ||
75 | bool generateTestMain = false; | ||
76 | bool staticLinking = false; | ||
77 | bool generateDebugInfo = false; | ||
78 | bool disableVerifier = false; | ||
79 | bool testMode = false; | ||
80 | bool comparableOutput = false; | ||
81 | }; | ||
82 | |||
83 | /** | ||
84 | * Helper class to set up the driver and command line parser | ||
85 | */ | ||
86 | class Driver { | ||
87 | public: | ||
88 | // Constructors | ||
89 | ✗ | Driver() = default; | |
90 |
3/6✓ Branch 0 (5→6) taken 8 times.
✗ Branch 1 (5→23) not taken.
✓ Branch 2 (8→9) taken 8 times.
✗ Branch 3 (8→17) not taken.
✓ Branch 4 (9→10) taken 8 times.
✗ Branch 5 (9→15) not taken.
|
40 | explicit Driver(bool dryRun) : dryRun(dryRun) {} |
91 | Driver(const Driver &) = delete; | ||
92 | |||
93 | // Public methods | ||
94 | void init(); | ||
95 | int parse(int argc, const char *argv[]); | ||
96 | void enrich(); | ||
97 | void runBinary() const; | ||
98 | |||
99 | // Public members | ||
100 | CliOptions cliOptions; | ||
101 | bool shouldCompile = false; | ||
102 | bool shouldInstall = false; | ||
103 | bool shouldUninstall = false; | ||
104 | bool shouldExecute = false; | ||
105 | bool dryRun = false; // For unit testing purposes | ||
106 | |||
107 | private: | ||
108 | // Private methods | ||
109 | void addBuildSubcommand(); | ||
110 | void addRunSubcommand(); | ||
111 | void addTestSubcommand(); | ||
112 | void addInstallSubcommand(); | ||
113 | void addUninstallSubcommand(); | ||
114 | void addCompileSubcommandOptions(CLI::App *subCmd); | ||
115 | static void ensureNotDockerized(); | ||
116 | |||
117 | // Members | ||
118 | CLI::App app = CLI::App{"Spice Programming Language", "spice"}; | ||
119 | }; | ||
120 | |||
121 | } // namespace spice::compiler | ||
122 |