Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 1 | //===-- llvm-lto: a simple command-line program to link modules with LTO --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This program takes in a list of bitcode files, links them, performs link-time |
| 11 | // optimization, and outputs an object file. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 15 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 16 | #include "llvm/LTO/LTOModule.h" |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/Support/ManagedStatic.h" |
| 19 | #include "llvm/Support/PrettyStackTrace.h" |
| 20 | #include "llvm/Support/Signals.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 22 | #include "llvm/Support/TargetSelect.h" |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore, |
| 27 | cl::desc("<input bitcode files>")); |
| 28 | |
| 29 | static cl::opt<std::string> OutputFilename("o", |
| 30 | cl::desc("Override output filename"), |
| 31 | cl::init(""), |
| 32 | cl::value_desc("filename")); |
| 33 | |
| 34 | int main(int argc, char **argv) { |
| 35 | // Print a stack trace if we signal out. |
| 36 | sys::PrintStackTraceOnErrorSignal(); |
| 37 | PrettyStackTraceProgram X(argc, argv); |
| 38 | |
| 39 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 40 | cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n"); |
| 41 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 42 | // Initialize the configured targets. |
| 43 | InitializeAllTargets(); |
| 44 | InitializeAllTargetMCs(); |
| 45 | InitializeAllAsmPrinters(); |
| 46 | InitializeAllAsmParsers(); |
| 47 | |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 48 | unsigned BaseArg = 0; |
| 49 | std::string ErrorMessage; |
| 50 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 51 | LTOCodeGenerator CodeGen; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 52 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 53 | CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC); |
| 54 | CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 55 | |
| 56 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 57 | std::string error; |
| 58 | OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(), |
| 59 | error)); |
| 60 | if (!error.empty()) { |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 61 | errs() << argv[0] << ": error loading file '" << InputFilenames[i] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 62 | << "': " << error << "\n"; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 63 | return 1; |
| 64 | } |
| 65 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 66 | |
| 67 | if (!CodeGen.addModule(Module.get(), error)) { |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 68 | errs() << argv[0] << ": error adding file '" << InputFilenames[i] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 69 | << "': " << error << "\n"; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 70 | return 1; |
| 71 | } |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (!OutputFilename.empty()) { |
| 75 | size_t len = 0; |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 76 | std::string ErrorInfo; |
| 77 | const void *Code = CodeGen.compile(&len, ErrorInfo); |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 78 | if (Code == NULL) { |
| 79 | errs() << argv[0] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 80 | << ": error compiling the code: " << ErrorInfo << "\n"; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 84 | raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo); |
| 85 | if (!ErrorInfo.empty()) { |
| 86 | errs() << argv[0] << ": error opening the file '" << OutputFilename |
| 87 | << "': " << ErrorInfo << "\n"; |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | FileStream.write(reinterpret_cast<const char *>(Code), len); |
| 92 | } else { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 93 | std::string ErrorInfo; |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 94 | const char *OutputName = NULL; |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 95 | if (!CodeGen.compile_to_file(&OutputName, ErrorInfo)) { |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 96 | errs() << argv[0] |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame^] | 97 | << ": error compiling the code: " << ErrorInfo |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 98 | << "\n"; |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 103 | } |
| 104 | |
Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |