blob: 82a2c8288f105b6b35e8ee55f7d13d5ae6189c68 [file] [log] [blame]
Peter Collingbourne88fae0e2013-09-19 22:15:52 +00001//===-- 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 Collingbournecc488542013-09-24 23:52:22 +000015#include "llvm/LTO/LTOCodeGenerator.h"
16#include "llvm/LTO/LTOModule.h"
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000017#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 Collingbournecc488542013-09-24 23:52:22 +000022#include "llvm/Support/TargetSelect.h"
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000023
24using namespace llvm;
25
26static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
27 cl::desc("<input bitcode files>"));
28
29static cl::opt<std::string> OutputFilename("o",
30 cl::desc("Override output filename"),
31 cl::init(""),
32 cl::value_desc("filename"));
33
34int 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 Collingbournecc488542013-09-24 23:52:22 +000042 // Initialize the configured targets.
43 InitializeAllTargets();
44 InitializeAllTargetMCs();
45 InitializeAllAsmPrinters();
46 InitializeAllAsmParsers();
47
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000048 unsigned BaseArg = 0;
49 std::string ErrorMessage;
50
Peter Collingbournecc488542013-09-24 23:52:22 +000051 LTOCodeGenerator CodeGen;
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000052
Peter Collingbournecc488542013-09-24 23:52:22 +000053 CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
54 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000055
56 for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
Peter Collingbournecc488542013-09-24 23:52:22 +000057 std::string error;
58 OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
59 error));
60 if (!error.empty()) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000061 errs() << argv[0] << ": error loading file '" << InputFilenames[i]
Peter Collingbournecc488542013-09-24 23:52:22 +000062 << "': " << error << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000063 return 1;
64 }
65
Peter Collingbournecc488542013-09-24 23:52:22 +000066
67 if (!CodeGen.addModule(Module.get(), error)) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000068 errs() << argv[0] << ": error adding file '" << InputFilenames[i]
Peter Collingbournecc488542013-09-24 23:52:22 +000069 << "': " << error << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000070 return 1;
71 }
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000072 }
73
74 if (!OutputFilename.empty()) {
75 size_t len = 0;
Peter Collingbournecc488542013-09-24 23:52:22 +000076 std::string ErrorInfo;
77 const void *Code = CodeGen.compile(&len, ErrorInfo);
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000078 if (Code == NULL) {
79 errs() << argv[0]
Peter Collingbournecc488542013-09-24 23:52:22 +000080 << ": error compiling the code: " << ErrorInfo << "\n";
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000081 return 1;
82 }
83
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000084 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 Collingbournecc488542013-09-24 23:52:22 +000093 std::string ErrorInfo;
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000094 const char *OutputName = NULL;
Peter Collingbournecc488542013-09-24 23:52:22 +000095 if (!CodeGen.compile_to_file(&OutputName, ErrorInfo)) {
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000096 errs() << argv[0]
Peter Collingbournecc488542013-09-24 23:52:22 +000097 << ": error compiling the code: " << ErrorInfo
Peter Collingbourne88fae0e2013-09-19 22:15:52 +000098 << "\n";
99 return 1;
100 }
101
102 outs() << "Wrote native object file '" << OutputName << "'\n";
103 }
104
Peter Collingbourne88fae0e2013-09-19 22:15:52 +0000105 return 0;
106}