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