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