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