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