Move LTO support library to a component, allowing it to be tested
[oota-llvm.git] / tools / llvm-lto / llvm-lto.cpp
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/LTO/LTOCodeGenerator.h"
16 #include "llvm/LTO/LTOModule.h"
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"
22 #include "llvm/Support/TargetSelect.h"
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
42   // Initialize the configured targets.
43   InitializeAllTargets();
44   InitializeAllTargetMCs();
45   InitializeAllAsmPrinters();
46   InitializeAllAsmParsers();
47
48   unsigned BaseArg = 0;
49   std::string ErrorMessage;
50
51   LTOCodeGenerator CodeGen;
52
53   CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
54   CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
55
56   for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
57     std::string error;
58     OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
59                                                          error));
60     if (!error.empty()) {
61       errs() << argv[0] << ": error loading file '" << InputFilenames[i]
62              << "': " << error << "\n";
63       return 1;
64     }
65
66
67     if (!CodeGen.addModule(Module.get(), error)) {
68       errs() << argv[0] << ": error adding file '" << InputFilenames[i]
69              << "': " << error << "\n";
70       return 1;
71     }
72   }
73
74   if (!OutputFilename.empty()) {
75     size_t len = 0;
76     std::string ErrorInfo;
77     const void *Code = CodeGen.compile(&len, ErrorInfo);
78     if (Code == NULL) {
79       errs() << argv[0]
80              << ": error compiling the code: " << ErrorInfo << "\n";
81       return 1;
82     }
83
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 {
93     std::string ErrorInfo;
94     const char *OutputName = NULL;
95     if (!CodeGen.compile_to_file(&OutputName, ErrorInfo)) {
96       errs() << argv[0]
97              << ": error compiling the code: " << ErrorInfo
98              << "\n";
99       return 1;
100     }
101
102     outs() << "Wrote native object file '" << OutputName << "'\n";
103   }
104
105   return 0;
106 }