5f1dccb378ad6f537af6bf1b07cf3d9976335b21
[oota-llvm.git] / tools / llvm-link / llvm-link.cpp
1 //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
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 utility may be invoked in the following manner:
11 //  llvm-link a.bc b.bc c.bc -o x.bc
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Linker/Linker.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/AutoUpgrade.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/UseListOrder.h"
24 #include "llvm/IR/Verifier.h"
25 #include "llvm/IRReader/IRReader.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/Signals.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/SystemUtils.h"
34 #include "llvm/Support/ToolOutputFile.h"
35 #include <memory>
36 using namespace llvm;
37
38 static cl::list<std::string>
39 InputFilenames(cl::Positional, cl::OneOrMore,
40                cl::desc("<input bitcode files>"));
41
42 static cl::opt<std::string>
43 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
44                cl::value_desc("filename"));
45
46 static cl::opt<bool>
47 Force("f", cl::desc("Enable binary output on terminals"));
48
49 static cl::opt<bool>
50 OutputAssembly("S",
51          cl::desc("Write output as LLVM assembly"), cl::Hidden);
52
53 static cl::opt<bool>
54 Verbose("v", cl::desc("Print information about actions taken"));
55
56 static cl::opt<bool>
57 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
58
59 static cl::opt<bool>
60 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
61                  cl::init(false));
62
63 // Read the specified bitcode file in and return it. This routine searches the
64 // link path for the specified file to try to find it...
65 //
66 static std::unique_ptr<Module>
67 loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
68   SMDiagnostic Err;
69   if (Verbose) errs() << "Loading '" << FN << "'\n";
70   std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
71   if (!Result)
72     Err.print(argv0, errs());
73
74   Result->materializeMetadata();
75   UpgradeDebugInfo(*Result);
76
77   return Result;
78 }
79
80 static void diagnosticHandler(const DiagnosticInfo &DI) {
81   unsigned Severity = DI.getSeverity();
82   switch (Severity) {
83   case DS_Error:
84     errs() << "ERROR: ";
85     break;
86   case DS_Warning:
87     if (SuppressWarnings)
88       return;
89     errs() << "WARNING: ";
90     break;
91   case DS_Remark:
92   case DS_Note:
93     llvm_unreachable("Only expecting warnings and errors");
94   }
95
96   DiagnosticPrinterRawOStream DP(errs());
97   DI.print(DP);
98   errs() << '\n';
99 }
100
101 int main(int argc, char **argv) {
102   // Print a stack trace if we signal out.
103   sys::PrintStackTraceOnErrorSignal();
104   PrettyStackTraceProgram X(argc, argv);
105
106   LLVMContext &Context = getGlobalContext();
107   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
108
109   // Turn on -preserve-bc-uselistorder by default, but let the command-line
110   // override it.
111   setPreserveBitcodeUseListOrder(true);
112
113   cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
114
115   auto Composite = make_unique<Module>("llvm-link", Context);
116   Linker L(Composite.get(), diagnosticHandler);
117
118   for (unsigned i = 0; i < InputFilenames.size(); ++i) {
119     std::unique_ptr<Module> M = loadFile(argv[0], InputFilenames[i], Context);
120     if (!M.get()) {
121       errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
122       return 1;
123     }
124
125     if (verifyModule(*M, &errs())) {
126       errs() << argv[0] << ": " << InputFilenames[i]
127              << ": error: input module is broken!\n";
128       return 1;
129     }
130
131     if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
132
133     if (L.linkInModule(M.get()))
134       return 1;
135   }
136
137   if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
138
139   std::error_code EC;
140   tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
141   if (EC) {
142     errs() << EC.message() << '\n';
143     return 1;
144   }
145
146   if (verifyModule(*Composite, &errs())) {
147     errs() << argv[0] << ": error: linked module is broken!\n";
148     return 1;
149   }
150
151   if (Verbose) errs() << "Writing bitcode...\n";
152   if (OutputAssembly) {
153     Out.os() << *Composite;
154   } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
155     WriteBitcodeToFile(Composite.get(), Out.os());
156
157   // Declare success.
158   Out.keep();
159
160   return 0;
161 }