Reid Spencer | a3f1855 | 2004-08-13 20:25:54 +0000 | [diff] [blame] | 1 | //===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===// |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 3 | // |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 6 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 7 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 8 | // |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
Reid Spencer | a3f1855 | 2004-08-13 20:25:54 +0000 | [diff] [blame] | 11 | // This file implements the bulk of the LLVM Compiler Driver (llvmc). |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 12 | // |
Chris Lattner | 74f48d1 | 2006-05-29 18:52:05 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 14 | |
| 15 | #include "CompilerDriver.h" |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 16 | #include "ConfigLexer.h" |
Reid Spencer | a01439a | 2004-08-24 13:55:17 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 18 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Timer.h" |
Reid Spencer | 93426ba | 2004-08-29 20:02:28 +0000 | [diff] [blame] | 20 | #include "llvm/System/Signals.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Brian Gaeke | fabf41f | 2004-12-20 04:02:01 +0000 | [diff] [blame] | 23 | #include "llvm/Config/alloca.h" |
Misha Brukman | baec07c | 2005-04-20 04:51:29 +0000 | [diff] [blame] | 24 | #include <iostream> |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 28 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 29 | void WriteAction(CompilerDriver::Action* action ) { |
| 30 | std::cerr << action->program.c_str(); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 31 | std::vector<std::string>::const_iterator I = action->args.begin(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 32 | while (I != action->args.end()) { |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 33 | std::cerr << ' ' << *I; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 34 | ++I; |
| 35 | } |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 36 | std::cerr << '\n'; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void DumpAction(CompilerDriver::Action* action) { |
| 40 | std::cerr << "command = " << action->program.c_str(); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 41 | std::vector<std::string>::const_iterator I = action->args.begin(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 42 | while (I != action->args.end()) { |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 43 | std::cerr << ' ' << *I; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 44 | ++I; |
| 45 | } |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 46 | std::cerr << '\n'; |
| 47 | std::cerr << "flags = " << action->flags << '\n'; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){ |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 51 | std::cerr << "Configuration Data For '" << cd->langName << "' (" << type |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 52 | << ")\n"; |
| 53 | std::cerr << "PreProcessor: "; |
| 54 | DumpAction(&cd->PreProcessor); |
| 55 | std::cerr << "Translator: "; |
| 56 | DumpAction(&cd->Translator); |
| 57 | std::cerr << "Optimizer: "; |
| 58 | DumpAction(&cd->Optimizer); |
| 59 | std::cerr << "Assembler: "; |
| 60 | DumpAction(&cd->Assembler); |
| 61 | std::cerr << "Linker: "; |
| 62 | DumpAction(&cd->Linker); |
| 63 | } |
| 64 | |
| 65 | /// This specifies the passes to run for OPT_FAST_COMPILE (-O1) |
| 66 | /// which should reduce the volume of code and make compilation |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 67 | /// faster. This is also safe on any llvm module. |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 68 | static const char* DefaultFastCompileOptimizations[] = { |
| 69 | "-simplifycfg", "-mem2reg", "-instcombine" |
| 70 | }; |
| 71 | |
| 72 | class CompilerDriverImpl : public CompilerDriver { |
| 73 | /// @name Constructors |
| 74 | /// @{ |
| 75 | public: |
| 76 | CompilerDriverImpl(ConfigDataProvider& confDatProv ) |
| 77 | : cdp(&confDatProv) |
| 78 | , finalPhase(LINKING) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 79 | , optLevel(OPT_FAST_COMPILE) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 80 | , Flags(0) |
| 81 | , machine() |
| 82 | , LibraryPaths() |
| 83 | , TempDir() |
| 84 | , AdditionalArgs() |
| 85 | { |
| 86 | TempDir = sys::Path::GetTemporaryDirectory(); |
| 87 | sys::RemoveDirectoryOnSignal(TempDir); |
| 88 | AdditionalArgs.reserve(NUM_PHASES); |
| 89 | StringVector emptyVec; |
| 90 | for (unsigned i = 0; i < NUM_PHASES; ++i) |
| 91 | AdditionalArgs.push_back(emptyVec); |
| 92 | } |
| 93 | |
| 94 | virtual ~CompilerDriverImpl() { |
| 95 | cleanup(); |
| 96 | cdp = 0; |
| 97 | LibraryPaths.clear(); |
| 98 | IncludePaths.clear(); |
| 99 | Defines.clear(); |
| 100 | TempDir.clear(); |
| 101 | AdditionalArgs.clear(); |
| 102 | fOptions.clear(); |
| 103 | MOptions.clear(); |
| 104 | WOptions.clear(); |
| 105 | } |
| 106 | |
| 107 | /// @} |
| 108 | /// @name Methods |
| 109 | /// @{ |
| 110 | public: |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 111 | virtual void setFinalPhase(Phases phase) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 112 | finalPhase = phase; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 115 | virtual void setOptimization(OptimizationLevels level) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 116 | optLevel = level; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 119 | virtual void setDriverFlags(unsigned flags) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 120 | Flags = flags & DRIVER_FLAGS_MASK; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 123 | virtual void setOutputMachine(const std::string& machineName) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 124 | machine = machineName; |
| 125 | } |
| 126 | |
| 127 | virtual void setPhaseArgs(Phases phase, const StringVector& opts) { |
| 128 | assert(phase <= LINKING && phase >= PREPROCESSING); |
| 129 | AdditionalArgs[phase] = opts; |
| 130 | } |
| 131 | |
| 132 | virtual void setIncludePaths(const StringVector& paths) { |
| 133 | StringVector::const_iterator I = paths.begin(); |
| 134 | StringVector::const_iterator E = paths.end(); |
| 135 | while (I != E) { |
| 136 | sys::Path tmp; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 137 | tmp.set(*I); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 138 | IncludePaths.push_back(tmp); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 139 | ++I; |
| 140 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 143 | virtual void setSymbolDefines(const StringVector& defs) { |
| 144 | Defines = defs; |
| 145 | } |
| 146 | |
| 147 | virtual void setLibraryPaths(const StringVector& paths) { |
| 148 | StringVector::const_iterator I = paths.begin(); |
| 149 | StringVector::const_iterator E = paths.end(); |
| 150 | while (I != E) { |
| 151 | sys::Path tmp; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 152 | tmp.set(*I); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 153 | LibraryPaths.push_back(tmp); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 154 | ++I; |
| 155 | } |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 158 | virtual void addLibraryPath(const sys::Path& libPath) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 159 | LibraryPaths.push_back(libPath); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 160 | } |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 161 | |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 162 | virtual void addToolPath(const sys::Path& toolPath) { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 163 | ToolPaths.push_back(toolPath); |
| 164 | } |
| 165 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 166 | virtual void setfPassThrough(const StringVector& fOpts) { |
| 167 | fOptions = fOpts; |
| 168 | } |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 169 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 170 | /// @brief Set the list of -M options to be passed through |
| 171 | virtual void setMPassThrough(const StringVector& MOpts) { |
| 172 | MOptions = MOpts; |
| 173 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 174 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 175 | /// @brief Set the list of -W options to be passed through |
| 176 | virtual void setWPassThrough(const StringVector& WOpts) { |
| 177 | WOptions = WOpts; |
| 178 | } |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 179 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 180 | /// @} |
| 181 | /// @name Functions |
| 182 | /// @{ |
| 183 | private: |
| 184 | bool isSet(DriverFlags flag) { |
| 185 | return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags); |
| 186 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 187 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 188 | void cleanup() { |
| 189 | if (!isSet(KEEP_TEMPS_FLAG)) { |
Chris Lattner | 33b0e9c | 2006-08-01 18:12:29 +0000 | [diff] [blame] | 190 | sys::FileStatus Status; |
| 191 | if (!TempDir.getFileStatus(Status) && Status.isDir) |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 192 | TempDir.eraseFromDisk(/*remove_contents=*/true); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 193 | } else { |
Reid Spencer | 12786d5 | 2004-12-13 08:53:36 +0000 | [diff] [blame] | 194 | std::cout << "Temporary files are in " << TempDir << "\n"; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 197 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 198 | sys::Path MakeTempFile(const std::string& basename, |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 199 | const std::string& suffix) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 200 | sys::Path result(TempDir); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 201 | if (!result.appendComponent(basename)) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 202 | throw basename + ": can't use this file name"; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 203 | if (!result.appendSuffix(suffix)) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 204 | throw suffix + ": can't use this file suffix"; |
| 205 | return result; |
| 206 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 207 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 208 | Action* GetAction(ConfigData* cd, |
| 209 | const sys::Path& input, |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 210 | const sys::Path& output, |
| 211 | Phases phase) |
| 212 | { |
| 213 | Action* pat = 0; ///< The pattern/template for the action |
| 214 | Action* action = new Action; ///< The actual action to execute |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 215 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 216 | // Get the action pattern |
| 217 | switch (phase) { |
| 218 | case PREPROCESSING: pat = &cd->PreProcessor; break; |
| 219 | case TRANSLATION: pat = &cd->Translator; break; |
| 220 | case OPTIMIZATION: pat = &cd->Optimizer; break; |
| 221 | case ASSEMBLY: pat = &cd->Assembler; break; |
| 222 | case LINKING: pat = &cd->Linker; break; |
| 223 | default: |
| 224 | assert(!"Invalid driver phase!"); |
| 225 | break; |
| 226 | } |
| 227 | assert(pat != 0 && "Invalid command pattern"); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 228 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 229 | // Copy over some pattern things that don't need to change |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 230 | action->flags = pat->flags; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 231 | |
Reid Spencer | 3b4c5d7 | 2006-08-16 20:31:44 +0000 | [diff] [blame] | 232 | // See if program starts with wildcard... |
| 233 | std::string programName=pat->program.toString(); |
| 234 | if (programName[0] == '%' && programName.length() >2) { |
| 235 | switch(programName[1]){ |
| 236 | case 'b': |
| 237 | if (programName.substr(0,8) == "%bindir%") { |
| 238 | std::string tmp(LLVM_BINDIR); |
| 239 | tmp.append(programName.substr(8)); |
| 240 | pat->program.set(tmp); |
| 241 | } |
| 242 | break; |
| 243 | case 'l': |
| 244 | if (programName.substr(0,12) == "%llvmgccdir%"){ |
| 245 | std::string tmp(LLVMGCCDIR); |
| 246 | tmp.append(programName.substr(12)); |
| 247 | pat->program.set(tmp); |
| 248 | }else if (programName.substr(0,13) == "%llvmgccarch%"){ |
| 249 | std::string tmp(LLVMGCCARCH); |
| 250 | tmp.append(programName.substr(13)); |
| 251 | pat->program.set(tmp); |
| 252 | }else if (programName.substr(0,9) == "%llvmgcc%"){ |
| 253 | std::string tmp(LLVMGCC); |
| 254 | tmp.append(programName.substr(9)); |
| 255 | pat->program.set(tmp); |
| 256 | }else if (programName.substr(0,9) == "%llvmgxx%"){ |
| 257 | std::string tmp(LLVMGXX); |
| 258 | tmp.append(programName.substr(9)); |
| 259 | pat->program.set(tmp); |
| 260 | }else if (programName.substr(0,9) == "%llvmcc1%"){ |
| 261 | std::string tmp(LLVMCC1); |
| 262 | tmp.append(programName.substr(9)); |
| 263 | pat->program.set(tmp); |
| 264 | }else if (programName.substr(0,13) == "%llvmcc1plus%"){ |
| 265 | std::string tmp(LLVMCC1PLUS); |
| 266 | tmp.append(programName.substr(13)); |
| 267 | pat->program.set(tmp); |
| 268 | }else if (programName.substr(0,8) == "%libdir%") { |
| 269 | std::string tmp(LLVM_LIBDIR); |
| 270 | tmp.append(programName.substr(8)); |
| 271 | pat->program.set(tmp); |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | action->program = pat->program; |
| 277 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 278 | // Do the substitutions from the pattern to the actual |
| 279 | StringVector::iterator PI = pat->args.begin(); |
| 280 | StringVector::iterator PE = pat->args.end(); |
| 281 | while (PI != PE) { |
| 282 | if ((*PI)[0] == '%' && PI->length() >2) { |
| 283 | bool found = true; |
| 284 | switch ((*PI)[1]) { |
| 285 | case 'a': |
| 286 | if (*PI == "%args%") { |
| 287 | if (AdditionalArgs.size() > unsigned(phase)) |
| 288 | if (!AdditionalArgs[phase].empty()) { |
| 289 | // Get specific options for each kind of action type |
| 290 | StringVector& addargs = AdditionalArgs[phase]; |
| 291 | // Add specific options for each kind of action type |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 292 | action->args.insert(action->args.end(), addargs.begin(), |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 293 | addargs.end()); |
| 294 | } |
| 295 | } else |
| 296 | found = false; |
| 297 | break; |
Reid Spencer | cc97cfc | 2005-05-19 00:52:28 +0000 | [diff] [blame] | 298 | case 'b': |
| 299 | if (*PI == "%bindir%") { |
| 300 | std::string tmp(*PI); |
| 301 | tmp.replace(0,8,LLVM_BINDIR); |
| 302 | action->args.push_back(tmp); |
| 303 | } else |
| 304 | found = false; |
| 305 | break; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 306 | case 'd': |
| 307 | if (*PI == "%defs%") { |
| 308 | StringVector::iterator I = Defines.begin(); |
| 309 | StringVector::iterator E = Defines.end(); |
| 310 | while (I != E) { |
| 311 | action->args.push_back( std::string("-D") + *I); |
| 312 | ++I; |
| 313 | } |
| 314 | } else |
| 315 | found = false; |
| 316 | break; |
| 317 | case 'f': |
| 318 | if (*PI == "%fOpts%") { |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 319 | if (!fOptions.empty()) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 320 | action->args.insert(action->args.end(), fOptions.begin(), |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 321 | fOptions.end()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 322 | } else |
| 323 | found = false; |
| 324 | break; |
| 325 | case 'i': |
| 326 | if (*PI == "%in%") { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 327 | action->args.push_back(input.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 328 | } else if (*PI == "%incls%") { |
| 329 | PathVector::iterator I = IncludePaths.begin(); |
| 330 | PathVector::iterator E = IncludePaths.end(); |
| 331 | while (I != E) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 332 | action->args.push_back( std::string("-I") + I->toString() ); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 333 | ++I; |
| 334 | } |
| 335 | } else |
| 336 | found = false; |
| 337 | break; |
| 338 | case 'l': |
Reid Spencer | cc97cfc | 2005-05-19 00:52:28 +0000 | [diff] [blame] | 339 | if ((*PI)[1] == 'l') { |
| 340 | std::string tmp(*PI); |
| 341 | if (*PI == "%llvmgccdir%") |
| 342 | tmp.replace(0,12,LLVMGCCDIR); |
| 343 | else if (*PI == "%llvmgccarch%") |
| 344 | tmp.replace(0,13,LLVMGCCARCH); |
| 345 | else if (*PI == "%llvmgcc%") |
| 346 | tmp.replace(0,9,LLVMGCC); |
| 347 | else if (*PI == "%llvmgxx%") |
| 348 | tmp.replace(0,9,LLVMGXX); |
| 349 | else if (*PI == "%llvmcc1%") |
| 350 | tmp.replace(0,9,LLVMCC1); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 351 | else if (*PI == "%llvmcc1plus%") |
Reid Spencer | cc97cfc | 2005-05-19 00:52:28 +0000 | [diff] [blame] | 352 | tmp.replace(0,9,LLVMCC1); |
| 353 | else |
| 354 | found = false; |
| 355 | if (found) |
| 356 | action->args.push_back(tmp); |
| 357 | } else if (*PI == "%libs%") { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 358 | PathVector::iterator I = LibraryPaths.begin(); |
| 359 | PathVector::iterator E = LibraryPaths.end(); |
| 360 | while (I != E) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 361 | action->args.push_back( std::string("-L") + I->toString() ); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 362 | ++I; |
| 363 | } |
Reid Spencer | cc97cfc | 2005-05-19 00:52:28 +0000 | [diff] [blame] | 364 | } else if (*PI == "%libdir%") { |
| 365 | std::string tmp(*PI); |
| 366 | tmp.replace(0,8,LLVM_LIBDIR); |
| 367 | action->args.push_back(tmp); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 368 | } else |
| 369 | found = false; |
| 370 | break; |
| 371 | case 'o': |
| 372 | if (*PI == "%out%") { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 373 | action->args.push_back(output.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 374 | } else if (*PI == "%opt%") { |
| 375 | if (!isSet(EMIT_RAW_FLAG)) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 376 | if (cd->opts.size() > static_cast<unsigned>(optLevel) && |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 377 | !cd->opts[optLevel].empty()) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 378 | action->args.insert(action->args.end(), |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 379 | cd->opts[optLevel].begin(), |
| 380 | cd->opts[optLevel].end()); |
| 381 | else |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 382 | throw std::string("Optimization options for level ") + |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 383 | utostr(unsigned(optLevel)) + " were not specified"; |
| 384 | } |
| 385 | } else |
| 386 | found = false; |
| 387 | break; |
| 388 | case 's': |
| 389 | if (*PI == "%stats%") { |
| 390 | if (isSet(SHOW_STATS_FLAG)) |
| 391 | action->args.push_back("-stats"); |
| 392 | } else |
| 393 | found = false; |
| 394 | break; |
| 395 | case 't': |
| 396 | if (*PI == "%target%") { |
| 397 | action->args.push_back(std::string("-march=") + machine); |
| 398 | } else if (*PI == "%time%") { |
| 399 | if (isSet(TIME_PASSES_FLAG)) |
| 400 | action->args.push_back("-time-passes"); |
| 401 | } else |
| 402 | found = false; |
| 403 | break; |
| 404 | case 'v': |
| 405 | if (*PI == "%verbose%") { |
| 406 | if (isSet(VERBOSE_FLAG)) |
| 407 | action->args.push_back("-v"); |
| 408 | } else |
| 409 | found = false; |
| 410 | break; |
| 411 | case 'M': |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 412 | if (*PI == "%Mopts%") { |
| 413 | if (!MOptions.empty()) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 414 | action->args.insert(action->args.end(), MOptions.begin(), |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 415 | MOptions.end()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 416 | } else |
| 417 | found = false; |
| 418 | break; |
| 419 | case 'W': |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 420 | if (*PI == "%Wopts%") { |
| 421 | for (StringVector::iterator I = WOptions.begin(), |
| 422 | E = WOptions.end(); I != E ; ++I ) { |
Misha Brukman | 0b86148 | 2005-05-03 20:30:34 +0000 | [diff] [blame] | 423 | action->args.push_back(std::string("-W") + *I); |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 424 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 425 | } else |
| 426 | found = false; |
| 427 | break; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 428 | default: |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 429 | found = false; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 430 | break; |
| 431 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 432 | if (!found) { |
| 433 | // Did it even look like a substitution? |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 434 | if (PI->length()>1 && (*PI)[0] == '%' && |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 435 | (*PI)[PI->length()-1] == '%') { |
| 436 | throw std::string("Invalid substitution token: '") + *PI + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 437 | "' for command '" + pat->program.toString() + "'"; |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 438 | } else if (!PI->empty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 439 | // It's not a legal substitution, just pass it through |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 440 | action->args.push_back(*PI); |
| 441 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 442 | } |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 443 | } else if (!PI->empty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 444 | // Its not a substitution, just put it in the action |
| 445 | action->args.push_back(*PI); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 446 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 447 | PI++; |
| 448 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 449 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 450 | // Finally, we're done |
| 451 | return action; |
| 452 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 453 | |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 454 | int DoAction(Action*action, std::string& ErrMsg) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 455 | assert(action != 0 && "Invalid Action!"); |
| 456 | if (isSet(VERBOSE_FLAG)) |
| 457 | WriteAction(action); |
| 458 | if (!isSet(DRY_RUN_FLAG)) { |
| 459 | sys::Path progpath = sys::Program::FindProgramByName( |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 460 | action->program.toString()); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 461 | if (progpath.isEmpty()) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 462 | throw std::string("Can't find program '" + |
| 463 | action->program.toString()+"'"); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 464 | else if (progpath.canExecute()) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 465 | action->program = progpath; |
| 466 | else |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 467 | throw std::string("Program '"+action->program.toString()+ |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 468 | "' is not executable."); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 469 | |
| 470 | // Invoke the program |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 471 | const char** Args = (const char**) |
Reid Spencer | c30088f | 2005-04-11 05:48:04 +0000 | [diff] [blame] | 472 | alloca(sizeof(const char*)*(action->args.size()+2)); |
| 473 | Args[0] = action->program.toString().c_str(); |
Reid Spencer | 3b4c5d7 | 2006-08-16 20:31:44 +0000 | [diff] [blame] | 474 | for (unsigned i = 1; i <= action->args.size(); ++i) |
| 475 | Args[i] = action->args[i-1].c_str(); |
| 476 | Args[action->args.size()+1] = 0; // null terminate list. |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 477 | if (isSet(TIME_ACTIONS_FLAG)) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 478 | Timer timer(action->program.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 479 | timer.startTimer(); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 480 | int resultCode = |
| 481 | sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 482 | timer.stopTimer(); |
| 483 | timer.print(timer,std::cerr); |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 484 | return resultCode; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 485 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 486 | else |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 487 | return |
| 488 | sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 489 | } |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 490 | return 0; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 491 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 492 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 493 | /// This method tries various variants of a linkage item's file |
| 494 | /// name to see if it can find an appropriate file to link with |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 495 | /// in the directories of the LibraryPaths. |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 496 | llvm::sys::Path GetPathForLinkageItem(const std::string& link_item, |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 497 | bool native = false) { |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 498 | sys::Path fullpath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 499 | fullpath.set(link_item); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 500 | if (fullpath.canRead()) |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 501 | return fullpath; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 502 | for (PathVector::iterator PI = LibraryPaths.begin(), |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 503 | PE = LibraryPaths.end(); PI != PE; ++PI) { |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 504 | fullpath.set(PI->toString()); |
| 505 | fullpath.appendComponent(link_item); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 506 | if (fullpath.canRead()) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 507 | return fullpath; |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 508 | if (native) { |
| 509 | fullpath.appendSuffix("a"); |
| 510 | } else { |
| 511 | fullpath.appendSuffix("bc"); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 512 | if (fullpath.canRead()) |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 513 | return fullpath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 514 | fullpath.eraseSuffix(); |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 515 | fullpath.appendSuffix("o"); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 516 | if (fullpath.canRead()) |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 517 | return fullpath; |
| 518 | fullpath = *PI; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 519 | fullpath.appendComponent(std::string("lib") + link_item); |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 520 | fullpath.appendSuffix("a"); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 521 | if (fullpath.canRead()) |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 522 | return fullpath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 523 | fullpath.eraseSuffix(); |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 524 | fullpath.appendSuffix("so"); |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 525 | if (fullpath.canRead()) |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 526 | return fullpath; |
| 527 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | // Didn't find one. |
| 531 | fullpath.clear(); |
| 532 | return fullpath; |
| 533 | } |
| 534 | |
| 535 | /// This method processes a linkage item. The item could be a |
| 536 | /// Bytecode file needing translation to native code and that is |
| 537 | /// dependent on other bytecode libraries, or a native code |
| 538 | /// library that should just be linked into the program. |
| 539 | bool ProcessLinkageItem(const llvm::sys::Path& link_item, |
| 540 | SetVector<sys::Path>& set, |
| 541 | std::string& err) { |
| 542 | // First, see if the unadorned file name is not readable. If so, |
| 543 | // we must track down the file in the lib search path. |
| 544 | sys::Path fullpath; |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 545 | if (!link_item.canRead()) { |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 546 | // look for the library using the -L arguments specified |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 547 | // on the command line. |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 548 | fullpath = GetPathForLinkageItem(link_item.toString()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 549 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 550 | // If we didn't find the file in any of the library search paths |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 551 | // we have to bail. No where else to look. |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 552 | if (fullpath.isEmpty()) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 553 | err = |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 554 | std::string("Can't find linkage item '") + link_item.toString() + "'"; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 555 | return false; |
| 556 | } |
| 557 | } else { |
| 558 | fullpath = link_item; |
| 559 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 560 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 561 | // If we got here fullpath is the path to the file, and its readable. |
| 562 | set.insert(fullpath); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 563 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 564 | // If its an LLVM bytecode file ... |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 565 | if (fullpath.isBytecodeFile()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 566 | // Process the dependent libraries recursively |
| 567 | Module::LibraryListType modlibs; |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 568 | if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 569 | // Traverse the dependent libraries list |
| 570 | Module::lib_iterator LI = modlibs.begin(); |
| 571 | Module::lib_iterator LE = modlibs.end(); |
| 572 | while ( LI != LE ) { |
| 573 | if (!ProcessLinkageItem(sys::Path(*LI),set,err)) { |
| 574 | if (err.empty()) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 575 | err = std::string("Library '") + *LI + |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 576 | "' is not valid for linking but is required by file '" + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 577 | fullpath.toString() + "'"; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 578 | } else { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 579 | err += " which is required by file '" + fullpath.toString() + "'"; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 580 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 581 | return false; |
| 582 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 583 | ++LI; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 584 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 585 | } else if (err.empty()) { |
| 586 | err = std::string( |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 587 | "The dependent libraries could not be extracted from '") + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 588 | fullpath.toString(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 589 | return false; |
| 590 | } |
| 591 | } |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | /// @} |
| 596 | /// @name Methods |
| 597 | /// @{ |
| 598 | public: |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 599 | virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 600 | try { |
| 601 | // Echo the configuration of options if we're running verbose |
| 602 | if (isSet(DEBUG_FLAG)) { |
| 603 | std::cerr << "Compiler Driver Options:\n"; |
| 604 | std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n"; |
| 605 | std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n"; |
| 606 | std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n"; |
| 607 | std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n"; |
| 608 | std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n"; |
| 609 | std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n"; |
| 610 | std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n"; |
| 611 | std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n"; |
| 612 | std::cerr << "OutputMachine = " << machine << "\n"; |
| 613 | InputList::const_iterator I = InpList.begin(); |
| 614 | while ( I != InpList.end() ) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 615 | std::cerr << "Input: " << I->first << "(" << I->second |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 616 | << ")\n"; |
| 617 | ++I; |
| 618 | } |
Reid Spencer | 12786d5 | 2004-12-13 08:53:36 +0000 | [diff] [blame] | 619 | std::cerr << "Output: " << Output << "\n"; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 622 | // If there's no input, we're done. |
| 623 | if (InpList.empty()) |
| 624 | throw std::string("Nothing to compile."); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 625 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 626 | // If they are asking for linking and didn't provide an output |
| 627 | // file then its an error (no way for us to "make up" a meaningful |
| 628 | // file name based on the various linker input files). |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 629 | if (finalPhase == LINKING && Output.isEmpty()) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 630 | throw std::string( |
| 631 | "An output file name must be specified for linker output"); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 632 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 633 | // If they are not asking for linking, provided an output file and |
| 634 | // there is more than one input file, its an error |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 635 | if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 636 | throw std::string("An output file name cannot be specified ") + |
| 637 | "with more than one input file name when not linking"; |
| 638 | |
| 639 | // This vector holds all the resulting actions of the following loop. |
| 640 | std::vector<Action*> actions; |
| 641 | |
| 642 | /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases |
| 643 | // for each input item |
| 644 | SetVector<sys::Path> LinkageItems; |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 645 | StringVector LibFiles; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 646 | InputList::const_iterator I = InpList.begin(); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 647 | for (InputList::const_iterator I = InpList.begin(), E = InpList.end(); |
| 648 | I != E; ++I ) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 649 | // Get the suffix of the file name |
| 650 | const std::string& ftype = I->second; |
| 651 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 652 | // If its a library, bytecode file, or object file, save |
| 653 | // it for linking below and short circuit the |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 654 | // pre-processing/translation/assembly phases |
| 655 | if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 656 | // We shouldn't get any of these types of files unless we're |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 657 | // later going to link. Enforce this limit now. |
| 658 | if (finalPhase != LINKING) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 659 | throw std::string( |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 660 | "Pre-compiled objects found but linking not requested"); |
| 661 | } |
| 662 | if (ftype.empty()) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 663 | LibFiles.push_back(I->first.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 664 | else |
| 665 | LinkageItems.insert(I->first); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 666 | continue; // short circuit remainder of loop |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 667 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 668 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 669 | // At this point, we know its something we need to translate |
| 670 | // and/or optimize. See if we can get the configuration data |
| 671 | // for this kind of file. |
| 672 | ConfigData* cd = cdp->ProvideConfigData(I->second); |
| 673 | if (cd == 0) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 674 | throw std::string("Files of type '") + I->second + |
| 675 | "' are not recognized."; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 676 | if (isSet(DEBUG_FLAG)) |
| 677 | DumpConfigData(cd,I->second); |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 678 | |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 679 | // Add the config data's library paths to the end of the list |
| 680 | for (StringVector::iterator LPI = cd->libpaths.begin(), |
| 681 | LPE = cd->libpaths.end(); LPI != LPE; ++LPI){ |
| 682 | LibraryPaths.push_back(sys::Path(*LPI)); |
| 683 | } |
| 684 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 685 | // Initialize the input and output files |
| 686 | sys::Path InFile(I->first); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 687 | sys::Path OutFile(I->first.getBasename()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 688 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 689 | // PRE-PROCESSING PHASE |
| 690 | Action& action = cd->PreProcessor; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 691 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 692 | // Get the preprocessing action, if needed, or error if appropriate |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 693 | if (!action.program.isEmpty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 694 | if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) { |
| 695 | if (finalPhase == PREPROCESSING) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 696 | if (Output.isEmpty()) { |
| 697 | OutFile.appendSuffix("E"); |
| 698 | actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING)); |
| 699 | } else { |
| 700 | actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING)); |
| 701 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 702 | } else { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 703 | sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E")); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 704 | actions.push_back(GetAction(cd,InFile,TempFile, |
| 705 | PREPROCESSING)); |
| 706 | InFile = TempFile; |
| 707 | } |
| 708 | } |
| 709 | } else if (finalPhase == PREPROCESSING) { |
| 710 | throw cd->langName + " does not support pre-processing"; |
| 711 | } else if (action.isSet(REQUIRED_FLAG)) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 712 | throw std::string("Don't know how to pre-process ") + |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 713 | cd->langName + " files"; |
| 714 | } |
| 715 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 716 | // Short-circuit remaining actions if all they want is |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 717 | // pre-processing |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 718 | if (finalPhase == PREPROCESSING) { continue; }; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 719 | |
| 720 | /// TRANSLATION PHASE |
| 721 | action = cd->Translator; |
| 722 | |
| 723 | // Get the translation action, if needed, or error if appropriate |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 724 | if (!action.program.isEmpty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 725 | if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) { |
| 726 | if (finalPhase == TRANSLATION) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 727 | if (Output.isEmpty()) { |
| 728 | OutFile.appendSuffix("o"); |
| 729 | actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION)); |
| 730 | } else { |
| 731 | actions.push_back(GetAction(cd,InFile,Output,TRANSLATION)); |
| 732 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 733 | } else { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 734 | sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans")); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 735 | actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION)); |
| 736 | InFile = TempFile; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 739 | // ll -> bc Helper |
| 740 | if (action.isSet(OUTPUT_IS_ASM_FLAG)) { |
| 741 | /// The output of the translator is an LLVM Assembly program |
| 742 | /// We need to translate it to bytecode |
| 743 | Action* action = new Action(); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 744 | action->program.set("llvm-as"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 745 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 746 | action->args.push_back("-o"); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 747 | InFile.appendSuffix("bc"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 748 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 749 | actions.push_back(action); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 750 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 751 | } |
| 752 | } else if (finalPhase == TRANSLATION) { |
| 753 | throw cd->langName + " does not support translation"; |
| 754 | } else if (action.isSet(REQUIRED_FLAG)) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 755 | throw std::string("Don't know how to translate ") + |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 756 | cd->langName + " files"; |
| 757 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 758 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 759 | // Short-circuit remaining actions if all they want is translation |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 760 | if (finalPhase == TRANSLATION) { continue; } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 761 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 762 | /// OPTIMIZATION PHASE |
| 763 | action = cd->Optimizer; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 764 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 765 | // Get the optimization action, if needed, or error if appropriate |
| 766 | if (!isSet(EMIT_RAW_FLAG)) { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 767 | if (!action.program.isEmpty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 768 | if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) { |
| 769 | if (finalPhase == OPTIMIZATION) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 770 | if (Output.isEmpty()) { |
| 771 | OutFile.appendSuffix("o"); |
| 772 | actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION)); |
| 773 | } else { |
| 774 | actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION)); |
| 775 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 776 | } else { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 777 | sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt")); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 778 | actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION)); |
| 779 | InFile = TempFile; |
| 780 | } |
| 781 | // ll -> bc Helper |
| 782 | if (action.isSet(OUTPUT_IS_ASM_FLAG)) { |
| 783 | /// The output of the optimizer is an LLVM Assembly program |
| 784 | /// We need to translate it to bytecode with llvm-as |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 785 | Action* action = new Action(); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 786 | action->program.set("llvm-as"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 787 | action->args.push_back(InFile.toString()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 788 | action->args.push_back("-f"); |
| 789 | action->args.push_back("-o"); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 790 | InFile.appendSuffix("bc"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 791 | action->args.push_back(InFile.toString()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 792 | actions.push_back(action); |
| 793 | } |
| 794 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 795 | } else if (finalPhase == OPTIMIZATION) { |
| 796 | throw cd->langName + " does not support optimization"; |
| 797 | } else if (action.isSet(REQUIRED_FLAG)) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 798 | throw std::string("Don't know how to optimize ") + |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 799 | cd->langName + " files"; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 800 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 801 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 802 | |
| 803 | // Short-circuit remaining actions if all they want is optimization |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 804 | if (finalPhase == OPTIMIZATION) { continue; } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 805 | |
| 806 | /// ASSEMBLY PHASE |
| 807 | action = cd->Assembler; |
| 808 | |
| 809 | if (finalPhase == ASSEMBLY) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 810 | |
| 811 | // Build either a native compilation action or a disassembly action |
| 812 | Action* action = new Action(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 813 | if (isSet(EMIT_NATIVE_FLAG)) { |
| 814 | // Use llc to get the native assembly file |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 815 | action->program.set("llc"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 816 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 817 | action->args.push_back("-f"); |
| 818 | action->args.push_back("-o"); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 819 | if (Output.isEmpty()) { |
| 820 | OutFile.appendSuffix("o"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 821 | action->args.push_back(OutFile.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 822 | } else { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 823 | action->args.push_back(Output.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 824 | } |
| 825 | actions.push_back(action); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 826 | } else { |
| 827 | // Just convert back to llvm assembly with llvm-dis |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 828 | action->program.set("llvm-dis"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 829 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 830 | action->args.push_back("-f"); |
| 831 | action->args.push_back("-o"); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 832 | if (Output.isEmpty()) { |
| 833 | OutFile.appendSuffix("ll"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 834 | action->args.push_back(OutFile.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 835 | } else { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 836 | action->args.push_back(Output.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 837 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 840 | // Put the action on the list |
| 841 | actions.push_back(action); |
| 842 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 843 | // Short circuit the rest of the loop, we don't want to link |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 844 | continue; |
| 845 | } |
| 846 | |
| 847 | // Register the result of the actions as a link candidate |
| 848 | LinkageItems.insert(InFile); |
| 849 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 850 | } // end while loop over each input file |
| 851 | |
| 852 | /// RUN THE COMPILATION ACTIONS |
| 853 | std::vector<Action*>::iterator AI = actions.begin(); |
| 854 | std::vector<Action*>::iterator AE = actions.end(); |
| 855 | while (AI != AE) { |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 856 | int ActionResult = DoAction(*AI, ErrMsg); |
| 857 | if (ActionResult != 0) |
| 858 | return ActionResult; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 859 | AI++; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 862 | /// LINKING PHASE |
| 863 | if (finalPhase == LINKING) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 864 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 865 | // Insert the platform-specific system libraries to the path list |
Reid Spencer | 11db4b8 | 2004-12-13 03:01:26 +0000 | [diff] [blame] | 866 | std::vector<sys::Path> SysLibs; |
| 867 | sys::Path::GetSystemLibraryPaths(SysLibs); |
| 868 | LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 869 | |
| 870 | // Set up the linking action with llvm-ld |
| 871 | Action* link = new Action(); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 872 | link->program.set("llvm-ld"); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 873 | |
| 874 | // Add in the optimization level requested |
| 875 | switch (optLevel) { |
| 876 | case OPT_FAST_COMPILE: |
| 877 | link->args.push_back("-O1"); |
| 878 | break; |
| 879 | case OPT_SIMPLE: |
| 880 | link->args.push_back("-O2"); |
| 881 | break; |
| 882 | case OPT_AGGRESSIVE: |
| 883 | link->args.push_back("-O3"); |
| 884 | break; |
| 885 | case OPT_LINK_TIME: |
| 886 | link->args.push_back("-O4"); |
| 887 | break; |
| 888 | case OPT_AGGRESSIVE_LINK_TIME: |
| 889 | link->args.push_back("-O5"); |
| 890 | break; |
| 891 | case OPT_NONE: |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | // Add in all the linkage items we generated. This includes the |
| 896 | // output from the translation/optimization phases as well as any |
| 897 | // -l arguments specified. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 898 | for (PathVector::const_iterator I=LinkageItems.begin(), |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 899 | E=LinkageItems.end(); I != E; ++I ) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 900 | link->args.push_back(I->toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 901 | |
| 902 | // Add in all the libraries we found. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 903 | for (StringVector::const_iterator I=LibFiles.begin(), |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 904 | E=LibFiles.end(); I != E; ++I ) |
| 905 | link->args.push_back(std::string("-l")+*I); |
| 906 | |
| 907 | // Add in all the library paths to the command line |
| 908 | for (PathVector::const_iterator I=LibraryPaths.begin(), |
| 909 | E=LibraryPaths.end(); I != E; ++I) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 910 | link->args.push_back( std::string("-L") + I->toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 911 | |
| 912 | // Add in the additional linker arguments requested |
| 913 | for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(), |
| 914 | E=AdditionalArgs[LINKING].end(); I != E; ++I) |
| 915 | link->args.push_back( *I ); |
| 916 | |
| 917 | // Add in other optional flags |
| 918 | if (isSet(EMIT_NATIVE_FLAG)) |
| 919 | link->args.push_back("-native"); |
| 920 | if (isSet(VERBOSE_FLAG)) |
| 921 | link->args.push_back("-v"); |
| 922 | if (isSet(TIME_PASSES_FLAG)) |
| 923 | link->args.push_back("-time-passes"); |
| 924 | if (isSet(SHOW_STATS_FLAG)) |
| 925 | link->args.push_back("-stats"); |
| 926 | if (isSet(STRIP_OUTPUT_FLAG)) |
| 927 | link->args.push_back("-s"); |
| 928 | if (isSet(DEBUG_FLAG)) { |
| 929 | link->args.push_back("-debug"); |
| 930 | link->args.push_back("-debug-pass=Details"); |
| 931 | } |
| 932 | |
| 933 | // Add in mandatory flags |
| 934 | link->args.push_back("-o"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 935 | link->args.push_back(Output.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 936 | |
| 937 | // Execute the link |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame^] | 938 | int ActionResult = DoAction(link, ErrMsg); |
| 939 | if (ActionResult != 0) |
| 940 | return ActionResult; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 941 | } |
| 942 | } catch (std::string& msg) { |
| 943 | cleanup(); |
| 944 | throw; |
| 945 | } catch (...) { |
| 946 | cleanup(); |
| 947 | throw std::string("Unspecified error"); |
| 948 | } |
| 949 | cleanup(); |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | /// @} |
| 954 | /// @name Data |
| 955 | /// @{ |
| 956 | private: |
| 957 | ConfigDataProvider* cdp; ///< Where we get configuration data from |
| 958 | Phases finalPhase; ///< The final phase of compilation |
| 959 | OptimizationLevels optLevel; ///< The optimization level to apply |
| 960 | unsigned Flags; ///< The driver flags |
| 961 | std::string machine; ///< Target machine name |
| 962 | PathVector LibraryPaths; ///< -L options |
| 963 | PathVector IncludePaths; ///< -I options |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 964 | PathVector ToolPaths; ///< -B options |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 965 | StringVector Defines; ///< -D options |
| 966 | sys::Path TempDir; ///< Name of the temporary directory. |
| 967 | StringTable AdditionalArgs; ///< The -Txyz options |
| 968 | StringVector fOptions; ///< -f options |
| 969 | StringVector MOptions; ///< -M options |
| 970 | StringVector WOptions; ///< -W options |
| 971 | |
| 972 | /// @} |
| 973 | }; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | CompilerDriver::~CompilerDriver() { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Reid Spencer | cc97cfc | 2005-05-19 00:52:28 +0000 | [diff] [blame] | 979 | CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {} |
| 980 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 981 | CompilerDriver* |
| 982 | CompilerDriver::Get(ConfigDataProvider& CDP) { |
| 983 | return new CompilerDriverImpl(CDP); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | CompilerDriver::ConfigData::ConfigData() |
| 987 | : langName() |
| 988 | , PreProcessor() |
| 989 | , Translator() |
| 990 | , Optimizer() |
| 991 | , Assembler() |
| 992 | , Linker() |
| 993 | { |
| 994 | StringVector emptyVec; |
| 995 | for (unsigned i = 0; i < NUM_PHASES; ++i) |
| 996 | opts.push_back(emptyVec); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 997 | } |