blob: 4be9f138ae27c5b88494273d5fa23684dd24d558 [file] [log] [blame]
Reid Spencera3f18552004-08-13 20:25:54 +00001//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
Reid Spencer5c56dc12004-08-13 20:22:43 +00002//
Misha Brukman3da94ae2005-04-22 00:00:37 +00003//
Reid Spencer5c56dc12004-08-13 20:22:43 +00004// The LLVM Compiler Infrastructure
5//
Misha Brukman3da94ae2005-04-22 00:00:37 +00006// This file was developed by Reid Spencer and is distributed under the
Reid Spencer5c56dc12004-08-13 20:22:43 +00007// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00008//
Reid Spencer5c56dc12004-08-13 20:22:43 +00009//===----------------------------------------------------------------------===//
10//
Reid Spencera3f18552004-08-13 20:25:54 +000011// This file implements the bulk of the LLVM Compiler Driver (llvmc).
Reid Spencer5c56dc12004-08-13 20:22:43 +000012//
Chris Lattner74f48d12006-05-29 18:52:05 +000013//===----------------------------------------------------------------------===//
Reid Spencer5c56dc12004-08-13 20:22:43 +000014
15#include "CompilerDriver.h"
Reid Spencerbf437722004-08-15 08:19:46 +000016#include "ConfigLexer.h"
Reid Spencera01439a2004-08-24 13:55:17 +000017#include "llvm/Module.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000018#include "llvm/ModuleProvider.h"
Chris Lattner5e8edbb2007-05-06 05:51:37 +000019#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner5e8edbb2007-05-06 05:51:37 +000020#include "llvm/Support/MemoryBuffer.h"
Reid Spencer54fafe42004-09-14 01:58:45 +000021#include "llvm/Support/Timer.h"
Reid Spencer93426ba2004-08-29 20:02:28 +000022#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/StringExtras.h"
Brian Gaekefabf41f2004-12-20 04:02:01 +000025#include "llvm/Config/alloca.h"
Misha Brukmanbaec07c2005-04-20 04:51:29 +000026#include <iostream>
Reid Spencer5c56dc12004-08-13 20:22:43 +000027using namespace llvm;
28
Chris Lattner5e8edbb2007-05-06 05:51:37 +000029
Reid Spencer5c56dc12004-08-13 20:22:43 +000030namespace {
Reid Spencer5c56dc12004-08-13 20:22:43 +000031
Reid Spenceraf77d742004-10-28 04:05:06 +000032void WriteAction(CompilerDriver::Action* action ) {
33 std::cerr << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000034 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000035 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000036 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000037 ++I;
38 }
Misha Brukman0b861482005-05-03 20:30:34 +000039 std::cerr << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000040}
41
42void DumpAction(CompilerDriver::Action* action) {
43 std::cerr << "command = " << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000044 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000045 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000046 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000047 ++I;
48 }
Misha Brukman0b861482005-05-03 20:30:34 +000049 std::cerr << '\n';
50 std::cerr << "flags = " << action->flags << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000051}
52
53void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
Misha Brukman3da94ae2005-04-22 00:00:37 +000054 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
Reid Spenceraf77d742004-10-28 04:05:06 +000055 << ")\n";
56 std::cerr << "PreProcessor: ";
57 DumpAction(&cd->PreProcessor);
58 std::cerr << "Translator: ";
59 DumpAction(&cd->Translator);
60 std::cerr << "Optimizer: ";
61 DumpAction(&cd->Optimizer);
62 std::cerr << "Assembler: ";
63 DumpAction(&cd->Assembler);
64 std::cerr << "Linker: ";
65 DumpAction(&cd->Linker);
66}
67
Gabor Greifa99be512007-07-05 17:07:56 +000068static bool GetBitcodeDependentLibraries(const std::string &fname,
69 Module::LibraryListType& deplibs,
70 std::string* ErrMsg) {
Chris Lattner5e8edbb2007-05-06 05:51:37 +000071 ModuleProvider *MP = 0;
Chris Lattner065344d2007-05-06 23:45:49 +000072 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(fname)) {
Chris Lattner44dadff2007-05-06 09:29:57 +000073 MP = getBitcodeModuleProvider(Buffer);
74 if (MP == 0) delete Buffer;
Chris Lattner5e8edbb2007-05-06 05:51:37 +000075 }
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000076 if (!MP) {
77 deplibs.clear();
78 return true;
79 }
Chris Lattner5e8edbb2007-05-06 05:51:37 +000080 deplibs = MP->getModule()->getLibraries();
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000081 delete MP;
82 return false;
83}
84
85
Reid Spenceraf77d742004-10-28 04:05:06 +000086class CompilerDriverImpl : public CompilerDriver {
87/// @name Constructors
88/// @{
89public:
90 CompilerDriverImpl(ConfigDataProvider& confDatProv )
91 : cdp(&confDatProv)
92 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000093 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000094 , Flags(0)
95 , machine()
96 , LibraryPaths()
97 , TempDir()
98 , AdditionalArgs()
99 {
Reid Spenceraf77d742004-10-28 04:05:06 +0000100 AdditionalArgs.reserve(NUM_PHASES);
101 StringVector emptyVec;
102 for (unsigned i = 0; i < NUM_PHASES; ++i)
103 AdditionalArgs.push_back(emptyVec);
104 }
105
106 virtual ~CompilerDriverImpl() {
107 cleanup();
108 cdp = 0;
109 LibraryPaths.clear();
110 IncludePaths.clear();
111 Defines.clear();
112 TempDir.clear();
113 AdditionalArgs.clear();
114 fOptions.clear();
115 MOptions.clear();
116 WOptions.clear();
117 }
118
119/// @}
120/// @name Methods
121/// @{
122public:
Misha Brukman0b861482005-05-03 20:30:34 +0000123 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000124 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000125 }
126
Misha Brukman0b861482005-05-03 20:30:34 +0000127 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000128 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000129 }
130
Misha Brukman0b861482005-05-03 20:30:34 +0000131 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000132 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000133 }
134
Misha Brukman0b861482005-05-03 20:30:34 +0000135 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000136 machine = machineName;
137 }
138
139 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
140 assert(phase <= LINKING && phase >= PREPROCESSING);
141 AdditionalArgs[phase] = opts;
142 }
143
144 virtual void setIncludePaths(const StringVector& paths) {
145 StringVector::const_iterator I = paths.begin();
146 StringVector::const_iterator E = paths.end();
147 while (I != E) {
148 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000149 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000150 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000151 ++I;
152 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000153 }
154
Reid Spenceraf77d742004-10-28 04:05:06 +0000155 virtual void setSymbolDefines(const StringVector& defs) {
156 Defines = defs;
157 }
158
159 virtual void setLibraryPaths(const StringVector& paths) {
160 StringVector::const_iterator I = paths.begin();
161 StringVector::const_iterator E = paths.end();
162 while (I != E) {
163 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000164 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000165 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000166 ++I;
167 }
Reid Spencerbf437722004-08-15 08:19:46 +0000168 }
169
Misha Brukman0b861482005-05-03 20:30:34 +0000170 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000171 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000172 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000173
Misha Brukman0b861482005-05-03 20:30:34 +0000174 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000175 ToolPaths.push_back(toolPath);
176 }
177
Reid Spenceraf77d742004-10-28 04:05:06 +0000178 virtual void setfPassThrough(const StringVector& fOpts) {
179 fOptions = fOpts;
180 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000181
Reid Spenceraf77d742004-10-28 04:05:06 +0000182 /// @brief Set the list of -M options to be passed through
183 virtual void setMPassThrough(const StringVector& MOpts) {
184 MOptions = MOpts;
185 }
Reid Spencerbae68252004-08-19 04:49:47 +0000186
Reid Spenceraf77d742004-10-28 04:05:06 +0000187 /// @brief Set the list of -W options to be passed through
188 virtual void setWPassThrough(const StringVector& WOpts) {
189 WOptions = WOpts;
190 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000191
Reid Spenceraf77d742004-10-28 04:05:06 +0000192/// @}
193/// @name Functions
194/// @{
195private:
196 bool isSet(DriverFlags flag) {
197 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
198 }
Reid Spencerbae68252004-08-19 04:49:47 +0000199
Reid Spenceraf77d742004-10-28 04:05:06 +0000200 void cleanup() {
201 if (!isSet(KEEP_TEMPS_FLAG)) {
Reid Spencerb9125b12007-04-08 20:08:01 +0000202 const sys::FileStatus *Status = TempDir.getFileStatus();
Reid Spencer8475ec02007-03-29 19:05:44 +0000203 if (Status && Status->isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000204 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000205 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000206 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000207 }
208 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000209
Misha Brukman3da94ae2005-04-22 00:00:37 +0000210 sys::Path MakeTempFile(const std::string& basename,
Reid Spencer48744762006-08-22 19:01:30 +0000211 const std::string& suffix,
212 std::string* ErrMsg) {
213 if (TempDir.isEmpty()) {
214 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
215 if (TempDir.isEmpty())
216 return sys::Path();
217 sys::RemoveDirectoryOnSignal(TempDir);
218 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000219 sys::Path result(TempDir);
Reid Spencer48744762006-08-22 19:01:30 +0000220 if (!result.appendComponent(basename)) {
221 if (ErrMsg)
222 *ErrMsg = basename + ": can't use this file name";
223 return sys::Path();
224 }
225 if (!result.appendSuffix(suffix)) {
226 if (ErrMsg)
227 *ErrMsg = suffix + ": can't use this file suffix";
228 return sys::Path();
229 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000230 return result;
231 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000232
Misha Brukman3da94ae2005-04-22 00:00:37 +0000233 Action* GetAction(ConfigData* cd,
234 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000235 const sys::Path& output,
236 Phases phase)
237 {
238 Action* pat = 0; ///< The pattern/template for the action
239 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000240
Reid Spenceraf77d742004-10-28 04:05:06 +0000241 // Get the action pattern
242 switch (phase) {
243 case PREPROCESSING: pat = &cd->PreProcessor; break;
244 case TRANSLATION: pat = &cd->Translator; break;
245 case OPTIMIZATION: pat = &cd->Optimizer; break;
246 case ASSEMBLY: pat = &cd->Assembler; break;
247 case LINKING: pat = &cd->Linker; break;
248 default:
249 assert(!"Invalid driver phase!");
250 break;
251 }
252 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000253
Reid Spenceraf77d742004-10-28 04:05:06 +0000254 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000255 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000256
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000257 // See if program starts with wildcard...
258 std::string programName=pat->program.toString();
259 if (programName[0] == '%' && programName.length() >2) {
260 switch(programName[1]){
261 case 'b':
262 if (programName.substr(0,8) == "%bindir%") {
263 std::string tmp(LLVM_BINDIR);
264 tmp.append(programName.substr(8));
265 pat->program.set(tmp);
266 }
267 break;
268 case 'l':
269 if (programName.substr(0,12) == "%llvmgccdir%"){
270 std::string tmp(LLVMGCCDIR);
271 tmp.append(programName.substr(12));
272 pat->program.set(tmp);
273 }else if (programName.substr(0,13) == "%llvmgccarch%"){
274 std::string tmp(LLVMGCCARCH);
275 tmp.append(programName.substr(13));
276 pat->program.set(tmp);
277 }else if (programName.substr(0,9) == "%llvmgcc%"){
278 std::string tmp(LLVMGCC);
279 tmp.append(programName.substr(9));
280 pat->program.set(tmp);
281 }else if (programName.substr(0,9) == "%llvmgxx%"){
282 std::string tmp(LLVMGXX);
283 tmp.append(programName.substr(9));
284 pat->program.set(tmp);
285 }else if (programName.substr(0,9) == "%llvmcc1%"){
286 std::string tmp(LLVMCC1);
287 tmp.append(programName.substr(9));
288 pat->program.set(tmp);
289 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
290 std::string tmp(LLVMCC1PLUS);
291 tmp.append(programName.substr(13));
292 pat->program.set(tmp);
293 }else if (programName.substr(0,8) == "%libdir%") {
294 std::string tmp(LLVM_LIBDIR);
295 tmp.append(programName.substr(8));
296 pat->program.set(tmp);
297 }
298 break;
299 }
300 }
301 action->program = pat->program;
302
Reid Spenceraf77d742004-10-28 04:05:06 +0000303 // Do the substitutions from the pattern to the actual
304 StringVector::iterator PI = pat->args.begin();
305 StringVector::iterator PE = pat->args.end();
306 while (PI != PE) {
307 if ((*PI)[0] == '%' && PI->length() >2) {
308 bool found = true;
309 switch ((*PI)[1]) {
310 case 'a':
311 if (*PI == "%args%") {
312 if (AdditionalArgs.size() > unsigned(phase))
313 if (!AdditionalArgs[phase].empty()) {
314 // Get specific options for each kind of action type
315 StringVector& addargs = AdditionalArgs[phase];
316 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000317 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000318 addargs.end());
319 }
320 } else
321 found = false;
322 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000323 case 'b':
324 if (*PI == "%bindir%") {
325 std::string tmp(*PI);
326 tmp.replace(0,8,LLVM_BINDIR);
327 action->args.push_back(tmp);
328 } else
329 found = false;
330 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000331 case 'd':
332 if (*PI == "%defs%") {
333 StringVector::iterator I = Defines.begin();
334 StringVector::iterator E = Defines.end();
335 while (I != E) {
336 action->args.push_back( std::string("-D") + *I);
337 ++I;
338 }
339 } else
340 found = false;
341 break;
342 case 'f':
343 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000344 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000345 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000346 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000347 } else
348 found = false;
349 break;
350 case 'i':
351 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000352 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000353 } else if (*PI == "%incls%") {
354 PathVector::iterator I = IncludePaths.begin();
355 PathVector::iterator E = IncludePaths.end();
356 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000357 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000358 ++I;
359 }
360 } else
361 found = false;
362 break;
363 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000364 if ((*PI)[1] == 'l') {
365 std::string tmp(*PI);
366 if (*PI == "%llvmgccdir%")
367 tmp.replace(0,12,LLVMGCCDIR);
368 else if (*PI == "%llvmgccarch%")
369 tmp.replace(0,13,LLVMGCCARCH);
370 else if (*PI == "%llvmgcc%")
371 tmp.replace(0,9,LLVMGCC);
372 else if (*PI == "%llvmgxx%")
373 tmp.replace(0,9,LLVMGXX);
374 else if (*PI == "%llvmcc1%")
375 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000376 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000377 tmp.replace(0,9,LLVMCC1);
378 else
379 found = false;
380 if (found)
381 action->args.push_back(tmp);
382 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000383 PathVector::iterator I = LibraryPaths.begin();
384 PathVector::iterator E = LibraryPaths.end();
385 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000386 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000387 ++I;
388 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000389 } else if (*PI == "%libdir%") {
390 std::string tmp(*PI);
391 tmp.replace(0,8,LLVM_LIBDIR);
392 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000393 } else
394 found = false;
395 break;
396 case 'o':
397 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000398 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000399 } else if (*PI == "%opt%") {
400 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000401 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000402 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000403 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000404 cd->opts[optLevel].begin(),
405 cd->opts[optLevel].end());
406 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000407 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000408 utostr(unsigned(optLevel)) + " were not specified";
409 }
410 } else
411 found = false;
412 break;
413 case 's':
414 if (*PI == "%stats%") {
415 if (isSet(SHOW_STATS_FLAG))
416 action->args.push_back("-stats");
417 } else
418 found = false;
419 break;
420 case 't':
421 if (*PI == "%target%") {
422 action->args.push_back(std::string("-march=") + machine);
423 } else if (*PI == "%time%") {
424 if (isSet(TIME_PASSES_FLAG))
425 action->args.push_back("-time-passes");
426 } else
427 found = false;
428 break;
429 case 'v':
430 if (*PI == "%verbose%") {
431 if (isSet(VERBOSE_FLAG))
432 action->args.push_back("-v");
433 } else
434 found = false;
435 break;
436 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000437 if (*PI == "%Mopts%") {
438 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000439 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000440 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000441 } else
442 found = false;
443 break;
444 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000445 if (*PI == "%Wopts%") {
446 for (StringVector::iterator I = WOptions.begin(),
447 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000448 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000449 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000450 } else
451 found = false;
452 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000453 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000454 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000455 break;
456 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000457 if (!found) {
458 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000459 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000460 (*PI)[PI->length()-1] == '%') {
461 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000462 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000463 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000464 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000465 action->args.push_back(*PI);
466 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000467 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000468 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000469 // Its not a substitution, just put it in the action
470 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000471 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000472 PI++;
473 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000474
Reid Spenceraf77d742004-10-28 04:05:06 +0000475 // Finally, we're done
476 return action;
477 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000478
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000479 int DoAction(Action*action, std::string& ErrMsg) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000480 assert(action != 0 && "Invalid Action!");
481 if (isSet(VERBOSE_FLAG))
482 WriteAction(action);
483 if (!isSet(DRY_RUN_FLAG)) {
484 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000485 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000486 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000487 throw std::string("Can't find program '" +
488 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000489 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000490 action->program = progpath;
491 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000492 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000493 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000494
495 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000496 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000497 alloca(sizeof(const char*)*(action->args.size()+2));
498 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000499 for (unsigned i = 1; i <= action->args.size(); ++i)
500 Args[i] = action->args[i-1].c_str();
501 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000502 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000503 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000504 timer.startTimer();
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000505 int resultCode =
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000506 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000507 timer.stopTimer();
508 timer.print(timer,std::cerr);
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000509 return resultCode;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000510 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000511 else
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000512 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000513 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000514 }
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000515 return 0;
Reid Spenceraf77d742004-10-28 04:05:06 +0000516 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000517
Reid Spenceraf77d742004-10-28 04:05:06 +0000518 /// This method tries various variants of a linkage item's file
519 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000520 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000521 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000522 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000523 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000524 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000525 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000526 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000527 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000528 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000529 fullpath.set(PI->toString());
530 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000531 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000532 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000533 if (native) {
534 fullpath.appendSuffix("a");
535 } else {
536 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000537 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000538 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000539 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000540 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000541 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000542 return fullpath;
543 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000544 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000545 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000546 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000547 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000548 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000549 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000550 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000551 return fullpath;
552 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000553 }
554
555 // Didn't find one.
556 fullpath.clear();
557 return fullpath;
558 }
559
560 /// This method processes a linkage item. The item could be a
Gabor Greifa99be512007-07-05 17:07:56 +0000561 /// Bitcode file needing translation to native code and that is
562 /// dependent on other bitcode libraries, or a native code
Reid Spenceraf77d742004-10-28 04:05:06 +0000563 /// library that should just be linked into the program.
564 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
565 SetVector<sys::Path>& set,
566 std::string& err) {
567 // First, see if the unadorned file name is not readable. If so,
568 // we must track down the file in the lib search path.
569 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000570 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000571 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000572 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000573 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000574
Reid Spenceraf77d742004-10-28 04:05:06 +0000575 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000576 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000577 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000578 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000579 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000580 return false;
581 }
582 } else {
583 fullpath = link_item;
584 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000585
Reid Spenceraf77d742004-10-28 04:05:06 +0000586 // If we got here fullpath is the path to the file, and its readable.
587 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000588
Gabor Greifa99be512007-07-05 17:07:56 +0000589 // If its an LLVM bitcode file ...
590 if (fullpath.isBitcodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000591 // Process the dependent libraries recursively
592 Module::LibraryListType modlibs;
Gabor Greifa99be512007-07-05 17:07:56 +0000593 if (GetBitcodeDependentLibraries(fullpath.toString(),modlibs, &err)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000594 // Traverse the dependent libraries list
595 Module::lib_iterator LI = modlibs.begin();
596 Module::lib_iterator LE = modlibs.end();
597 while ( LI != LE ) {
598 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
599 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000600 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000601 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000602 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000603 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000604 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000605 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000606 return false;
607 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000608 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000609 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000610 } else if (err.empty()) {
611 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000612 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000613 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000614 return false;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000615 } else
616 return false;
Reid Spenceraf77d742004-10-28 04:05:06 +0000617 }
618 return true;
619 }
620
621/// @}
622/// @name Methods
623/// @{
624public:
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000625 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000626 try {
627 // Echo the configuration of options if we're running verbose
628 if (isSet(DEBUG_FLAG)) {
629 std::cerr << "Compiler Driver Options:\n";
630 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
631 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
632 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
633 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
634 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
635 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
636 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
637 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
638 std::cerr << "OutputMachine = " << machine << "\n";
639 InputList::const_iterator I = InpList.begin();
640 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000641 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000642 << ")\n";
643 ++I;
644 }
Reid Spencer12786d52004-12-13 08:53:36 +0000645 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000646 }
647
Reid Spenceraf77d742004-10-28 04:05:06 +0000648 // If there's no input, we're done.
649 if (InpList.empty())
650 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000651
Reid Spenceraf77d742004-10-28 04:05:06 +0000652 // If they are asking for linking and didn't provide an output
653 // file then its an error (no way for us to "make up" a meaningful
654 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000655 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000656 throw std::string(
657 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000658
Reid Spenceraf77d742004-10-28 04:05:06 +0000659 // If they are not asking for linking, provided an output file and
660 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000661 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000662 throw std::string("An output file name cannot be specified ") +
663 "with more than one input file name when not linking";
664
665 // This vector holds all the resulting actions of the following loop.
666 std::vector<Action*> actions;
667
668 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
669 // for each input item
670 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000671 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000672 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000673 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
674 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000675 // Get the suffix of the file name
676 const std::string& ftype = I->second;
677
Gabor Greifa99be512007-07-05 17:07:56 +0000678 // If its a library, bitcode file, or object file, save
Misha Brukman3da94ae2005-04-22 00:00:37 +0000679 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000680 // pre-processing/translation/assembly phases
681 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000682 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000683 // later going to link. Enforce this limit now.
684 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000685 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000686 "Pre-compiled objects found but linking not requested");
687 }
688 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000689 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000690 else
691 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000692 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000693 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000694
Reid Spenceraf77d742004-10-28 04:05:06 +0000695 // At this point, we know its something we need to translate
696 // and/or optimize. See if we can get the configuration data
697 // for this kind of file.
698 ConfigData* cd = cdp->ProvideConfigData(I->second);
699 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000700 throw std::string("Files of type '") + I->second +
701 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000702 if (isSet(DEBUG_FLAG))
703 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000704
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000705 // Add the config data's library paths to the end of the list
706 for (StringVector::iterator LPI = cd->libpaths.begin(),
707 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
708 LibraryPaths.push_back(sys::Path(*LPI));
709 }
710
Reid Spenceraf77d742004-10-28 04:05:06 +0000711 // Initialize the input and output files
712 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000713 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000714
Reid Spenceraf77d742004-10-28 04:05:06 +0000715 // PRE-PROCESSING PHASE
716 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000717
Reid Spenceraf77d742004-10-28 04:05:06 +0000718 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000719 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000720 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
721 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000722 if (Output.isEmpty()) {
723 OutFile.appendSuffix("E");
724 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
725 } else {
726 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
727 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000728 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000729 sys::Path TempFile(
730 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
731 if (TempFile.isEmpty())
732 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000733 actions.push_back(GetAction(cd,InFile,TempFile,
734 PREPROCESSING));
735 InFile = TempFile;
736 }
737 }
738 } else if (finalPhase == PREPROCESSING) {
739 throw cd->langName + " does not support pre-processing";
740 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000741 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000742 cd->langName + " files";
743 }
744
Misha Brukman3da94ae2005-04-22 00:00:37 +0000745 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000746 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000747 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000748
749 /// TRANSLATION PHASE
750 action = cd->Translator;
751
752 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000753 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000754 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
755 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000756 if (Output.isEmpty()) {
757 OutFile.appendSuffix("o");
758 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
759 } else {
760 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
761 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000762 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000763 sys::Path TempFile(
764 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
765 if (TempFile.isEmpty())
766 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000767 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
768 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000769 }
770
Reid Spenceraf77d742004-10-28 04:05:06 +0000771 // ll -> bc Helper
772 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
773 /// The output of the translator is an LLVM Assembly program
Gabor Greifa99be512007-07-05 17:07:56 +0000774 /// We need to translate it to bitcode
Reid Spenceraf77d742004-10-28 04:05:06 +0000775 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000776 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000777 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000778 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000779 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000780 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000781 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000782 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000783 }
784 } else if (finalPhase == TRANSLATION) {
785 throw cd->langName + " does not support translation";
786 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000787 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000788 cd->langName + " files";
789 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000790
Reid Spenceraf77d742004-10-28 04:05:06 +0000791 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000792 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000793
Reid Spenceraf77d742004-10-28 04:05:06 +0000794 /// OPTIMIZATION PHASE
795 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000796
Reid Spenceraf77d742004-10-28 04:05:06 +0000797 // Get the optimization action, if needed, or error if appropriate
798 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000799 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000800 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
801 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000802 if (Output.isEmpty()) {
803 OutFile.appendSuffix("o");
804 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
805 } else {
806 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
807 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000808 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000809 sys::Path TempFile(
810 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
811 if (TempFile.isEmpty())
812 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000813 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
814 InFile = TempFile;
815 }
816 // ll -> bc Helper
817 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
818 /// The output of the optimizer is an LLVM Assembly program
Gabor Greifa99be512007-07-05 17:07:56 +0000819 /// We need to translate it to bitcode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000820 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000821 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000822 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000823 action->args.push_back("-f");
824 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000825 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000826 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000827 actions.push_back(action);
828 }
829 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000830 } else if (finalPhase == OPTIMIZATION) {
831 throw cd->langName + " does not support optimization";
832 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000833 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000834 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000835 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000836 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000837
838 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000839 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000840
841 /// ASSEMBLY PHASE
842 action = cd->Assembler;
843
844 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000845
846 // Build either a native compilation action or a disassembly action
847 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000848 if (isSet(EMIT_NATIVE_FLAG)) {
849 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000850 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000851 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000852 action->args.push_back("-f");
853 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000854 if (Output.isEmpty()) {
855 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000856 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000857 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000858 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000859 }
860 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000861 } else {
862 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000863 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000864 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000865 action->args.push_back("-f");
866 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000867 if (Output.isEmpty()) {
868 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000869 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000870 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000871 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000872 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000873 }
874
Reid Spencer679a7232004-11-20 20:39:33 +0000875 // Put the action on the list
876 actions.push_back(action);
877
Misha Brukman3da94ae2005-04-22 00:00:37 +0000878 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000879 continue;
880 }
881
882 // Register the result of the actions as a link candidate
883 LinkageItems.insert(InFile);
884
Reid Spenceraf77d742004-10-28 04:05:06 +0000885 } // end while loop over each input file
886
887 /// RUN THE COMPILATION ACTIONS
888 std::vector<Action*>::iterator AI = actions.begin();
889 std::vector<Action*>::iterator AE = actions.end();
890 while (AI != AE) {
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000891 int ActionResult = DoAction(*AI, ErrMsg);
892 if (ActionResult != 0)
893 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000894 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000895 }
896
Reid Spenceraf77d742004-10-28 04:05:06 +0000897 /// LINKING PHASE
898 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000899
Reid Spenceraf77d742004-10-28 04:05:06 +0000900 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000901 std::vector<sys::Path> SysLibs;
902 sys::Path::GetSystemLibraryPaths(SysLibs);
903 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000904
905 // Set up the linking action with llvm-ld
906 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000907 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000908
909 // Add in the optimization level requested
910 switch (optLevel) {
911 case OPT_FAST_COMPILE:
912 link->args.push_back("-O1");
913 break;
914 case OPT_SIMPLE:
915 link->args.push_back("-O2");
916 break;
917 case OPT_AGGRESSIVE:
918 link->args.push_back("-O3");
919 break;
920 case OPT_LINK_TIME:
921 link->args.push_back("-O4");
922 break;
923 case OPT_AGGRESSIVE_LINK_TIME:
924 link->args.push_back("-O5");
925 break;
926 case OPT_NONE:
927 break;
928 }
929
930 // Add in all the linkage items we generated. This includes the
931 // output from the translation/optimization phases as well as any
932 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000933 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000934 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000935 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000936
937 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000938 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000939 E=LibFiles.end(); I != E; ++I )
940 link->args.push_back(std::string("-l")+*I);
941
942 // Add in all the library paths to the command line
943 for (PathVector::const_iterator I=LibraryPaths.begin(),
944 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000945 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000946
947 // Add in the additional linker arguments requested
948 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
949 E=AdditionalArgs[LINKING].end(); I != E; ++I)
950 link->args.push_back( *I );
951
952 // Add in other optional flags
953 if (isSet(EMIT_NATIVE_FLAG))
954 link->args.push_back("-native");
955 if (isSet(VERBOSE_FLAG))
956 link->args.push_back("-v");
957 if (isSet(TIME_PASSES_FLAG))
958 link->args.push_back("-time-passes");
959 if (isSet(SHOW_STATS_FLAG))
960 link->args.push_back("-stats");
961 if (isSet(STRIP_OUTPUT_FLAG))
962 link->args.push_back("-s");
963 if (isSet(DEBUG_FLAG)) {
964 link->args.push_back("-debug");
965 link->args.push_back("-debug-pass=Details");
966 }
967
968 // Add in mandatory flags
969 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000970 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000971
972 // Execute the link
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000973 int ActionResult = DoAction(link, ErrMsg);
974 if (ActionResult != 0)
975 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000976 }
977 } catch (std::string& msg) {
978 cleanup();
979 throw;
980 } catch (...) {
981 cleanup();
982 throw std::string("Unspecified error");
983 }
984 cleanup();
985 return 0;
986 }
987
988/// @}
989/// @name Data
990/// @{
991private:
992 ConfigDataProvider* cdp; ///< Where we get configuration data from
993 Phases finalPhase; ///< The final phase of compilation
994 OptimizationLevels optLevel; ///< The optimization level to apply
995 unsigned Flags; ///< The driver flags
996 std::string machine; ///< Target machine name
997 PathVector LibraryPaths; ///< -L options
998 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000999 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +00001000 StringVector Defines; ///< -D options
Reid Spencerb9125b12007-04-08 20:08:01 +00001001 sys::PathWithStatus TempDir; ///< Name of the temporary directory.
Reid Spenceraf77d742004-10-28 04:05:06 +00001002 StringTable AdditionalArgs; ///< The -Txyz options
1003 StringVector fOptions; ///< -f options
1004 StringVector MOptions; ///< -M options
1005 StringVector WOptions; ///< -W options
1006
1007/// @}
1008};
Reid Spencer5c56dc12004-08-13 20:22:43 +00001009}
1010
1011CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +00001012}
1013
Reid Spencercc97cfc2005-05-19 00:52:28 +00001014CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1015
Reid Spencer52c2dc12004-08-29 19:26:56 +00001016CompilerDriver*
1017CompilerDriver::Get(ConfigDataProvider& CDP) {
1018 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +00001019}
1020
1021CompilerDriver::ConfigData::ConfigData()
1022 : langName()
1023 , PreProcessor()
1024 , Translator()
1025 , Optimizer()
1026 , Assembler()
1027 , Linker()
1028{
1029 StringVector emptyVec;
1030 for (unsigned i = 0; i < NUM_PHASES; ++i)
1031 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +00001032}