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