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 sys::FileStatus Status;
199 if (!TempDir.getFileStatus(Status) && Status.isDir)
200 TempDir.eraseFromDisk(/*remove_contents=*/true);
202 std::cout << "Temporary files are in " << TempDir << "\n";
206 sys::Path MakeTempFile(const std::string& basename,
207 const std::string& suffix,
208 std::string* ErrMsg) {
209 if (TempDir.isEmpty()) {
210 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
211 if (TempDir.isEmpty())
213 sys::RemoveDirectoryOnSignal(TempDir);
215 sys::Path result(TempDir);
216 if (!result.appendComponent(basename)) {
218 *ErrMsg = basename + ": can't use this file name";
221 if (!result.appendSuffix(suffix)) {
223 *ErrMsg = suffix + ": can't use this file suffix";
229 Action* GetAction(ConfigData* cd,
230 const sys::Path& input,
231 const sys::Path& output,
234 Action* pat = 0; ///< The pattern/template for the action
235 Action* action = new Action; ///< The actual action to execute
237 // Get the action pattern
239 case PREPROCESSING: pat = &cd->PreProcessor; break;
240 case TRANSLATION: pat = &cd->Translator; break;
241 case OPTIMIZATION: pat = &cd->Optimizer; break;
242 case ASSEMBLY: pat = &cd->Assembler; break;
243 case LINKING: pat = &cd->Linker; break;
245 assert(!"Invalid driver phase!");
248 assert(pat != 0 && "Invalid command pattern");
250 // Copy over some pattern things that don't need to change
251 action->flags = pat->flags;
253 // See if program starts with wildcard...
254 std::string programName=pat->program.toString();
255 if (programName[0] == '%' && programName.length() >2) {
256 switch(programName[1]){
258 if (programName.substr(0,8) == "%bindir%") {
259 std::string tmp(LLVM_BINDIR);
260 tmp.append(programName.substr(8));
261 pat->program.set(tmp);
265 if (programName.substr(0,12) == "%llvmgccdir%"){
266 std::string tmp(LLVMGCCDIR);
267 tmp.append(programName.substr(12));
268 pat->program.set(tmp);
269 }else if (programName.substr(0,13) == "%llvmgccarch%"){
270 std::string tmp(LLVMGCCARCH);
271 tmp.append(programName.substr(13));
272 pat->program.set(tmp);
273 }else if (programName.substr(0,9) == "%llvmgcc%"){
274 std::string tmp(LLVMGCC);
275 tmp.append(programName.substr(9));
276 pat->program.set(tmp);
277 }else if (programName.substr(0,9) == "%llvmgxx%"){
278 std::string tmp(LLVMGXX);
279 tmp.append(programName.substr(9));
280 pat->program.set(tmp);
281 }else if (programName.substr(0,9) == "%llvmcc1%"){
282 std::string tmp(LLVMCC1);
283 tmp.append(programName.substr(9));
284 pat->program.set(tmp);
285 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
286 std::string tmp(LLVMCC1PLUS);
287 tmp.append(programName.substr(13));
288 pat->program.set(tmp);
289 }else if (programName.substr(0,8) == "%libdir%") {
290 std::string tmp(LLVM_LIBDIR);
291 tmp.append(programName.substr(8));
292 pat->program.set(tmp);
297 action->program = pat->program;
299 // Do the substitutions from the pattern to the actual
300 StringVector::iterator PI = pat->args.begin();
301 StringVector::iterator PE = pat->args.end();
303 if ((*PI)[0] == '%' && PI->length() >2) {
307 if (*PI == "%args%") {
308 if (AdditionalArgs.size() > unsigned(phase))
309 if (!AdditionalArgs[phase].empty()) {
310 // Get specific options for each kind of action type
311 StringVector& addargs = AdditionalArgs[phase];
312 // Add specific options for each kind of action type
313 action->args.insert(action->args.end(), addargs.begin(),
320 if (*PI == "%bindir%") {
321 std::string tmp(*PI);
322 tmp.replace(0,8,LLVM_BINDIR);
323 action->args.push_back(tmp);
328 if (*PI == "%defs%") {
329 StringVector::iterator I = Defines.begin();
330 StringVector::iterator E = Defines.end();
332 action->args.push_back( std::string("-D") + *I);
339 if (*PI == "%fOpts%") {
340 if (!fOptions.empty())
341 action->args.insert(action->args.end(), fOptions.begin(),
348 action->args.push_back(input.toString());
349 } else if (*PI == "%incls%") {
350 PathVector::iterator I = IncludePaths.begin();
351 PathVector::iterator E = IncludePaths.end();
353 action->args.push_back( std::string("-I") + I->toString() );
360 if ((*PI)[1] == 'l') {
361 std::string tmp(*PI);
362 if (*PI == "%llvmgccdir%")
363 tmp.replace(0,12,LLVMGCCDIR);
364 else if (*PI == "%llvmgccarch%")
365 tmp.replace(0,13,LLVMGCCARCH);
366 else if (*PI == "%llvmgcc%")
367 tmp.replace(0,9,LLVMGCC);
368 else if (*PI == "%llvmgxx%")
369 tmp.replace(0,9,LLVMGXX);
370 else if (*PI == "%llvmcc1%")
371 tmp.replace(0,9,LLVMCC1);
372 else if (*PI == "%llvmcc1plus%")
373 tmp.replace(0,9,LLVMCC1);
377 action->args.push_back(tmp);
378 } else if (*PI == "%libs%") {
379 PathVector::iterator I = LibraryPaths.begin();
380 PathVector::iterator E = LibraryPaths.end();
382 action->args.push_back( std::string("-L") + I->toString() );
385 } else if (*PI == "%libdir%") {
386 std::string tmp(*PI);
387 tmp.replace(0,8,LLVM_LIBDIR);
388 action->args.push_back(tmp);
393 if (*PI == "%out%") {
394 action->args.push_back(output.toString());
395 } else if (*PI == "%opt%") {
396 if (!isSet(EMIT_RAW_FLAG)) {
397 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
398 !cd->opts[optLevel].empty())
399 action->args.insert(action->args.end(),
400 cd->opts[optLevel].begin(),
401 cd->opts[optLevel].end());
403 throw std::string("Optimization options for level ") +
404 utostr(unsigned(optLevel)) + " were not specified";
410 if (*PI == "%stats%") {
411 if (isSet(SHOW_STATS_FLAG))
412 action->args.push_back("-stats");
417 if (*PI == "%target%") {
418 action->args.push_back(std::string("-march=") + machine);
419 } else if (*PI == "%time%") {
420 if (isSet(TIME_PASSES_FLAG))
421 action->args.push_back("-time-passes");
426 if (*PI == "%verbose%") {
427 if (isSet(VERBOSE_FLAG))
428 action->args.push_back("-v");
433 if (*PI == "%Mopts%") {
434 if (!MOptions.empty())
435 action->args.insert(action->args.end(), MOptions.begin(),
441 if (*PI == "%Wopts%") {
442 for (StringVector::iterator I = WOptions.begin(),
443 E = WOptions.end(); I != E ; ++I ) {
444 action->args.push_back(std::string("-W") + *I);
454 // Did it even look like a substitution?
455 if (PI->length()>1 && (*PI)[0] == '%' &&
456 (*PI)[PI->length()-1] == '%') {
457 throw std::string("Invalid substitution token: '") + *PI +
458 "' for command '" + pat->program.toString() + "'";
459 } else if (!PI->empty()) {
460 // It's not a legal substitution, just pass it through
461 action->args.push_back(*PI);
464 } else if (!PI->empty()) {
465 // Its not a substitution, just put it in the action
466 action->args.push_back(*PI);
471 // Finally, we're done
475 int DoAction(Action*action, std::string& ErrMsg) {
476 assert(action != 0 && "Invalid Action!");
477 if (isSet(VERBOSE_FLAG))
479 if (!isSet(DRY_RUN_FLAG)) {
480 sys::Path progpath = sys::Program::FindProgramByName(
481 action->program.toString());
482 if (progpath.isEmpty())
483 throw std::string("Can't find program '" +
484 action->program.toString()+"'");
485 else if (progpath.canExecute())
486 action->program = progpath;
488 throw std::string("Program '"+action->program.toString()+
489 "' is not executable.");
491 // Invoke the program
492 const char** Args = (const char**)
493 alloca(sizeof(const char*)*(action->args.size()+2));
494 Args[0] = action->program.toString().c_str();
495 for (unsigned i = 1; i <= action->args.size(); ++i)
496 Args[i] = action->args[i-1].c_str();
497 Args[action->args.size()+1] = 0; // null terminate list.
498 if (isSet(TIME_ACTIONS_FLAG)) {
499 Timer timer(action->program.toString());
502 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,0, &ErrMsg);
504 timer.print(timer,std::cerr);
509 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0,0, &ErrMsg);
514 /// This method tries various variants of a linkage item's file
515 /// name to see if it can find an appropriate file to link with
516 /// in the directories of the LibraryPaths.
517 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
518 bool native = false) {
520 fullpath.set(link_item);
521 if (fullpath.canRead())
523 for (PathVector::iterator PI = LibraryPaths.begin(),
524 PE = LibraryPaths.end(); PI != PE; ++PI) {
525 fullpath.set(PI->toString());
526 fullpath.appendComponent(link_item);
527 if (fullpath.canRead())
530 fullpath.appendSuffix("a");
532 fullpath.appendSuffix("bc");
533 if (fullpath.canRead())
535 fullpath.eraseSuffix();
536 fullpath.appendSuffix("o");
537 if (fullpath.canRead())
540 fullpath.appendComponent(std::string("lib") + link_item);
541 fullpath.appendSuffix("a");
542 if (fullpath.canRead())
544 fullpath.eraseSuffix();
545 fullpath.appendSuffix("so");
546 if (fullpath.canRead())
556 /// This method processes a linkage item. The item could be a
557 /// Bytecode file needing translation to native code and that is
558 /// dependent on other bytecode libraries, or a native code
559 /// library that should just be linked into the program.
560 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
561 SetVector<sys::Path>& set,
563 // First, see if the unadorned file name is not readable. If so,
564 // we must track down the file in the lib search path.
566 if (!link_item.canRead()) {
567 // look for the library using the -L arguments specified
568 // on the command line.
569 fullpath = GetPathForLinkageItem(link_item.toString());
571 // If we didn't find the file in any of the library search paths
572 // we have to bail. No where else to look.
573 if (fullpath.isEmpty()) {
575 std::string("Can't find linkage item '") + link_item.toString() + "'";
579 fullpath = link_item;
582 // If we got here fullpath is the path to the file, and its readable.
583 set.insert(fullpath);
585 // If its an LLVM bytecode file ...
586 if (fullpath.isBytecodeFile()) {
587 // Process the dependent libraries recursively
588 Module::LibraryListType modlibs;
589 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,
590 Compressor::decompressToNewBuffer,
592 // Traverse the dependent libraries list
593 Module::lib_iterator LI = modlibs.begin();
594 Module::lib_iterator LE = modlibs.end();
596 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
598 err = std::string("Library '") + *LI +
599 "' is not valid for linking but is required by file '" +
600 fullpath.toString() + "'";
602 err += " which is required by file '" + fullpath.toString() + "'";
608 } else if (err.empty()) {
610 "The dependent libraries could not be extracted from '") +
623 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
625 // Echo the configuration of options if we're running verbose
626 if (isSet(DEBUG_FLAG)) {
627 std::cerr << "Compiler Driver Options:\n";
628 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
629 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
630 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
631 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
632 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
633 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
634 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
635 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
636 std::cerr << "OutputMachine = " << machine << "\n";
637 InputList::const_iterator I = InpList.begin();
638 while ( I != InpList.end() ) {
639 std::cerr << "Input: " << I->first << "(" << I->second
643 std::cerr << "Output: " << Output << "\n";
646 // If there's no input, we're done.
648 throw std::string("Nothing to compile.");
650 // If they are asking for linking and didn't provide an output
651 // file then its an error (no way for us to "make up" a meaningful
652 // file name based on the various linker input files).
653 if (finalPhase == LINKING && Output.isEmpty())
655 "An output file name must be specified for linker output");
657 // If they are not asking for linking, provided an output file and
658 // there is more than one input file, its an error
659 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
660 throw std::string("An output file name cannot be specified ") +
661 "with more than one input file name when not linking";
663 // This vector holds all the resulting actions of the following loop.
664 std::vector<Action*> actions;
666 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
667 // for each input item
668 SetVector<sys::Path> LinkageItems;
669 StringVector LibFiles;
670 InputList::const_iterator I = InpList.begin();
671 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
673 // Get the suffix of the file name
674 const std::string& ftype = I->second;
676 // If its a library, bytecode file, or object file, save
677 // it for linking below and short circuit the
678 // pre-processing/translation/assembly phases
679 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
680 // We shouldn't get any of these types of files unless we're
681 // later going to link. Enforce this limit now.
682 if (finalPhase != LINKING) {
684 "Pre-compiled objects found but linking not requested");
687 LibFiles.push_back(I->first.toString());
689 LinkageItems.insert(I->first);
690 continue; // short circuit remainder of loop
693 // At this point, we know its something we need to translate
694 // and/or optimize. See if we can get the configuration data
695 // for this kind of file.
696 ConfigData* cd = cdp->ProvideConfigData(I->second);
698 throw std::string("Files of type '") + I->second +
699 "' are not recognized.";
700 if (isSet(DEBUG_FLAG))
701 DumpConfigData(cd,I->second);
703 // Add the config data's library paths to the end of the list
704 for (StringVector::iterator LPI = cd->libpaths.begin(),
705 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
706 LibraryPaths.push_back(sys::Path(*LPI));
709 // Initialize the input and output files
710 sys::Path InFile(I->first);
711 sys::Path OutFile(I->first.getBasename());
713 // PRE-PROCESSING PHASE
714 Action& action = cd->PreProcessor;
716 // Get the preprocessing action, if needed, or error if appropriate
717 if (!action.program.isEmpty()) {
718 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
719 if (finalPhase == PREPROCESSING) {
720 if (Output.isEmpty()) {
721 OutFile.appendSuffix("E");
722 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
724 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
728 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
729 if (TempFile.isEmpty())
731 actions.push_back(GetAction(cd,InFile,TempFile,
736 } else if (finalPhase == PREPROCESSING) {
737 throw cd->langName + " does not support pre-processing";
738 } else if (action.isSet(REQUIRED_FLAG)) {
739 throw std::string("Don't know how to pre-process ") +
740 cd->langName + " files";
743 // Short-circuit remaining actions if all they want is
745 if (finalPhase == PREPROCESSING) { continue; };
747 /// TRANSLATION PHASE
748 action = cd->Translator;
750 // Get the translation action, if needed, or error if appropriate
751 if (!action.program.isEmpty()) {
752 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
753 if (finalPhase == TRANSLATION) {
754 if (Output.isEmpty()) {
755 OutFile.appendSuffix("o");
756 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
758 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
762 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
763 if (TempFile.isEmpty())
765 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
770 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
771 /// The output of the translator is an LLVM Assembly program
772 /// We need to translate it to bytecode
773 Action* action = new Action();
774 action->program.set("llvm-as");
775 action->args.push_back(InFile.toString());
776 action->args.push_back("-o");
777 InFile.appendSuffix("bc");
778 action->args.push_back(InFile.toString());
779 actions.push_back(action);
782 } else if (finalPhase == TRANSLATION) {
783 throw cd->langName + " does not support translation";
784 } else if (action.isSet(REQUIRED_FLAG)) {
785 throw std::string("Don't know how to translate ") +
786 cd->langName + " files";
789 // Short-circuit remaining actions if all they want is translation
790 if (finalPhase == TRANSLATION) { continue; }
792 /// OPTIMIZATION PHASE
793 action = cd->Optimizer;
795 // Get the optimization action, if needed, or error if appropriate
796 if (!isSet(EMIT_RAW_FLAG)) {
797 if (!action.program.isEmpty()) {
798 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
799 if (finalPhase == OPTIMIZATION) {
800 if (Output.isEmpty()) {
801 OutFile.appendSuffix("o");
802 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
804 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
808 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
809 if (TempFile.isEmpty())
811 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
815 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
816 /// The output of the optimizer is an LLVM Assembly program
817 /// We need to translate it to bytecode with llvm-as
818 Action* action = new Action();
819 action->program.set("llvm-as");
820 action->args.push_back(InFile.toString());
821 action->args.push_back("-f");
822 action->args.push_back("-o");
823 InFile.appendSuffix("bc");
824 action->args.push_back(InFile.toString());
825 actions.push_back(action);
828 } else if (finalPhase == OPTIMIZATION) {
829 throw cd->langName + " does not support optimization";
830 } else if (action.isSet(REQUIRED_FLAG)) {
831 throw std::string("Don't know how to optimize ") +
832 cd->langName + " files";
836 // Short-circuit remaining actions if all they want is optimization
837 if (finalPhase == OPTIMIZATION) { continue; }
840 action = cd->Assembler;
842 if (finalPhase == ASSEMBLY) {
844 // Build either a native compilation action or a disassembly action
845 Action* action = new Action();
846 if (isSet(EMIT_NATIVE_FLAG)) {
847 // Use llc to get the native assembly file
848 action->program.set("llc");
849 action->args.push_back(InFile.toString());
850 action->args.push_back("-f");
851 action->args.push_back("-o");
852 if (Output.isEmpty()) {
853 OutFile.appendSuffix("o");
854 action->args.push_back(OutFile.toString());
856 action->args.push_back(Output.toString());
858 actions.push_back(action);
860 // Just convert back to llvm assembly with llvm-dis
861 action->program.set("llvm-dis");
862 action->args.push_back(InFile.toString());
863 action->args.push_back("-f");
864 action->args.push_back("-o");
865 if (Output.isEmpty()) {
866 OutFile.appendSuffix("ll");
867 action->args.push_back(OutFile.toString());
869 action->args.push_back(Output.toString());
873 // Put the action on the list
874 actions.push_back(action);
876 // Short circuit the rest of the loop, we don't want to link
880 // Register the result of the actions as a link candidate
881 LinkageItems.insert(InFile);
883 } // end while loop over each input file
885 /// RUN THE COMPILATION ACTIONS
886 std::vector<Action*>::iterator AI = actions.begin();
887 std::vector<Action*>::iterator AE = actions.end();
889 int ActionResult = DoAction(*AI, ErrMsg);
890 if (ActionResult != 0)
896 if (finalPhase == LINKING) {
898 // Insert the platform-specific system libraries to the path list
899 std::vector<sys::Path> SysLibs;
900 sys::Path::GetSystemLibraryPaths(SysLibs);
901 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
903 // Set up the linking action with llvm-ld
904 Action* link = new Action();
905 link->program.set("llvm-ld");
907 // Add in the optimization level requested
909 case OPT_FAST_COMPILE:
910 link->args.push_back("-O1");
913 link->args.push_back("-O2");
916 link->args.push_back("-O3");
919 link->args.push_back("-O4");
921 case OPT_AGGRESSIVE_LINK_TIME:
922 link->args.push_back("-O5");
928 // Add in all the linkage items we generated. This includes the
929 // output from the translation/optimization phases as well as any
930 // -l arguments specified.
931 for (PathVector::const_iterator I=LinkageItems.begin(),
932 E=LinkageItems.end(); I != E; ++I )
933 link->args.push_back(I->toString());
935 // Add in all the libraries we found.
936 for (StringVector::const_iterator I=LibFiles.begin(),
937 E=LibFiles.end(); I != E; ++I )
938 link->args.push_back(std::string("-l")+*I);
940 // Add in all the library paths to the command line
941 for (PathVector::const_iterator I=LibraryPaths.begin(),
942 E=LibraryPaths.end(); I != E; ++I)
943 link->args.push_back( std::string("-L") + I->toString());
945 // Add in the additional linker arguments requested
946 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
947 E=AdditionalArgs[LINKING].end(); I != E; ++I)
948 link->args.push_back( *I );
950 // Add in other optional flags
951 if (isSet(EMIT_NATIVE_FLAG))
952 link->args.push_back("-native");
953 if (isSet(VERBOSE_FLAG))
954 link->args.push_back("-v");
955 if (isSet(TIME_PASSES_FLAG))
956 link->args.push_back("-time-passes");
957 if (isSet(SHOW_STATS_FLAG))
958 link->args.push_back("-stats");
959 if (isSet(STRIP_OUTPUT_FLAG))
960 link->args.push_back("-s");
961 if (isSet(DEBUG_FLAG)) {
962 link->args.push_back("-debug");
963 link->args.push_back("-debug-pass=Details");
966 // Add in mandatory flags
967 link->args.push_back("-o");
968 link->args.push_back(Output.toString());
971 int ActionResult = DoAction(link, ErrMsg);
972 if (ActionResult != 0)
975 } catch (std::string& msg) {
980 throw std::string("Unspecified error");
990 ConfigDataProvider* cdp; ///< Where we get configuration data from
991 Phases finalPhase; ///< The final phase of compilation
992 OptimizationLevels optLevel; ///< The optimization level to apply
993 unsigned Flags; ///< The driver flags
994 std::string machine; ///< Target machine name
995 PathVector LibraryPaths; ///< -L options
996 PathVector IncludePaths; ///< -I options
997 PathVector ToolPaths; ///< -B options
998 StringVector Defines; ///< -D options
999 sys::Path TempDir; ///< Name of the temporary directory.
1000 StringTable AdditionalArgs; ///< The -Txyz options
1001 StringVector fOptions; ///< -f options
1002 StringVector MOptions; ///< -M options
1003 StringVector WOptions; ///< -W options
1009 CompilerDriver::~CompilerDriver() {
1012 CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1015 CompilerDriver::Get(ConfigDataProvider& CDP) {
1016 return new CompilerDriverImpl(CDP);
1019 CompilerDriver::ConfigData::ConfigData()
1027 StringVector emptyVec;
1028 for (unsigned i = 0; i < NUM_PHASES; ++i)
1029 opts.push_back(emptyVec);