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 static 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);
74 Module* M = MP->releaseModule(ErrMsg);
75 deplibs = M->getLibraries();
82 class CompilerDriverImpl : public CompilerDriver {
83 /// @name Constructors
86 CompilerDriverImpl(ConfigDataProvider& confDatProv )
89 , optLevel(OPT_FAST_COMPILE)
96 AdditionalArgs.reserve(NUM_PHASES);
97 StringVector emptyVec;
98 for (unsigned i = 0; i < NUM_PHASES; ++i)
99 AdditionalArgs.push_back(emptyVec);
102 virtual ~CompilerDriverImpl() {
105 LibraryPaths.clear();
106 IncludePaths.clear();
109 AdditionalArgs.clear();
119 virtual void setFinalPhase(Phases phase) {
123 virtual void setOptimization(OptimizationLevels level) {
127 virtual void setDriverFlags(unsigned flags) {
128 Flags = flags & DRIVER_FLAGS_MASK;
131 virtual void setOutputMachine(const std::string& machineName) {
132 machine = machineName;
135 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
136 assert(phase <= LINKING && phase >= PREPROCESSING);
137 AdditionalArgs[phase] = opts;
140 virtual void setIncludePaths(const StringVector& paths) {
141 StringVector::const_iterator I = paths.begin();
142 StringVector::const_iterator E = paths.end();
146 IncludePaths.push_back(tmp);
151 virtual void setSymbolDefines(const StringVector& defs) {
155 virtual void setLibraryPaths(const StringVector& paths) {
156 StringVector::const_iterator I = paths.begin();
157 StringVector::const_iterator E = paths.end();
161 LibraryPaths.push_back(tmp);
166 virtual void addLibraryPath(const sys::Path& libPath) {
167 LibraryPaths.push_back(libPath);
170 virtual void addToolPath(const sys::Path& toolPath) {
171 ToolPaths.push_back(toolPath);
174 virtual void setfPassThrough(const StringVector& fOpts) {
178 /// @brief Set the list of -M options to be passed through
179 virtual void setMPassThrough(const StringVector& MOpts) {
183 /// @brief Set the list of -W options to be passed through
184 virtual void setWPassThrough(const StringVector& WOpts) {
192 bool isSet(DriverFlags flag) {
193 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
197 if (!isSet(KEEP_TEMPS_FLAG)) {
198 const sys::FileStatus *Status =
199 sys::PathWithStatus(TempDir).getFileStatus();
200 if (Status && Status->isDir)
201 TempDir.eraseFromDisk(/*remove_contents=*/true);
203 std::cout << "Temporary files are in " << TempDir << "\n";
207 sys::Path MakeTempFile(const std::string& basename,
208 const std::string& suffix,
209 std::string* ErrMsg) {
210 if (TempDir.isEmpty()) {
211 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
212 if (TempDir.isEmpty())
214 sys::RemoveDirectoryOnSignal(TempDir);
216 sys::Path result(TempDir);
217 if (!result.appendComponent(basename)) {
219 *ErrMsg = basename + ": can't use this file name";
222 if (!result.appendSuffix(suffix)) {
224 *ErrMsg = suffix + ": can't use this file suffix";
230 Action* GetAction(ConfigData* cd,
231 const sys::Path& input,
232 const sys::Path& output,
235 Action* pat = 0; ///< The pattern/template for the action
236 Action* action = new Action; ///< The actual action to execute
238 // Get the action pattern
240 case PREPROCESSING: pat = &cd->PreProcessor; break;
241 case TRANSLATION: pat = &cd->Translator; break;
242 case OPTIMIZATION: pat = &cd->Optimizer; break;
243 case ASSEMBLY: pat = &cd->Assembler; break;
244 case LINKING: pat = &cd->Linker; break;
246 assert(!"Invalid driver phase!");
249 assert(pat != 0 && "Invalid command pattern");
251 // Copy over some pattern things that don't need to change
252 action->flags = pat->flags;
254 // See if program starts with wildcard...
255 std::string programName=pat->program.toString();
256 if (programName[0] == '%' && programName.length() >2) {
257 switch(programName[1]){
259 if (programName.substr(0,8) == "%bindir%") {
260 std::string tmp(LLVM_BINDIR);
261 tmp.append(programName.substr(8));
262 pat->program.set(tmp);
266 if (programName.substr(0,12) == "%llvmgccdir%"){
267 std::string tmp(LLVMGCCDIR);
268 tmp.append(programName.substr(12));
269 pat->program.set(tmp);
270 }else if (programName.substr(0,13) == "%llvmgccarch%"){
271 std::string tmp(LLVMGCCARCH);
272 tmp.append(programName.substr(13));
273 pat->program.set(tmp);
274 }else if (programName.substr(0,9) == "%llvmgcc%"){
275 std::string tmp(LLVMGCC);
276 tmp.append(programName.substr(9));
277 pat->program.set(tmp);
278 }else if (programName.substr(0,9) == "%llvmgxx%"){
279 std::string tmp(LLVMGXX);
280 tmp.append(programName.substr(9));
281 pat->program.set(tmp);
282 }else if (programName.substr(0,9) == "%llvmcc1%"){
283 std::string tmp(LLVMCC1);
284 tmp.append(programName.substr(9));
285 pat->program.set(tmp);
286 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
287 std::string tmp(LLVMCC1PLUS);
288 tmp.append(programName.substr(13));
289 pat->program.set(tmp);
290 }else if (programName.substr(0,8) == "%libdir%") {
291 std::string tmp(LLVM_LIBDIR);
292 tmp.append(programName.substr(8));
293 pat->program.set(tmp);
298 action->program = pat->program;
300 // Do the substitutions from the pattern to the actual
301 StringVector::iterator PI = pat->args.begin();
302 StringVector::iterator PE = pat->args.end();
304 if ((*PI)[0] == '%' && PI->length() >2) {
308 if (*PI == "%args%") {
309 if (AdditionalArgs.size() > unsigned(phase))
310 if (!AdditionalArgs[phase].empty()) {
311 // Get specific options for each kind of action type
312 StringVector& addargs = AdditionalArgs[phase];
313 // Add specific options for each kind of action type
314 action->args.insert(action->args.end(), addargs.begin(),
321 if (*PI == "%bindir%") {
322 std::string tmp(*PI);
323 tmp.replace(0,8,LLVM_BINDIR);
324 action->args.push_back(tmp);
329 if (*PI == "%defs%") {
330 StringVector::iterator I = Defines.begin();
331 StringVector::iterator E = Defines.end();
333 action->args.push_back( std::string("-D") + *I);
340 if (*PI == "%fOpts%") {
341 if (!fOptions.empty())
342 action->args.insert(action->args.end(), fOptions.begin(),
349 action->args.push_back(input.toString());
350 } else if (*PI == "%incls%") {
351 PathVector::iterator I = IncludePaths.begin();
352 PathVector::iterator E = IncludePaths.end();
354 action->args.push_back( std::string("-I") + I->toString() );
361 if ((*PI)[1] == 'l') {
362 std::string tmp(*PI);
363 if (*PI == "%llvmgccdir%")
364 tmp.replace(0,12,LLVMGCCDIR);
365 else if (*PI == "%llvmgccarch%")
366 tmp.replace(0,13,LLVMGCCARCH);
367 else if (*PI == "%llvmgcc%")
368 tmp.replace(0,9,LLVMGCC);
369 else if (*PI == "%llvmgxx%")
370 tmp.replace(0,9,LLVMGXX);
371 else if (*PI == "%llvmcc1%")
372 tmp.replace(0,9,LLVMCC1);
373 else if (*PI == "%llvmcc1plus%")
374 tmp.replace(0,9,LLVMCC1);
378 action->args.push_back(tmp);
379 } else if (*PI == "%libs%") {
380 PathVector::iterator I = LibraryPaths.begin();
381 PathVector::iterator E = LibraryPaths.end();
383 action->args.push_back( std::string("-L") + I->toString() );
386 } else if (*PI == "%libdir%") {
387 std::string tmp(*PI);
388 tmp.replace(0,8,LLVM_LIBDIR);
389 action->args.push_back(tmp);
394 if (*PI == "%out%") {
395 action->args.push_back(output.toString());
396 } else if (*PI == "%opt%") {
397 if (!isSet(EMIT_RAW_FLAG)) {
398 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
399 !cd->opts[optLevel].empty())
400 action->args.insert(action->args.end(),
401 cd->opts[optLevel].begin(),
402 cd->opts[optLevel].end());
404 throw std::string("Optimization options for level ") +
405 utostr(unsigned(optLevel)) + " were not specified";
411 if (*PI == "%stats%") {
412 if (isSet(SHOW_STATS_FLAG))
413 action->args.push_back("-stats");
418 if (*PI == "%target%") {
419 action->args.push_back(std::string("-march=") + machine);
420 } else if (*PI == "%time%") {
421 if (isSet(TIME_PASSES_FLAG))
422 action->args.push_back("-time-passes");
427 if (*PI == "%verbose%") {
428 if (isSet(VERBOSE_FLAG))
429 action->args.push_back("-v");
434 if (*PI == "%Mopts%") {
435 if (!MOptions.empty())
436 action->args.insert(action->args.end(), MOptions.begin(),
442 if (*PI == "%Wopts%") {
443 for (StringVector::iterator I = WOptions.begin(),
444 E = WOptions.end(); I != E ; ++I ) {
445 action->args.push_back(std::string("-W") + *I);
455 // Did it even look like a substitution?
456 if (PI->length()>1 && (*PI)[0] == '%' &&
457 (*PI)[PI->length()-1] == '%') {
458 throw std::string("Invalid substitution token: '") + *PI +
459 "' for command '" + pat->program.toString() + "'";
460 } else if (!PI->empty()) {
461 // It's not a legal substitution, just pass it through
462 action->args.push_back(*PI);
465 } else if (!PI->empty()) {
466 // Its not a substitution, just put it in the action
467 action->args.push_back(*PI);
472 // Finally, we're done
476 int DoAction(Action*action, std::string& ErrMsg) {
477 assert(action != 0 && "Invalid Action!");
478 if (isSet(VERBOSE_FLAG))
480 if (!isSet(DRY_RUN_FLAG)) {
481 sys::Path progpath = sys::Program::FindProgramByName(
482 action->program.toString());
483 if (progpath.isEmpty())
484 throw std::string("Can't find program '" +
485 action->program.toString()+"'");
486 else if (progpath.canExecute())
487 action->program = progpath;
489 throw std::string("Program '"+action->program.toString()+
490 "' is not executable.");
492 // Invoke the program
493 const char** Args = (const char**)
494 alloca(sizeof(const char*)*(action->args.size()+2));
495 Args[0] = action->program.toString().c_str();
496 for (unsigned i = 1; i <= action->args.size(); ++i)
497 Args[i] = action->args[i-1].c_str();
498 Args[action->args.size()+1] = 0; // null terminate list.
499 if (isSet(TIME_ACTIONS_FLAG)) {
500 Timer timer(action->program.toString());
503 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
505 timer.print(timer,std::cerr);
510 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
515 /// This method tries various variants of a linkage item's file
516 /// name to see if it can find an appropriate file to link with
517 /// in the directories of the LibraryPaths.
518 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
519 bool native = false) {
521 fullpath.set(link_item);
522 if (fullpath.canRead())
524 for (PathVector::iterator PI = LibraryPaths.begin(),
525 PE = LibraryPaths.end(); PI != PE; ++PI) {
526 fullpath.set(PI->toString());
527 fullpath.appendComponent(link_item);
528 if (fullpath.canRead())
531 fullpath.appendSuffix("a");
533 fullpath.appendSuffix("bc");
534 if (fullpath.canRead())
536 fullpath.eraseSuffix();
537 fullpath.appendSuffix("o");
538 if (fullpath.canRead())
541 fullpath.appendComponent(std::string("lib") + link_item);
542 fullpath.appendSuffix("a");
543 if (fullpath.canRead())
545 fullpath.eraseSuffix();
546 fullpath.appendSuffix("so");
547 if (fullpath.canRead())
557 /// This method processes a linkage item. The item could be a
558 /// Bytecode file needing translation to native code and that is
559 /// dependent on other bytecode libraries, or a native code
560 /// library that should just be linked into the program.
561 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
562 SetVector<sys::Path>& set,
564 // First, see if the unadorned file name is not readable. If so,
565 // we must track down the file in the lib search path.
567 if (!link_item.canRead()) {
568 // look for the library using the -L arguments specified
569 // on the command line.
570 fullpath = GetPathForLinkageItem(link_item.toString());
572 // If we didn't find the file in any of the library search paths
573 // we have to bail. No where else to look.
574 if (fullpath.isEmpty()) {
576 std::string("Can't find linkage item '") + link_item.toString() + "'";
580 fullpath = link_item;
583 // If we got here fullpath is the path to the file, and its readable.
584 set.insert(fullpath);
586 // If its an LLVM bytecode file ...
587 if (fullpath.isBytecodeFile()) {
588 // Process the dependent libraries recursively
589 Module::LibraryListType modlibs;
590 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,
591 Compressor::decompressToNewBuffer,
593 // Traverse the dependent libraries list
594 Module::lib_iterator LI = modlibs.begin();
595 Module::lib_iterator LE = modlibs.end();
597 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
599 err = std::string("Library '") + *LI +
600 "' is not valid for linking but is required by file '" +
601 fullpath.toString() + "'";
603 err += " which is required by file '" + fullpath.toString() + "'";
609 } else if (err.empty()) {
611 "The dependent libraries could not be extracted from '") +
624 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
626 // Echo the configuration of options if we're running verbose
627 if (isSet(DEBUG_FLAG)) {
628 std::cerr << "Compiler Driver Options:\n";
629 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
630 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
631 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
632 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
633 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
634 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
635 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
636 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
637 std::cerr << "OutputMachine = " << machine << "\n";
638 InputList::const_iterator I = InpList.begin();
639 while ( I != InpList.end() ) {
640 std::cerr << "Input: " << I->first << "(" << I->second
644 std::cerr << "Output: " << Output << "\n";
647 // If there's no input, we're done.
649 throw std::string("Nothing to compile.");
651 // If they are asking for linking and didn't provide an output
652 // file then its an error (no way for us to "make up" a meaningful
653 // file name based on the various linker input files).
654 if (finalPhase == LINKING && Output.isEmpty())
656 "An output file name must be specified for linker output");
658 // If they are not asking for linking, provided an output file and
659 // there is more than one input file, its an error
660 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
661 throw std::string("An output file name cannot be specified ") +
662 "with more than one input file name when not linking";
664 // This vector holds all the resulting actions of the following loop.
665 std::vector<Action*> actions;
667 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
668 // for each input item
669 SetVector<sys::Path> LinkageItems;
670 StringVector LibFiles;
671 InputList::const_iterator I = InpList.begin();
672 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
674 // Get the suffix of the file name
675 const std::string& ftype = I->second;
677 // If its a library, bytecode file, or object file, save
678 // it for linking below and short circuit the
679 // pre-processing/translation/assembly phases
680 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
681 // We shouldn't get any of these types of files unless we're
682 // later going to link. Enforce this limit now.
683 if (finalPhase != LINKING) {
685 "Pre-compiled objects found but linking not requested");
688 LibFiles.push_back(I->first.toString());
690 LinkageItems.insert(I->first);
691 continue; // short circuit remainder of loop
694 // At this point, we know its something we need to translate
695 // and/or optimize. See if we can get the configuration data
696 // for this kind of file.
697 ConfigData* cd = cdp->ProvideConfigData(I->second);
699 throw std::string("Files of type '") + I->second +
700 "' are not recognized.";
701 if (isSet(DEBUG_FLAG))
702 DumpConfigData(cd,I->second);
704 // Add the config data's library paths to the end of the list
705 for (StringVector::iterator LPI = cd->libpaths.begin(),
706 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
707 LibraryPaths.push_back(sys::Path(*LPI));
710 // Initialize the input and output files
711 sys::Path InFile(I->first);
712 sys::Path OutFile(I->first.getBasename());
714 // PRE-PROCESSING PHASE
715 Action& action = cd->PreProcessor;
717 // Get the preprocessing action, if needed, or error if appropriate
718 if (!action.program.isEmpty()) {
719 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
720 if (finalPhase == PREPROCESSING) {
721 if (Output.isEmpty()) {
722 OutFile.appendSuffix("E");
723 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
725 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
729 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
730 if (TempFile.isEmpty())
732 actions.push_back(GetAction(cd,InFile,TempFile,
737 } else if (finalPhase == PREPROCESSING) {
738 throw cd->langName + " does not support pre-processing";
739 } else if (action.isSet(REQUIRED_FLAG)) {
740 throw std::string("Don't know how to pre-process ") +
741 cd->langName + " files";
744 // Short-circuit remaining actions if all they want is
746 if (finalPhase == PREPROCESSING) { continue; };
748 /// TRANSLATION PHASE
749 action = cd->Translator;
751 // Get the translation action, if needed, or error if appropriate
752 if (!action.program.isEmpty()) {
753 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
754 if (finalPhase == TRANSLATION) {
755 if (Output.isEmpty()) {
756 OutFile.appendSuffix("o");
757 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
759 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
763 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
764 if (TempFile.isEmpty())
766 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
771 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
772 /// The output of the translator is an LLVM Assembly program
773 /// We need to translate it to bytecode
774 Action* action = new Action();
775 action->program.set("llvm-as");
776 action->args.push_back(InFile.toString());
777 action->args.push_back("-o");
778 InFile.appendSuffix("bc");
779 action->args.push_back(InFile.toString());
780 actions.push_back(action);
783 } else if (finalPhase == TRANSLATION) {
784 throw cd->langName + " does not support translation";
785 } else if (action.isSet(REQUIRED_FLAG)) {
786 throw std::string("Don't know how to translate ") +
787 cd->langName + " files";
790 // Short-circuit remaining actions if all they want is translation
791 if (finalPhase == TRANSLATION) { continue; }
793 /// OPTIMIZATION PHASE
794 action = cd->Optimizer;
796 // Get the optimization action, if needed, or error if appropriate
797 if (!isSet(EMIT_RAW_FLAG)) {
798 if (!action.program.isEmpty()) {
799 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
800 if (finalPhase == OPTIMIZATION) {
801 if (Output.isEmpty()) {
802 OutFile.appendSuffix("o");
803 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
805 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
809 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
810 if (TempFile.isEmpty())
812 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
816 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
817 /// The output of the optimizer is an LLVM Assembly program
818 /// We need to translate it to bytecode with llvm-as
819 Action* action = new Action();
820 action->program.set("llvm-as");
821 action->args.push_back(InFile.toString());
822 action->args.push_back("-f");
823 action->args.push_back("-o");
824 InFile.appendSuffix("bc");
825 action->args.push_back(InFile.toString());
826 actions.push_back(action);
829 } else if (finalPhase == OPTIMIZATION) {
830 throw cd->langName + " does not support optimization";
831 } else if (action.isSet(REQUIRED_FLAG)) {
832 throw std::string("Don't know how to optimize ") +
833 cd->langName + " files";
837 // Short-circuit remaining actions if all they want is optimization
838 if (finalPhase == OPTIMIZATION) { continue; }
841 action = cd->Assembler;
843 if (finalPhase == ASSEMBLY) {
845 // Build either a native compilation action or a disassembly action
846 Action* action = new Action();
847 if (isSet(EMIT_NATIVE_FLAG)) {
848 // Use llc to get the native assembly file
849 action->program.set("llc");
850 action->args.push_back(InFile.toString());
851 action->args.push_back("-f");
852 action->args.push_back("-o");
853 if (Output.isEmpty()) {
854 OutFile.appendSuffix("o");
855 action->args.push_back(OutFile.toString());
857 action->args.push_back(Output.toString());
859 actions.push_back(action);
861 // Just convert back to llvm assembly with llvm-dis
862 action->program.set("llvm-dis");
863 action->args.push_back(InFile.toString());
864 action->args.push_back("-f");
865 action->args.push_back("-o");
866 if (Output.isEmpty()) {
867 OutFile.appendSuffix("ll");
868 action->args.push_back(OutFile.toString());
870 action->args.push_back(Output.toString());
874 // Put the action on the list
875 actions.push_back(action);
877 // Short circuit the rest of the loop, we don't want to link
881 // Register the result of the actions as a link candidate
882 LinkageItems.insert(InFile);
884 } // end while loop over each input file
886 /// RUN THE COMPILATION ACTIONS
887 std::vector<Action*>::iterator AI = actions.begin();
888 std::vector<Action*>::iterator AE = actions.end();
890 int ActionResult = DoAction(*AI, ErrMsg);
891 if (ActionResult != 0)
897 if (finalPhase == LINKING) {
899 // Insert the platform-specific system libraries to the path list
900 std::vector<sys::Path> SysLibs;
901 sys::Path::GetSystemLibraryPaths(SysLibs);
902 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
904 // Set up the linking action with llvm-ld
905 Action* link = new Action();
906 link->program.set("llvm-ld");
908 // Add in the optimization level requested
910 case OPT_FAST_COMPILE:
911 link->args.push_back("-O1");
914 link->args.push_back("-O2");
917 link->args.push_back("-O3");
920 link->args.push_back("-O4");
922 case OPT_AGGRESSIVE_LINK_TIME:
923 link->args.push_back("-O5");
929 // Add in all the linkage items we generated. This includes the
930 // output from the translation/optimization phases as well as any
931 // -l arguments specified.
932 for (PathVector::const_iterator I=LinkageItems.begin(),
933 E=LinkageItems.end(); I != E; ++I )
934 link->args.push_back(I->toString());
936 // Add in all the libraries we found.
937 for (StringVector::const_iterator I=LibFiles.begin(),
938 E=LibFiles.end(); I != E; ++I )
939 link->args.push_back(std::string("-l")+*I);
941 // Add in all the library paths to the command line
942 for (PathVector::const_iterator I=LibraryPaths.begin(),
943 E=LibraryPaths.end(); I != E; ++I)
944 link->args.push_back( std::string("-L") + I->toString());
946 // Add in the additional linker arguments requested
947 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
948 E=AdditionalArgs[LINKING].end(); I != E; ++I)
949 link->args.push_back( *I );
951 // Add in other optional flags
952 if (isSet(EMIT_NATIVE_FLAG))
953 link->args.push_back("-native");
954 if (isSet(VERBOSE_FLAG))
955 link->args.push_back("-v");
956 if (isSet(TIME_PASSES_FLAG))
957 link->args.push_back("-time-passes");
958 if (isSet(SHOW_STATS_FLAG))
959 link->args.push_back("-stats");
960 if (isSet(STRIP_OUTPUT_FLAG))
961 link->args.push_back("-s");
962 if (isSet(DEBUG_FLAG)) {
963 link->args.push_back("-debug");
964 link->args.push_back("-debug-pass=Details");
967 // Add in mandatory flags
968 link->args.push_back("-o");
969 link->args.push_back(Output.toString());
972 int ActionResult = DoAction(link, ErrMsg);
973 if (ActionResult != 0)
976 } catch (std::string& msg) {
981 throw std::string("Unspecified error");
991 ConfigDataProvider* cdp; ///< Where we get configuration data from
992 Phases finalPhase; ///< The final phase of compilation
993 OptimizationLevels optLevel; ///< The optimization level to apply
994 unsigned Flags; ///< The driver flags
995 std::string machine; ///< Target machine name
996 PathVector LibraryPaths; ///< -L options
997 PathVector IncludePaths; ///< -I options
998 PathVector ToolPaths; ///< -B options
999 StringVector Defines; ///< -D options
1000 sys::Path TempDir; ///< Name of the temporary directory.
1001 StringTable AdditionalArgs; ///< The -Txyz options
1002 StringVector fOptions; ///< -f options
1003 StringVector MOptions; ///< -M options
1004 StringVector WOptions; ///< -W options
1010 CompilerDriver::~CompilerDriver() {
1013 CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1016 CompilerDriver::Get(ConfigDataProvider& CDP) {
1017 return new CompilerDriverImpl(CDP);
1020 CompilerDriver::ConfigData::ConfigData()
1028 StringVector emptyVec;
1029 for (unsigned i = 0; i < NUM_PHASES; ++i)
1030 opts.push_back(emptyVec);