1 //===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
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.
9 //===----------------------------------------------------------------------===//
11 // This file implements the bulk of the LLVM Compiler Driver (llvmc).
13 //===----------------------------------------------------------------------===//
15 #include "CompilerDriver.h"
16 #include "ConfigLexer.h"
17 #include "llvm/Module.h"
18 #include "llvm/Bytecode/Reader.h"
19 #include "llvm/Support/Timer.h"
20 #include "llvm/System/Signals.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Config/alloca.h"
29 void WriteAction(CompilerDriver::Action* action ) {
30 std::cerr << action->program.c_str();
31 std::vector<std::string>::const_iterator I = action->args.begin();
32 while (I != action->args.end()) {
33 std::cerr << ' ' << *I;
39 void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
41 std::vector<std::string>::const_iterator I = action->args.begin();
42 while (I != action->args.end()) {
43 std::cerr << ' ' << *I;
47 std::cerr << "flags = " << action->flags << '\n';
50 void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
51 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
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);
65 /// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
66 /// which should reduce the volume of code and make compilation
67 /// faster. This is also safe on any llvm module.
68 static const char* DefaultFastCompileOptimizations[] = {
69 "-simplifycfg", "-mem2reg", "-instcombine"
72 class CompilerDriverImpl : public CompilerDriver {
73 /// @name Constructors
76 CompilerDriverImpl(ConfigDataProvider& confDatProv )
79 , optLevel(OPT_FAST_COMPILE)
86 TempDir = sys::Path::GetTemporaryDirectory();
87 sys::RemoveDirectoryOnSignal(TempDir);
88 AdditionalArgs.reserve(NUM_PHASES);
89 StringVector emptyVec;
90 for (unsigned i = 0; i < NUM_PHASES; ++i)
91 AdditionalArgs.push_back(emptyVec);
94 virtual ~CompilerDriverImpl() {
101 AdditionalArgs.clear();
111 virtual void setFinalPhase(Phases phase) {
115 virtual void setOptimization(OptimizationLevels level) {
119 virtual void setDriverFlags(unsigned flags) {
120 Flags = flags & DRIVER_FLAGS_MASK;
123 virtual void setOutputMachine(const std::string& machineName) {
124 machine = machineName;
127 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
128 assert(phase <= LINKING && phase >= PREPROCESSING);
129 AdditionalArgs[phase] = opts;
132 virtual void setIncludePaths(const StringVector& paths) {
133 StringVector::const_iterator I = paths.begin();
134 StringVector::const_iterator E = paths.end();
138 IncludePaths.push_back(tmp);
143 virtual void setSymbolDefines(const StringVector& defs) {
147 virtual void setLibraryPaths(const StringVector& paths) {
148 StringVector::const_iterator I = paths.begin();
149 StringVector::const_iterator E = paths.end();
153 LibraryPaths.push_back(tmp);
158 virtual void addLibraryPath(const sys::Path& libPath) {
159 LibraryPaths.push_back(libPath);
162 virtual void addToolPath(const sys::Path& toolPath) {
163 ToolPaths.push_back(toolPath);
166 virtual void setfPassThrough(const StringVector& fOpts) {
170 /// @brief Set the list of -M options to be passed through
171 virtual void setMPassThrough(const StringVector& MOpts) {
175 /// @brief Set the list of -W options to be passed through
176 virtual void setWPassThrough(const StringVector& WOpts) {
184 bool isSet(DriverFlags flag) {
185 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
189 if (!isSet(KEEP_TEMPS_FLAG)) {
190 sys::FileStatus Status;
191 if (!TempDir.getFileStatus(Status) && Status.isDir)
192 TempDir.eraseFromDisk(/*remove_contents=*/true);
194 std::cout << "Temporary files are in " << TempDir << "\n";
198 sys::Path MakeTempFile(const std::string& basename,
199 const std::string& suffix) {
200 sys::Path result(TempDir);
201 if (!result.appendComponent(basename))
202 throw basename + ": can't use this file name";
203 if (!result.appendSuffix(suffix))
204 throw suffix + ": can't use this file suffix";
208 Action* GetAction(ConfigData* cd,
209 const sys::Path& input,
210 const sys::Path& output,
213 Action* pat = 0; ///< The pattern/template for the action
214 Action* action = new Action; ///< The actual action to execute
216 // Get the action pattern
218 case PREPROCESSING: pat = &cd->PreProcessor; break;
219 case TRANSLATION: pat = &cd->Translator; break;
220 case OPTIMIZATION: pat = &cd->Optimizer; break;
221 case ASSEMBLY: pat = &cd->Assembler; break;
222 case LINKING: pat = &cd->Linker; break;
224 assert(!"Invalid driver phase!");
227 assert(pat != 0 && "Invalid command pattern");
229 // Copy over some pattern things that don't need to change
230 action->flags = pat->flags;
232 // See if program starts with wildcard...
233 std::string programName=pat->program.toString();
234 if (programName[0] == '%' && programName.length() >2) {
235 switch(programName[1]){
237 if (programName.substr(0,8) == "%bindir%") {
238 std::string tmp(LLVM_BINDIR);
239 tmp.append(programName.substr(8));
240 pat->program.set(tmp);
244 if (programName.substr(0,12) == "%llvmgccdir%"){
245 std::string tmp(LLVMGCCDIR);
246 tmp.append(programName.substr(12));
247 pat->program.set(tmp);
248 }else if (programName.substr(0,13) == "%llvmgccarch%"){
249 std::string tmp(LLVMGCCARCH);
250 tmp.append(programName.substr(13));
251 pat->program.set(tmp);
252 }else if (programName.substr(0,9) == "%llvmgcc%"){
253 std::string tmp(LLVMGCC);
254 tmp.append(programName.substr(9));
255 pat->program.set(tmp);
256 }else if (programName.substr(0,9) == "%llvmgxx%"){
257 std::string tmp(LLVMGXX);
258 tmp.append(programName.substr(9));
259 pat->program.set(tmp);
260 }else if (programName.substr(0,9) == "%llvmcc1%"){
261 std::string tmp(LLVMCC1);
262 tmp.append(programName.substr(9));
263 pat->program.set(tmp);
264 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
265 std::string tmp(LLVMCC1PLUS);
266 tmp.append(programName.substr(13));
267 pat->program.set(tmp);
268 }else if (programName.substr(0,8) == "%libdir%") {
269 std::string tmp(LLVM_LIBDIR);
270 tmp.append(programName.substr(8));
271 pat->program.set(tmp);
276 action->program = pat->program;
278 // Do the substitutions from the pattern to the actual
279 StringVector::iterator PI = pat->args.begin();
280 StringVector::iterator PE = pat->args.end();
282 if ((*PI)[0] == '%' && PI->length() >2) {
286 if (*PI == "%args%") {
287 if (AdditionalArgs.size() > unsigned(phase))
288 if (!AdditionalArgs[phase].empty()) {
289 // Get specific options for each kind of action type
290 StringVector& addargs = AdditionalArgs[phase];
291 // Add specific options for each kind of action type
292 action->args.insert(action->args.end(), addargs.begin(),
299 if (*PI == "%bindir%") {
300 std::string tmp(*PI);
301 tmp.replace(0,8,LLVM_BINDIR);
302 action->args.push_back(tmp);
307 if (*PI == "%defs%") {
308 StringVector::iterator I = Defines.begin();
309 StringVector::iterator E = Defines.end();
311 action->args.push_back( std::string("-D") + *I);
318 if (*PI == "%fOpts%") {
319 if (!fOptions.empty())
320 action->args.insert(action->args.end(), fOptions.begin(),
327 action->args.push_back(input.toString());
328 } else if (*PI == "%incls%") {
329 PathVector::iterator I = IncludePaths.begin();
330 PathVector::iterator E = IncludePaths.end();
332 action->args.push_back( std::string("-I") + I->toString() );
339 if ((*PI)[1] == 'l') {
340 std::string tmp(*PI);
341 if (*PI == "%llvmgccdir%")
342 tmp.replace(0,12,LLVMGCCDIR);
343 else if (*PI == "%llvmgccarch%")
344 tmp.replace(0,13,LLVMGCCARCH);
345 else if (*PI == "%llvmgcc%")
346 tmp.replace(0,9,LLVMGCC);
347 else if (*PI == "%llvmgxx%")
348 tmp.replace(0,9,LLVMGXX);
349 else if (*PI == "%llvmcc1%")
350 tmp.replace(0,9,LLVMCC1);
351 else if (*PI == "%llvmcc1plus%")
352 tmp.replace(0,9,LLVMCC1);
356 action->args.push_back(tmp);
357 } else if (*PI == "%libs%") {
358 PathVector::iterator I = LibraryPaths.begin();
359 PathVector::iterator E = LibraryPaths.end();
361 action->args.push_back( std::string("-L") + I->toString() );
364 } else if (*PI == "%libdir%") {
365 std::string tmp(*PI);
366 tmp.replace(0,8,LLVM_LIBDIR);
367 action->args.push_back(tmp);
372 if (*PI == "%out%") {
373 action->args.push_back(output.toString());
374 } else if (*PI == "%opt%") {
375 if (!isSet(EMIT_RAW_FLAG)) {
376 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
377 !cd->opts[optLevel].empty())
378 action->args.insert(action->args.end(),
379 cd->opts[optLevel].begin(),
380 cd->opts[optLevel].end());
382 throw std::string("Optimization options for level ") +
383 utostr(unsigned(optLevel)) + " were not specified";
389 if (*PI == "%stats%") {
390 if (isSet(SHOW_STATS_FLAG))
391 action->args.push_back("-stats");
396 if (*PI == "%target%") {
397 action->args.push_back(std::string("-march=") + machine);
398 } else if (*PI == "%time%") {
399 if (isSet(TIME_PASSES_FLAG))
400 action->args.push_back("-time-passes");
405 if (*PI == "%verbose%") {
406 if (isSet(VERBOSE_FLAG))
407 action->args.push_back("-v");
412 if (*PI == "%Mopts%") {
413 if (!MOptions.empty())
414 action->args.insert(action->args.end(), MOptions.begin(),
420 if (*PI == "%Wopts%") {
421 for (StringVector::iterator I = WOptions.begin(),
422 E = WOptions.end(); I != E ; ++I ) {
423 action->args.push_back(std::string("-W") + *I);
433 // Did it even look like a substitution?
434 if (PI->length()>1 && (*PI)[0] == '%' &&
435 (*PI)[PI->length()-1] == '%') {
436 throw std::string("Invalid substitution token: '") + *PI +
437 "' for command '" + pat->program.toString() + "'";
438 } else if (!PI->empty()) {
439 // It's not a legal substitution, just pass it through
440 action->args.push_back(*PI);
443 } else if (!PI->empty()) {
444 // Its not a substitution, just put it in the action
445 action->args.push_back(*PI);
450 // Finally, we're done
454 bool DoAction(Action*action) {
455 assert(action != 0 && "Invalid Action!");
456 if (isSet(VERBOSE_FLAG))
458 if (!isSet(DRY_RUN_FLAG)) {
459 sys::Path progpath = sys::Program::FindProgramByName(
460 action->program.toString());
461 if (progpath.isEmpty())
462 throw std::string("Can't find program '" +
463 action->program.toString()+"'");
464 else if (progpath.canExecute())
465 action->program = progpath;
467 throw std::string("Program '"+action->program.toString()+
468 "' is not executable.");
470 // Invoke the program
471 const char** Args = (const char**)
472 alloca(sizeof(const char*)*(action->args.size()+2));
473 Args[0] = action->program.toString().c_str();
474 for (unsigned i = 1; i <= action->args.size(); ++i)
475 Args[i] = action->args[i-1].c_str();
476 Args[action->args.size()+1] = 0; // null terminate list.
477 if (isSet(TIME_ACTIONS_FLAG)) {
478 Timer timer(action->program.toString());
480 int resultCode = sys::Program::ExecuteAndWait(action->program, Args);
482 timer.print(timer,std::cerr);
483 return resultCode == 0;
486 return 0 == sys::Program::ExecuteAndWait(action->program, Args);
491 /// This method tries various variants of a linkage item's file
492 /// name to see if it can find an appropriate file to link with
493 /// in the directories of the LibraryPaths.
494 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
495 bool native = false) {
497 fullpath.set(link_item);
498 if (fullpath.canRead())
500 for (PathVector::iterator PI = LibraryPaths.begin(),
501 PE = LibraryPaths.end(); PI != PE; ++PI) {
502 fullpath.set(PI->toString());
503 fullpath.appendComponent(link_item);
504 if (fullpath.canRead())
507 fullpath.appendSuffix("a");
509 fullpath.appendSuffix("bc");
510 if (fullpath.canRead())
512 fullpath.eraseSuffix();
513 fullpath.appendSuffix("o");
514 if (fullpath.canRead())
517 fullpath.appendComponent(std::string("lib") + link_item);
518 fullpath.appendSuffix("a");
519 if (fullpath.canRead())
521 fullpath.eraseSuffix();
522 fullpath.appendSuffix("so");
523 if (fullpath.canRead())
533 /// This method processes a linkage item. The item could be a
534 /// Bytecode file needing translation to native code and that is
535 /// dependent on other bytecode libraries, or a native code
536 /// library that should just be linked into the program.
537 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
538 SetVector<sys::Path>& set,
540 // First, see if the unadorned file name is not readable. If so,
541 // we must track down the file in the lib search path.
543 if (!link_item.canRead()) {
544 // look for the library using the -L arguments specified
545 // on the command line.
546 fullpath = GetPathForLinkageItem(link_item.toString());
548 // If we didn't find the file in any of the library search paths
549 // we have to bail. No where else to look.
550 if (fullpath.isEmpty()) {
552 std::string("Can't find linkage item '") + link_item.toString() + "'";
556 fullpath = link_item;
559 // If we got here fullpath is the path to the file, and its readable.
560 set.insert(fullpath);
562 // If its an LLVM bytecode file ...
563 if (fullpath.isBytecodeFile()) {
564 // Process the dependent libraries recursively
565 Module::LibraryListType modlibs;
566 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) {
567 // Traverse the dependent libraries list
568 Module::lib_iterator LI = modlibs.begin();
569 Module::lib_iterator LE = modlibs.end();
571 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
573 err = std::string("Library '") + *LI +
574 "' is not valid for linking but is required by file '" +
575 fullpath.toString() + "'";
577 err += " which is required by file '" + fullpath.toString() + "'";
583 } else if (err.empty()) {
585 "The dependent libraries could not be extracted from '") +
597 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
599 // Echo the configuration of options if we're running verbose
600 if (isSet(DEBUG_FLAG)) {
601 std::cerr << "Compiler Driver Options:\n";
602 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
603 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
604 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
605 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
606 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
607 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
608 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
609 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
610 std::cerr << "OutputMachine = " << machine << "\n";
611 InputList::const_iterator I = InpList.begin();
612 while ( I != InpList.end() ) {
613 std::cerr << "Input: " << I->first << "(" << I->second
617 std::cerr << "Output: " << Output << "\n";
620 // If there's no input, we're done.
622 throw std::string("Nothing to compile.");
624 // If they are asking for linking and didn't provide an output
625 // file then its an error (no way for us to "make up" a meaningful
626 // file name based on the various linker input files).
627 if (finalPhase == LINKING && Output.isEmpty())
629 "An output file name must be specified for linker output");
631 // If they are not asking for linking, provided an output file and
632 // there is more than one input file, its an error
633 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
634 throw std::string("An output file name cannot be specified ") +
635 "with more than one input file name when not linking";
637 // This vector holds all the resulting actions of the following loop.
638 std::vector<Action*> actions;
640 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
641 // for each input item
642 SetVector<sys::Path> LinkageItems;
643 StringVector LibFiles;
644 InputList::const_iterator I = InpList.begin();
645 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
647 // Get the suffix of the file name
648 const std::string& ftype = I->second;
650 // If its a library, bytecode file, or object file, save
651 // it for linking below and short circuit the
652 // pre-processing/translation/assembly phases
653 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
654 // We shouldn't get any of these types of files unless we're
655 // later going to link. Enforce this limit now.
656 if (finalPhase != LINKING) {
658 "Pre-compiled objects found but linking not requested");
661 LibFiles.push_back(I->first.toString());
663 LinkageItems.insert(I->first);
664 continue; // short circuit remainder of loop
667 // At this point, we know its something we need to translate
668 // and/or optimize. See if we can get the configuration data
669 // for this kind of file.
670 ConfigData* cd = cdp->ProvideConfigData(I->second);
672 throw std::string("Files of type '") + I->second +
673 "' are not recognized.";
674 if (isSet(DEBUG_FLAG))
675 DumpConfigData(cd,I->second);
677 // Add the config data's library paths to the end of the list
678 for (StringVector::iterator LPI = cd->libpaths.begin(),
679 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
680 LibraryPaths.push_back(sys::Path(*LPI));
683 // Initialize the input and output files
684 sys::Path InFile(I->first);
685 sys::Path OutFile(I->first.getBasename());
687 // PRE-PROCESSING PHASE
688 Action& action = cd->PreProcessor;
690 // Get the preprocessing action, if needed, or error if appropriate
691 if (!action.program.isEmpty()) {
692 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
693 if (finalPhase == PREPROCESSING) {
694 if (Output.isEmpty()) {
695 OutFile.appendSuffix("E");
696 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
698 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
701 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
702 actions.push_back(GetAction(cd,InFile,TempFile,
707 } else if (finalPhase == PREPROCESSING) {
708 throw cd->langName + " does not support pre-processing";
709 } else if (action.isSet(REQUIRED_FLAG)) {
710 throw std::string("Don't know how to pre-process ") +
711 cd->langName + " files";
714 // Short-circuit remaining actions if all they want is
716 if (finalPhase == PREPROCESSING) { continue; };
718 /// TRANSLATION PHASE
719 action = cd->Translator;
721 // Get the translation action, if needed, or error if appropriate
722 if (!action.program.isEmpty()) {
723 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
724 if (finalPhase == TRANSLATION) {
725 if (Output.isEmpty()) {
726 OutFile.appendSuffix("o");
727 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
729 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
732 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
733 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
738 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
739 /// The output of the translator is an LLVM Assembly program
740 /// We need to translate it to bytecode
741 Action* action = new Action();
742 action->program.set("llvm-as");
743 action->args.push_back(InFile.toString());
744 action->args.push_back("-o");
745 InFile.appendSuffix("bc");
746 action->args.push_back(InFile.toString());
747 actions.push_back(action);
750 } else if (finalPhase == TRANSLATION) {
751 throw cd->langName + " does not support translation";
752 } else if (action.isSet(REQUIRED_FLAG)) {
753 throw std::string("Don't know how to translate ") +
754 cd->langName + " files";
757 // Short-circuit remaining actions if all they want is translation
758 if (finalPhase == TRANSLATION) { continue; }
760 /// OPTIMIZATION PHASE
761 action = cd->Optimizer;
763 // Get the optimization action, if needed, or error if appropriate
764 if (!isSet(EMIT_RAW_FLAG)) {
765 if (!action.program.isEmpty()) {
766 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
767 if (finalPhase == OPTIMIZATION) {
768 if (Output.isEmpty()) {
769 OutFile.appendSuffix("o");
770 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
772 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
775 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
776 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
780 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
781 /// The output of the optimizer is an LLVM Assembly program
782 /// We need to translate it to bytecode with llvm-as
783 Action* action = new Action();
784 action->program.set("llvm-as");
785 action->args.push_back(InFile.toString());
786 action->args.push_back("-f");
787 action->args.push_back("-o");
788 InFile.appendSuffix("bc");
789 action->args.push_back(InFile.toString());
790 actions.push_back(action);
793 } else if (finalPhase == OPTIMIZATION) {
794 throw cd->langName + " does not support optimization";
795 } else if (action.isSet(REQUIRED_FLAG)) {
796 throw std::string("Don't know how to optimize ") +
797 cd->langName + " files";
801 // Short-circuit remaining actions if all they want is optimization
802 if (finalPhase == OPTIMIZATION) { continue; }
805 action = cd->Assembler;
807 if (finalPhase == ASSEMBLY) {
809 // Build either a native compilation action or a disassembly action
810 Action* action = new Action();
811 if (isSet(EMIT_NATIVE_FLAG)) {
812 // Use llc to get the native assembly file
813 action->program.set("llc");
814 action->args.push_back(InFile.toString());
815 action->args.push_back("-f");
816 action->args.push_back("-o");
817 if (Output.isEmpty()) {
818 OutFile.appendSuffix("o");
819 action->args.push_back(OutFile.toString());
821 action->args.push_back(Output.toString());
823 actions.push_back(action);
825 // Just convert back to llvm assembly with llvm-dis
826 action->program.set("llvm-dis");
827 action->args.push_back(InFile.toString());
828 action->args.push_back("-f");
829 action->args.push_back("-o");
830 if (Output.isEmpty()) {
831 OutFile.appendSuffix("ll");
832 action->args.push_back(OutFile.toString());
834 action->args.push_back(Output.toString());
838 // Put the action on the list
839 actions.push_back(action);
841 // Short circuit the rest of the loop, we don't want to link
845 // Register the result of the actions as a link candidate
846 LinkageItems.insert(InFile);
848 } // end while loop over each input file
850 /// RUN THE COMPILATION ACTIONS
851 std::vector<Action*>::iterator AI = actions.begin();
852 std::vector<Action*>::iterator AE = actions.end();
855 throw std::string("Action failed");
860 if (finalPhase == LINKING) {
862 // Insert the platform-specific system libraries to the path list
863 std::vector<sys::Path> SysLibs;
864 sys::Path::GetSystemLibraryPaths(SysLibs);
865 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
867 // Set up the linking action with llvm-ld
868 Action* link = new Action();
869 link->program.set("llvm-ld");
871 // Add in the optimization level requested
873 case OPT_FAST_COMPILE:
874 link->args.push_back("-O1");
877 link->args.push_back("-O2");
880 link->args.push_back("-O3");
883 link->args.push_back("-O4");
885 case OPT_AGGRESSIVE_LINK_TIME:
886 link->args.push_back("-O5");
892 // Add in all the linkage items we generated. This includes the
893 // output from the translation/optimization phases as well as any
894 // -l arguments specified.
895 for (PathVector::const_iterator I=LinkageItems.begin(),
896 E=LinkageItems.end(); I != E; ++I )
897 link->args.push_back(I->toString());
899 // Add in all the libraries we found.
900 for (StringVector::const_iterator I=LibFiles.begin(),
901 E=LibFiles.end(); I != E; ++I )
902 link->args.push_back(std::string("-l")+*I);
904 // Add in all the library paths to the command line
905 for (PathVector::const_iterator I=LibraryPaths.begin(),
906 E=LibraryPaths.end(); I != E; ++I)
907 link->args.push_back( std::string("-L") + I->toString());
909 // Add in the additional linker arguments requested
910 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
911 E=AdditionalArgs[LINKING].end(); I != E; ++I)
912 link->args.push_back( *I );
914 // Add in other optional flags
915 if (isSet(EMIT_NATIVE_FLAG))
916 link->args.push_back("-native");
917 if (isSet(VERBOSE_FLAG))
918 link->args.push_back("-v");
919 if (isSet(TIME_PASSES_FLAG))
920 link->args.push_back("-time-passes");
921 if (isSet(SHOW_STATS_FLAG))
922 link->args.push_back("-stats");
923 if (isSet(STRIP_OUTPUT_FLAG))
924 link->args.push_back("-s");
925 if (isSet(DEBUG_FLAG)) {
926 link->args.push_back("-debug");
927 link->args.push_back("-debug-pass=Details");
930 // Add in mandatory flags
931 link->args.push_back("-o");
932 link->args.push_back(Output.toString());
936 throw std::string("Action failed");
938 } catch (std::string& msg) {
943 throw std::string("Unspecified error");
953 ConfigDataProvider* cdp; ///< Where we get configuration data from
954 Phases finalPhase; ///< The final phase of compilation
955 OptimizationLevels optLevel; ///< The optimization level to apply
956 unsigned Flags; ///< The driver flags
957 std::string machine; ///< Target machine name
958 PathVector LibraryPaths; ///< -L options
959 PathVector IncludePaths; ///< -I options
960 PathVector ToolPaths; ///< -B options
961 StringVector Defines; ///< -D options
962 sys::Path TempDir; ///< Name of the temporary directory.
963 StringTable AdditionalArgs; ///< The -Txyz options
964 StringVector fOptions; ///< -f options
965 StringVector MOptions; ///< -M options
966 StringVector WOptions; ///< -W options
972 CompilerDriver::~CompilerDriver() {
975 CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
978 CompilerDriver::Get(ConfigDataProvider& CDP) {
979 return new CompilerDriverImpl(CDP);
982 CompilerDriver::ConfigData::ConfigData()
990 StringVector emptyVec;
991 for (unsigned i = 0; i < NUM_PHASES; ++i)
992 opts.push_back(emptyVec);