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 AdditionalArgs.reserve(NUM_PHASES);
87 StringVector emptyVec;
88 for (unsigned i = 0; i < NUM_PHASES; ++i)
89 AdditionalArgs.push_back(emptyVec);
92 virtual ~CompilerDriverImpl() {
99 AdditionalArgs.clear();
109 virtual void setFinalPhase(Phases phase) {
113 virtual void setOptimization(OptimizationLevels level) {
117 virtual void setDriverFlags(unsigned flags) {
118 Flags = flags & DRIVER_FLAGS_MASK;
121 virtual void setOutputMachine(const std::string& machineName) {
122 machine = machineName;
125 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
126 assert(phase <= LINKING && phase >= PREPROCESSING);
127 AdditionalArgs[phase] = opts;
130 virtual void setIncludePaths(const StringVector& paths) {
131 StringVector::const_iterator I = paths.begin();
132 StringVector::const_iterator E = paths.end();
136 IncludePaths.push_back(tmp);
141 virtual void setSymbolDefines(const StringVector& defs) {
145 virtual void setLibraryPaths(const StringVector& paths) {
146 StringVector::const_iterator I = paths.begin();
147 StringVector::const_iterator E = paths.end();
151 LibraryPaths.push_back(tmp);
156 virtual void addLibraryPath(const sys::Path& libPath) {
157 LibraryPaths.push_back(libPath);
160 virtual void addToolPath(const sys::Path& toolPath) {
161 ToolPaths.push_back(toolPath);
164 virtual void setfPassThrough(const StringVector& fOpts) {
168 /// @brief Set the list of -M options to be passed through
169 virtual void setMPassThrough(const StringVector& MOpts) {
173 /// @brief Set the list of -W options to be passed through
174 virtual void setWPassThrough(const StringVector& WOpts) {
182 bool isSet(DriverFlags flag) {
183 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
187 if (!isSet(KEEP_TEMPS_FLAG)) {
188 sys::FileStatus Status;
189 if (!TempDir.getFileStatus(Status) && Status.isDir)
190 TempDir.eraseFromDisk(/*remove_contents=*/true);
192 std::cout << "Temporary files are in " << TempDir << "\n";
196 sys::Path MakeTempFile(const std::string& basename,
197 const std::string& suffix,
198 std::string* ErrMsg) {
199 if (TempDir.isEmpty()) {
200 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
201 if (TempDir.isEmpty())
203 sys::RemoveDirectoryOnSignal(TempDir);
205 sys::Path result(TempDir);
206 if (!result.appendComponent(basename)) {
208 *ErrMsg = basename + ": can't use this file name";
211 if (!result.appendSuffix(suffix)) {
213 *ErrMsg = suffix + ": can't use this file suffix";
219 Action* GetAction(ConfigData* cd,
220 const sys::Path& input,
221 const sys::Path& output,
224 Action* pat = 0; ///< The pattern/template for the action
225 Action* action = new Action; ///< The actual action to execute
227 // Get the action pattern
229 case PREPROCESSING: pat = &cd->PreProcessor; break;
230 case TRANSLATION: pat = &cd->Translator; break;
231 case OPTIMIZATION: pat = &cd->Optimizer; break;
232 case ASSEMBLY: pat = &cd->Assembler; break;
233 case LINKING: pat = &cd->Linker; break;
235 assert(!"Invalid driver phase!");
238 assert(pat != 0 && "Invalid command pattern");
240 // Copy over some pattern things that don't need to change
241 action->flags = pat->flags;
243 // See if program starts with wildcard...
244 std::string programName=pat->program.toString();
245 if (programName[0] == '%' && programName.length() >2) {
246 switch(programName[1]){
248 if (programName.substr(0,8) == "%bindir%") {
249 std::string tmp(LLVM_BINDIR);
250 tmp.append(programName.substr(8));
251 pat->program.set(tmp);
255 if (programName.substr(0,12) == "%llvmgccdir%"){
256 std::string tmp(LLVMGCCDIR);
257 tmp.append(programName.substr(12));
258 pat->program.set(tmp);
259 }else if (programName.substr(0,13) == "%llvmgccarch%"){
260 std::string tmp(LLVMGCCARCH);
261 tmp.append(programName.substr(13));
262 pat->program.set(tmp);
263 }else if (programName.substr(0,9) == "%llvmgcc%"){
264 std::string tmp(LLVMGCC);
265 tmp.append(programName.substr(9));
266 pat->program.set(tmp);
267 }else if (programName.substr(0,9) == "%llvmgxx%"){
268 std::string tmp(LLVMGXX);
269 tmp.append(programName.substr(9));
270 pat->program.set(tmp);
271 }else if (programName.substr(0,9) == "%llvmcc1%"){
272 std::string tmp(LLVMCC1);
273 tmp.append(programName.substr(9));
274 pat->program.set(tmp);
275 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
276 std::string tmp(LLVMCC1PLUS);
277 tmp.append(programName.substr(13));
278 pat->program.set(tmp);
279 }else if (programName.substr(0,8) == "%libdir%") {
280 std::string tmp(LLVM_LIBDIR);
281 tmp.append(programName.substr(8));
282 pat->program.set(tmp);
287 action->program = pat->program;
289 // Do the substitutions from the pattern to the actual
290 StringVector::iterator PI = pat->args.begin();
291 StringVector::iterator PE = pat->args.end();
293 if ((*PI)[0] == '%' && PI->length() >2) {
297 if (*PI == "%args%") {
298 if (AdditionalArgs.size() > unsigned(phase))
299 if (!AdditionalArgs[phase].empty()) {
300 // Get specific options for each kind of action type
301 StringVector& addargs = AdditionalArgs[phase];
302 // Add specific options for each kind of action type
303 action->args.insert(action->args.end(), addargs.begin(),
310 if (*PI == "%bindir%") {
311 std::string tmp(*PI);
312 tmp.replace(0,8,LLVM_BINDIR);
313 action->args.push_back(tmp);
318 if (*PI == "%defs%") {
319 StringVector::iterator I = Defines.begin();
320 StringVector::iterator E = Defines.end();
322 action->args.push_back( std::string("-D") + *I);
329 if (*PI == "%fOpts%") {
330 if (!fOptions.empty())
331 action->args.insert(action->args.end(), fOptions.begin(),
338 action->args.push_back(input.toString());
339 } else if (*PI == "%incls%") {
340 PathVector::iterator I = IncludePaths.begin();
341 PathVector::iterator E = IncludePaths.end();
343 action->args.push_back( std::string("-I") + I->toString() );
350 if ((*PI)[1] == 'l') {
351 std::string tmp(*PI);
352 if (*PI == "%llvmgccdir%")
353 tmp.replace(0,12,LLVMGCCDIR);
354 else if (*PI == "%llvmgccarch%")
355 tmp.replace(0,13,LLVMGCCARCH);
356 else if (*PI == "%llvmgcc%")
357 tmp.replace(0,9,LLVMGCC);
358 else if (*PI == "%llvmgxx%")
359 tmp.replace(0,9,LLVMGXX);
360 else if (*PI == "%llvmcc1%")
361 tmp.replace(0,9,LLVMCC1);
362 else if (*PI == "%llvmcc1plus%")
363 tmp.replace(0,9,LLVMCC1);
367 action->args.push_back(tmp);
368 } else if (*PI == "%libs%") {
369 PathVector::iterator I = LibraryPaths.begin();
370 PathVector::iterator E = LibraryPaths.end();
372 action->args.push_back( std::string("-L") + I->toString() );
375 } else if (*PI == "%libdir%") {
376 std::string tmp(*PI);
377 tmp.replace(0,8,LLVM_LIBDIR);
378 action->args.push_back(tmp);
383 if (*PI == "%out%") {
384 action->args.push_back(output.toString());
385 } else if (*PI == "%opt%") {
386 if (!isSet(EMIT_RAW_FLAG)) {
387 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
388 !cd->opts[optLevel].empty())
389 action->args.insert(action->args.end(),
390 cd->opts[optLevel].begin(),
391 cd->opts[optLevel].end());
393 throw std::string("Optimization options for level ") +
394 utostr(unsigned(optLevel)) + " were not specified";
400 if (*PI == "%stats%") {
401 if (isSet(SHOW_STATS_FLAG))
402 action->args.push_back("-stats");
407 if (*PI == "%target%") {
408 action->args.push_back(std::string("-march=") + machine);
409 } else if (*PI == "%time%") {
410 if (isSet(TIME_PASSES_FLAG))
411 action->args.push_back("-time-passes");
416 if (*PI == "%verbose%") {
417 if (isSet(VERBOSE_FLAG))
418 action->args.push_back("-v");
423 if (*PI == "%Mopts%") {
424 if (!MOptions.empty())
425 action->args.insert(action->args.end(), MOptions.begin(),
431 if (*PI == "%Wopts%") {
432 for (StringVector::iterator I = WOptions.begin(),
433 E = WOptions.end(); I != E ; ++I ) {
434 action->args.push_back(std::string("-W") + *I);
444 // Did it even look like a substitution?
445 if (PI->length()>1 && (*PI)[0] == '%' &&
446 (*PI)[PI->length()-1] == '%') {
447 throw std::string("Invalid substitution token: '") + *PI +
448 "' for command '" + pat->program.toString() + "'";
449 } else if (!PI->empty()) {
450 // It's not a legal substitution, just pass it through
451 action->args.push_back(*PI);
454 } else if (!PI->empty()) {
455 // Its not a substitution, just put it in the action
456 action->args.push_back(*PI);
461 // Finally, we're done
465 int DoAction(Action*action, std::string& ErrMsg) {
466 assert(action != 0 && "Invalid Action!");
467 if (isSet(VERBOSE_FLAG))
469 if (!isSet(DRY_RUN_FLAG)) {
470 sys::Path progpath = sys::Program::FindProgramByName(
471 action->program.toString());
472 if (progpath.isEmpty())
473 throw std::string("Can't find program '" +
474 action->program.toString()+"'");
475 else if (progpath.canExecute())
476 action->program = progpath;
478 throw std::string("Program '"+action->program.toString()+
479 "' is not executable.");
481 // Invoke the program
482 const char** Args = (const char**)
483 alloca(sizeof(const char*)*(action->args.size()+2));
484 Args[0] = action->program.toString().c_str();
485 for (unsigned i = 1; i <= action->args.size(); ++i)
486 Args[i] = action->args[i-1].c_str();
487 Args[action->args.size()+1] = 0; // null terminate list.
488 if (isSet(TIME_ACTIONS_FLAG)) {
489 Timer timer(action->program.toString());
492 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg);
494 timer.print(timer,std::cerr);
499 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg);
504 /// This method tries various variants of a linkage item's file
505 /// name to see if it can find an appropriate file to link with
506 /// in the directories of the LibraryPaths.
507 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
508 bool native = false) {
510 fullpath.set(link_item);
511 if (fullpath.canRead())
513 for (PathVector::iterator PI = LibraryPaths.begin(),
514 PE = LibraryPaths.end(); PI != PE; ++PI) {
515 fullpath.set(PI->toString());
516 fullpath.appendComponent(link_item);
517 if (fullpath.canRead())
520 fullpath.appendSuffix("a");
522 fullpath.appendSuffix("bc");
523 if (fullpath.canRead())
525 fullpath.eraseSuffix();
526 fullpath.appendSuffix("o");
527 if (fullpath.canRead())
530 fullpath.appendComponent(std::string("lib") + link_item);
531 fullpath.appendSuffix("a");
532 if (fullpath.canRead())
534 fullpath.eraseSuffix();
535 fullpath.appendSuffix("so");
536 if (fullpath.canRead())
546 /// This method processes a linkage item. The item could be a
547 /// Bytecode file needing translation to native code and that is
548 /// dependent on other bytecode libraries, or a native code
549 /// library that should just be linked into the program.
550 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
551 SetVector<sys::Path>& set,
553 // First, see if the unadorned file name is not readable. If so,
554 // we must track down the file in the lib search path.
556 if (!link_item.canRead()) {
557 // look for the library using the -L arguments specified
558 // on the command line.
559 fullpath = GetPathForLinkageItem(link_item.toString());
561 // If we didn't find the file in any of the library search paths
562 // we have to bail. No where else to look.
563 if (fullpath.isEmpty()) {
565 std::string("Can't find linkage item '") + link_item.toString() + "'";
569 fullpath = link_item;
572 // If we got here fullpath is the path to the file, and its readable.
573 set.insert(fullpath);
575 // If its an LLVM bytecode file ...
576 if (fullpath.isBytecodeFile()) {
577 // Process the dependent libraries recursively
578 Module::LibraryListType modlibs;
579 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,&err)) {
580 // Traverse the dependent libraries list
581 Module::lib_iterator LI = modlibs.begin();
582 Module::lib_iterator LE = modlibs.end();
584 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
586 err = std::string("Library '") + *LI +
587 "' is not valid for linking but is required by file '" +
588 fullpath.toString() + "'";
590 err += " which is required by file '" + fullpath.toString() + "'";
596 } else if (err.empty()) {
598 "The dependent libraries could not be extracted from '") +
611 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
613 // Echo the configuration of options if we're running verbose
614 if (isSet(DEBUG_FLAG)) {
615 std::cerr << "Compiler Driver Options:\n";
616 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
617 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
618 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
619 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
620 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
621 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
622 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
623 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
624 std::cerr << "OutputMachine = " << machine << "\n";
625 InputList::const_iterator I = InpList.begin();
626 while ( I != InpList.end() ) {
627 std::cerr << "Input: " << I->first << "(" << I->second
631 std::cerr << "Output: " << Output << "\n";
634 // If there's no input, we're done.
636 throw std::string("Nothing to compile.");
638 // If they are asking for linking and didn't provide an output
639 // file then its an error (no way for us to "make up" a meaningful
640 // file name based on the various linker input files).
641 if (finalPhase == LINKING && Output.isEmpty())
643 "An output file name must be specified for linker output");
645 // If they are not asking for linking, provided an output file and
646 // there is more than one input file, its an error
647 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
648 throw std::string("An output file name cannot be specified ") +
649 "with more than one input file name when not linking";
651 // This vector holds all the resulting actions of the following loop.
652 std::vector<Action*> actions;
654 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
655 // for each input item
656 SetVector<sys::Path> LinkageItems;
657 StringVector LibFiles;
658 InputList::const_iterator I = InpList.begin();
659 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
661 // Get the suffix of the file name
662 const std::string& ftype = I->second;
664 // If its a library, bytecode file, or object file, save
665 // it for linking below and short circuit the
666 // pre-processing/translation/assembly phases
667 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
668 // We shouldn't get any of these types of files unless we're
669 // later going to link. Enforce this limit now.
670 if (finalPhase != LINKING) {
672 "Pre-compiled objects found but linking not requested");
675 LibFiles.push_back(I->first.toString());
677 LinkageItems.insert(I->first);
678 continue; // short circuit remainder of loop
681 // At this point, we know its something we need to translate
682 // and/or optimize. See if we can get the configuration data
683 // for this kind of file.
684 ConfigData* cd = cdp->ProvideConfigData(I->second);
686 throw std::string("Files of type '") + I->second +
687 "' are not recognized.";
688 if (isSet(DEBUG_FLAG))
689 DumpConfigData(cd,I->second);
691 // Add the config data's library paths to the end of the list
692 for (StringVector::iterator LPI = cd->libpaths.begin(),
693 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
694 LibraryPaths.push_back(sys::Path(*LPI));
697 // Initialize the input and output files
698 sys::Path InFile(I->first);
699 sys::Path OutFile(I->first.getBasename());
701 // PRE-PROCESSING PHASE
702 Action& action = cd->PreProcessor;
704 // Get the preprocessing action, if needed, or error if appropriate
705 if (!action.program.isEmpty()) {
706 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
707 if (finalPhase == PREPROCESSING) {
708 if (Output.isEmpty()) {
709 OutFile.appendSuffix("E");
710 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
712 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
716 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
717 if (TempFile.isEmpty())
719 actions.push_back(GetAction(cd,InFile,TempFile,
724 } else if (finalPhase == PREPROCESSING) {
725 throw cd->langName + " does not support pre-processing";
726 } else if (action.isSet(REQUIRED_FLAG)) {
727 throw std::string("Don't know how to pre-process ") +
728 cd->langName + " files";
731 // Short-circuit remaining actions if all they want is
733 if (finalPhase == PREPROCESSING) { continue; };
735 /// TRANSLATION PHASE
736 action = cd->Translator;
738 // Get the translation action, if needed, or error if appropriate
739 if (!action.program.isEmpty()) {
740 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
741 if (finalPhase == TRANSLATION) {
742 if (Output.isEmpty()) {
743 OutFile.appendSuffix("o");
744 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
746 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
750 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
751 if (TempFile.isEmpty())
753 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
758 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
759 /// The output of the translator is an LLVM Assembly program
760 /// We need to translate it to bytecode
761 Action* action = new Action();
762 action->program.set("llvm-as");
763 action->args.push_back(InFile.toString());
764 action->args.push_back("-o");
765 InFile.appendSuffix("bc");
766 action->args.push_back(InFile.toString());
767 actions.push_back(action);
770 } else if (finalPhase == TRANSLATION) {
771 throw cd->langName + " does not support translation";
772 } else if (action.isSet(REQUIRED_FLAG)) {
773 throw std::string("Don't know how to translate ") +
774 cd->langName + " files";
777 // Short-circuit remaining actions if all they want is translation
778 if (finalPhase == TRANSLATION) { continue; }
780 /// OPTIMIZATION PHASE
781 action = cd->Optimizer;
783 // Get the optimization action, if needed, or error if appropriate
784 if (!isSet(EMIT_RAW_FLAG)) {
785 if (!action.program.isEmpty()) {
786 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
787 if (finalPhase == OPTIMIZATION) {
788 if (Output.isEmpty()) {
789 OutFile.appendSuffix("o");
790 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
792 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
796 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
797 if (TempFile.isEmpty())
799 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
803 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
804 /// The output of the optimizer is an LLVM Assembly program
805 /// We need to translate it to bytecode with llvm-as
806 Action* action = new Action();
807 action->program.set("llvm-as");
808 action->args.push_back(InFile.toString());
809 action->args.push_back("-f");
810 action->args.push_back("-o");
811 InFile.appendSuffix("bc");
812 action->args.push_back(InFile.toString());
813 actions.push_back(action);
816 } else if (finalPhase == OPTIMIZATION) {
817 throw cd->langName + " does not support optimization";
818 } else if (action.isSet(REQUIRED_FLAG)) {
819 throw std::string("Don't know how to optimize ") +
820 cd->langName + " files";
824 // Short-circuit remaining actions if all they want is optimization
825 if (finalPhase == OPTIMIZATION) { continue; }
828 action = cd->Assembler;
830 if (finalPhase == ASSEMBLY) {
832 // Build either a native compilation action or a disassembly action
833 Action* action = new Action();
834 if (isSet(EMIT_NATIVE_FLAG)) {
835 // Use llc to get the native assembly file
836 action->program.set("llc");
837 action->args.push_back(InFile.toString());
838 action->args.push_back("-f");
839 action->args.push_back("-o");
840 if (Output.isEmpty()) {
841 OutFile.appendSuffix("o");
842 action->args.push_back(OutFile.toString());
844 action->args.push_back(Output.toString());
846 actions.push_back(action);
848 // Just convert back to llvm assembly with llvm-dis
849 action->program.set("llvm-dis");
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("ll");
855 action->args.push_back(OutFile.toString());
857 action->args.push_back(Output.toString());
861 // Put the action on the list
862 actions.push_back(action);
864 // Short circuit the rest of the loop, we don't want to link
868 // Register the result of the actions as a link candidate
869 LinkageItems.insert(InFile);
871 } // end while loop over each input file
873 /// RUN THE COMPILATION ACTIONS
874 std::vector<Action*>::iterator AI = actions.begin();
875 std::vector<Action*>::iterator AE = actions.end();
877 int ActionResult = DoAction(*AI, ErrMsg);
878 if (ActionResult != 0)
884 if (finalPhase == LINKING) {
886 // Insert the platform-specific system libraries to the path list
887 std::vector<sys::Path> SysLibs;
888 sys::Path::GetSystemLibraryPaths(SysLibs);
889 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
891 // Set up the linking action with llvm-ld
892 Action* link = new Action();
893 link->program.set("llvm-ld");
895 // Add in the optimization level requested
897 case OPT_FAST_COMPILE:
898 link->args.push_back("-O1");
901 link->args.push_back("-O2");
904 link->args.push_back("-O3");
907 link->args.push_back("-O4");
909 case OPT_AGGRESSIVE_LINK_TIME:
910 link->args.push_back("-O5");
916 // Add in all the linkage items we generated. This includes the
917 // output from the translation/optimization phases as well as any
918 // -l arguments specified.
919 for (PathVector::const_iterator I=LinkageItems.begin(),
920 E=LinkageItems.end(); I != E; ++I )
921 link->args.push_back(I->toString());
923 // Add in all the libraries we found.
924 for (StringVector::const_iterator I=LibFiles.begin(),
925 E=LibFiles.end(); I != E; ++I )
926 link->args.push_back(std::string("-l")+*I);
928 // Add in all the library paths to the command line
929 for (PathVector::const_iterator I=LibraryPaths.begin(),
930 E=LibraryPaths.end(); I != E; ++I)
931 link->args.push_back( std::string("-L") + I->toString());
933 // Add in the additional linker arguments requested
934 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
935 E=AdditionalArgs[LINKING].end(); I != E; ++I)
936 link->args.push_back( *I );
938 // Add in other optional flags
939 if (isSet(EMIT_NATIVE_FLAG))
940 link->args.push_back("-native");
941 if (isSet(VERBOSE_FLAG))
942 link->args.push_back("-v");
943 if (isSet(TIME_PASSES_FLAG))
944 link->args.push_back("-time-passes");
945 if (isSet(SHOW_STATS_FLAG))
946 link->args.push_back("-stats");
947 if (isSet(STRIP_OUTPUT_FLAG))
948 link->args.push_back("-s");
949 if (isSet(DEBUG_FLAG)) {
950 link->args.push_back("-debug");
951 link->args.push_back("-debug-pass=Details");
954 // Add in mandatory flags
955 link->args.push_back("-o");
956 link->args.push_back(Output.toString());
959 int ActionResult = DoAction(link, ErrMsg);
960 if (ActionResult != 0)
963 } catch (std::string& msg) {
968 throw std::string("Unspecified error");
978 ConfigDataProvider* cdp; ///< Where we get configuration data from
979 Phases finalPhase; ///< The final phase of compilation
980 OptimizationLevels optLevel; ///< The optimization level to apply
981 unsigned Flags; ///< The driver flags
982 std::string machine; ///< Target machine name
983 PathVector LibraryPaths; ///< -L options
984 PathVector IncludePaths; ///< -I options
985 PathVector ToolPaths; ///< -B options
986 StringVector Defines; ///< -D options
987 sys::Path TempDir; ///< Name of the temporary directory.
988 StringTable AdditionalArgs; ///< The -Txyz options
989 StringVector fOptions; ///< -f options
990 StringVector MOptions; ///< -M options
991 StringVector WOptions; ///< -W options
997 CompilerDriver::~CompilerDriver() {
1000 CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1003 CompilerDriver::Get(ConfigDataProvider& CDP) {
1004 return new CompilerDriverImpl(CDP);
1007 CompilerDriver::ConfigData::ConfigData()
1015 StringVector emptyVec;
1016 for (unsigned i = 0; i < NUM_PHASES; ++i)
1017 opts.push_back(emptyVec);