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