blob: 251da3429064d455690ae84f3500362f283a3b02 [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 Lattner5e8edbb2007-05-06 05:51:37 +000018#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer52c2dc12004-08-29 19:26:56 +000019#include "llvm/Bytecode/Reader.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
30static bool Bitcode = false;
31
Reid Spencer5c56dc12004-08-13 20:22:43 +000032namespace {
Reid Spencer5c56dc12004-08-13 20:22:43 +000033
Reid Spenceraf77d742004-10-28 04:05:06 +000034void WriteAction(CompilerDriver::Action* action ) {
35 std::cerr << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000036 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000037 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000038 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000039 ++I;
40 }
Misha Brukman0b861482005-05-03 20:30:34 +000041 std::cerr << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000042}
43
44void DumpAction(CompilerDriver::Action* action) {
45 std::cerr << "command = " << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000046 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000047 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000048 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000049 ++I;
50 }
Misha Brukman0b861482005-05-03 20:30:34 +000051 std::cerr << '\n';
52 std::cerr << "flags = " << action->flags << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000053}
54
55void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
Misha Brukman3da94ae2005-04-22 00:00:37 +000056 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
Reid Spenceraf77d742004-10-28 04:05:06 +000057 << ")\n";
58 std::cerr << "PreProcessor: ";
59 DumpAction(&cd->PreProcessor);
60 std::cerr << "Translator: ";
61 DumpAction(&cd->Translator);
62 std::cerr << "Optimizer: ";
63 DumpAction(&cd->Optimizer);
64 std::cerr << "Assembler: ";
65 DumpAction(&cd->Assembler);
66 std::cerr << "Linker: ";
67 DumpAction(&cd->Linker);
68}
69
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000070static bool GetBytecodeDependentLibraries(const std::string &fname,
71 Module::LibraryListType& deplibs,
72 BCDecompressor_t *BCDC,
73 std::string* ErrMsg) {
Chris Lattner5e8edbb2007-05-06 05:51:37 +000074 ModuleProvider *MP = 0;
75 if (Bitcode) {
76 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&fname[0],
77 fname.size())) {
78 MP = getBitcodeModuleProvider(Buffer);
79 if (MP == 0) delete Buffer;
80 }
81 } else {
82 MP = getBytecodeModuleProvider(fname, BCDC, ErrMsg);
83 }
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000084 if (!MP) {
85 deplibs.clear();
86 return true;
87 }
Chris Lattner5e8edbb2007-05-06 05:51:37 +000088 deplibs = MP->getModule()->getLibraries();
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000089 delete MP;
90 return false;
91}
92
93
Reid Spenceraf77d742004-10-28 04:05:06 +000094class CompilerDriverImpl : public CompilerDriver {
95/// @name Constructors
96/// @{
97public:
98 CompilerDriverImpl(ConfigDataProvider& confDatProv )
99 : cdp(&confDatProv)
100 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000101 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +0000102 , Flags(0)
103 , machine()
104 , LibraryPaths()
105 , TempDir()
106 , AdditionalArgs()
107 {
Reid Spenceraf77d742004-10-28 04:05:06 +0000108 AdditionalArgs.reserve(NUM_PHASES);
109 StringVector emptyVec;
110 for (unsigned i = 0; i < NUM_PHASES; ++i)
111 AdditionalArgs.push_back(emptyVec);
112 }
113
114 virtual ~CompilerDriverImpl() {
115 cleanup();
116 cdp = 0;
117 LibraryPaths.clear();
118 IncludePaths.clear();
119 Defines.clear();
120 TempDir.clear();
121 AdditionalArgs.clear();
122 fOptions.clear();
123 MOptions.clear();
124 WOptions.clear();
125 }
126
127/// @}
128/// @name Methods
129/// @{
130public:
Misha Brukman0b861482005-05-03 20:30:34 +0000131 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000132 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000133 }
134
Misha Brukman0b861482005-05-03 20:30:34 +0000135 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000136 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000137 }
138
Misha Brukman0b861482005-05-03 20:30:34 +0000139 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000140 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000141 }
142
Misha Brukman0b861482005-05-03 20:30:34 +0000143 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000144 machine = machineName;
145 }
146
147 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
148 assert(phase <= LINKING && phase >= PREPROCESSING);
149 AdditionalArgs[phase] = opts;
150 }
151
152 virtual void setIncludePaths(const StringVector& paths) {
153 StringVector::const_iterator I = paths.begin();
154 StringVector::const_iterator E = paths.end();
155 while (I != E) {
156 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000157 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000158 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000159 ++I;
160 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000161 }
162
Reid Spenceraf77d742004-10-28 04:05:06 +0000163 virtual void setSymbolDefines(const StringVector& defs) {
164 Defines = defs;
165 }
166
167 virtual void setLibraryPaths(const StringVector& paths) {
168 StringVector::const_iterator I = paths.begin();
169 StringVector::const_iterator E = paths.end();
170 while (I != E) {
171 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000172 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000173 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000174 ++I;
175 }
Reid Spencerbf437722004-08-15 08:19:46 +0000176 }
177
Misha Brukman0b861482005-05-03 20:30:34 +0000178 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000179 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000180 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000181
Misha Brukman0b861482005-05-03 20:30:34 +0000182 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000183 ToolPaths.push_back(toolPath);
184 }
185
Reid Spenceraf77d742004-10-28 04:05:06 +0000186 virtual void setfPassThrough(const StringVector& fOpts) {
187 fOptions = fOpts;
188 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000189
Reid Spenceraf77d742004-10-28 04:05:06 +0000190 /// @brief Set the list of -M options to be passed through
191 virtual void setMPassThrough(const StringVector& MOpts) {
192 MOptions = MOpts;
193 }
Reid Spencerbae68252004-08-19 04:49:47 +0000194
Reid Spenceraf77d742004-10-28 04:05:06 +0000195 /// @brief Set the list of -W options to be passed through
196 virtual void setWPassThrough(const StringVector& WOpts) {
197 WOptions = WOpts;
198 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000199
Reid Spenceraf77d742004-10-28 04:05:06 +0000200/// @}
201/// @name Functions
202/// @{
203private:
204 bool isSet(DriverFlags flag) {
205 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
206 }
Reid Spencerbae68252004-08-19 04:49:47 +0000207
Reid Spenceraf77d742004-10-28 04:05:06 +0000208 void cleanup() {
209 if (!isSet(KEEP_TEMPS_FLAG)) {
Reid Spencerb9125b12007-04-08 20:08:01 +0000210 const sys::FileStatus *Status = TempDir.getFileStatus();
Reid Spencer8475ec02007-03-29 19:05:44 +0000211 if (Status && Status->isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000212 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000213 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000214 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000215 }
216 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000217
Misha Brukman3da94ae2005-04-22 00:00:37 +0000218 sys::Path MakeTempFile(const std::string& basename,
Reid Spencer48744762006-08-22 19:01:30 +0000219 const std::string& suffix,
220 std::string* ErrMsg) {
221 if (TempDir.isEmpty()) {
222 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
223 if (TempDir.isEmpty())
224 return sys::Path();
225 sys::RemoveDirectoryOnSignal(TempDir);
226 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000227 sys::Path result(TempDir);
Reid Spencer48744762006-08-22 19:01:30 +0000228 if (!result.appendComponent(basename)) {
229 if (ErrMsg)
230 *ErrMsg = basename + ": can't use this file name";
231 return sys::Path();
232 }
233 if (!result.appendSuffix(suffix)) {
234 if (ErrMsg)
235 *ErrMsg = suffix + ": can't use this file suffix";
236 return sys::Path();
237 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000238 return result;
239 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000240
Misha Brukman3da94ae2005-04-22 00:00:37 +0000241 Action* GetAction(ConfigData* cd,
242 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000243 const sys::Path& output,
244 Phases phase)
245 {
246 Action* pat = 0; ///< The pattern/template for the action
247 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000248
Reid Spenceraf77d742004-10-28 04:05:06 +0000249 // Get the action pattern
250 switch (phase) {
251 case PREPROCESSING: pat = &cd->PreProcessor; break;
252 case TRANSLATION: pat = &cd->Translator; break;
253 case OPTIMIZATION: pat = &cd->Optimizer; break;
254 case ASSEMBLY: pat = &cd->Assembler; break;
255 case LINKING: pat = &cd->Linker; break;
256 default:
257 assert(!"Invalid driver phase!");
258 break;
259 }
260 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000261
Reid Spenceraf77d742004-10-28 04:05:06 +0000262 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000263 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000264
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000265 // See if program starts with wildcard...
266 std::string programName=pat->program.toString();
267 if (programName[0] == '%' && programName.length() >2) {
268 switch(programName[1]){
269 case 'b':
270 if (programName.substr(0,8) == "%bindir%") {
271 std::string tmp(LLVM_BINDIR);
272 tmp.append(programName.substr(8));
273 pat->program.set(tmp);
274 }
275 break;
276 case 'l':
277 if (programName.substr(0,12) == "%llvmgccdir%"){
278 std::string tmp(LLVMGCCDIR);
279 tmp.append(programName.substr(12));
280 pat->program.set(tmp);
281 }else if (programName.substr(0,13) == "%llvmgccarch%"){
282 std::string tmp(LLVMGCCARCH);
283 tmp.append(programName.substr(13));
284 pat->program.set(tmp);
285 }else if (programName.substr(0,9) == "%llvmgcc%"){
286 std::string tmp(LLVMGCC);
287 tmp.append(programName.substr(9));
288 pat->program.set(tmp);
289 }else if (programName.substr(0,9) == "%llvmgxx%"){
290 std::string tmp(LLVMGXX);
291 tmp.append(programName.substr(9));
292 pat->program.set(tmp);
293 }else if (programName.substr(0,9) == "%llvmcc1%"){
294 std::string tmp(LLVMCC1);
295 tmp.append(programName.substr(9));
296 pat->program.set(tmp);
297 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
298 std::string tmp(LLVMCC1PLUS);
299 tmp.append(programName.substr(13));
300 pat->program.set(tmp);
301 }else if (programName.substr(0,8) == "%libdir%") {
302 std::string tmp(LLVM_LIBDIR);
303 tmp.append(programName.substr(8));
304 pat->program.set(tmp);
305 }
306 break;
307 }
308 }
309 action->program = pat->program;
310
Reid Spenceraf77d742004-10-28 04:05:06 +0000311 // Do the substitutions from the pattern to the actual
312 StringVector::iterator PI = pat->args.begin();
313 StringVector::iterator PE = pat->args.end();
314 while (PI != PE) {
315 if ((*PI)[0] == '%' && PI->length() >2) {
316 bool found = true;
317 switch ((*PI)[1]) {
318 case 'a':
319 if (*PI == "%args%") {
320 if (AdditionalArgs.size() > unsigned(phase))
321 if (!AdditionalArgs[phase].empty()) {
322 // Get specific options for each kind of action type
323 StringVector& addargs = AdditionalArgs[phase];
324 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000325 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000326 addargs.end());
327 }
328 } else
329 found = false;
330 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000331 case 'b':
332 if (*PI == "%bindir%") {
333 std::string tmp(*PI);
334 tmp.replace(0,8,LLVM_BINDIR);
335 action->args.push_back(tmp);
336 } else
337 found = false;
338 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000339 case 'd':
340 if (*PI == "%defs%") {
341 StringVector::iterator I = Defines.begin();
342 StringVector::iterator E = Defines.end();
343 while (I != E) {
344 action->args.push_back( std::string("-D") + *I);
345 ++I;
346 }
347 } else
348 found = false;
349 break;
350 case 'f':
351 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000352 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000353 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000354 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000355 } else
356 found = false;
357 break;
358 case 'i':
359 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000360 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000361 } else if (*PI == "%incls%") {
362 PathVector::iterator I = IncludePaths.begin();
363 PathVector::iterator E = IncludePaths.end();
364 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000365 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000366 ++I;
367 }
368 } else
369 found = false;
370 break;
371 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000372 if ((*PI)[1] == 'l') {
373 std::string tmp(*PI);
374 if (*PI == "%llvmgccdir%")
375 tmp.replace(0,12,LLVMGCCDIR);
376 else if (*PI == "%llvmgccarch%")
377 tmp.replace(0,13,LLVMGCCARCH);
378 else if (*PI == "%llvmgcc%")
379 tmp.replace(0,9,LLVMGCC);
380 else if (*PI == "%llvmgxx%")
381 tmp.replace(0,9,LLVMGXX);
382 else if (*PI == "%llvmcc1%")
383 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000384 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000385 tmp.replace(0,9,LLVMCC1);
386 else
387 found = false;
388 if (found)
389 action->args.push_back(tmp);
390 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000391 PathVector::iterator I = LibraryPaths.begin();
392 PathVector::iterator E = LibraryPaths.end();
393 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000394 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000395 ++I;
396 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000397 } else if (*PI == "%libdir%") {
398 std::string tmp(*PI);
399 tmp.replace(0,8,LLVM_LIBDIR);
400 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000401 } else
402 found = false;
403 break;
404 case 'o':
405 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000406 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000407 } else if (*PI == "%opt%") {
408 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000409 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000410 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000411 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000412 cd->opts[optLevel].begin(),
413 cd->opts[optLevel].end());
414 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000415 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000416 utostr(unsigned(optLevel)) + " were not specified";
417 }
418 } else
419 found = false;
420 break;
421 case 's':
422 if (*PI == "%stats%") {
423 if (isSet(SHOW_STATS_FLAG))
424 action->args.push_back("-stats");
425 } else
426 found = false;
427 break;
428 case 't':
429 if (*PI == "%target%") {
430 action->args.push_back(std::string("-march=") + machine);
431 } else if (*PI == "%time%") {
432 if (isSet(TIME_PASSES_FLAG))
433 action->args.push_back("-time-passes");
434 } else
435 found = false;
436 break;
437 case 'v':
438 if (*PI == "%verbose%") {
439 if (isSet(VERBOSE_FLAG))
440 action->args.push_back("-v");
441 } else
442 found = false;
443 break;
444 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000445 if (*PI == "%Mopts%") {
446 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000447 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000448 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000449 } else
450 found = false;
451 break;
452 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000453 if (*PI == "%Wopts%") {
454 for (StringVector::iterator I = WOptions.begin(),
455 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000456 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000457 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000458 } else
459 found = false;
460 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000461 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000462 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000463 break;
464 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000465 if (!found) {
466 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000467 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000468 (*PI)[PI->length()-1] == '%') {
469 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000470 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000471 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000472 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000473 action->args.push_back(*PI);
474 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000475 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000476 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000477 // Its not a substitution, just put it in the action
478 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000479 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000480 PI++;
481 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000482
Reid Spenceraf77d742004-10-28 04:05:06 +0000483 // Finally, we're done
484 return action;
485 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000486
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000487 int DoAction(Action*action, std::string& ErrMsg) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000488 assert(action != 0 && "Invalid Action!");
489 if (isSet(VERBOSE_FLAG))
490 WriteAction(action);
491 if (!isSet(DRY_RUN_FLAG)) {
492 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000493 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000494 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000495 throw std::string("Can't find program '" +
496 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000497 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000498 action->program = progpath;
499 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000500 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000501 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000502
503 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000504 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000505 alloca(sizeof(const char*)*(action->args.size()+2));
506 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000507 for (unsigned i = 1; i <= action->args.size(); ++i)
508 Args[i] = action->args[i-1].c_str();
509 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000510 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000511 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000512 timer.startTimer();
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000513 int resultCode =
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 timer.stopTimer();
516 timer.print(timer,std::cerr);
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000517 return resultCode;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000518 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000519 else
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000520 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000521 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000522 }
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000523 return 0;
Reid Spenceraf77d742004-10-28 04:05:06 +0000524 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000525
Reid Spenceraf77d742004-10-28 04:05:06 +0000526 /// This method tries various variants of a linkage item's file
527 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000528 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000529 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000530 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000531 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000532 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000533 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000534 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000535 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000536 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000537 fullpath.set(PI->toString());
538 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000539 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000540 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000541 if (native) {
542 fullpath.appendSuffix("a");
543 } else {
544 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000545 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000546 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000547 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000548 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000549 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000550 return fullpath;
551 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000552 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000553 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000554 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000555 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000556 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000557 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000558 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000559 return fullpath;
560 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000561 }
562
563 // Didn't find one.
564 fullpath.clear();
565 return fullpath;
566 }
567
568 /// This method processes a linkage item. The item could be a
569 /// Bytecode file needing translation to native code and that is
570 /// dependent on other bytecode libraries, or a native code
571 /// library that should just be linked into the program.
572 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
573 SetVector<sys::Path>& set,
574 std::string& err) {
575 // First, see if the unadorned file name is not readable. If so,
576 // we must track down the file in the lib search path.
577 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000578 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000579 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000580 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000581 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000582
Reid Spenceraf77d742004-10-28 04:05:06 +0000583 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000584 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000585 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000586 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000587 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000588 return false;
589 }
590 } else {
591 fullpath = link_item;
592 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000593
Reid Spenceraf77d742004-10-28 04:05:06 +0000594 // If we got here fullpath is the path to the file, and its readable.
595 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000596
Reid Spenceraf77d742004-10-28 04:05:06 +0000597 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000598 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000599 // Process the dependent libraries recursively
600 Module::LibraryListType modlibs;
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000601 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,
602 Compressor::decompressToNewBuffer,
603 &err)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000604 // Traverse the dependent libraries list
605 Module::lib_iterator LI = modlibs.begin();
606 Module::lib_iterator LE = modlibs.end();
607 while ( LI != LE ) {
608 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
609 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000610 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000611 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000612 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000613 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000614 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000615 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000616 return false;
617 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000618 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000619 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000620 } else if (err.empty()) {
621 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000622 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000623 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000624 return false;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000625 } else
626 return false;
Reid Spenceraf77d742004-10-28 04:05:06 +0000627 }
628 return true;
629 }
630
631/// @}
632/// @name Methods
633/// @{
634public:
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000635 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000636 try {
637 // Echo the configuration of options if we're running verbose
638 if (isSet(DEBUG_FLAG)) {
639 std::cerr << "Compiler Driver Options:\n";
640 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
641 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
642 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
643 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
644 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
645 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
646 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
647 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
648 std::cerr << "OutputMachine = " << machine << "\n";
649 InputList::const_iterator I = InpList.begin();
650 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000651 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000652 << ")\n";
653 ++I;
654 }
Reid Spencer12786d52004-12-13 08:53:36 +0000655 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000656 }
657
Reid Spenceraf77d742004-10-28 04:05:06 +0000658 // If there's no input, we're done.
659 if (InpList.empty())
660 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000661
Reid Spenceraf77d742004-10-28 04:05:06 +0000662 // If they are asking for linking and didn't provide an output
663 // file then its an error (no way for us to "make up" a meaningful
664 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000665 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000666 throw std::string(
667 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000668
Reid Spenceraf77d742004-10-28 04:05:06 +0000669 // If they are not asking for linking, provided an output file and
670 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000671 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000672 throw std::string("An output file name cannot be specified ") +
673 "with more than one input file name when not linking";
674
675 // This vector holds all the resulting actions of the following loop.
676 std::vector<Action*> actions;
677
678 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
679 // for each input item
680 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000681 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000682 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000683 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
684 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000685 // Get the suffix of the file name
686 const std::string& ftype = I->second;
687
Misha Brukman3da94ae2005-04-22 00:00:37 +0000688 // If its a library, bytecode file, or object file, save
689 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000690 // pre-processing/translation/assembly phases
691 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000692 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000693 // later going to link. Enforce this limit now.
694 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000695 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000696 "Pre-compiled objects found but linking not requested");
697 }
698 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000699 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000700 else
701 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000702 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000703 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000704
Reid Spenceraf77d742004-10-28 04:05:06 +0000705 // At this point, we know its something we need to translate
706 // and/or optimize. See if we can get the configuration data
707 // for this kind of file.
708 ConfigData* cd = cdp->ProvideConfigData(I->second);
709 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000710 throw std::string("Files of type '") + I->second +
711 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000712 if (isSet(DEBUG_FLAG))
713 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000714
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000715 // Add the config data's library paths to the end of the list
716 for (StringVector::iterator LPI = cd->libpaths.begin(),
717 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
718 LibraryPaths.push_back(sys::Path(*LPI));
719 }
720
Reid Spenceraf77d742004-10-28 04:05:06 +0000721 // Initialize the input and output files
722 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000723 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000724
Reid Spenceraf77d742004-10-28 04:05:06 +0000725 // PRE-PROCESSING PHASE
726 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000727
Reid Spenceraf77d742004-10-28 04:05:06 +0000728 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000729 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000730 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
731 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000732 if (Output.isEmpty()) {
733 OutFile.appendSuffix("E");
734 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
735 } else {
736 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
737 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000738 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000739 sys::Path TempFile(
740 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
741 if (TempFile.isEmpty())
742 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000743 actions.push_back(GetAction(cd,InFile,TempFile,
744 PREPROCESSING));
745 InFile = TempFile;
746 }
747 }
748 } else if (finalPhase == PREPROCESSING) {
749 throw cd->langName + " does not support pre-processing";
750 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000751 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000752 cd->langName + " files";
753 }
754
Misha Brukman3da94ae2005-04-22 00:00:37 +0000755 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000756 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000757 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000758
759 /// TRANSLATION PHASE
760 action = cd->Translator;
761
762 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000763 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000764 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
765 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000766 if (Output.isEmpty()) {
767 OutFile.appendSuffix("o");
768 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
769 } else {
770 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
771 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000772 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000773 sys::Path TempFile(
774 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
775 if (TempFile.isEmpty())
776 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000777 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
778 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000779 }
780
Reid Spenceraf77d742004-10-28 04:05:06 +0000781 // ll -> bc Helper
782 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
783 /// The output of the translator is an LLVM Assembly program
784 /// We need to translate it to bytecode
785 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000786 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000787 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000788 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000789 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000790 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000791 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000792 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000793 }
794 } else if (finalPhase == TRANSLATION) {
795 throw cd->langName + " does not support translation";
796 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000797 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000798 cd->langName + " files";
799 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000800
Reid Spenceraf77d742004-10-28 04:05:06 +0000801 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000802 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000803
Reid Spenceraf77d742004-10-28 04:05:06 +0000804 /// OPTIMIZATION PHASE
805 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000806
Reid Spenceraf77d742004-10-28 04:05:06 +0000807 // Get the optimization action, if needed, or error if appropriate
808 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000809 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000810 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
811 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000812 if (Output.isEmpty()) {
813 OutFile.appendSuffix("o");
814 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
815 } else {
816 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
817 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000818 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000819 sys::Path TempFile(
820 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
821 if (TempFile.isEmpty())
822 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000823 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
824 InFile = TempFile;
825 }
826 // ll -> bc Helper
827 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
828 /// The output of the optimizer is an LLVM Assembly program
829 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000830 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000831 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000832 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000833 action->args.push_back("-f");
834 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000835 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000836 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000837 actions.push_back(action);
838 }
839 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000840 } else if (finalPhase == OPTIMIZATION) {
841 throw cd->langName + " does not support optimization";
842 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000843 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000844 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000845 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000846 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000847
848 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000849 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000850
851 /// ASSEMBLY PHASE
852 action = cd->Assembler;
853
854 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000855
856 // Build either a native compilation action or a disassembly action
857 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000858 if (isSet(EMIT_NATIVE_FLAG)) {
859 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000860 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000861 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000862 action->args.push_back("-f");
863 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000864 if (Output.isEmpty()) {
865 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000866 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000867 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000868 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000869 }
870 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000871 } else {
872 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000873 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000874 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000875 action->args.push_back("-f");
876 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000877 if (Output.isEmpty()) {
878 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000879 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000880 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000881 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000882 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000883 }
884
Reid Spencer679a7232004-11-20 20:39:33 +0000885 // Put the action on the list
886 actions.push_back(action);
887
Misha Brukman3da94ae2005-04-22 00:00:37 +0000888 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000889 continue;
890 }
891
892 // Register the result of the actions as a link candidate
893 LinkageItems.insert(InFile);
894
Reid Spenceraf77d742004-10-28 04:05:06 +0000895 } // end while loop over each input file
896
897 /// RUN THE COMPILATION ACTIONS
898 std::vector<Action*>::iterator AI = actions.begin();
899 std::vector<Action*>::iterator AE = actions.end();
900 while (AI != AE) {
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000901 int ActionResult = DoAction(*AI, ErrMsg);
902 if (ActionResult != 0)
903 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000904 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000905 }
906
Reid Spenceraf77d742004-10-28 04:05:06 +0000907 /// LINKING PHASE
908 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000909
Reid Spenceraf77d742004-10-28 04:05:06 +0000910 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000911 std::vector<sys::Path> SysLibs;
912 sys::Path::GetSystemLibraryPaths(SysLibs);
913 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000914
915 // Set up the linking action with llvm-ld
916 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000917 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000918
919 // Add in the optimization level requested
920 switch (optLevel) {
921 case OPT_FAST_COMPILE:
922 link->args.push_back("-O1");
923 break;
924 case OPT_SIMPLE:
925 link->args.push_back("-O2");
926 break;
927 case OPT_AGGRESSIVE:
928 link->args.push_back("-O3");
929 break;
930 case OPT_LINK_TIME:
931 link->args.push_back("-O4");
932 break;
933 case OPT_AGGRESSIVE_LINK_TIME:
934 link->args.push_back("-O5");
935 break;
936 case OPT_NONE:
937 break;
938 }
939
940 // Add in all the linkage items we generated. This includes the
941 // output from the translation/optimization phases as well as any
942 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000943 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000944 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000945 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000946
947 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000948 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000949 E=LibFiles.end(); I != E; ++I )
950 link->args.push_back(std::string("-l")+*I);
951
952 // Add in all the library paths to the command line
953 for (PathVector::const_iterator I=LibraryPaths.begin(),
954 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000955 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000956
957 // Add in the additional linker arguments requested
958 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
959 E=AdditionalArgs[LINKING].end(); I != E; ++I)
960 link->args.push_back( *I );
961
962 // Add in other optional flags
963 if (isSet(EMIT_NATIVE_FLAG))
964 link->args.push_back("-native");
965 if (isSet(VERBOSE_FLAG))
966 link->args.push_back("-v");
967 if (isSet(TIME_PASSES_FLAG))
968 link->args.push_back("-time-passes");
969 if (isSet(SHOW_STATS_FLAG))
970 link->args.push_back("-stats");
971 if (isSet(STRIP_OUTPUT_FLAG))
972 link->args.push_back("-s");
973 if (isSet(DEBUG_FLAG)) {
974 link->args.push_back("-debug");
975 link->args.push_back("-debug-pass=Details");
976 }
977
978 // Add in mandatory flags
979 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000980 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000981
982 // Execute the link
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000983 int ActionResult = DoAction(link, ErrMsg);
984 if (ActionResult != 0)
985 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000986 }
987 } catch (std::string& msg) {
988 cleanup();
989 throw;
990 } catch (...) {
991 cleanup();
992 throw std::string("Unspecified error");
993 }
994 cleanup();
995 return 0;
996 }
997
998/// @}
999/// @name Data
1000/// @{
1001private:
1002 ConfigDataProvider* cdp; ///< Where we get configuration data from
1003 Phases finalPhase; ///< The final phase of compilation
1004 OptimizationLevels optLevel; ///< The optimization level to apply
1005 unsigned Flags; ///< The driver flags
1006 std::string machine; ///< Target machine name
1007 PathVector LibraryPaths; ///< -L options
1008 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +00001009 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +00001010 StringVector Defines; ///< -D options
Reid Spencerb9125b12007-04-08 20:08:01 +00001011 sys::PathWithStatus TempDir; ///< Name of the temporary directory.
Reid Spenceraf77d742004-10-28 04:05:06 +00001012 StringTable AdditionalArgs; ///< The -Txyz options
1013 StringVector fOptions; ///< -f options
1014 StringVector MOptions; ///< -M options
1015 StringVector WOptions; ///< -W options
1016
1017/// @}
1018};
Reid Spencer5c56dc12004-08-13 20:22:43 +00001019}
1020
1021CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +00001022}
1023
Reid Spencercc97cfc2005-05-19 00:52:28 +00001024CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1025
Reid Spencer52c2dc12004-08-29 19:26:56 +00001026CompilerDriver*
1027CompilerDriver::Get(ConfigDataProvider& CDP) {
1028 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +00001029}
1030
1031CompilerDriver::ConfigData::ConfigData()
1032 : langName()
1033 , PreProcessor()
1034 , Translator()
1035 , Optimizer()
1036 , Assembler()
1037 , Linker()
1038{
1039 StringVector emptyVec;
1040 for (unsigned i = 0; i < NUM_PHASES; ++i)
1041 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +00001042}