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