blob: 46015df8cb93db419dff2f512ad23e88522a8c9b [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"
Reid Spencer52c2dc12004-08-29 19:26:56 +000018#include "llvm/Bytecode/Reader.h"
Reid Spencer54fafe42004-09-14 01:58:45 +000019#include "llvm/Support/Timer.h"
Reid Spencer93426ba2004-08-29 20:02:28 +000020#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/StringExtras.h"
Brian Gaekefabf41f2004-12-20 04:02:01 +000023#include "llvm/Config/alloca.h"
Misha Brukmanbaec07c2005-04-20 04:51:29 +000024#include <iostream>
Reid Spencer5c56dc12004-08-13 20:22:43 +000025using namespace llvm;
26
27namespace {
Reid Spencer5c56dc12004-08-13 20:22:43 +000028
Reid Spenceraf77d742004-10-28 04:05:06 +000029void WriteAction(CompilerDriver::Action* action ) {
30 std::cerr << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000031 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000032 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000033 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000034 ++I;
35 }
Misha Brukman0b861482005-05-03 20:30:34 +000036 std::cerr << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000037}
38
39void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000041 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000042 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000043 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000044 ++I;
45 }
Misha Brukman0b861482005-05-03 20:30:34 +000046 std::cerr << '\n';
47 std::cerr << "flags = " << action->flags << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000048}
49
50void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
Misha Brukman3da94ae2005-04-22 00:00:37 +000051 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
Reid Spenceraf77d742004-10-28 04:05:06 +000052 << ")\n";
53 std::cerr << "PreProcessor: ";
54 DumpAction(&cd->PreProcessor);
55 std::cerr << "Translator: ";
56 DumpAction(&cd->Translator);
57 std::cerr << "Optimizer: ";
58 DumpAction(&cd->Optimizer);
59 std::cerr << "Assembler: ";
60 DumpAction(&cd->Assembler);
61 std::cerr << "Linker: ";
62 DumpAction(&cd->Linker);
63}
64
Chris Lattner7cf7c2b2007-02-07 23:48:32 +000065static bool GetBytecodeDependentLibraries(const std::string &fname,
66 Module::LibraryListType& deplibs,
67 BCDecompressor_t *BCDC,
68 std::string* ErrMsg) {
69 ModuleProvider* MP = getBytecodeModuleProvider(fname, BCDC, ErrMsg);
70 if (!MP) {
71 deplibs.clear();
72 return true;
73 }
74 Module* M = MP->releaseModule(ErrMsg);
75 deplibs = M->getLibraries();
76 delete M;
77 delete MP;
78 return false;
79}
80
81
Reid Spenceraf77d742004-10-28 04:05:06 +000082class CompilerDriverImpl : public CompilerDriver {
83/// @name Constructors
84/// @{
85public:
86 CompilerDriverImpl(ConfigDataProvider& confDatProv )
87 : cdp(&confDatProv)
88 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000089 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000090 , Flags(0)
91 , machine()
92 , LibraryPaths()
93 , TempDir()
94 , AdditionalArgs()
95 {
Reid Spenceraf77d742004-10-28 04:05:06 +000096 AdditionalArgs.reserve(NUM_PHASES);
97 StringVector emptyVec;
98 for (unsigned i = 0; i < NUM_PHASES; ++i)
99 AdditionalArgs.push_back(emptyVec);
100 }
101
102 virtual ~CompilerDriverImpl() {
103 cleanup();
104 cdp = 0;
105 LibraryPaths.clear();
106 IncludePaths.clear();
107 Defines.clear();
108 TempDir.clear();
109 AdditionalArgs.clear();
110 fOptions.clear();
111 MOptions.clear();
112 WOptions.clear();
113 }
114
115/// @}
116/// @name Methods
117/// @{
118public:
Misha Brukman0b861482005-05-03 20:30:34 +0000119 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000120 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000121 }
122
Misha Brukman0b861482005-05-03 20:30:34 +0000123 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000124 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000125 }
126
Misha Brukman0b861482005-05-03 20:30:34 +0000127 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000128 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000129 }
130
Misha Brukman0b861482005-05-03 20:30:34 +0000131 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000132 machine = machineName;
133 }
134
135 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
136 assert(phase <= LINKING && phase >= PREPROCESSING);
137 AdditionalArgs[phase] = opts;
138 }
139
140 virtual void setIncludePaths(const StringVector& paths) {
141 StringVector::const_iterator I = paths.begin();
142 StringVector::const_iterator E = paths.end();
143 while (I != E) {
144 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000145 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000146 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000147 ++I;
148 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000149 }
150
Reid Spenceraf77d742004-10-28 04:05:06 +0000151 virtual void setSymbolDefines(const StringVector& defs) {
152 Defines = defs;
153 }
154
155 virtual void setLibraryPaths(const StringVector& paths) {
156 StringVector::const_iterator I = paths.begin();
157 StringVector::const_iterator E = paths.end();
158 while (I != E) {
159 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000160 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000161 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000162 ++I;
163 }
Reid Spencerbf437722004-08-15 08:19:46 +0000164 }
165
Misha Brukman0b861482005-05-03 20:30:34 +0000166 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000167 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000168 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169
Misha Brukman0b861482005-05-03 20:30:34 +0000170 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000171 ToolPaths.push_back(toolPath);
172 }
173
Reid Spenceraf77d742004-10-28 04:05:06 +0000174 virtual void setfPassThrough(const StringVector& fOpts) {
175 fOptions = fOpts;
176 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000177
Reid Spenceraf77d742004-10-28 04:05:06 +0000178 /// @brief Set the list of -M options to be passed through
179 virtual void setMPassThrough(const StringVector& MOpts) {
180 MOptions = MOpts;
181 }
Reid Spencerbae68252004-08-19 04:49:47 +0000182
Reid Spenceraf77d742004-10-28 04:05:06 +0000183 /// @brief Set the list of -W options to be passed through
184 virtual void setWPassThrough(const StringVector& WOpts) {
185 WOptions = WOpts;
186 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000187
Reid Spenceraf77d742004-10-28 04:05:06 +0000188/// @}
189/// @name Functions
190/// @{
191private:
192 bool isSet(DriverFlags flag) {
193 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
194 }
Reid Spencerbae68252004-08-19 04:49:47 +0000195
Reid Spenceraf77d742004-10-28 04:05:06 +0000196 void cleanup() {
197 if (!isSet(KEEP_TEMPS_FLAG)) {
Reid Spencerb9125b12007-04-08 20:08:01 +0000198 const sys::FileStatus *Status = TempDir.getFileStatus();
Reid Spencer8475ec02007-03-29 19:05:44 +0000199 if (Status && Status->isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000200 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000201 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000202 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000203 }
204 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000205
Misha Brukman3da94ae2005-04-22 00:00:37 +0000206 sys::Path MakeTempFile(const std::string& basename,
Reid Spencer48744762006-08-22 19:01:30 +0000207 const std::string& suffix,
208 std::string* ErrMsg) {
209 if (TempDir.isEmpty()) {
210 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
211 if (TempDir.isEmpty())
212 return sys::Path();
213 sys::RemoveDirectoryOnSignal(TempDir);
214 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000215 sys::Path result(TempDir);
Reid Spencer48744762006-08-22 19:01:30 +0000216 if (!result.appendComponent(basename)) {
217 if (ErrMsg)
218 *ErrMsg = basename + ": can't use this file name";
219 return sys::Path();
220 }
221 if (!result.appendSuffix(suffix)) {
222 if (ErrMsg)
223 *ErrMsg = suffix + ": can't use this file suffix";
224 return sys::Path();
225 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000226 return result;
227 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000228
Misha Brukman3da94ae2005-04-22 00:00:37 +0000229 Action* GetAction(ConfigData* cd,
230 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000231 const sys::Path& output,
232 Phases phase)
233 {
234 Action* pat = 0; ///< The pattern/template for the action
235 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000236
Reid Spenceraf77d742004-10-28 04:05:06 +0000237 // Get the action pattern
238 switch (phase) {
239 case PREPROCESSING: pat = &cd->PreProcessor; break;
240 case TRANSLATION: pat = &cd->Translator; break;
241 case OPTIMIZATION: pat = &cd->Optimizer; break;
242 case ASSEMBLY: pat = &cd->Assembler; break;
243 case LINKING: pat = &cd->Linker; break;
244 default:
245 assert(!"Invalid driver phase!");
246 break;
247 }
248 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000249
Reid Spenceraf77d742004-10-28 04:05:06 +0000250 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000251 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000252
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000253 // See if program starts with wildcard...
254 std::string programName=pat->program.toString();
255 if (programName[0] == '%' && programName.length() >2) {
256 switch(programName[1]){
257 case 'b':
258 if (programName.substr(0,8) == "%bindir%") {
259 std::string tmp(LLVM_BINDIR);
260 tmp.append(programName.substr(8));
261 pat->program.set(tmp);
262 }
263 break;
264 case 'l':
265 if (programName.substr(0,12) == "%llvmgccdir%"){
266 std::string tmp(LLVMGCCDIR);
267 tmp.append(programName.substr(12));
268 pat->program.set(tmp);
269 }else if (programName.substr(0,13) == "%llvmgccarch%"){
270 std::string tmp(LLVMGCCARCH);
271 tmp.append(programName.substr(13));
272 pat->program.set(tmp);
273 }else if (programName.substr(0,9) == "%llvmgcc%"){
274 std::string tmp(LLVMGCC);
275 tmp.append(programName.substr(9));
276 pat->program.set(tmp);
277 }else if (programName.substr(0,9) == "%llvmgxx%"){
278 std::string tmp(LLVMGXX);
279 tmp.append(programName.substr(9));
280 pat->program.set(tmp);
281 }else if (programName.substr(0,9) == "%llvmcc1%"){
282 std::string tmp(LLVMCC1);
283 tmp.append(programName.substr(9));
284 pat->program.set(tmp);
285 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
286 std::string tmp(LLVMCC1PLUS);
287 tmp.append(programName.substr(13));
288 pat->program.set(tmp);
289 }else if (programName.substr(0,8) == "%libdir%") {
290 std::string tmp(LLVM_LIBDIR);
291 tmp.append(programName.substr(8));
292 pat->program.set(tmp);
293 }
294 break;
295 }
296 }
297 action->program = pat->program;
298
Reid Spenceraf77d742004-10-28 04:05:06 +0000299 // Do the substitutions from the pattern to the actual
300 StringVector::iterator PI = pat->args.begin();
301 StringVector::iterator PE = pat->args.end();
302 while (PI != PE) {
303 if ((*PI)[0] == '%' && PI->length() >2) {
304 bool found = true;
305 switch ((*PI)[1]) {
306 case 'a':
307 if (*PI == "%args%") {
308 if (AdditionalArgs.size() > unsigned(phase))
309 if (!AdditionalArgs[phase].empty()) {
310 // Get specific options for each kind of action type
311 StringVector& addargs = AdditionalArgs[phase];
312 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000313 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000314 addargs.end());
315 }
316 } else
317 found = false;
318 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000319 case 'b':
320 if (*PI == "%bindir%") {
321 std::string tmp(*PI);
322 tmp.replace(0,8,LLVM_BINDIR);
323 action->args.push_back(tmp);
324 } else
325 found = false;
326 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000327 case 'd':
328 if (*PI == "%defs%") {
329 StringVector::iterator I = Defines.begin();
330 StringVector::iterator E = Defines.end();
331 while (I != E) {
332 action->args.push_back( std::string("-D") + *I);
333 ++I;
334 }
335 } else
336 found = false;
337 break;
338 case 'f':
339 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000340 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000341 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000342 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000343 } else
344 found = false;
345 break;
346 case 'i':
347 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000348 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000349 } else if (*PI == "%incls%") {
350 PathVector::iterator I = IncludePaths.begin();
351 PathVector::iterator E = IncludePaths.end();
352 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000353 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000354 ++I;
355 }
356 } else
357 found = false;
358 break;
359 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000360 if ((*PI)[1] == 'l') {
361 std::string tmp(*PI);
362 if (*PI == "%llvmgccdir%")
363 tmp.replace(0,12,LLVMGCCDIR);
364 else if (*PI == "%llvmgccarch%")
365 tmp.replace(0,13,LLVMGCCARCH);
366 else if (*PI == "%llvmgcc%")
367 tmp.replace(0,9,LLVMGCC);
368 else if (*PI == "%llvmgxx%")
369 tmp.replace(0,9,LLVMGXX);
370 else if (*PI == "%llvmcc1%")
371 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000372 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000373 tmp.replace(0,9,LLVMCC1);
374 else
375 found = false;
376 if (found)
377 action->args.push_back(tmp);
378 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000379 PathVector::iterator I = LibraryPaths.begin();
380 PathVector::iterator E = LibraryPaths.end();
381 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000382 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000383 ++I;
384 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000385 } else if (*PI == "%libdir%") {
386 std::string tmp(*PI);
387 tmp.replace(0,8,LLVM_LIBDIR);
388 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000389 } else
390 found = false;
391 break;
392 case 'o':
393 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000394 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000395 } else if (*PI == "%opt%") {
396 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000397 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000398 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000399 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000400 cd->opts[optLevel].begin(),
401 cd->opts[optLevel].end());
402 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000403 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000404 utostr(unsigned(optLevel)) + " were not specified";
405 }
406 } else
407 found = false;
408 break;
409 case 's':
410 if (*PI == "%stats%") {
411 if (isSet(SHOW_STATS_FLAG))
412 action->args.push_back("-stats");
413 } else
414 found = false;
415 break;
416 case 't':
417 if (*PI == "%target%") {
418 action->args.push_back(std::string("-march=") + machine);
419 } else if (*PI == "%time%") {
420 if (isSet(TIME_PASSES_FLAG))
421 action->args.push_back("-time-passes");
422 } else
423 found = false;
424 break;
425 case 'v':
426 if (*PI == "%verbose%") {
427 if (isSet(VERBOSE_FLAG))
428 action->args.push_back("-v");
429 } else
430 found = false;
431 break;
432 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000433 if (*PI == "%Mopts%") {
434 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000435 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000436 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000437 } else
438 found = false;
439 break;
440 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000441 if (*PI == "%Wopts%") {
442 for (StringVector::iterator I = WOptions.begin(),
443 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000444 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000445 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000446 } else
447 found = false;
448 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000449 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000450 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000451 break;
452 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000453 if (!found) {
454 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000455 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000456 (*PI)[PI->length()-1] == '%') {
457 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000458 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000459 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000460 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000461 action->args.push_back(*PI);
462 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000463 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000464 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000465 // Its not a substitution, just put it in the action
466 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000467 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000468 PI++;
469 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000470
Reid Spenceraf77d742004-10-28 04:05:06 +0000471 // Finally, we're done
472 return action;
473 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000474
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000475 int DoAction(Action*action, std::string& ErrMsg) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000476 assert(action != 0 && "Invalid Action!");
477 if (isSet(VERBOSE_FLAG))
478 WriteAction(action);
479 if (!isSet(DRY_RUN_FLAG)) {
480 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000481 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000482 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000483 throw std::string("Can't find program '" +
484 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000485 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000486 action->program = progpath;
487 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000488 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000489 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000490
491 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000492 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000493 alloca(sizeof(const char*)*(action->args.size()+2));
494 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000495 for (unsigned i = 1; i <= action->args.size(); ++i)
496 Args[i] = action->args[i-1].c_str();
497 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000498 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000499 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000500 timer.startTimer();
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000501 int resultCode =
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000502 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000503 timer.stopTimer();
504 timer.print(timer,std::cerr);
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000505 return resultCode;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000506 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000507 else
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000508 return
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +0000509 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000510 }
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000511 return 0;
Reid Spenceraf77d742004-10-28 04:05:06 +0000512 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000513
Reid Spenceraf77d742004-10-28 04:05:06 +0000514 /// This method tries various variants of a linkage item's file
515 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000516 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000517 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000518 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000519 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000520 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000521 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000522 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000523 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000524 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000525 fullpath.set(PI->toString());
526 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000527 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000528 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000529 if (native) {
530 fullpath.appendSuffix("a");
531 } else {
532 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000533 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000534 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000535 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000536 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000537 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000538 return fullpath;
539 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000540 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000541 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000542 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000543 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000544 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000545 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000546 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000547 return fullpath;
548 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000549 }
550
551 // Didn't find one.
552 fullpath.clear();
553 return fullpath;
554 }
555
556 /// This method processes a linkage item. The item could be a
557 /// Bytecode file needing translation to native code and that is
558 /// dependent on other bytecode libraries, or a native code
559 /// library that should just be linked into the program.
560 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
561 SetVector<sys::Path>& set,
562 std::string& err) {
563 // First, see if the unadorned file name is not readable. If so,
564 // we must track down the file in the lib search path.
565 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000566 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000567 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000568 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000569 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000570
Reid Spenceraf77d742004-10-28 04:05:06 +0000571 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000572 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000573 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000574 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000575 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000576 return false;
577 }
578 } else {
579 fullpath = link_item;
580 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000581
Reid Spenceraf77d742004-10-28 04:05:06 +0000582 // If we got here fullpath is the path to the file, and its readable.
583 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000584
Reid Spenceraf77d742004-10-28 04:05:06 +0000585 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000586 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000587 // Process the dependent libraries recursively
588 Module::LibraryListType modlibs;
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000589 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,
590 Compressor::decompressToNewBuffer,
591 &err)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000592 // Traverse the dependent libraries list
593 Module::lib_iterator LI = modlibs.begin();
594 Module::lib_iterator LE = modlibs.end();
595 while ( LI != LE ) {
596 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
597 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000598 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000599 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000600 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000601 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000602 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000603 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000604 return false;
605 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000606 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000607 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000608 } else if (err.empty()) {
609 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000610 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000611 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000612 return false;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000613 } else
614 return false;
Reid Spenceraf77d742004-10-28 04:05:06 +0000615 }
616 return true;
617 }
618
619/// @}
620/// @name Methods
621/// @{
622public:
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000623 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000624 try {
625 // Echo the configuration of options if we're running verbose
626 if (isSet(DEBUG_FLAG)) {
627 std::cerr << "Compiler Driver Options:\n";
628 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
629 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
630 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
631 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
632 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
633 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
634 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
635 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
636 std::cerr << "OutputMachine = " << machine << "\n";
637 InputList::const_iterator I = InpList.begin();
638 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000639 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000640 << ")\n";
641 ++I;
642 }
Reid Spencer12786d52004-12-13 08:53:36 +0000643 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000644 }
645
Reid Spenceraf77d742004-10-28 04:05:06 +0000646 // If there's no input, we're done.
647 if (InpList.empty())
648 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000649
Reid Spenceraf77d742004-10-28 04:05:06 +0000650 // If they are asking for linking and didn't provide an output
651 // file then its an error (no way for us to "make up" a meaningful
652 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000653 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000654 throw std::string(
655 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000656
Reid Spenceraf77d742004-10-28 04:05:06 +0000657 // If they are not asking for linking, provided an output file and
658 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000659 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000660 throw std::string("An output file name cannot be specified ") +
661 "with more than one input file name when not linking";
662
663 // This vector holds all the resulting actions of the following loop.
664 std::vector<Action*> actions;
665
666 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
667 // for each input item
668 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000669 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000670 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000671 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
672 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000673 // Get the suffix of the file name
674 const std::string& ftype = I->second;
675
Misha Brukman3da94ae2005-04-22 00:00:37 +0000676 // If its a library, bytecode file, or object file, save
677 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000678 // pre-processing/translation/assembly phases
679 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000680 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000681 // later going to link. Enforce this limit now.
682 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000683 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000684 "Pre-compiled objects found but linking not requested");
685 }
686 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000687 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000688 else
689 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000690 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000691 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000692
Reid Spenceraf77d742004-10-28 04:05:06 +0000693 // At this point, we know its something we need to translate
694 // and/or optimize. See if we can get the configuration data
695 // for this kind of file.
696 ConfigData* cd = cdp->ProvideConfigData(I->second);
697 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000698 throw std::string("Files of type '") + I->second +
699 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000700 if (isSet(DEBUG_FLAG))
701 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000702
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000703 // Add the config data's library paths to the end of the list
704 for (StringVector::iterator LPI = cd->libpaths.begin(),
705 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
706 LibraryPaths.push_back(sys::Path(*LPI));
707 }
708
Reid Spenceraf77d742004-10-28 04:05:06 +0000709 // Initialize the input and output files
710 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000711 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000712
Reid Spenceraf77d742004-10-28 04:05:06 +0000713 // PRE-PROCESSING PHASE
714 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000715
Reid Spenceraf77d742004-10-28 04:05:06 +0000716 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000717 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000718 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
719 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000720 if (Output.isEmpty()) {
721 OutFile.appendSuffix("E");
722 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
723 } else {
724 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
725 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000726 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000727 sys::Path TempFile(
728 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
729 if (TempFile.isEmpty())
730 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000731 actions.push_back(GetAction(cd,InFile,TempFile,
732 PREPROCESSING));
733 InFile = TempFile;
734 }
735 }
736 } else if (finalPhase == PREPROCESSING) {
737 throw cd->langName + " does not support pre-processing";
738 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000739 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000740 cd->langName + " files";
741 }
742
Misha Brukman3da94ae2005-04-22 00:00:37 +0000743 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000744 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000745 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000746
747 /// TRANSLATION PHASE
748 action = cd->Translator;
749
750 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000751 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000752 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
753 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000754 if (Output.isEmpty()) {
755 OutFile.appendSuffix("o");
756 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
757 } else {
758 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
759 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000760 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000761 sys::Path TempFile(
762 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
763 if (TempFile.isEmpty())
764 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000765 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
766 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000767 }
768
Reid Spenceraf77d742004-10-28 04:05:06 +0000769 // ll -> bc Helper
770 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
771 /// The output of the translator is an LLVM Assembly program
772 /// We need to translate it to bytecode
773 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000774 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000775 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000776 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000777 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000778 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000779 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000780 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000781 }
782 } else if (finalPhase == TRANSLATION) {
783 throw cd->langName + " does not support translation";
784 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000785 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000786 cd->langName + " files";
787 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000788
Reid Spenceraf77d742004-10-28 04:05:06 +0000789 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000790 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000791
Reid Spenceraf77d742004-10-28 04:05:06 +0000792 /// OPTIMIZATION PHASE
793 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000794
Reid Spenceraf77d742004-10-28 04:05:06 +0000795 // Get the optimization action, if needed, or error if appropriate
796 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000797 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000798 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
799 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000800 if (Output.isEmpty()) {
801 OutFile.appendSuffix("o");
802 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
803 } else {
804 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
805 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000806 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000807 sys::Path TempFile(
808 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
809 if (TempFile.isEmpty())
810 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000811 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
812 InFile = TempFile;
813 }
814 // ll -> bc Helper
815 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
816 /// The output of the optimizer is an LLVM Assembly program
817 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000818 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000819 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000820 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000821 action->args.push_back("-f");
822 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000823 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000824 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000825 actions.push_back(action);
826 }
827 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000828 } else if (finalPhase == OPTIMIZATION) {
829 throw cd->langName + " does not support optimization";
830 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000831 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000832 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000833 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000834 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000835
836 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000837 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000838
839 /// ASSEMBLY PHASE
840 action = cd->Assembler;
841
842 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000843
844 // Build either a native compilation action or a disassembly action
845 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000846 if (isSet(EMIT_NATIVE_FLAG)) {
847 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000848 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000849 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000850 action->args.push_back("-f");
851 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000852 if (Output.isEmpty()) {
853 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000854 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000855 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000856 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000857 }
858 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000859 } else {
860 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000861 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000862 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000863 action->args.push_back("-f");
864 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000865 if (Output.isEmpty()) {
866 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000867 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000868 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000869 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000870 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000871 }
872
Reid Spencer679a7232004-11-20 20:39:33 +0000873 // Put the action on the list
874 actions.push_back(action);
875
Misha Brukman3da94ae2005-04-22 00:00:37 +0000876 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000877 continue;
878 }
879
880 // Register the result of the actions as a link candidate
881 LinkageItems.insert(InFile);
882
Reid Spenceraf77d742004-10-28 04:05:06 +0000883 } // end while loop over each input file
884
885 /// RUN THE COMPILATION ACTIONS
886 std::vector<Action*>::iterator AI = actions.begin();
887 std::vector<Action*>::iterator AE = actions.end();
888 while (AI != AE) {
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000889 int ActionResult = DoAction(*AI, ErrMsg);
890 if (ActionResult != 0)
891 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000892 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000893 }
894
Reid Spenceraf77d742004-10-28 04:05:06 +0000895 /// LINKING PHASE
896 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000897
Reid Spenceraf77d742004-10-28 04:05:06 +0000898 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000899 std::vector<sys::Path> SysLibs;
900 sys::Path::GetSystemLibraryPaths(SysLibs);
901 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000902
903 // Set up the linking action with llvm-ld
904 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000905 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000906
907 // Add in the optimization level requested
908 switch (optLevel) {
909 case OPT_FAST_COMPILE:
910 link->args.push_back("-O1");
911 break;
912 case OPT_SIMPLE:
913 link->args.push_back("-O2");
914 break;
915 case OPT_AGGRESSIVE:
916 link->args.push_back("-O3");
917 break;
918 case OPT_LINK_TIME:
919 link->args.push_back("-O4");
920 break;
921 case OPT_AGGRESSIVE_LINK_TIME:
922 link->args.push_back("-O5");
923 break;
924 case OPT_NONE:
925 break;
926 }
927
928 // Add in all the linkage items we generated. This includes the
929 // output from the translation/optimization phases as well as any
930 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000931 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000932 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000933 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000934
935 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000936 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000937 E=LibFiles.end(); I != E; ++I )
938 link->args.push_back(std::string("-l")+*I);
939
940 // Add in all the library paths to the command line
941 for (PathVector::const_iterator I=LibraryPaths.begin(),
942 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000943 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000944
945 // Add in the additional linker arguments requested
946 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
947 E=AdditionalArgs[LINKING].end(); I != E; ++I)
948 link->args.push_back( *I );
949
950 // Add in other optional flags
951 if (isSet(EMIT_NATIVE_FLAG))
952 link->args.push_back("-native");
953 if (isSet(VERBOSE_FLAG))
954 link->args.push_back("-v");
955 if (isSet(TIME_PASSES_FLAG))
956 link->args.push_back("-time-passes");
957 if (isSet(SHOW_STATS_FLAG))
958 link->args.push_back("-stats");
959 if (isSet(STRIP_OUTPUT_FLAG))
960 link->args.push_back("-s");
961 if (isSet(DEBUG_FLAG)) {
962 link->args.push_back("-debug");
963 link->args.push_back("-debug-pass=Details");
964 }
965
966 // Add in mandatory flags
967 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000968 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000969
970 // Execute the link
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000971 int ActionResult = DoAction(link, ErrMsg);
972 if (ActionResult != 0)
973 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000974 }
975 } catch (std::string& msg) {
976 cleanup();
977 throw;
978 } catch (...) {
979 cleanup();
980 throw std::string("Unspecified error");
981 }
982 cleanup();
983 return 0;
984 }
985
986/// @}
987/// @name Data
988/// @{
989private:
990 ConfigDataProvider* cdp; ///< Where we get configuration data from
991 Phases finalPhase; ///< The final phase of compilation
992 OptimizationLevels optLevel; ///< The optimization level to apply
993 unsigned Flags; ///< The driver flags
994 std::string machine; ///< Target machine name
995 PathVector LibraryPaths; ///< -L options
996 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000997 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +0000998 StringVector Defines; ///< -D options
Reid Spencerb9125b12007-04-08 20:08:01 +0000999 sys::PathWithStatus TempDir; ///< Name of the temporary directory.
Reid Spenceraf77d742004-10-28 04:05:06 +00001000 StringTable AdditionalArgs; ///< The -Txyz options
1001 StringVector fOptions; ///< -f options
1002 StringVector MOptions; ///< -M options
1003 StringVector WOptions; ///< -W options
1004
1005/// @}
1006};
Reid Spencer5c56dc12004-08-13 20:22:43 +00001007}
1008
1009CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +00001010}
1011
Reid Spencercc97cfc2005-05-19 00:52:28 +00001012CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1013
Reid Spencer52c2dc12004-08-29 19:26:56 +00001014CompilerDriver*
1015CompilerDriver::Get(ConfigDataProvider& CDP) {
1016 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +00001017}
1018
1019CompilerDriver::ConfigData::ConfigData()
1020 : langName()
1021 , PreProcessor()
1022 , Translator()
1023 , Optimizer()
1024 , Assembler()
1025 , Linker()
1026{
1027 StringVector emptyVec;
1028 for (unsigned i = 0; i < NUM_PHASES; ++i)
1029 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +00001030}