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 | |
| 15 | #include "llvm-c/lto.h" |
| 16 | #include "llvm/Support/CommandLine.h" |
| 17 | #include "llvm/Support/ManagedStatic.h" |
| 18 | #include "llvm/Support/PrettyStackTrace.h" |
| 19 | #include "llvm/Support/Signals.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore, |
| 25 | cl::desc("<input bitcode files>")); |
| 26 | |
| 27 | static cl::opt<std::string> OutputFilename("o", |
| 28 | cl::desc("Override output filename"), |
| 29 | cl::init(""), |
| 30 | cl::value_desc("filename")); |
| 31 | |
| 32 | int main(int argc, char **argv) { |
| 33 | // Print a stack trace if we signal out. |
| 34 | sys::PrintStackTraceOnErrorSignal(); |
| 35 | PrettyStackTraceProgram X(argc, argv); |
| 36 | |
| 37 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 38 | cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n"); |
| 39 | |
| 40 | unsigned BaseArg = 0; |
| 41 | std::string ErrorMessage; |
| 42 | |
| 43 | lto_code_gen_t code_gen = lto_codegen_create(); |
| 44 | if (code_gen == NULL) |
| 45 | errs() << argv[0] << ": error creating a code generation module: " |
| 46 | << lto_get_error_message() << "\n"; |
| 47 | |
| 48 | lto_codegen_set_pic_model(code_gen, LTO_CODEGEN_PIC_MODEL_DYNAMIC); |
| 49 | lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF); |
| 50 | |
| 51 | for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) { |
| 52 | lto_module_t BitcodeModule = lto_module_create(InputFilenames[i].c_str()); |
| 53 | if (BitcodeModule == NULL) { |
| 54 | errs() << argv[0] << ": error loading file '" << InputFilenames[i] |
| 55 | << "': " << lto_get_error_message() << "\n"; |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | if (lto_codegen_add_module(code_gen, BitcodeModule)) { |
| 60 | errs() << argv[0] << ": error adding file '" << InputFilenames[i] |
| 61 | << "': " << lto_get_error_message() << "\n"; |
| 62 | lto_module_dispose(BitcodeModule); |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | lto_module_dispose(BitcodeModule); |
| 67 | } |
| 68 | |
| 69 | if (!OutputFilename.empty()) { |
| 70 | size_t len = 0; |
| 71 | const void *Code = lto_codegen_compile(code_gen, &len); |
| 72 | if (Code == NULL) { |
| 73 | errs() << argv[0] |
| 74 | << ": error compiling the code: " << lto_get_error_message() |
| 75 | << "\n"; |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | std::string ErrorInfo; |
| 80 | raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo); |
| 81 | if (!ErrorInfo.empty()) { |
| 82 | errs() << argv[0] << ": error opening the file '" << OutputFilename |
| 83 | << "': " << ErrorInfo << "\n"; |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | FileStream.write(reinterpret_cast<const char *>(Code), len); |
| 88 | } else { |
| 89 | const char *OutputName = NULL; |
| 90 | if (lto_codegen_compile_to_file(code_gen, &OutputName)) { |
| 91 | errs() << argv[0] |
| 92 | << ": error compiling the code: " << lto_get_error_message() |
| 93 | << "\n"; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | outs() << "Wrote native object file '" << OutputName << "'\n"; |
| 98 | } |
| 99 | |
| 100 | lto_codegen_dispose(code_gen); |
| 101 | |
| 102 | return 0; |
| 103 | } |