blob: 2f94ee84c789cfb3aa1fffad96c764a1a958fac7 [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
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000068static bool GetBytecodeDependentLibraries(const std::string &fname,
69 Module::LibraryListType& deplibs,
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000070 std::string* ErrMsg) {
Chris Lattner5e8edbb2007-05-06 05:51:37 +000071 ModuleProvider *MP = 0;
Chris Lattner44dadff2007-05-06 09:29:57 +000072 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&fname[0],
73 fname.size())) {
74 MP = getBitcodeModuleProvider(Buffer);
75 if (MP == 0) delete Buffer;
Chris Lattner5e8edbb2007-05-06 05:51:37 +000076 }
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000077 if (!MP) {
78 deplibs.clear();
79 return true;
80 }
Chris Lattner5e8edbb2007-05-06 05:51:37 +000081 deplibs = MP->getModule()->getLibraries();
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000082 delete MP;
83 return false;
84}
85
86
Reid Spenceraf77d742004-10-28 04:05:06 +000087class CompilerDriverImpl : public CompilerDriver {
88/// @name Constructors
89/// @{
90public:
91 CompilerDriverImpl(ConfigDataProvider& confDatProv )
92 : cdp(&confDatProv)
93 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000094 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000095 , Flags(0)
96 , machine()
97 , LibraryPaths()
98 , TempDir()
99 , AdditionalArgs()
100 {
Reid Spenceraf77d742004-10-28 04:05:06 +0000101 AdditionalArgs.reserve(NUM_PHASES);
102 StringVector emptyVec;
103 for (unsigned i = 0; i < NUM_PHASES; ++i)
104 AdditionalArgs.push_back(emptyVec);
105 }
106
107 virtual ~CompilerDriverImpl() {
108 cleanup();
109 cdp = 0;
110 LibraryPaths.clear();
111 IncludePaths.clear();
112 Defines.clear();
113 TempDir.clear();
114 AdditionalArgs.clear();
115 fOptions.clear();
116 MOptions.clear();
117 WOptions.clear();
118 }
119
120/// @}
121/// @name Methods
122/// @{
123public:
Misha Brukman0b861482005-05-03 20:30:34 +0000124 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000125 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000126 }
127
Misha Brukman0b861482005-05-03 20:30:34 +0000128 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000129 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000130 }
131
Misha Brukman0b861482005-05-03 20:30:34 +0000132 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000133 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000134 }
135
Misha Brukman0b861482005-05-03 20:30:34 +0000136 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000137 machine = machineName;
138 }
139
140 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
141 assert(phase <= LINKING && phase >= PREPROCESSING);
142 AdditionalArgs[phase] = opts;
143 }
144
145 virtual void setIncludePaths(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 Spencerdd04df02005-07-07 23:21:43 +0000150 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000151 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000152 ++I;
153 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000154 }
155
Reid Spenceraf77d742004-10-28 04:05:06 +0000156 virtual void setSymbolDefines(const StringVector& defs) {
157 Defines = defs;
158 }
159
160 virtual void setLibraryPaths(const StringVector& paths) {
161 StringVector::const_iterator I = paths.begin();
162 StringVector::const_iterator E = paths.end();
163 while (I != E) {
164 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000165 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000166 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000167 ++I;
168 }
Reid Spencerbf437722004-08-15 08:19:46 +0000169 }
170
Misha Brukman0b861482005-05-03 20:30:34 +0000171 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000172 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000173 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000174
Misha Brukman0b861482005-05-03 20:30:34 +0000175 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000176 ToolPaths.push_back(toolPath);
177 }
178
Reid Spenceraf77d742004-10-28 04:05:06 +0000179 virtual void setfPassThrough(const StringVector& fOpts) {
180 fOptions = fOpts;
181 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000182
Reid Spenceraf77d742004-10-28 04:05:06 +0000183 /// @brief Set the list of -M options to be passed through
184 virtual void setMPassThrough(const StringVector& MOpts) {
185 MOptions = MOpts;
186 }
Reid Spencerbae68252004-08-19 04:49:47 +0000187
Reid Spenceraf77d742004-10-28 04:05:06 +0000188 /// @brief Set the list of -W options to be passed through
189 virtual void setWPassThrough(const StringVector& WOpts) {
190 WOptions = WOpts;
191 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000192
Reid Spenceraf77d742004-10-28 04:05:06 +0000193/// @}
194/// @name Functions
195/// @{
196private:
197 bool isSet(DriverFlags flag) {
198 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
199 }
Reid Spencerbae68252004-08-19 04:49:47 +0000200
Reid Spenceraf77d742004-10-28 04:05:06 +0000201 void cleanup() {
202 if (!isSet(KEEP_TEMPS_FLAG)) {
Reid Spencerb9125b12007-04-08 20:08:01 +0000203 const sys::FileStatus *Status = TempDir.getFileStatus();
Reid Spencer8475ec02007-03-29 19:05:44 +0000204 if (Status && Status->isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000205 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000206 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000207 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000208 }
209 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000210
Misha Brukman3da94ae2005-04-22 00:00:37 +0000211 sys::Path MakeTempFile(const std::string& basename,
Reid Spencer48744762006-08-22 19:01:30 +0000212 const std::string& suffix,
213 std::string* ErrMsg) {
214 if (TempDir.isEmpty()) {
215 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
216 if (TempDir.isEmpty())
217 return sys::Path();
218 sys::RemoveDirectoryOnSignal(TempDir);
219 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000220 sys::Path result(TempDir);
Reid Spencer48744762006-08-22 19:01:30 +0000221 if (!result.appendComponent(basename)) {
222 if (ErrMsg)
223 *ErrMsg = basename + ": can't use this file name";
224 return sys::Path();
225 }
226 if (!result.appendSuffix(suffix)) {
227 if (ErrMsg)
228 *ErrMsg = suffix + ": can't use this file suffix";
229 return sys::Path();
230 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000231 return result;
232 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000233
Misha Brukman3da94ae2005-04-22 00:00:37 +0000234 Action* GetAction(ConfigData* cd,
235 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000236 const sys::Path& output,
237 Phases phase)
238 {
239 Action* pat = 0; ///< The pattern/template for the action
240 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000241
Reid Spenceraf77d742004-10-28 04:05:06 +0000242 // Get the action pattern
243 switch (phase) {
244 case PREPROCESSING: pat = &cd->PreProcessor; break;
245 case TRANSLATION: pat = &cd->Translator; break;
246 case OPTIMIZATION: pat = &cd->Optimizer; break;
247 case ASSEMBLY: pat = &cd->Assembler; break;
248 case LINKING: pat = &cd->Linker; break;
249 default:
250 assert(!"Invalid driver phase!");
251 break;
252 }
253 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000254
Reid Spenceraf77d742004-10-28 04:05:06 +0000255 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000256 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000257
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000258 // See if program starts with wildcard...
259 std::string programName=pat->program.toString();
260 if (programName[0] == '%' && programName.length() >2) {
261 switch(programName[1]){
262 case 'b':
263 if (programName.substr(0,8) == "%bindir%") {
264 std::string tmp(LLVM_BINDIR);
265 tmp.append(programName.substr(8));
266 pat->program.set(tmp);
267 }
268 break;
269 case 'l':
270 if (programName.substr(0,12) == "%llvmgccdir%"){
271 std::string tmp(LLVMGCCDIR);
272 tmp.append(programName.substr(12));
273 pat->program.set(tmp);
274 }else if (programName.substr(0,13) == "%llvmgccarch%"){
275 std::string tmp(LLVMGCCARCH);
276 tmp.append(programName.substr(13));
277 pat->program.set(tmp);
278 }else if (programName.substr(0,9) == "%llvmgcc%"){
279 std::string tmp(LLVMGCC);
280 tmp.append(programName.substr(9));
281 pat->program.set(tmp);
282 }else if (programName.substr(0,9) == "%llvmgxx%"){
283 std::string tmp(LLVMGXX);
284 tmp.append(programName.substr(9));
285 pat->program.set(tmp);
286 }else if (programName.substr(0,9) == "%llvmcc1%"){
287 std::string tmp(LLVMCC1);
288 tmp.append(programName.substr(9));
289 pat->program.set(tmp);
290 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
291 std::string tmp(LLVMCC1PLUS);
292 tmp.append(programName.substr(13));
293 pat->program.set(tmp);
294 }else if (programName.substr(0,8) == "%libdir%") {
295 std::string tmp(LLVM_LIBDIR);
296 tmp.append(programName.substr(8));
297 pat->program.set(tmp);
298 }
299 break;
300 }
301 }
302 action->program = pat->program;
303
Reid Spenceraf77d742004-10-28 04:05:06 +0000304 // Do the substitutions from the pattern to the actual
305 StringVector::iterator PI = pat->args.begin();
306 StringVector::iterator PE = pat->args.end();
307 while (PI != PE) {
308 if ((*PI)[0] == '%' && PI->length() >2) {
309 bool found = true;
310 switch ((*PI)[1]) {
311 case 'a':
312 if (*PI == "%args%") {
313 if (AdditionalArgs.size() > unsigned(phase))
314 if (!AdditionalArgs[phase].empty()) {
315 // Get specific options for each kind of action type
316 StringVector& addargs = AdditionalArgs[phase];
317 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000318 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000319 addargs.end());
320 }
321 } else
322 found = false;
323 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000324 case 'b':
325 if (*PI == "%bindir%") {
326 std::string tmp(*PI);
327 tmp.replace(0,8,LLVM_BINDIR);
328 action->args.push_back(tmp);
329 } else
330 found = false;
331 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000332 case 'd':
333 if (*PI == "%defs%") {
334 StringVector::iterator I = Defines.begin();
335 StringVector::iterator E = Defines.end();
336 while (I != E) {
337 action->args.push_back( std::string("-D") + *I);
338 ++I;
339 }
340 } else
341 found = false;
342 break;
343 case 'f':
344 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000345 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000346 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000347 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000348 } else
349 found = false;
350 break;
351 case 'i':
352 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000353 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000354 } else if (*PI == "%incls%") {
355 PathVector::iterator I = IncludePaths.begin();
356 PathVector::iterator E = IncludePaths.end();
357 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000358 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000359 ++I;
360 }
361 } else
362 found = false;
363 break;
364 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000365 if ((*PI)[1] == 'l') {
366 std::string tmp(*PI);
367 if (*PI == "%llvmgccdir%")
368 tmp.replace(0,12,LLVMGCCDIR);
369 else if (*PI == "%llvmgccarch%")
370 tmp.replace(0,13,LLVMGCCARCH);
371 else if (*PI == "%llvmgcc%")
372 tmp.replace(0,9,LLVMGCC);
373 else if (*PI == "%llvmgxx%")
374 tmp.replace(0,9,LLVMGXX);
375 else if (*PI == "%llvmcc1%")
376 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000377 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000378 tmp.replace(0,9,LLVMCC1);
379 else
380 found = false;
381 if (found)
382 action->args.push_back(tmp);
383 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000384 PathVector::iterator I = LibraryPaths.begin();
385 PathVector::iterator E = LibraryPaths.end();
386 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000387 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000388 ++I;
389 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000390 } else if (*PI == "%libdir%") {
391 std::string tmp(*PI);
392 tmp.replace(0,8,LLVM_LIBDIR);
393 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000394 } else
395 found = false;
396 break;
397 case 'o':
398 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000399 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000400 } else if (*PI == "%opt%") {
401 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000402 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000403 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000404 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000405 cd->opts[optLevel].begin(),
406 cd->opts[optLevel].end());
407 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000408 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000409 utostr(unsigned(optLevel)) + " were not specified";
410 }
411 } else
412 found = false;
413 break;
414 case 's':
415 if (*PI == "%stats%") {
416 if (isSet(SHOW_STATS_FLAG))
417 action->args.push_back("-stats");
418 } else
419 found = false;
420 break;
421 case 't':
422 if (*PI == "%target%") {
423 action->args.push_back(std::string("-march=") + machine);
424 } else if (*PI == "%time%") {
425 if (isSet(TIME_PASSES_FLAG))
426 action->args.push_back("-time-passes");
427 } else
428 found = false;
429 break;
430 case 'v':
431 if (*PI == "%verbose%") {
432 if (isSet(VERBOSE_FLAG))
433 action->args.push_back("-v");
434 } else
435 found = false;
436 break;
437 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000438 if (*PI == "%Mopts%") {
439 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000440 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000441 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000442 } else
443 found = false;
444 break;
445 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000446 if (*PI == "%Wopts%") {
447 for (StringVector::iterator I = WOptions.begin(),
448 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000449 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000450 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000451 } else
452 found = false;
453 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000454 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000455 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000456 break;
457 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000458 if (!found) {
459 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000460 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000461 (*PI)[PI->length()-1] == '%') {
462 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000463 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000464 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000465 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000466 action->args.push_back(*PI);
467 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000468 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000469 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000470 // Its not a substitution, just put it in the action
471 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000472 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000473 PI++;
474 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000475
Reid Spenceraf77d742004-10-28 04:05:06 +0000476 // Finally, we're done
477 return action;
478 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000479
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000480 int DoAction(Action*action, std::string& ErrMsg) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000481 assert(action != 0 && "Invalid Action!");
482 if (isSet(VERBOSE_FLAG))
483 WriteAction(action);
484 if (!isSet(DRY_RUN_FLAG)) {
485 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000486 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000487 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000488 throw std::string("Can't find program '" +
489 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000490 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000491 action->program = progpath;
492 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000493 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000494 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000495
496 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000497 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000498 alloca(sizeof(const char*)*(action->args.size()+2));
499 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000500 for (unsigned i = 1; i <= action->args.size(); ++i)
501 Args[i] = action->args[i-1].c_str();
502 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000503 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000504 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000505 timer.startTimer();
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000506 int resultCode =
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000507 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000508 timer.stopTimer();
509 timer.print(timer,std::cerr);
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000510 return resultCode;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000511 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000512 else
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000513 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000514 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000515 }
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000516 return 0;
Reid Spenceraf77d742004-10-28 04:05:06 +0000517 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000518
Reid Spenceraf77d742004-10-28 04:05:06 +0000519 /// This method tries various variants of a linkage item's file
520 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000521 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000522 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000523 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000524 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000525 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000526 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000527 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000528 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000529 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000530 fullpath.set(PI->toString());
531 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000532 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000533 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000534 if (native) {
535 fullpath.appendSuffix("a");
536 } else {
537 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000538 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000539 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000540 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000541 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000542 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000543 return fullpath;
544 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000545 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000546 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000547 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000548 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000549 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000550 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000551 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000552 return fullpath;
553 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000554 }
555
556 // Didn't find one.
557 fullpath.clear();
558 return fullpath;
559 }
560
561 /// This method processes a linkage item. The item could be a
562 /// Bytecode file needing translation to native code and that is
563 /// dependent on other bytecode libraries, or a native code
564 /// library that should just be linked into the program.
565 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
566 SetVector<sys::Path>& set,
567 std::string& err) {
568 // First, see if the unadorned file name is not readable. If so,
569 // we must track down the file in the lib search path.
570 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000571 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000572 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000573 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000574 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000575
Reid Spenceraf77d742004-10-28 04:05:06 +0000576 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000577 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000578 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000579 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000580 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000581 return false;
582 }
583 } else {
584 fullpath = link_item;
585 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000586
Reid Spenceraf77d742004-10-28 04:05:06 +0000587 // If we got here fullpath is the path to the file, and its readable.
588 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000589
Reid Spenceraf77d742004-10-28 04:05:06 +0000590 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000591 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000592 // Process the dependent libraries recursively
593 Module::LibraryListType modlibs;
Chris Lattner44dadff2007-05-06 09:29:57 +0000594 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs, &err)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000595 // Traverse the dependent libraries list
596 Module::lib_iterator LI = modlibs.begin();
597 Module::lib_iterator LE = modlibs.end();
598 while ( LI != LE ) {
599 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
600 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000601 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000602 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000603 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000604 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000605 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000606 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000607 return false;
608 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000609 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000610 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000611 } else if (err.empty()) {
612 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000613 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000614 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000615 return false;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000616 } else
617 return false;
Reid Spenceraf77d742004-10-28 04:05:06 +0000618 }
619 return true;
620 }
621
622/// @}
623/// @name Methods
624/// @{
625public:
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000626 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000627 try {
628 // Echo the configuration of options if we're running verbose
629 if (isSet(DEBUG_FLAG)) {
630 std::cerr << "Compiler Driver Options:\n";
631 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
632 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
633 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
634 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
635 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
636 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
637 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
638 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
639 std::cerr << "OutputMachine = " << machine << "\n";
640 InputList::const_iterator I = InpList.begin();
641 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000642 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000643 << ")\n";
644 ++I;
645 }
Reid Spencer12786d52004-12-13 08:53:36 +0000646 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000647 }
648
Reid Spenceraf77d742004-10-28 04:05:06 +0000649 // If there's no input, we're done.
650 if (InpList.empty())
651 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000652
Reid Spenceraf77d742004-10-28 04:05:06 +0000653 // If they are asking for linking and didn't provide an output
654 // file then its an error (no way for us to "make up" a meaningful
655 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000656 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000657 throw std::string(
658 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000659
Reid Spenceraf77d742004-10-28 04:05:06 +0000660 // If they are not asking for linking, provided an output file and
661 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000662 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000663 throw std::string("An output file name cannot be specified ") +
664 "with more than one input file name when not linking";
665
666 // This vector holds all the resulting actions of the following loop.
667 std::vector<Action*> actions;
668
669 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
670 // for each input item
671 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000672 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000673 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000674 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
675 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000676 // Get the suffix of the file name
677 const std::string& ftype = I->second;
678
Misha Brukman3da94ae2005-04-22 00:00:37 +0000679 // If its a library, bytecode file, or object file, save
680 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000681 // pre-processing/translation/assembly phases
682 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000683 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000684 // later going to link. Enforce this limit now.
685 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000686 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000687 "Pre-compiled objects found but linking not requested");
688 }
689 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000690 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000691 else
692 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000693 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000694 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000695
Reid Spenceraf77d742004-10-28 04:05:06 +0000696 // At this point, we know its something we need to translate
697 // and/or optimize. See if we can get the configuration data
698 // for this kind of file.
699 ConfigData* cd = cdp->ProvideConfigData(I->second);
700 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000701 throw std::string("Files of type '") + I->second +
702 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000703 if (isSet(DEBUG_FLAG))
704 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000705
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000706 // Add the config data's library paths to the end of the list
707 for (StringVector::iterator LPI = cd->libpaths.begin(),
708 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
709 LibraryPaths.push_back(sys::Path(*LPI));
710 }
711
Reid Spenceraf77d742004-10-28 04:05:06 +0000712 // Initialize the input and output files
713 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000714 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000715
Reid Spenceraf77d742004-10-28 04:05:06 +0000716 // PRE-PROCESSING PHASE
717 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000718
Reid Spenceraf77d742004-10-28 04:05:06 +0000719 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000720 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000721 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
722 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000723 if (Output.isEmpty()) {
724 OutFile.appendSuffix("E");
725 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
726 } else {
727 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
728 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000729 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000730 sys::Path TempFile(
731 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
732 if (TempFile.isEmpty())
733 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000734 actions.push_back(GetAction(cd,InFile,TempFile,
735 PREPROCESSING));
736 InFile = TempFile;
737 }
738 }
739 } else if (finalPhase == PREPROCESSING) {
740 throw cd->langName + " does not support pre-processing";
741 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000742 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000743 cd->langName + " files";
744 }
745
Misha Brukman3da94ae2005-04-22 00:00:37 +0000746 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000747 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000748 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000749
750 /// TRANSLATION PHASE
751 action = cd->Translator;
752
753 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000754 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000755 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
756 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000757 if (Output.isEmpty()) {
758 OutFile.appendSuffix("o");
759 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
760 } else {
761 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
762 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000763 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000764 sys::Path TempFile(
765 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
766 if (TempFile.isEmpty())
767 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000768 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
769 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000770 }
771
Reid Spenceraf77d742004-10-28 04:05:06 +0000772 // ll -> bc Helper
773 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
774 /// The output of the translator is an LLVM Assembly program
775 /// We need to translate it to bytecode
776 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000777 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000778 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000779 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000780 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000781 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000782 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000783 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000784 }
785 } else if (finalPhase == TRANSLATION) {
786 throw cd->langName + " does not support translation";
787 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000788 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000789 cd->langName + " files";
790 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000791
Reid Spenceraf77d742004-10-28 04:05:06 +0000792 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000793 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000794
Reid Spenceraf77d742004-10-28 04:05:06 +0000795 /// OPTIMIZATION PHASE
796 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000797
Reid Spenceraf77d742004-10-28 04:05:06 +0000798 // Get the optimization action, if needed, or error if appropriate
799 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000800 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000801 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
802 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000803 if (Output.isEmpty()) {
804 OutFile.appendSuffix("o");
805 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
806 } else {
807 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
808 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000809 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000810 sys::Path TempFile(
811 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
812 if (TempFile.isEmpty())
813 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000814 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
815 InFile = TempFile;
816 }
817 // ll -> bc Helper
818 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
819 /// The output of the optimizer is an LLVM Assembly program
820 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000821 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000822 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000823 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000824 action->args.push_back("-f");
825 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000826 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000827 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000828 actions.push_back(action);
829 }
830 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000831 } else if (finalPhase == OPTIMIZATION) {
832 throw cd->langName + " does not support optimization";
833 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000834 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000835 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000836 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000837 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000838
839 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000840 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000841
842 /// ASSEMBLY PHASE
843 action = cd->Assembler;
844
845 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000846
847 // Build either a native compilation action or a disassembly action
848 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000849 if (isSet(EMIT_NATIVE_FLAG)) {
850 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000851 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000852 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000853 action->args.push_back("-f");
854 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000855 if (Output.isEmpty()) {
856 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000857 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000858 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000859 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000860 }
861 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000862 } else {
863 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000864 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000865 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000866 action->args.push_back("-f");
867 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000868 if (Output.isEmpty()) {
869 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000870 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000871 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000872 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000873 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000874 }
875
Reid Spencer679a7232004-11-20 20:39:33 +0000876 // Put the action on the list
877 actions.push_back(action);
878
Misha Brukman3da94ae2005-04-22 00:00:37 +0000879 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000880 continue;
881 }
882
883 // Register the result of the actions as a link candidate
884 LinkageItems.insert(InFile);
885
Reid Spenceraf77d742004-10-28 04:05:06 +0000886 } // end while loop over each input file
887
888 /// RUN THE COMPILATION ACTIONS
889 std::vector<Action*>::iterator AI = actions.begin();
890 std::vector<Action*>::iterator AE = actions.end();
891 while (AI != AE) {
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000892 int ActionResult = DoAction(*AI, ErrMsg);
893 if (ActionResult != 0)
894 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000895 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000896 }
897
Reid Spenceraf77d742004-10-28 04:05:06 +0000898 /// LINKING PHASE
899 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000900
Reid Spenceraf77d742004-10-28 04:05:06 +0000901 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000902 std::vector<sys::Path> SysLibs;
903 sys::Path::GetSystemLibraryPaths(SysLibs);
904 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000905
906 // Set up the linking action with llvm-ld
907 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000908 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000909
910 // Add in the optimization level requested
911 switch (optLevel) {
912 case OPT_FAST_COMPILE:
913 link->args.push_back("-O1");
914 break;
915 case OPT_SIMPLE:
916 link->args.push_back("-O2");
917 break;
918 case OPT_AGGRESSIVE:
919 link->args.push_back("-O3");
920 break;
921 case OPT_LINK_TIME:
922 link->args.push_back("-O4");
923 break;
924 case OPT_AGGRESSIVE_LINK_TIME:
925 link->args.push_back("-O5");
926 break;
927 case OPT_NONE:
928 break;
929 }
930
931 // Add in all the linkage items we generated. This includes the
932 // output from the translation/optimization phases as well as any
933 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000934 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000935 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000936 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000937
938 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000939 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000940 E=LibFiles.end(); I != E; ++I )
941 link->args.push_back(std::string("-l")+*I);
942
943 // Add in all the library paths to the command line
944 for (PathVector::const_iterator I=LibraryPaths.begin(),
945 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000946 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000947
948 // Add in the additional linker arguments requested
949 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
950 E=AdditionalArgs[LINKING].end(); I != E; ++I)
951 link->args.push_back( *I );
952
953 // Add in other optional flags
954 if (isSet(EMIT_NATIVE_FLAG))
955 link->args.push_back("-native");
956 if (isSet(VERBOSE_FLAG))
957 link->args.push_back("-v");
958 if (isSet(TIME_PASSES_FLAG))
959 link->args.push_back("-time-passes");
960 if (isSet(SHOW_STATS_FLAG))
961 link->args.push_back("-stats");
962 if (isSet(STRIP_OUTPUT_FLAG))
963 link->args.push_back("-s");
964 if (isSet(DEBUG_FLAG)) {
965 link->args.push_back("-debug");
966 link->args.push_back("-debug-pass=Details");
967 }
968
969 // Add in mandatory flags
970 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000971 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000972
973 // Execute the link
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000974 int ActionResult = DoAction(link, ErrMsg);
975 if (ActionResult != 0)
976 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000977 }
978 } catch (std::string& msg) {
979 cleanup();
980 throw;
981 } catch (...) {
982 cleanup();
983 throw std::string("Unspecified error");
984 }
985 cleanup();
986 return 0;
987 }
988
989/// @}
990/// @name Data
991/// @{
992private:
993 ConfigDataProvider* cdp; ///< Where we get configuration data from
994 Phases finalPhase; ///< The final phase of compilation
995 OptimizationLevels optLevel; ///< The optimization level to apply
996 unsigned Flags; ///< The driver flags
997 std::string machine; ///< Target machine name
998 PathVector LibraryPaths; ///< -L options
999 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +00001000 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +00001001 StringVector Defines; ///< -D options
Reid Spencerb9125b12007-04-08 20:08:01 +00001002 sys::PathWithStatus TempDir; ///< Name of the temporary directory.
Reid Spenceraf77d742004-10-28 04:05:06 +00001003 StringTable AdditionalArgs; ///< The -Txyz options
1004 StringVector fOptions; ///< -f options
1005 StringVector MOptions; ///< -M options
1006 StringVector WOptions; ///< -W options
1007
1008/// @}
1009};
Reid Spencer5c56dc12004-08-13 20:22:43 +00001010}
1011
1012CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +00001013}
1014
Reid Spencercc97cfc2005-05-19 00:52:28 +00001015CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1016
Reid Spencer52c2dc12004-08-29 19:26:56 +00001017CompilerDriver*
1018CompilerDriver::Get(ConfigDataProvider& CDP) {
1019 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +00001020}
1021
1022CompilerDriver::ConfigData::ConfigData()
1023 : langName()
1024 , PreProcessor()
1025 , Translator()
1026 , Optimizer()
1027 , Assembler()
1028 , Linker()
1029{
1030 StringVector emptyVec;
1031 for (unsigned i = 0; i < NUM_PHASES; ++i)
1032 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +00001033}