Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 1 | //===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===// |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tool provides a single point of access to the LLVM compilation tools. |
| 11 | // It has many options. To discover the options supported please refer to the |
| 12 | // tools' manual page (docs/CommandGuide/html/llvmc.html) or run the tool with |
| 13 | // the --help option. |
| 14 | // |
| 15 | //===------------------------------------------------------------------------=== |
| 16 | |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 17 | #include "CompilerDriver.h" |
Reid Spencer | f51a87c | 2004-08-19 21:52:49 +0000 | [diff] [blame] | 18 | #include "Configuration.h" |
Reid Spencer | 4495632 | 2004-08-24 17:54:26 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 20 | #include "llvm/System/Signals.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 22 | #include <iostream> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 26 | namespace { |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 27 | //===------------------------------------------------------------------------=== |
| 28 | //=== PHASE OPTIONS |
| 29 | //===------------------------------------------------------------------------=== |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 30 | cl::opt<CompilerDriver::Phases> FinalPhase( |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 31 | cl::desc("Choose final phase of compilation:"), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 32 | cl::init(CompilerDriver::LINKING), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 33 | cl::values( |
| 34 | clEnumValN(CompilerDriver::PREPROCESSING,"E", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 35 | "Stop compilation after pre-processing phase"), |
| 36 | clEnumValN(CompilerDriver::TRANSLATION, "t", |
| 37 | "Stop compilation after translation phase"), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 38 | clEnumValN(CompilerDriver::OPTIMIZATION,"c", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 39 | "Stop compilation after optimization phase"), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 40 | clEnumValN(CompilerDriver::ASSEMBLY,"S", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 41 | "Stop compilation after assembly phase"), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 42 | clEnumValEnd |
| 43 | ) |
| 44 | ); |
| 45 | |
| 46 | //===------------------------------------------------------------------------=== |
| 47 | //=== OPTIMIZATION OPTIONS |
| 48 | //===------------------------------------------------------------------------=== |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 49 | cl::opt<CompilerDriver::OptimizationLevels> OptLevel( |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 50 | cl::desc("Choose level of optimization to apply:"), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 51 | cl::init(CompilerDriver::OPT_FAST_COMPILE), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 52 | cl::values( |
| 53 | clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 54 | "An alias for the -O1 option."), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 55 | clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O1", |
| 56 | "Optimize for compilation speed, not execution speed."), |
| 57 | clEnumValN(CompilerDriver::OPT_SIMPLE,"O2", |
| 58 | "Perform simple translation time optimizations"), |
| 59 | clEnumValN(CompilerDriver::OPT_AGGRESSIVE,"O3", |
| 60 | "Perform aggressive translation time optimizations"), |
| 61 | clEnumValN(CompilerDriver::OPT_LINK_TIME,"O4", |
| 62 | "Perform link time optimizations"), |
| 63 | clEnumValN(CompilerDriver::OPT_AGGRESSIVE_LINK_TIME,"O5", |
| 64 | "Perform aggressive link time optimizations"), |
| 65 | clEnumValEnd |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | //===------------------------------------------------------------------------=== |
| 70 | //=== TOOL OPTIONS |
| 71 | //===------------------------------------------------------------------------=== |
| 72 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 73 | cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 74 | cl::desc("Pass specific options to the pre-processor"), |
| 75 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 76 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 77 | cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 78 | cl::desc("Pass specific options to the assembler"), |
| 79 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 80 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 81 | cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore, |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 82 | cl::desc("Pass specific options to the assembler"), |
| 83 | cl::value_desc("option")); |
| 84 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 85 | cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 86 | cl::desc("Pass specific options to the optimizer"), |
| 87 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 88 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 89 | cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 90 | cl::desc("Pass specific options to the linker"), |
| 91 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 92 | |
| 93 | //===------------------------------------------------------------------------=== |
| 94 | //=== INPUT OPTIONS |
| 95 | //===------------------------------------------------------------------------=== |
| 96 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 97 | cl::list<std::string> LibPaths("L", cl::Prefix, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 98 | cl::desc("Specify a library search path"), cl::value_desc("directory")); |
| 99 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 100 | cl::list<std::string> Libraries("l", cl::Prefix, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 101 | cl::desc("Specify libraries to link to"), cl::value_desc("library prefix")); |
| 102 | |
Reid Spencer | 7c14fd1 | 2004-08-30 06:27:32 +0000 | [diff] [blame] | 103 | cl::list<std::string> Includes("I", cl::Prefix, |
| 104 | cl::desc("Specify location to search for included source"), |
| 105 | cl::value_desc("include directory")); |
| 106 | |
| 107 | cl::list<std::string> Defines("D", cl::Prefix, |
| 108 | cl::desc("Specify a symbol to define for source configuration"), |
| 109 | cl::value_desc("symbol definition")); |
| 110 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 111 | |
| 112 | //===------------------------------------------------------------------------=== |
| 113 | //=== OUTPUT OPTIONS |
| 114 | //===------------------------------------------------------------------------=== |
| 115 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 116 | cl::opt<std::string> OutputFilename("o", |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 117 | cl::desc("Override output filename"), cl::value_desc("filename")); |
| 118 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 119 | cl::opt<bool> ForceOutput("f", cl::Optional, cl::init(false), |
| 120 | cl::desc("Force output files to be overridden")); |
| 121 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 122 | cl::opt<std::string> OutputMachine("m", cl::Prefix, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 123 | cl::desc("Specify a target machine"), cl::value_desc("machine")); |
| 124 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 125 | cl::opt<bool> Native("native", cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 126 | cl::desc("Generative native object and executables instead of bytecode")); |
| 127 | |
Reid Spencer | 7c14fd1 | 2004-08-30 06:27:32 +0000 | [diff] [blame] | 128 | cl::opt<bool> DebugOutput("g", cl::init(false), |
| 129 | cl::desc("Generate objects that include debug symbols")); |
| 130 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 131 | //===------------------------------------------------------------------------=== |
| 132 | //=== INFORMATION OPTIONS |
| 133 | //===------------------------------------------------------------------------=== |
| 134 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 135 | cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 136 | cl::desc("Do everything but perform the compilation actions")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 137 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 138 | cl::alias DryRunAlias("y", cl::Optional, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 139 | cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun)); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 140 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 141 | cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 142 | cl::desc("Print out each action taken")); |
| 143 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 144 | cl::alias VerboseAlias("v", cl::Optional, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 145 | cl::desc("Alias for -verbose"), cl::aliasopt(Verbose)); |
| 146 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 147 | cl::opt<bool> Debug("debug", cl::Optional, cl::init(false), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 148 | cl::Hidden, cl::desc("Print out debugging information")); |
| 149 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 150 | cl::alias DebugAlias("d", cl::Optional, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 151 | cl::desc("Alias for -debug"), cl::aliasopt(Debug)); |
| 152 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 153 | cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 154 | cl::desc("Print execution time for each action taken")); |
| 155 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 156 | cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false), |
| 157 | cl::desc("Print statistics accumulated during optimization")); |
| 158 | |
Reid Spencer | 7c14fd1 | 2004-08-30 06:27:32 +0000 | [diff] [blame] | 159 | cl::list<std::string> Warnings("W", cl::Prefix, |
| 160 | cl::desc("Provide warnings for additional classes of errors"), |
| 161 | cl::value_desc("warning category")); |
| 162 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 163 | //===------------------------------------------------------------------------=== |
| 164 | //=== ADVANCED OPTIONS |
| 165 | //===------------------------------------------------------------------------=== |
| 166 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 167 | static cl::opt<std::string> ConfigDir("config-dir", cl::Optional, |
| 168 | cl::desc("Specify a configuration directory to override defaults"), |
| 169 | cl::value_desc("directory")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 170 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 171 | static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 172 | cl::desc("Emit raw, unoptimized code")); |
| 173 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 174 | static cl::opt<bool> PipeCommands("pipe", cl::Optional, |
| 175 | cl::desc("Invoke sub-commands by linking input/output with pipes")); |
| 176 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 177 | static cl::opt<bool> KeepTemps("keep-temps", cl::Optional, |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 178 | cl::desc("Don't delete the temporary files created during compilation")); |
| 179 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 180 | //===------------------------------------------------------------------------=== |
| 181 | //=== POSITIONAL OPTIONS |
| 182 | //===------------------------------------------------------------------------=== |
| 183 | |
| 184 | static cl::list<std::string> Files(cl::Positional, cl::OneOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 185 | cl::desc("[Sources/objects/libraries]")); |
| 186 | |
| 187 | static cl::list<std::string> Languages("x", cl::ZeroOrMore, |
| 188 | cl::desc("Specify the source language for subsequent files"), |
| 189 | cl::value_desc("language")); |
| 190 | |
| 191 | //===------------------------------------------------------------------------=== |
| 192 | //=== GetFileType - determine type of a file |
| 193 | //===------------------------------------------------------------------------=== |
| 194 | const std::string GetFileType(const std::string& fname, unsigned pos ) { |
| 195 | static std::vector<std::string>::iterator langIt = Languages.begin(); |
| 196 | static std::string CurrLang = ""; |
| 197 | |
| 198 | // If a -x LANG option has been specified .. |
| 199 | if ( langIt != Languages.end() ) |
| 200 | // If the -x LANG option came before the current file on command line |
| 201 | if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) { |
| 202 | // use that language |
| 203 | CurrLang = *langIt++; |
| 204 | return CurrLang; |
| 205 | } |
| 206 | |
| 207 | // If there's a current language in effect |
| 208 | if (!CurrLang.empty()) |
| 209 | return CurrLang; // use that language |
| 210 | |
| 211 | // otherwise just determine lang from the filename's suffix |
| 212 | return fname.substr( fname.rfind('.',fname.size()) + 1 ); |
| 213 | } |
| 214 | |
| 215 | } // end anonymous namespace |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 216 | |
| 217 | |
| 218 | /// @brief The main program for llvmc |
| 219 | int main(int argc, char **argv) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 220 | // Make sure we print stack trace if we get bad signals |
| 221 | sys::PrintStackTraceOnErrorSignal(); |
| 222 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 223 | try { |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 224 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 225 | // Parse the command line options |
| 226 | cl::ParseCommandLineOptions(argc, argv, |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 227 | " LLVM Compiler Driver (llvmc)\n\n" |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 228 | " This program provides easy invocation of the LLVM tool set\n" |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 229 | " and other compiler tools.\n" |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 230 | ); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 231 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 232 | // Deal with unimplemented options. |
| 233 | if (PipeCommands) |
Reid Spencer | 6386009 | 2004-08-30 00:06:52 +0000 | [diff] [blame] | 234 | throw std::string("Not implemented yet: -pipe"); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 236 | if (OutputFilename.empty()) |
| 237 | if (OptLevel == CompilerDriver::LINKING) |
| 238 | OutputFilename = "a.out"; |
| 239 | else |
Reid Spencer | 6386009 | 2004-08-30 00:06:52 +0000 | [diff] [blame] | 240 | throw std::string("An output file must be specified. Please use the -o option"); |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 241 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 242 | // Construct the ConfigDataProvider object |
| 243 | LLVMC_ConfigDataProvider Provider; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 244 | Provider.setConfigDir(sys::Path(ConfigDir)); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 245 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 246 | // Construct the CompilerDriver object |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 247 | CompilerDriver* CD = CompilerDriver::Get(Provider); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 248 | |
Reid Spencer | aff3c77 | 2004-08-24 22:53:13 +0000 | [diff] [blame] | 249 | // If the LLVM_LIB_SEARCH_PATH environment variable is |
| 250 | // set, append it to the list of places to search for libraries |
| 251 | std::string srchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 252 | if (!srchPath.empty()) |
| 253 | LibPaths.push_back(srchPath); |
| 254 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 255 | // Set the driver flags based on command line options |
| 256 | unsigned flags = 0; |
| 257 | if (Verbose) flags |= CompilerDriver::VERBOSE_FLAG; |
| 258 | if (Debug) flags |= CompilerDriver::DEBUG_FLAG; |
| 259 | if (DryRun) flags |= CompilerDriver::DRY_RUN_FLAG; |
| 260 | if (ForceOutput) flags |= CompilerDriver::FORCE_FLAG; |
| 261 | if (Native) flags |= CompilerDriver::EMIT_NATIVE_FLAG; |
| 262 | if (EmitRawCode) flags |= CompilerDriver::EMIT_RAW_FLAG; |
| 263 | if (KeepTemps) flags |= CompilerDriver::KEEP_TEMPS_FLAG; |
| 264 | if (ShowStats) flags |= CompilerDriver::SHOW_STATS_FLAG; |
| 265 | if (TimeActions) flags |= CompilerDriver::TIME_ACTIONS_FLAG; |
| 266 | if (TimePassesIsEnabled) flags |= CompilerDriver::TIME_PASSES_FLAG; |
| 267 | CD->setDriverFlags(flags); |
| 268 | |
| 269 | // Specify requred parameters |
| 270 | CD->setFinalPhase(FinalPhase); |
| 271 | CD->setOptimization(OptLevel); |
| 272 | CD->setOutputMachine(OutputMachine); |
Reid Spencer | 7c14fd1 | 2004-08-30 06:27:32 +0000 | [diff] [blame] | 273 | CD->setIncludePaths(Includes); |
| 274 | CD->setSymbolDefines(Defines); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 275 | CD->setLibraryPaths(LibPaths); |
| 276 | |
| 277 | // Provide additional tool arguments |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 278 | if (!PreprocessorToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 279 | CD->setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 280 | if (!TranslatorToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 281 | CD->setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 282 | if (!OptimizerToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 283 | CD->setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 284 | if (!AssemblerToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 285 | CD->setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 286 | if (!LinkerToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 287 | CD->setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 288 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 289 | // Prepare the list of files to be compiled by the CompilerDriver. |
| 290 | CompilerDriver::InputList InpList; |
| 291 | std::vector<std::string>::iterator fileIt = Files.begin(); |
| 292 | std::vector<std::string>::iterator libIt = Libraries.begin(); |
| 293 | unsigned libPos = 0, filePos = 0; |
| 294 | while ( 1 ) { |
| 295 | if ( libIt != Libraries.end() ) |
| 296 | libPos = Libraries.getPosition( libIt - Libraries.begin() ); |
| 297 | else |
| 298 | libPos = 0; |
| 299 | if ( fileIt != Files.end() ) |
| 300 | filePos = Files.getPosition( fileIt - Files.begin() ); |
| 301 | else |
| 302 | filePos = 0; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 303 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 304 | if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) { |
| 305 | // Add a source file |
| 306 | InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos))); |
| 307 | ++fileIt; |
| 308 | } |
| 309 | else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) { |
| 310 | // Add a library |
| 311 | InpList.push_back( std::make_pair(*libIt++,"")); |
| 312 | } |
| 313 | else |
| 314 | break; // we're done with the list |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 315 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 316 | |
| 317 | // Tell the driver to do its thing |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 318 | int result = CD->execute(InpList,sys::Path(OutputFilename)); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 319 | if (result != 0) { |
Reid Spencer | 6386009 | 2004-08-30 00:06:52 +0000 | [diff] [blame] | 320 | throw std::string("Error executing actions. Terminated."); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 321 | return result; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 322 | } |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 323 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 324 | // All is good, return success |
| 325 | return 0; |
| 326 | } catch (std::string& msg) { |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 327 | std::cerr << argv[0] << ": " << msg << "\n"; |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 328 | } catch (...) { |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 329 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 330 | } |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 331 | } |