blob: 7dadf4332617a2b5803e8f39a8c38c82e0c7c1a5 [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//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file was developed by Reid Spencer and is distributed under the
7// University of Illinois Open Source License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
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//
13//===------------------------------------------------------------------------===
14
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 Spencera01439a2004-08-24 13:55:17 +000019#include "Support/FileUtilities.h"
Reid Spencer52c2dc12004-08-29 19:26:56 +000020#include "Support/SetVector.h"
Reid Spencera01439a2004-08-24 13:55:17 +000021#include "Support/StringExtras.h"
Reid Spencer5c56dc12004-08-13 20:22:43 +000022#include <iostream>
23
24using namespace llvm;
25
26namespace {
Reid Spencer5c56dc12004-08-13 20:22:43 +000027
Reid Spencerbae68252004-08-19 04:49:47 +000028 void WriteAction(CompilerDriver::Action* action ) {
Reid Spencer52c2dc12004-08-29 19:26:56 +000029 std::cerr << action->program.c_str();
Reid Spencerbae68252004-08-19 04:49:47 +000030 std::vector<std::string>::iterator I = action->args.begin();
31 while (I != action->args.end()) {
Reid Spencer68fb37a2004-08-14 09:37:15 +000032 std::cerr << " " + *I;
33 ++I;
34 }
35 std::cerr << "\n";
36 }
37
Reid Spencerbae68252004-08-19 04:49:47 +000038 void DumpAction(CompilerDriver::Action* action) {
Reid Spencer52c2dc12004-08-29 19:26:56 +000039 std::cerr << "command = " << action->program.c_str();
Reid Spencerbae68252004-08-19 04:49:47 +000040 std::vector<std::string>::iterator I = action->args.begin();
41 while (I != action->args.end()) {
Reid Spencerbf437722004-08-15 08:19:46 +000042 std::cerr << " " + *I;
43 ++I;
44 }
45 std::cerr << "\n";
Reid Spencerbae68252004-08-19 04:49:47 +000046 std::cerr << "flags = " << action->flags << "\n";
Reid Spencerbf437722004-08-15 08:19:46 +000047 }
48
Reid Spencer68fb37a2004-08-14 09:37:15 +000049 void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
50 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
51 << ")\n";
Reid Spencer68fb37a2004-08-14 09:37:15 +000052 std::cerr << "PreProcessor: ";
Reid Spencerbf437722004-08-15 08:19:46 +000053 DumpAction(&cd->PreProcessor);
Reid Spencer68fb37a2004-08-14 09:37:15 +000054 std::cerr << "Translator: ";
Reid Spencerbf437722004-08-15 08:19:46 +000055 DumpAction(&cd->Translator);
Reid Spencer68fb37a2004-08-14 09:37:15 +000056 std::cerr << "Optimizer: ";
Reid Spencerbf437722004-08-15 08:19:46 +000057 DumpAction(&cd->Optimizer);
Reid Spencer68fb37a2004-08-14 09:37:15 +000058 std::cerr << "Assembler: ";
Reid Spencerbf437722004-08-15 08:19:46 +000059 DumpAction(&cd->Assembler);
Reid Spencer68fb37a2004-08-14 09:37:15 +000060 std::cerr << "Linker: ";
Reid Spencerbf437722004-08-15 08:19:46 +000061 DumpAction(&cd->Linker);
Reid Spencer68fb37a2004-08-14 09:37:15 +000062 }
Reid Spencer5c56dc12004-08-13 20:22:43 +000063
Reid Spencer2a069fa2004-08-16 07:06:38 +000064 /// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
65 /// which should reduce the volume of code and make compilation
66 /// faster. This is also safe on any llvm module.
Reid Spencerbae68252004-08-19 04:49:47 +000067 static const char* DefaultFastCompileOptimizations[] = {
68 "-simplifycfg", "-mem2reg", "-instcombine"
Reid Spencer2a069fa2004-08-16 07:06:38 +000069 };
Reid Spencer5c56dc12004-08-13 20:22:43 +000070
Reid Spencer52c2dc12004-08-29 19:26:56 +000071 class CompilerDriverImpl : public CompilerDriver {
72 /// @name Constructors
73 /// @{
74 public:
75 CompilerDriverImpl(ConfigDataProvider& confDatProv )
76 : cdp(&confDatProv)
77 , finalPhase(LINKING)
78 , optLevel(OPT_FAST_COMPILE)
79 , Flags(0)
80 , machine()
81 , LibraryPaths()
82 , TempDir()
83 , AdditionalArgs()
84 {
85 TempDir = sys::Path::GetTemporaryDirectory();
86 sys::RemoveDirectoryOnSignal(TempDir);
87 AdditionalArgs.reserve(NUM_PHASES);
88 StringVector emptyVec;
89 for (unsigned i = 0; i < NUM_PHASES; ++i)
90 AdditionalArgs.push_back(emptyVec);
Reid Spencerbae68252004-08-19 04:49:47 +000091 }
Reid Spencerbae68252004-08-19 04:49:47 +000092
Reid Spencer52c2dc12004-08-29 19:26:56 +000093 virtual ~CompilerDriverImpl() {
94 cleanup();
95 cdp = 0;
96 LibraryPaths.clear();
97 AdditionalArgs.clear();
98 }
Reid Spencerbae68252004-08-19 04:49:47 +000099
Reid Spencer52c2dc12004-08-29 19:26:56 +0000100 /// @}
101 /// @name Methods
102 /// @{
103 public:
104 virtual void setFinalPhase( Phases phase ) {
105 finalPhase = phase;
106 }
107
108 virtual void setOptimization( OptimizationLevels level ) {
109 optLevel = level;
110 }
111
112 virtual void setDriverFlags( unsigned flags ) {
113 Flags = flags & DRIVER_FLAGS_MASK;
114 }
115
116 virtual void setOutputMachine( const std::string& machineName ) {
117 machine = machineName;
118 }
119
120 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
121 assert(phase <= LINKING && phase >= PREPROCESSING);
122 AdditionalArgs[phase] = opts;
123 }
124
125 virtual void setLibraryPaths(const StringVector& paths) {
126 StringVector::const_iterator I = paths.begin();
127 StringVector::const_iterator E = paths.end();
128 while (I != E) {
129 sys::Path tmp;
130 tmp.set_directory(*I);
131 LibraryPaths.push_back(tmp);
132 ++I;
133 }
134 }
135
136 virtual void addLibraryPath( const sys::Path& libPath ) {
137 LibraryPaths.push_back(libPath);
138 }
139
140 /// @}
141 /// @name Functions
142 /// @{
143 private:
144 bool isSet(DriverFlags flag) {
145 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
146 }
147
148 void cleanup() {
149 if (!isSet(KEEP_TEMPS_FLAG)) {
150 if (TempDir.is_directory() && TempDir.writable())
151 TempDir.destroy_directory(/*remove_contents=*/true);
152 } else {
153 std::cout << "Temporary files are in " << TempDir.get() << "\n";
154 }
155 }
156
157 sys::Path MakeTempFile(const std::string& basename, const std::string& suffix ) {
158 sys::Path result(TempDir);
159 if (!result.append_file(basename))
160 throw basename + ": can't use this file name";
161 if (!result.append_suffix(suffix))
162 throw suffix + ": can't use this file suffix";
163 return result;
164 }
165
166 Action* GetAction(ConfigData* cd,
167 const sys::Path& input,
168 const sys::Path& output,
169 Phases phase)
170 {
171 Action* pat = 0; ///< The pattern/template for the action
172 Action* action = new Action; ///< The actual action to execute
173
174 // Get the action pattern
175 switch (phase) {
176 case PREPROCESSING: pat = &cd->PreProcessor; break;
177 case TRANSLATION: pat = &cd->Translator; break;
178 case OPTIMIZATION: pat = &cd->Optimizer; break;
179 case ASSEMBLY: pat = &cd->Assembler; break;
180 case LINKING: pat = &cd->Linker; break;
181 default:
182 assert(!"Invalid driver phase!");
183 break;
184 }
185 assert(pat != 0 && "Invalid command pattern");
186
187 // Copy over some pattern things that don't need to change
188 action->program = pat->program;
189 action->flags = pat->flags;
190
191 // Do the substitutions from the pattern to the actual
192 StringVector::iterator PI = pat->args.begin();
193 StringVector::iterator PE = pat->args.end();
194 while (PI != PE) {
195 if ((*PI)[0] == '%') {
196 if (*PI == "%in%") {
197 action->args.push_back(input.get());
198 } else if (*PI == "%out%") {
199 action->args.push_back(output.get());
200 } else if (*PI == "%time%") {
201 if (isSet(TIME_PASSES_FLAG))
202 action->args.push_back("-time-passes");
203 } else if (*PI == "%stats%") {
204 if (isSet(SHOW_STATS_FLAG))
205 action->args.push_back("-stats");
206 } else if (*PI == "%force%") {
207 if (isSet(FORCE_FLAG))
208 action->args.push_back("-f");
209 } else if (*PI == "%verbose%") {
210 if (isSet(VERBOSE_FLAG))
211 action->args.push_back("-v");
212 } else if (*PI == "%target%") {
213 // FIXME: Ignore for now
214 } else if (*PI == "%opt%") {
215 if (!isSet(EMIT_RAW_FLAG)) {
216 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
217 !cd->opts[optLevel].empty())
218 action->args.insert(action->args.end(), cd->opts[optLevel].begin(),
219 cd->opts[optLevel].end());
220 else
221 throw std::string("Optimization options for level ") +
222 utostr(unsigned(optLevel)) + " were not specified";
223 }
224 } else if (*PI == "%args%") {
225 if (AdditionalArgs.size() > unsigned(phase))
226 if (!AdditionalArgs[phase].empty()) {
227 // Get specific options for each kind of action type
228 StringVector& addargs = AdditionalArgs[phase];
229 // Add specific options for each kind of action type
230 action->args.insert(action->args.end(), addargs.begin(), addargs.end());
231 }
232 } else {
233 throw "Invalid substitution name" + *PI;
234 }
235 } else {
236 // Its not a substitution, just put it in the action
237 action->args.push_back(*PI);
238 }
239 PI++;
240 }
241
242
243 // Finally, we're done
244 return action;
245 }
246
247 bool DoAction(Action*action) {
248 assert(action != 0 && "Invalid Action!");
249 if (isSet(VERBOSE_FLAG))
250 WriteAction(action);
251 if (!isSet(DRY_RUN_FLAG)) {
252 action->program = sys::Program::FindProgramByName(action->program.get());
253 if (action->program.is_empty())
254 throw "Can't find program '" + action->program.get() + "'";
255
256 // Invoke the program
257 return 0 == action->program.ExecuteAndWait(action->args);
258 }
259 return true;
260 }
261
262 /// This method tries various variants of a linkage item's file
263 /// name to see if it can find an appropriate file to link with
264 /// in the directory specified.
265 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
266 const sys::Path& dir) {
267 sys::Path fullpath(dir);
268 fullpath.append_file(link_item);
269 fullpath.append_suffix("bc");
270 if (fullpath.readable())
271 return fullpath;
272 fullpath.elide_suffix();
273 fullpath.append_suffix("o");
274 if (fullpath.readable())
275 return fullpath;
276 fullpath = dir;
277 fullpath.append_file(std::string("lib") + link_item);
278 fullpath.append_suffix("a");
279 if (fullpath.readable())
280 return fullpath;
281 fullpath.elide_suffix();
282 fullpath.append_suffix("so");
283 if (fullpath.readable())
284 return fullpath;
285
286 // Didn't find one.
287 fullpath.clear();
288 return fullpath;
289 }
290
291 /// This method processes a linkage item. The item could be a
292 /// Bytecode file needing translation to native code and that is
293 /// dependent on other bytecode libraries, or a native code
294 /// library that should just be linked into the program.
295 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
296 SetVector<sys::Path>& set,
297 std::string& err) {
298 // First, see if the unadorned file name is not readable. If so,
299 // we must track down the file in the lib search path.
300 sys::Path fullpath;
301 if (!link_item.readable()) {
302 // First, look for the library using the -L arguments specified
303 // on the command line.
304 PathVector::iterator PI = LibraryPaths.begin();
305 PathVector::iterator PE = LibraryPaths.end();
306 while (PI != PE && fullpath.is_empty()) {
307 fullpath = GetPathForLinkageItem(link_item.get(),*PI);
308 ++PI;
309 }
310
311 // If we didn't find the file in any of the library search paths
312 // so we have to bail. No where else to look.
313 if (fullpath.is_empty()) {
314 err = std::string("Can't find linkage item '") + link_item.get() + "'";
315 return false;
316 }
317 } else {
318 fullpath = link_item;
319 }
320
321 // If we got here fullpath is the path to the file, and its readable.
322 set.insert(fullpath);
323
324 // If its an LLVM bytecode file ...
325 if (CheckMagic(fullpath.get(), "llvm")) {
326 // Process the dependent libraries recursively
327 Module::LibraryListType modlibs;
328 if (GetBytecodeDependentLibraries(fullpath.get(),modlibs)) {
329 // Traverse the dependent libraries list
330 Module::lib_iterator LI = modlibs.begin();
331 Module::lib_iterator LE = modlibs.end();
332 while ( LI != LE ) {
333 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
334 if (err.empty()) {
335 err = std::string("Library '") + *LI +
336 "' is not valid for linking but is required by file '" +
337 fullpath.get() + "'";
338 } else {
339 err += " which is required by file '" + fullpath.get() + "'";
340 }
341 return false;
342 }
343 ++LI;
344 }
345 } else if (err.empty()) {
346 err = std::string("The dependent libraries could not be extracted from '")
347 + fullpath.get();
348 return false;
349 }
350 }
351 return true;
352 }
353
354 /// @}
355 /// @name Methods
356 /// @{
357 public:
358 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
359 try {
360 // Echo the configuration of options if we're running verbose
361 if (isSet(DEBUG_FLAG)) {
362 std::cerr << "Compiler Driver Options:\n";
363 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
364 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
365 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
366 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
367 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
368 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
369 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
370 std::cerr << "ForceOutput = " << isSet(FORCE_FLAG) << "\n";
371 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
372 std::cerr << "OutputMachine = " << machine << "\n";
373 InputList::const_iterator I = InpList.begin();
374 while ( I != InpList.end() ) {
375 std::cerr << "Input: " << I->first.get() << "(" << I->second << ")\n";
376 ++I;
377 }
378 std::cerr << "Output: " << Output.get() << "\n";
379 }
380
381 // If there's no input, we're done.
382 if (InpList.empty())
383 throw std::string("Nothing to compile.");
384
385 // If they are asking for linking and didn't provide an output
386 // file then its an error (no way for us to "make up" a meaningful
387 // file name based on the various linker input files).
388 if (finalPhase == LINKING && Output.is_empty())
389 throw std::string(
390 "An output file name must be specified for linker output");
391
392 // This vector holds all the resulting actions of the following loop.
393 std::vector<Action*> actions;
394
395 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
396 // for each input item
397 SetVector<sys::Path> LinkageItems;
398 sys::Path OutFile(Output);
399 InputList::const_iterator I = InpList.begin();
400 while ( I != InpList.end() ) {
401 // Get the suffix of the file name
402 const std::string& ftype = I->second;
403
404 // If its a library, bytecode file, or object file, save
405 // it for linking below and short circuit the
406 // pre-processing/translation/assembly phases
407 if (ftype.empty() || ftype == "o" || ftype == "bc") {
408 // We shouldn't get any of these types of files unless we're
409 // later going to link. Enforce this limit now.
410 if (finalPhase != LINKING) {
411 throw std::string(
412 "Pre-compiled objects found but linking not requested");
413 }
414 LinkageItems.insert(I->first);
415 ++I; continue; // short circuit remainder of loop
416 }
417
418 // At this point, we know its something we need to translate
419 // and/or optimize. See if we can get the configuration data
420 // for this kind of file.
421 ConfigData* cd = cdp->ProvideConfigData(I->second);
422 if (cd == 0)
423 throw std::string("Files of type '") + I->second +
424 "' are not recognized.";
425 if (isSet(DEBUG_FLAG))
426 DumpConfigData(cd,I->second);
427
428 // Initialize the input file
429 sys::Path InFile(I->first);
430
431 // PRE-PROCESSING PHASE
432 Action& action = cd->PreProcessor;
433
434 // Get the preprocessing action, if needed, or error if appropriate
435 if (!action.program.is_empty()) {
436 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
437 if (finalPhase == PREPROCESSING)
438 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
439 else {
440 sys::Path TempFile(MakeTempFile(I->first.get(),"E"));
441 actions.push_back(GetAction(cd,InFile,TempFile,PREPROCESSING));
442 InFile = TempFile;
443 }
444 }
445 } else if (finalPhase == PREPROCESSING) {
446 throw cd->langName + " does not support pre-processing";
447 } else if (action.isSet(REQUIRED_FLAG)) {
448 throw std::string("Don't know how to pre-process ") +
449 cd->langName + " files";
450 }
451
452 // Short-circuit remaining actions if all they want is pre-processing
453 if (finalPhase == PREPROCESSING) { ++I; continue; };
454
455 /// TRANSLATION PHASE
456 action = cd->Translator;
457
458 // Get the translation action, if needed, or error if appropriate
459 if (!action.program.is_empty()) {
460 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
461 if (finalPhase == TRANSLATION)
462 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
463 else {
464 sys::Path TempFile(MakeTempFile(I->first.get(),"trans"));
465 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
466 InFile = TempFile;
467 }
468
469 // ll -> bc Helper
470 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
471 /// The output of the translator is an LLVM Assembly program
472 /// We need to translate it to bytecode
473 Action* action = new Action();
474 action->program.set_file("llvm-as");
475 action->args.push_back(InFile.get());
476 action->args.push_back("-o");
477 InFile.append_suffix("bc");
478 action->args.push_back(InFile.get());
479 actions.push_back(action);
480 }
481 }
482 } else if (finalPhase == TRANSLATION) {
483 throw cd->langName + " does not support translation";
484 } else if (action.isSet(REQUIRED_FLAG)) {
485 throw std::string("Don't know how to translate ") +
486 cd->langName + " files";
487 }
488
489 // Short-circuit remaining actions if all they want is translation
490 if (finalPhase == TRANSLATION) { ++I; continue; }
491
492 /// OPTIMIZATION PHASE
493 action = cd->Optimizer;
494
495 // Get the optimization action, if needed, or error if appropriate
496 if (!isSet(EMIT_RAW_FLAG)) {
497 if (!action.program.is_empty()) {
498 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
499 if (finalPhase == OPTIMIZATION)
500 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
501 else {
502 sys::Path TempFile(MakeTempFile(I->first.get(),"opt"));
503 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
504 InFile = TempFile;
505 }
506 // ll -> bc Helper
507 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
508 /// The output of the translator is an LLVM Assembly program
509 /// We need to translate it to bytecode
510 Action* action = new Action();
511 action->program.set_file("llvm-as");
512 action->args.push_back(InFile.get());
513 action->args.push_back("-f");
514 action->args.push_back("-o");
515 InFile.append_suffix("bc");
516 action->args.push_back(InFile.get());
517 actions.push_back(action);
518 }
519 }
520 } else if (finalPhase == OPTIMIZATION) {
521 throw cd->langName + " does not support optimization";
522 } else if (action.isSet(REQUIRED_FLAG)) {
523 throw std::string("Don't know how to optimize ") +
524 cd->langName + " files";
525 }
526 }
527
528 // Short-circuit remaining actions if all they want is optimization
529 if (finalPhase == OPTIMIZATION) { ++I; continue; }
530
531 /// ASSEMBLY PHASE
532 action = cd->Assembler;
533
534 if (finalPhase == ASSEMBLY || isSet(EMIT_NATIVE_FLAG)) {
535 if (isSet(EMIT_NATIVE_FLAG)) {
536 if (action.program.is_empty()) {
537 throw std::string("Native Assembler not specified for ") +
538 cd->langName + " files";
539 } else if (finalPhase == ASSEMBLY) {
540 actions.push_back(GetAction(cd,InFile,OutFile,ASSEMBLY));
541 } else {
542 sys::Path TempFile(MakeTempFile(I->first.get(),"S"));
543 actions.push_back(GetAction(cd,InFile,TempFile,ASSEMBLY));
544 InFile = TempFile;
545 }
546 } else {
547 // Just convert back to llvm assembly with llvm-dis
548 Action* action = new Action();
549 action->program.set_file("llvm-dis");
550 action->args.push_back(InFile.get());
551 action->args.push_back("-f");
552 action->args.push_back("-o");
553 action->args.push_back(OutFile.get());
554 actions.push_back(action);
555 }
556 }
557
558 // Short-circuit remaining actions if all they want is assembly output
559 if (finalPhase == ASSEMBLY) { ++I; continue; }
560
561 // Register the OutFile as a link candidate
562 LinkageItems.insert(InFile);
563
564 // Go to next file to be processed
565 ++I;
566 }
567
568 /// RUN THE COMPILATION ACTIONS
569 std::vector<Action*>::iterator AI = actions.begin();
570 std::vector<Action*>::iterator AE = actions.end();
571 while (AI != AE) {
572 if (!DoAction(*AI))
573 throw "Action failed";
574 AI++;
575 }
576
577 /// LINKING PHASE
578 actions.clear();
579 if (finalPhase == LINKING) {
580 if (isSet(EMIT_NATIVE_FLAG)) {
581 throw "llvmc doesn't know how to link native code yet";
582 } else {
583 // First, we need to examine the files to ensure that they all contain
584 // bytecode files. Since the final output is bytecode, we can only
585 // link bytecode.
586 SetVector<sys::Path>::const_iterator I = LinkageItems.begin();
587 SetVector<sys::Path>::const_iterator E = LinkageItems.end();
588 std::string errmsg;
589
590 while (I != E && ProcessLinkageItem(*I,LinkageItems,errmsg))
591 ++I;
592
593 if (!errmsg.empty())
594 throw errmsg;
595
596 // Insert the system libraries.
597 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath1());
598 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath2());
599
600 // We're emitting bytecode so let's build an llvm-link Action
601 Action* link = new Action();
602 link->program.set_file("llvm-link");
603 for (PathVector::const_iterator I=LinkageItems.begin(),
604 E=LinkageItems.end(); I != E; ++I )
605 link->args.push_back(I->get());
606 if (isSet(VERBOSE_FLAG))
607 link->args.push_back("-v");
608 link->args.push_back("-f");
609 link->args.push_back("-o");
610 link->args.push_back(OutFile.get());
611 if (isSet(TIME_PASSES_FLAG))
612 link->args.push_back("-time-passes");
613 if (isSet(SHOW_STATS_FLAG))
614 link->args.push_back("-stats");
615 actions.push_back(link);
616 }
617 }
618
619 /// RUN THE LINKING ACTIONS
620 AI = actions.begin();
621 AE = actions.end();
622 while (AI != AE) {
623 if (!DoAction(*AI))
624 throw std::string("Action failed");
625 AI++;
626 }
627 } catch (std::string& msg) {
628 cleanup();
629 throw;
630 } catch (...) {
631 cleanup();
632 throw std::string("Unspecified error");
633 }
634 cleanup();
635 return 0;
636 }
637
638 /// @}
639 /// @name Data
640 /// @{
641 private:
642 ConfigDataProvider* cdp; ///< Where we get configuration data from
643 Phases finalPhase; ///< The final phase of compilation
644 OptimizationLevels optLevel; ///< The optimization level to apply
645 unsigned Flags; ///< The driver flags
646 std::string machine; ///< Target machine name
647 PathVector LibraryPaths; ///< -L options
648 sys::Path TempDir; ///< Name of the temporary directory.
649 StringTable AdditionalArgs; ///< The -Txyz options
650
651 /// @}
652 };
Reid Spencer5c56dc12004-08-13 20:22:43 +0000653}
654
655CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000656}
657
658CompilerDriver*
659CompilerDriver::Get(ConfigDataProvider& CDP) {
660 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +0000661}
662
663CompilerDriver::ConfigData::ConfigData()
664 : langName()
665 , PreProcessor()
666 , Translator()
667 , Optimizer()
668 , Assembler()
669 , Linker()
670{
671 StringVector emptyVec;
672 for (unsigned i = 0; i < NUM_PHASES; ++i)
673 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000674}
675
Reid Spencer5c56dc12004-08-13 20:22:43 +0000676// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab