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