1 //===-- llvm-lto: a simple command-line program to link modules with LTO --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes in a list of bitcode files, links them, performs link-time
11 // optimization, and outputs an object file.
13 //===----------------------------------------------------------------------===//
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"
24 static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
25 cl::desc("<input bitcode files>"));
27 static cl::opt<std::string> OutputFilename("o",
28 cl::desc("Override output filename"),
30 cl::value_desc("filename"));
32 int main(int argc, char **argv) {
33 // Print a stack trace if we signal out.
34 sys::PrintStackTraceOnErrorSignal();
35 PrettyStackTraceProgram X(argc, argv);
37 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
38 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
41 std::string ErrorMessage;
43 lto_code_gen_t code_gen = lto_codegen_create();
45 errs() << argv[0] << ": error creating a code generation module: "
46 << lto_get_error_message() << "\n";
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);
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";
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);
66 lto_module_dispose(BitcodeModule);
69 if (!OutputFilename.empty()) {
71 const void *Code = lto_codegen_compile(code_gen, &len);
74 << ": error compiling the code: " << lto_get_error_message()
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";
87 FileStream.write(reinterpret_cast<const char *>(Code), len);
89 const char *OutputName = NULL;
90 if (lto_codegen_compile_to_file(code_gen, &OutputName)) {
92 << ": error compiling the code: " << lto_get_error_message()
97 outs() << "Wrote native object file '" << OutputName << "'\n";
100 lto_codegen_dispose(code_gen);