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 | 65bc4e0 | 2004-11-23 23:47:58 +0000 | [diff] [blame] | 30 | cl::opt<CompilerDriver::Phases> FinalPhase(cl::Optional, |
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 | 65bc4e0 | 2004-11-23 23:47:58 +0000 | [diff] [blame] | 49 | cl::opt<CompilerDriver::OptimizationLevels> OptLevel(cl::ZeroOrMore, |
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 | 2967b61 | 2004-10-28 03:56:16 +0000 | [diff] [blame] | 77 | cl::alias PreprocessorToolOptsAlias("Wp,", cl::ZeroOrMore, |
| 78 | cl::desc("Alias for -Tpre"), cl::aliasopt(PreprocessorToolOpts)); |
| 79 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 80 | cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 81 | cl::desc("Pass specific options to the assembler"), |
| 82 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 83 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 84 | cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore, |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 85 | cl::desc("Pass specific options to the assembler"), |
| 86 | cl::value_desc("option")); |
| 87 | |
Reid Spencer | 2967b61 | 2004-10-28 03:56:16 +0000 | [diff] [blame] | 88 | cl::alias AssemblerToolOptsAlias("Wa,", cl::ZeroOrMore, |
| 89 | cl::desc("Alias for -Tasm"), cl::aliasopt(AssemblerToolOpts)); |
| 90 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 91 | cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 92 | cl::desc("Pass specific options to the optimizer"), |
| 93 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 94 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 95 | cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 96 | cl::desc("Pass specific options to the linker"), |
| 97 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 98 | |
Reid Spencer | 2967b61 | 2004-10-28 03:56:16 +0000 | [diff] [blame] | 99 | cl::alias LinkerToolOptsAlias("Wl,", cl::ZeroOrMore, |
| 100 | cl::desc("Alias for -Tlnk"), cl::aliasopt(LinkerToolOpts)); |
| 101 | |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 102 | cl::list<std::string> fOpts("f", cl::ZeroOrMore, cl::Prefix, |
| 103 | cl::desc("Pass through -f options to compiler tools"), |
| 104 | cl::value_desc("optimization option")); |
| 105 | |
| 106 | cl::list<std::string> MOpts("M", cl::ZeroOrMore, cl::Prefix, |
| 107 | cl::desc("Pass through -M options to compiler tools"), |
| 108 | cl::value_desc("dependency option")); |
| 109 | |
| 110 | cl::list<std::string> WOpts("W", cl::ZeroOrMore, cl::Prefix, |
| 111 | cl::desc("Pass through -W options to compiler tools"), |
| 112 | cl::value_desc("warnings category")); |
| 113 | |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 114 | cl::list<std::string> BOpt("B", cl::ZeroOrMore, cl::Prefix, |
| 115 | cl::desc("Indicate where llvmc sub-tools are installed"), |
| 116 | cl::value_desc("directory path containing bin and lib directories")); |
| 117 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 118 | //===------------------------------------------------------------------------=== |
| 119 | //=== INPUT OPTIONS |
| 120 | //===------------------------------------------------------------------------=== |
| 121 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 122 | cl::list<std::string> LibPaths("L", cl::Prefix, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 123 | cl::desc("Specify a library search path"), cl::value_desc("directory")); |
Reid Spencer | 08602e5 | 2004-11-20 20:45:33 +0000 | [diff] [blame] | 124 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 125 | cl::list<std::string> Libraries("l", cl::Prefix, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 126 | cl::desc("Specify libraries to link to"), cl::value_desc("library prefix")); |
| 127 | |
Reid Spencer | 7c14fd1 | 2004-08-30 06:27:32 +0000 | [diff] [blame] | 128 | cl::list<std::string> Includes("I", cl::Prefix, |
| 129 | cl::desc("Specify location to search for included source"), |
| 130 | cl::value_desc("include directory")); |
| 131 | |
| 132 | cl::list<std::string> Defines("D", cl::Prefix, |
| 133 | cl::desc("Specify a symbol to define for source configuration"), |
| 134 | cl::value_desc("symbol definition")); |
| 135 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 136 | //===------------------------------------------------------------------------=== |
| 137 | //=== OUTPUT OPTIONS |
| 138 | //===------------------------------------------------------------------------=== |
| 139 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 140 | cl::opt<std::string> OutputFilename("o", |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 141 | cl::desc("Override output filename"), cl::value_desc("filename")); |
| 142 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 143 | cl::opt<std::string> OutputMachine("m", cl::Prefix, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 144 | cl::desc("Specify a target machine"), cl::value_desc("machine")); |
Reid Spencer | 2967b61 | 2004-10-28 03:56:16 +0000 | [diff] [blame] | 145 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 146 | cl::opt<bool> Native("native", cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 147 | cl::desc("Generative native object and executables instead of bytecode")); |
| 148 | |
Reid Spencer | 7c14fd1 | 2004-08-30 06:27:32 +0000 | [diff] [blame] | 149 | cl::opt<bool> DebugOutput("g", cl::init(false), |
| 150 | cl::desc("Generate objects that include debug symbols")); |
| 151 | |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 152 | cl::opt<bool> StripOutput("strip", cl::init(false), |
| 153 | cl::desc("Strip all symbols from linked output file")); |
| 154 | |
Reid Spencer | 65bc4e0 | 2004-11-23 23:47:58 +0000 | [diff] [blame] | 155 | cl::opt<std::string> PrintFileName("print-file-name", cl::Optional, |
| 156 | cl::value_desc("filename"), |
| 157 | cl::desc("Print the full path for the option's value")); |
| 158 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 159 | //===------------------------------------------------------------------------=== |
| 160 | //=== INFORMATION OPTIONS |
| 161 | //===------------------------------------------------------------------------=== |
| 162 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 163 | cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 164 | cl::desc("Do everything but perform the compilation actions")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 165 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 166 | cl::alias DryRunAlias("y", cl::Optional, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 167 | cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun)); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 168 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 169 | cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 170 | cl::desc("Print out each action taken")); |
| 171 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 172 | cl::alias VerboseAlias("v", cl::Optional, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 173 | cl::desc("Alias for -verbose"), cl::aliasopt(Verbose)); |
| 174 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 175 | cl::opt<bool> Debug("debug", cl::Optional, cl::init(false), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 176 | cl::Hidden, cl::desc("Print out debugging information")); |
| 177 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 178 | cl::alias DebugAlias("d", cl::Optional, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 179 | cl::desc("Alias for -debug"), cl::aliasopt(Debug)); |
| 180 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 181 | cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 182 | cl::desc("Print execution time for each action taken")); |
| 183 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 184 | cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false), |
| 185 | cl::desc("Print statistics accumulated during optimization")); |
| 186 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 187 | //===------------------------------------------------------------------------=== |
| 188 | //=== ADVANCED OPTIONS |
| 189 | //===------------------------------------------------------------------------=== |
| 190 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 191 | static cl::opt<std::string> ConfigDir("config-dir", cl::Optional, |
| 192 | cl::desc("Specify a configuration directory to override defaults"), |
| 193 | cl::value_desc("directory")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 194 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 195 | static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 196 | cl::desc("Emit raw, unoptimized code")); |
| 197 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 198 | static cl::opt<bool> PipeCommands("pipe", cl::Optional, |
| 199 | cl::desc("Invoke sub-commands by linking input/output with pipes")); |
| 200 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 201 | static cl::opt<bool> KeepTemps("keep-temps", cl::Optional, |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 202 | cl::desc("Don't delete the temporary files created during compilation")); |
| 203 | |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 204 | //===------------------------------------------------------------------------=== |
| 205 | //=== POSITIONAL OPTIONS |
| 206 | //===------------------------------------------------------------------------=== |
| 207 | |
Reid Spencer | 65bc4e0 | 2004-11-23 23:47:58 +0000 | [diff] [blame] | 208 | static cl::list<std::string> Files(cl::Positional, cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 209 | cl::desc("[Sources/objects/libraries]")); |
| 210 | |
| 211 | static cl::list<std::string> Languages("x", cl::ZeroOrMore, |
| 212 | cl::desc("Specify the source language for subsequent files"), |
| 213 | cl::value_desc("language")); |
| 214 | |
| 215 | //===------------------------------------------------------------------------=== |
| 216 | //=== GetFileType - determine type of a file |
| 217 | //===------------------------------------------------------------------------=== |
| 218 | const std::string GetFileType(const std::string& fname, unsigned pos ) { |
| 219 | static std::vector<std::string>::iterator langIt = Languages.begin(); |
| 220 | static std::string CurrLang = ""; |
| 221 | |
| 222 | // If a -x LANG option has been specified .. |
| 223 | if ( langIt != Languages.end() ) |
| 224 | // If the -x LANG option came before the current file on command line |
| 225 | if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) { |
| 226 | // use that language |
| 227 | CurrLang = *langIt++; |
| 228 | return CurrLang; |
| 229 | } |
| 230 | |
| 231 | // If there's a current language in effect |
| 232 | if (!CurrLang.empty()) |
| 233 | return CurrLang; // use that language |
| 234 | |
| 235 | // otherwise just determine lang from the filename's suffix |
| 236 | return fname.substr( fname.rfind('.',fname.size()) + 1 ); |
| 237 | } |
| 238 | |
| 239 | } // end anonymous namespace |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 240 | |
Reid Spencer | 65bc4e0 | 2004-11-23 23:47:58 +0000 | [diff] [blame] | 241 | void handleTerminatingOptions(CompilerDriver* CD) { |
| 242 | if (!PrintFileName.empty()) { |
| 243 | sys::Path path = CD->GetPathForLinkageItem(PrintFileName,false); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 244 | std::string p = path.toString(); |
Reid Spencer | 65bc4e0 | 2004-11-23 23:47:58 +0000 | [diff] [blame] | 245 | if (p.empty()) |
| 246 | std::cout << "Can't locate '" << PrintFileName << "'.\n"; |
| 247 | else |
| 248 | std::cout << p << "\n"; |
| 249 | exit(0); |
| 250 | } |
| 251 | } |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 252 | |
| 253 | /// @brief The main program for llvmc |
| 254 | int main(int argc, char **argv) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 255 | // Make sure we print stack trace if we get bad signals |
| 256 | sys::PrintStackTraceOnErrorSignal(); |
| 257 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 258 | try { |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 259 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 260 | // Parse the command line options |
| 261 | cl::ParseCommandLineOptions(argc, argv, |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 262 | " LLVM Compiler Driver (llvmc)\n\n" |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 263 | " This program provides easy invocation of the LLVM tool set\n" |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 264 | " and other compiler tools.\n" |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 265 | ); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 266 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 267 | // Deal with unimplemented options. |
| 268 | if (PipeCommands) |
Reid Spencer | 6386009 | 2004-08-30 00:06:52 +0000 | [diff] [blame] | 269 | throw std::string("Not implemented yet: -pipe"); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 270 | |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 271 | if (OutputFilename.empty()) |
| 272 | if (OptLevel == CompilerDriver::LINKING) |
| 273 | OutputFilename = "a.out"; |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 274 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 275 | // Construct the ConfigDataProvider object |
| 276 | LLVMC_ConfigDataProvider Provider; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 277 | Provider.setConfigDir(sys::Path(ConfigDir)); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 278 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 279 | // Construct the CompilerDriver object |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 280 | CompilerDriver* CD = CompilerDriver::Get(Provider); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 281 | |
Reid Spencer | aff3c77 | 2004-08-24 22:53:13 +0000 | [diff] [blame] | 282 | // If the LLVM_LIB_SEARCH_PATH environment variable is |
| 283 | // set, append it to the list of places to search for libraries |
| 284 | std::string srchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 285 | if (!srchPath.empty()) |
| 286 | LibPaths.push_back(srchPath); |
| 287 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 288 | // Set the driver flags based on command line options |
| 289 | unsigned flags = 0; |
| 290 | if (Verbose) flags |= CompilerDriver::VERBOSE_FLAG; |
| 291 | if (Debug) flags |= CompilerDriver::DEBUG_FLAG; |
| 292 | if (DryRun) flags |= CompilerDriver::DRY_RUN_FLAG; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 293 | if (Native) flags |= CompilerDriver::EMIT_NATIVE_FLAG; |
| 294 | if (EmitRawCode) flags |= CompilerDriver::EMIT_RAW_FLAG; |
| 295 | if (KeepTemps) flags |= CompilerDriver::KEEP_TEMPS_FLAG; |
| 296 | if (ShowStats) flags |= CompilerDriver::SHOW_STATS_FLAG; |
| 297 | if (TimeActions) flags |= CompilerDriver::TIME_ACTIONS_FLAG; |
| 298 | if (TimePassesIsEnabled) flags |= CompilerDriver::TIME_PASSES_FLAG; |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 299 | if (StripOutput) flags |= CompilerDriver::STRIP_OUTPUT_FLAG; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 300 | CD->setDriverFlags(flags); |
| 301 | |
| 302 | // Specify requred parameters |
| 303 | CD->setFinalPhase(FinalPhase); |
| 304 | CD->setOptimization(OptLevel); |
| 305 | CD->setOutputMachine(OutputMachine); |
Reid Spencer | 7c14fd1 | 2004-08-30 06:27:32 +0000 | [diff] [blame] | 306 | CD->setIncludePaths(Includes); |
| 307 | CD->setSymbolDefines(Defines); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 308 | CD->setLibraryPaths(LibPaths); |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 309 | CD->setfPassThrough(fOpts); |
| 310 | CD->setMPassThrough(MOpts); |
| 311 | CD->setWPassThrough(WOpts); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 312 | |
| 313 | // Provide additional tool arguments |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 314 | if (!PreprocessorToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 315 | CD->setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 316 | if (!TranslatorToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 317 | CD->setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 318 | if (!OptimizerToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 319 | CD->setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 320 | if (!AssemblerToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 321 | CD->setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 322 | if (!LinkerToolOpts.empty()) |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 323 | CD->setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 324 | |
Reid Spencer | 65bc4e0 | 2004-11-23 23:47:58 +0000 | [diff] [blame] | 325 | // Check for options that cause us to terminate before any significant work |
| 326 | // is done. |
| 327 | handleTerminatingOptions(CD); |
| 328 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 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; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 343 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 344 | if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) { |
| 345 | // Add a source file |
| 346 | InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos))); |
| 347 | ++fileIt; |
| 348 | } |
| 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 |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 355 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 356 | |
| 357 | // Tell the driver to do its thing |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 358 | int result = CD->execute(InpList,sys::Path(OutputFilename)); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 359 | if (result != 0) { |
Reid Spencer | 6386009 | 2004-08-30 00:06:52 +0000 | [diff] [blame] | 360 | throw std::string("Error executing actions. Terminated."); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 361 | return result; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 362 | } |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 363 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 364 | // All is good, return success |
| 365 | return 0; |
Reid Spencer | a3b4e09 | 2004-09-03 22:59:32 +0000 | [diff] [blame] | 366 | } catch (const std::string& msg) { |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 367 | std::cerr << argv[0] << ": " << msg << "\n"; |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 368 | } catch (...) { |
Reid Spencer | 2cf17a4 | 2004-08-24 14:05:30 +0000 | [diff] [blame] | 369 | std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 370 | } |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 371 | } |