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/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"
26 static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
27 cl::desc("<input bitcode files>"));
29 static cl::opt<std::string> OutputFilename("o",
30 cl::desc("Override output filename"),
32 cl::value_desc("filename"));
34 int main(int argc, char **argv) {
35 // Print a stack trace if we signal out.
36 sys::PrintStackTraceOnErrorSignal();
37 PrettyStackTraceProgram X(argc, argv);
39 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
40 cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
42 // Initialize the configured targets.
43 InitializeAllTargets();
44 InitializeAllTargetMCs();
45 InitializeAllAsmPrinters();
46 InitializeAllAsmParsers();
49 std::string ErrorMessage;
51 LTOCodeGenerator CodeGen;
53 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
54 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
56 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
58 OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
61 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
62 << "': " << error << "\n";
67 if (!CodeGen.addModule(Module.get(), error)) {
68 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
69 << "': " << error << "\n";
74 if (!OutputFilename.empty()) {
76 std::string ErrorInfo;
77 const void *Code = CodeGen.compile(&len, ErrorInfo);
80 << ": error compiling the code: " << ErrorInfo << "\n";
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";
91 FileStream.write(reinterpret_cast<const char *>(Code), len);
93 std::string ErrorInfo;
94 const char *OutputName = NULL;
95 if (!CodeGen.compile_to_file(&OutputName, ErrorInfo)) {
97 << ": error compiling the code: " << ErrorInfo
102 outs() << "Wrote native object file '" << OutputName << "'\n";