Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5f5a573 | 2007-12-29 20:44:31 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
| 17 | #include "CompilerDriver.h" |
| 18 | #include "Configuration.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/ManagedStatic.h" |
| 22 | #include "llvm/System/Signals.h" |
| 23 | #include <iostream> |
| 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | //=== PHASE OPTIONS |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | static cl::opt<CompilerDriver::Phases> FinalPhase(cl::Optional, |
| 30 | cl::desc("Choose final phase of compilation:"), |
| 31 | cl::init(CompilerDriver::LINKING), |
| 32 | cl::values( |
| 33 | clEnumValN(CompilerDriver::PREPROCESSING,"E", |
| 34 | "Stop compilation after pre-processing phase"), |
| 35 | clEnumValN(CompilerDriver::TRANSLATION, "t", |
| 36 | "Stop compilation after translation phase"), |
| 37 | clEnumValN(CompilerDriver::OPTIMIZATION,"c", |
| 38 | "Stop compilation after optimization phase"), |
| 39 | clEnumValN(CompilerDriver::ASSEMBLY,"S", |
| 40 | "Stop compilation after assembly phase"), |
| 41 | clEnumValEnd |
| 42 | ) |
| 43 | ); |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | //=== OPTIMIZATION OPTIONS |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | static cl::opt<CompilerDriver::OptimizationLevels> OptLevel(cl::ZeroOrMore, |
| 49 | cl::desc("Choose level of optimization to apply:"), |
| 50 | cl::init(CompilerDriver::OPT_FAST_COMPILE), |
| 51 | cl::values( |
| 52 | clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0", |
| 53 | "An alias for the -O1 option"), |
| 54 | clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O1", |
| 55 | "Optimize for compilation speed, not execution speed"), |
| 56 | clEnumValN(CompilerDriver::OPT_SIMPLE,"O2", |
| 57 | "Perform simple translation time optimizations"), |
| 58 | clEnumValN(CompilerDriver::OPT_AGGRESSIVE,"O3", |
| 59 | "Perform aggressive translation time optimizations"), |
| 60 | clEnumValN(CompilerDriver::OPT_LINK_TIME,"O4", |
| 61 | "Perform link time optimizations"), |
| 62 | clEnumValN(CompilerDriver::OPT_AGGRESSIVE_LINK_TIME,"O5", |
| 63 | "Perform aggressive link time optimizations"), |
| 64 | clEnumValEnd |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | //=== TOOL OPTIONS |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | |
| 72 | static cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore, |
| 73 | cl::desc("Pass specific options to the pre-processor"), |
| 74 | cl::value_desc("option")); |
| 75 | |
| 76 | static cl::alias PreprocessorToolOptsAlias("Wp,", cl::ZeroOrMore, |
| 77 | cl::desc("Alias for -Tpre"), cl::aliasopt(PreprocessorToolOpts)); |
| 78 | |
| 79 | static cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore, |
| 80 | cl::desc("Pass specific options to the assembler"), |
| 81 | cl::value_desc("option")); |
| 82 | |
| 83 | static cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore, |
| 84 | cl::desc("Pass specific options to the assembler"), |
| 85 | cl::value_desc("option")); |
| 86 | |
| 87 | static cl::alias AssemblerToolOptsAlias("Wa,", cl::ZeroOrMore, |
| 88 | cl::desc("Alias for -Tasm"), cl::aliasopt(AssemblerToolOpts)); |
| 89 | |
| 90 | static cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore, |
| 91 | cl::desc("Pass specific options to the optimizer"), |
| 92 | cl::value_desc("option")); |
| 93 | |
| 94 | static cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore, |
| 95 | cl::desc("Pass specific options to the linker"), |
| 96 | cl::value_desc("option")); |
| 97 | |
| 98 | static cl::alias LinkerToolOptsAlias("Wl,", cl::ZeroOrMore, |
| 99 | cl::desc("Alias for -Tlnk"), cl::aliasopt(LinkerToolOpts)); |
| 100 | |
| 101 | static cl::list<std::string> fOpts("f", cl::ZeroOrMore, cl::Prefix, |
| 102 | cl::desc("Pass through -f options to compiler tools"), |
| 103 | cl::value_desc("option")); |
| 104 | |
| 105 | static cl::list<std::string> MOpts("M", cl::ZeroOrMore, cl::Prefix, |
| 106 | cl::desc("Pass through -M options to compiler tools"), |
| 107 | cl::value_desc("option")); |
| 108 | |
| 109 | static cl::list<std::string> WOpts("W", cl::ZeroOrMore, cl::Prefix, |
| 110 | cl::desc("Pass through -W options to compiler tools"), |
| 111 | cl::value_desc("option")); |
| 112 | |
| 113 | static cl::list<std::string> BOpt("B", cl::ZeroOrMore, cl::Prefix, |
| 114 | cl::desc("Specify path to find llvmc sub-tools"), |
| 115 | cl::value_desc("dir")); |
| 116 | |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | //=== INPUT OPTIONS |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | |
| 121 | static cl::list<std::string> LibPaths("L", cl::Prefix, |
| 122 | cl::desc("Specify a library search path"), cl::value_desc("dir")); |
| 123 | |
| 124 | static cl::list<std::string> Libraries("l", cl::Prefix, |
| 125 | cl::desc("Specify base name of libraries to link to"), cl::value_desc("lib")); |
| 126 | |
| 127 | static cl::list<std::string> Includes("I", cl::Prefix, |
| 128 | cl::desc("Specify location to search for included source"), |
| 129 | cl::value_desc("dir")); |
| 130 | |
| 131 | static cl::list<std::string> Defines("D", cl::Prefix, |
| 132 | cl::desc("Specify a pre-processor symbol to define"), |
| 133 | cl::value_desc("symbol")); |
| 134 | |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | //=== OUTPUT OPTIONS |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
| 139 | static cl::opt<std::string> OutputFilename("o", |
| 140 | cl::desc("Override output filename"), cl::value_desc("file")); |
| 141 | |
| 142 | static cl::opt<std::string> OutputMachine("m", cl::Prefix, |
| 143 | cl::desc("Specify a target machine"), cl::value_desc("machine")); |
| 144 | |
| 145 | static cl::opt<bool> Native("native", cl::init(false), |
| 146 | cl::desc("Generative native code instead of bitcode")); |
| 147 | |
| 148 | static cl::opt<bool> DebugOutput("g", cl::init(false), |
| 149 | cl::desc("Generate objects that include debug symbols")); |
| 150 | |
| 151 | static cl::opt<bool> StripOutput("strip", cl::init(false), |
| 152 | cl::desc("Strip all symbols from linked output file")); |
| 153 | |
| 154 | static cl::opt<std::string> PrintFileName("print-fname", cl::Optional, |
| 155 | cl::value_desc("file"), |
| 156 | cl::desc("Print the full path for the option's value")); |
| 157 | |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | //=== INFORMATION OPTIONS |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | |
| 162 | static cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false), |
| 163 | cl::desc("Do everything but perform the compilation actions")); |
| 164 | |
| 165 | static cl::alias DryRunAlias("y", cl::Optional, |
| 166 | cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun)); |
| 167 | |
| 168 | static cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false), |
| 169 | cl::desc("Print out each action taken")); |
| 170 | |
| 171 | static cl::alias VerboseAlias("v", cl::Optional, |
| 172 | cl::desc("Alias for -verbose"), cl::aliasopt(Verbose)); |
| 173 | |
| 174 | static cl::opt<bool> Debug("debug", cl::Optional, cl::init(false), |
| 175 | cl::Hidden, cl::desc("Print out debugging information")); |
| 176 | |
| 177 | static cl::alias DebugAlias("d", cl::Optional, |
| 178 | cl::desc("Alias for -debug"), cl::aliasopt(Debug)); |
| 179 | |
| 180 | static cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false), |
| 181 | cl::desc("Print execution time for each action taken")); |
| 182 | |
| 183 | static cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false), |
| 184 | cl::desc("Print statistics accumulated during optimization")); |
| 185 | |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | //=== ADVANCED OPTIONS |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | |
| 190 | static cl::opt<std::string> ConfigDir("config-dir", cl::Optional, |
| 191 | cl::desc("Specify configuration directory to override defaults"), |
| 192 | cl::value_desc("dir")); |
| 193 | |
| 194 | static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional, |
| 195 | cl::desc("Emit raw, unoptimized code")); |
| 196 | |
| 197 | static cl::opt<bool> PipeCommands("pipe", cl::Optional, |
| 198 | cl::desc("Invoke sub-commands by linking input/output with pipes")); |
| 199 | |
| 200 | static cl::opt<bool> KeepTemps("keep-temps", cl::Optional, |
| 201 | cl::desc("Don't delete temporary files created by llvmc")); |
| 202 | |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | //=== POSITIONAL OPTIONS |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
| 207 | static cl::list<std::string> Files(cl::Positional, cl::ZeroOrMore, |
| 208 | cl::desc("[Sources/objects/libraries]")); |
| 209 | |
| 210 | static cl::list<std::string> Languages("x", cl::ZeroOrMore, |
| 211 | cl::desc("Specify the source language for subsequent files"), |
| 212 | cl::value_desc("language")); |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | //=== GetFileType - determine type of a file |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | static const std::string GetFileType(const std::string& fname, unsigned pos) { |
| 218 | static std::vector<std::string>::iterator langIt = Languages.begin(); |
| 219 | static std::string CurrLang = ""; |
| 220 | |
| 221 | // If a -x LANG option has been specified .. |
| 222 | if (langIt != Languages.end()) |
| 223 | // If the -x LANG option came before the current file on command line |
| 224 | if (Languages.getPosition( langIt - Languages.begin() ) < pos) { |
| 225 | // use that language |
| 226 | CurrLang = *langIt++; |
| 227 | return CurrLang; |
| 228 | } |
| 229 | |
| 230 | // If there's a current language in effect |
| 231 | if (!CurrLang.empty()) |
| 232 | return CurrLang; // use that language |
| 233 | |
| 234 | // otherwise just determine lang from the filename's suffix |
| 235 | return fname.substr(fname.rfind('.', fname.size()) + 1); |
| 236 | } |
| 237 | |
| 238 | static void handleTerminatingOptions(CompilerDriver* CD) { |
| 239 | if (!PrintFileName.empty()) { |
| 240 | sys::Path path = CD->GetPathForLinkageItem(PrintFileName, false); |
| 241 | std::string p = path.toString(); |
| 242 | if (p.empty()) |
| 243 | std::cout << "Can't locate `" << PrintFileName << "'.\n"; |
| 244 | else |
| 245 | std::cout << p << '\n'; |
| 246 | exit(0); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /// @brief The main program for llvmc |
| 251 | int main(int argc, char **argv) { |
| 252 | llvm_shutdown_obj X; // Call llvm_shutdown() on exit. |
| 253 | // Make sure we print stack trace if we get bad signals |
| 254 | sys::PrintStackTraceOnErrorSignal(); |
| 255 | |
Chris Lattner | c4fc7f6 | 2007-10-22 19:41:46 +0000 | [diff] [blame] | 256 | std::cout << "NOTE: llvmc is highly experimental and mostly useless right " |
| 257 | "now.\nPlease use llvm-gcc directly instead.\n\n"; |
| 258 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 259 | try { |
| 260 | |
| 261 | // Parse the command line options |
| 262 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 6099df8 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 263 | "LLVM Compiler Driver (llvmc)\n\n" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 264 | " This program provides easy invocation of the LLVM tool set\n" |
| 265 | " and other compiler tools.\n" |
| 266 | ); |
| 267 | |
| 268 | // Deal with unimplemented options. |
| 269 | if (PipeCommands) |
| 270 | throw std::string("Not implemented yet: -pipe"); |
| 271 | |
| 272 | if (OutputFilename.empty()) |
| 273 | if (OptLevel == CompilerDriver::LINKING) |
| 274 | OutputFilename = "a.out"; |
| 275 | |
| 276 | // Construct the ConfigDataProvider object |
| 277 | LLVMC_ConfigDataProvider Provider; |
| 278 | Provider.setConfigDir(sys::Path(ConfigDir)); |
| 279 | |
| 280 | // Construct the CompilerDriver object |
| 281 | CompilerDriver* CD = CompilerDriver::Get(Provider); |
| 282 | |
| 283 | // If the LLVM_LIB_SEARCH_PATH environment variable is |
| 284 | // set, append it to the list of places to search for libraries |
| 285 | char *srchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 286 | if (srchPath != NULL && strlen(srchPath) != 0) |
| 287 | LibPaths.push_back(std::string(srchPath)); |
| 288 | |
| 289 | // Set the driver flags based on command line options |
| 290 | unsigned flags = 0; |
| 291 | if (Verbose) flags |= CompilerDriver::VERBOSE_FLAG; |
| 292 | if (Debug) flags |= CompilerDriver::DEBUG_FLAG; |
| 293 | if (DryRun) flags |= CompilerDriver::DRY_RUN_FLAG; |
| 294 | if (Native) flags |= CompilerDriver::EMIT_NATIVE_FLAG; |
| 295 | if (EmitRawCode) flags |= CompilerDriver::EMIT_RAW_FLAG; |
| 296 | if (KeepTemps) flags |= CompilerDriver::KEEP_TEMPS_FLAG; |
| 297 | if (ShowStats) flags |= CompilerDriver::SHOW_STATS_FLAG; |
| 298 | if (TimeActions) flags |= CompilerDriver::TIME_ACTIONS_FLAG; |
| 299 | if (StripOutput) flags |= CompilerDriver::STRIP_OUTPUT_FLAG; |
| 300 | CD->setDriverFlags(flags); |
| 301 | |
| 302 | // Specify required parameters |
| 303 | CD->setFinalPhase(FinalPhase); |
| 304 | CD->setOptimization(OptLevel); |
| 305 | CD->setOutputMachine(OutputMachine); |
| 306 | CD->setIncludePaths(Includes); |
| 307 | CD->setSymbolDefines(Defines); |
| 308 | CD->setLibraryPaths(LibPaths); |
| 309 | CD->setfPassThrough(fOpts); |
| 310 | CD->setMPassThrough(MOpts); |
| 311 | CD->setWPassThrough(WOpts); |
| 312 | |
| 313 | // Provide additional tool arguments |
| 314 | if (!PreprocessorToolOpts.empty()) |
| 315 | CD->setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts); |
| 316 | if (!TranslatorToolOpts.empty()) |
| 317 | CD->setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts); |
| 318 | if (!OptimizerToolOpts.empty()) |
| 319 | CD->setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts); |
| 320 | if (!AssemblerToolOpts.empty()) |
| 321 | CD->setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts); |
| 322 | if (!LinkerToolOpts.empty()) |
| 323 | CD->setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts); |
| 324 | |
| 325 | // Check for options that cause us to terminate before any significant work |
| 326 | // is done. |
| 327 | handleTerminatingOptions(CD); |
| 328 | |
| 329 | // Prepare the list of files to be compiled by the CompilerDriver. |
| 330 | CompilerDriver::InputList InpList; |
| 331 | std::vector<std::string>::iterator fileIt = Files.begin(); |
| 332 | std::vector<std::string>::iterator libIt = Libraries.begin(); |
| 333 | unsigned libPos = 0, filePos = 0; |
| 334 | while ( 1 ) { |
| 335 | if (libIt != Libraries.end()) |
| 336 | libPos = Libraries.getPosition( libIt - Libraries.begin() ); |
| 337 | else |
| 338 | libPos = 0; |
| 339 | if (fileIt != Files.end()) |
| 340 | filePos = Files.getPosition(fileIt - Files.begin()); |
| 341 | else |
| 342 | filePos = 0; |
| 343 | |
| 344 | if (filePos != 0 && (libPos == 0 || filePos < libPos)) { |
| 345 | // Add a source file |
| 346 | InpList.push_back(std::make_pair(*fileIt, |
| 347 | GetFileType(*fileIt, filePos))); |
| 348 | ++fileIt; |
| 349 | } else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) { |
| 350 | // Add a library |
| 351 | InpList.push_back(std::make_pair(*libIt++, "")); |
| 352 | } |
| 353 | else |
| 354 | break; // we're done with the list |
| 355 | } |
| 356 | |
| 357 | // Tell the driver to do its thing |
| 358 | std::string ErrMsg; |
| 359 | int result = CD->execute(InpList, sys::Path(OutputFilename), ErrMsg); |
| 360 | if (result != 0) { |
| 361 | std::cerr << argv[0] << ": " << ErrMsg << '\n'; |
| 362 | return result; |
| 363 | } |
| 364 | |
| 365 | // All is good, return success |
| 366 | return 0; |
| 367 | } catch (const std::string& msg) { |
| 368 | std::cerr << argv[0] << ": " << msg << '\n'; |
| 369 | } catch (...) { |
| 370 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
| 371 | } |
| 372 | return 1; |
| 373 | } |