blob: 805ea73b3f621f608a9ff4bc33c7f66a9a671300 [file] [log] [blame]
Chris Lattner825937d2003-09-20 02:42:54 +00001//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner9d810e02001-10-13 07:06:23 +00009//
10// This utility may be invoked in the following manner:
Misha Brukmancf0c7442003-09-15 18:34:34 +000011// llvm-link a.bc b.bc c.bc -o x.bc
Chris Lattner9d810e02001-10-13 07:06:23 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000015#include "llvm/ADT/STLExtras.h"
Peter Collingbournec15d60b2017-05-01 20:42:32 +000016#include "llvm/Bitcode/BitcodeReader.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000017#include "llvm/Bitcode/BitcodeWriter.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000018#include "llvm/IR/AutoUpgrade.h"
Rafael Espindolad12b4a32014-10-25 04:06:10 +000019#include "llvm/IR/DiagnosticInfo.h"
20#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Module.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000023#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000024#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000025#include "llvm/IRReader/IRReader.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000026#include "llvm/Linker/Linker.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000028#include "llvm/Support/FileSystem.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000030#include "llvm/Support/Path.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Support/PrettyStackTrace.h"
32#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000033#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/Support/SystemUtils.h"
35#include "llvm/Support/ToolOutputFile.h"
Teresa Johnson0fca9052017-01-04 14:27:31 +000036#include "llvm/Transforms/IPO/FunctionImport.h"
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +000037#include "llvm/Transforms/IPO/Internalize.h"
Mehdi Amini8d051852016-03-19 00:40:31 +000038#include "llvm/Transforms/Utils/FunctionImportUtils.h"
39
Chris Lattner9d810e02001-10-13 07:06:23 +000040#include <memory>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000041#include <utility>
Brian Gaeke960707c2003-11-11 22:41:34 +000042using namespace llvm;
43
Chris Lattnerf5cad152002-07-22 02:10:13 +000044static cl::list<std::string>
45InputFilenames(cl::Positional, cl::OneOrMore,
Gabor Greife16561c2007-07-05 17:07:56 +000046 cl::desc("<input bitcode files>"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000047
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +000048static cl::list<std::string> OverridingInputs(
49 "override", cl::ZeroOrMore, cl::value_desc("filename"),
50 cl::desc(
51 "input bitcode file which can override previously defined symbol(s)"));
52
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000053// Option to simulate function importing for testing. This enables using
54// llvm-link to simulate ThinLTO backend processes.
55static cl::list<std::string> Imports(
56 "import", cl::ZeroOrMore, cl::value_desc("function:filename"),
57 cl::desc("Pair of function name and filename, where function should be "
58 "imported from bitcode in filename"));
59
Teresa Johnson26ab5772016-03-15 00:04:37 +000060// Option to support testing of function importing. The module summary
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000061// must be specified in the case were we request imports via the -import
62// option, as well as when compiling any module with functions that may be
63// exported (imported by a different llvm-link -import invocation), to ensure
64// consistent promotion and renaming of locals.
Teresa Johnson26ab5772016-03-15 00:04:37 +000065static cl::opt<std::string>
66 SummaryIndex("summary-index", cl::desc("Module summary index filename"),
67 cl::init(""), cl::value_desc("filename"));
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +000068
Chris Lattnerf5cad152002-07-22 02:10:13 +000069static cl::opt<std::string>
70OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
71 cl::value_desc("filename"));
72
Dan Gohman61a87962009-08-25 15:34:52 +000073static cl::opt<bool>
Artem Belevich020d4fb2015-09-01 17:55:55 +000074Internalize("internalize", cl::desc("Internalize linked symbols"));
75
76static cl::opt<bool>
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +000077 DisableDITypeMap("disable-debug-info-type-map",
78 cl::desc("Don't use a uniquing type map for debug info"));
79
80static cl::opt<bool>
Artem Belevich020d4fb2015-09-01 17:55:55 +000081OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
82
83static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000084Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000085
86static cl::opt<bool>
Mehdi Aminie4709272016-09-14 22:29:59 +000087 DisableLazyLoad("disable-lazy-loading",
Davide Italianoaedafd42016-10-09 17:15:04 +000088 cl::desc("Disable lazy module loading"));
Mehdi Aminie4709272016-09-14 22:29:59 +000089
90static cl::opt<bool>
91 OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden);
Dan Gohman2b09de92009-09-15 15:35:07 +000092
93static cl::opt<bool>
Chris Lattnerf5cad152002-07-22 02:10:13 +000094Verbose("v", cl::desc("Print information about actions taken"));
95
96static cl::opt<bool>
97DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
98
Eli Bendersky7da92ed2014-02-20 22:19:24 +000099static cl::opt<bool>
100SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
101 cl::init(false));
102
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000103static cl::opt<bool> PreserveBitcodeUseListOrder(
104 "preserve-bc-uselistorder",
105 cl::desc("Preserve use-list order when writing LLVM bitcode."),
106 cl::init(true), cl::Hidden);
107
108static cl::opt<bool> PreserveAssemblyUseListOrder(
109 "preserve-ll-uselistorder",
110 cl::desc("Preserve use-list order when writing LLVM assembly."),
111 cl::init(false), cl::Hidden);
112
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000113static ExitOnError ExitOnErr;
114
Rafael Espindolad233b062014-08-26 17:29:46 +0000115// Read the specified bitcode file in and return it. This routine searches the
116// link path for the specified file to try to find it...
Reid Spencerb956fc12004-09-12 23:39:42 +0000117//
Teresa Johnsone5a61912015-12-17 17:14:09 +0000118static std::unique_ptr<Module> loadFile(const char *argv0,
119 const std::string &FN,
120 LLVMContext &Context,
121 bool MaterializeMetadata = true) {
Dan Gohman3d2c9142009-09-12 21:55:12 +0000122 SMDiagnostic Err;
Rafael Espindolaf1f12732013-06-17 17:32:19 +0000123 if (Verbose) errs() << "Loading '" << FN << "'\n";
Mehdi Aminie4709272016-09-14 22:29:59 +0000124 std::unique_ptr<Module> Result;
125 if (DisableLazyLoad)
126 Result = parseIRFile(FN, Err, Context);
127 else
128 Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
129
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000130 if (!Result) {
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000131 Err.print(argv0, errs());
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000132 return nullptr;
133 }
Reid Spencerfe020a32004-09-11 04:32:42 +0000134
Teresa Johnsone5a61912015-12-17 17:14:09 +0000135 if (MaterializeMetadata) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000136 ExitOnErr(Result->materializeMetadata());
Teresa Johnsone5a61912015-12-17 17:14:09 +0000137 UpgradeDebugInfo(*Result);
138 }
Rafael Espindola2fcfb5e2015-03-27 15:55:06 +0000139
Rafael Espindola5c4f4a62014-08-26 18:03:35 +0000140 return Result;
Chris Lattnerae31f5b2001-10-24 06:23:00 +0000141}
Chris Lattner9d810e02001-10-13 07:06:23 +0000142
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000143namespace {
144
145/// Helper to load on demand a Module from file and cache it for subsequent
146/// queries during function importing.
147class ModuleLazyLoaderCache {
148 /// Cache of lazily loaded module for import.
149 StringMap<std::unique_ptr<Module>> ModuleMap;
150
151 /// Retrieve a Module from the cache or lazily load it on demand.
152 std::function<std::unique_ptr<Module>(const char *argv0,
153 const std::string &FileName)>
154 createLazyModule;
155
156public:
157 /// Create the loader, Module will be initialized in \p Context.
158 ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
159 const char *argv0, const std::string &FileName)>
160 createLazyModule)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000161 : createLazyModule(std::move(createLazyModule)) {}
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000162
163 /// Retrieve a Module from the cache or lazily load it on demand.
164 Module &operator()(const char *argv0, const std::string &FileName);
165
166 std::unique_ptr<Module> takeModule(const std::string &FileName) {
167 auto I = ModuleMap.find(FileName);
168 assert(I != ModuleMap.end());
169 std::unique_ptr<Module> Ret = std::move(I->second);
170 ModuleMap.erase(I);
171 return Ret;
172 }
173};
174
175// Get a Module for \p FileName from the cache, or load it lazily.
176Module &ModuleLazyLoaderCache::operator()(const char *argv0,
177 const std::string &Identifier) {
178 auto &Module = ModuleMap[Identifier];
179 if (!Module)
180 Module = createLazyModule(argv0, Identifier);
181 return *Module;
182}
183} // anonymous namespace
184
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000185namespace {
186struct LLVMLinkDiagnosticHandler : public DiagnosticHandler {
187 bool handleDiagnostics(const DiagnosticInfo &DI) override {
188 unsigned Severity = DI.getSeverity();
189 switch (Severity) {
190 case DS_Error:
191 errs() << "ERROR: ";
192 break;
193 case DS_Warning:
194 if (SuppressWarnings)
195 return true;
196 errs() << "WARNING: ";
197 break;
198 case DS_Remark:
199 case DS_Note:
200 llvm_unreachable("Only expecting warnings and errors");
201 }
Vivek Pandyadf8598d2017-09-15 19:53:54 +0000202
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000203 DiagnosticPrinterRawOStream DP(errs());
204 DI.print(DP);
205 errs() << '\n';
206 return true;
207 }
208};
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000209}
210
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000211/// Import any functions requested via the -import option.
Teresa Johnson0fca9052017-01-04 14:27:31 +0000212static bool importFunctions(const char *argv0, Module &DestModule) {
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000213 if (SummaryIndex.empty())
214 return true;
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000215 std::unique_ptr<ModuleSummaryIndex> Index =
216 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000217
218 // Map of Module -> List of globals to import from the Module
Teresa Johnson0fca9052017-01-04 14:27:31 +0000219 FunctionImporter::ImportMapTy ImportList;
220
221 auto ModuleLoader = [&DestModule](const char *argv0,
222 const std::string &Identifier) {
223 return loadFile(argv0, Identifier, DestModule.getContext(), false);
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000224 };
Teresa Johnson0fca9052017-01-04 14:27:31 +0000225
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000226 ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000227 for (const auto &Import : Imports) {
228 // Identify the requested function and its bitcode source file.
229 size_t Idx = Import.find(':');
230 if (Idx == std::string::npos) {
231 errs() << "Import parameter bad format: " << Import << "\n";
232 return false;
233 }
234 std::string FunctionName = Import.substr(0, Idx);
235 std::string FileName = Import.substr(Idx + 1, std::string::npos);
236
237 // Load the specified source module.
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000238 auto &SrcModule = ModuleLoaderCache(argv0, FileName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000239
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000240 if (verifyModule(SrcModule, &errs())) {
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000241 errs() << argv0 << ": " << FileName
242 << ": error: input module is broken!\n";
243 return false;
244 }
245
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000246 Function *F = SrcModule.getFunction(FunctionName);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000247 if (!F) {
248 errs() << "Ignoring import request for non-existent function "
249 << FunctionName << " from " << FileName << "\n";
250 continue;
251 }
252 // We cannot import weak_any functions without possibly affecting the
253 // order they are seen and selected by the linker, changing program
254 // semantics.
255 if (F->hasWeakAnyLinkage()) {
256 errs() << "Ignoring import request for weak-any function " << FunctionName
257 << " from " << FileName << "\n";
258 continue;
259 }
260
261 if (Verbose)
262 errs() << "Importing " << FunctionName << " from " << FileName << "\n";
263
Teresa Johnson0fca9052017-01-04 14:27:31 +0000264 auto &Entry = ImportList[FileName];
265 Entry.insert(std::make_pair(F->getGUID(), /* (Unused) threshold */ 1.0));
Teresa Johnsonf4cc7522016-03-24 19:52:20 +0000266 }
Teresa Johnson0fca9052017-01-04 14:27:31 +0000267 auto CachedModuleLoader = [&](StringRef Identifier) {
268 return ModuleLoaderCache.takeModule(Identifier);
269 };
270 FunctionImporter Importer(*Index, CachedModuleLoader);
271 ExitOnErr(Importer.importFunctions(DestModule, ImportList));
Teresa Johnsone5a61912015-12-17 17:14:09 +0000272
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000273 return true;
274}
275
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000276static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000277 const cl::list<std::string> &Files,
Artem Belevich020d4fb2015-09-01 17:55:55 +0000278 unsigned Flags) {
279 // Filter out flags that don't apply to the first file we load.
280 unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000281 // Similar to some flags, internalization doesn't apply to the first file.
282 bool InternalizeLinkedSymbols = false;
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000283 for (const auto &File : Files) {
284 std::unique_ptr<Module> M = loadFile(argv0, File, Context);
285 if (!M.get()) {
286 errs() << argv0 << ": error loading file '" << File << "'\n";
287 return false;
288 }
289
Rafael Espindola2211f012016-06-29 18:31:48 +0000290 // Note that when ODR merging types cannot verify input files in here When
291 // doing that debug metadata in the src module might already be pointing to
292 // the destination.
293 if (DisableDITypeMap && verifyModule(*M, &errs())) {
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000294 errs() << argv0 << ": " << File << ": error: input module is broken!\n";
295 return false;
296 }
297
Teresa Johnson26ab5772016-03-15 00:04:37 +0000298 // If a module summary index is supplied, load it so linkInModule can treat
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000299 // local functions/variables as exported and promote if necessary.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000300 if (!SummaryIndex.empty()) {
Peter Collingbourne6de481a2016-11-11 19:50:39 +0000301 std::unique_ptr<ModuleSummaryIndex> Index =
302 ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
Mehdi Amini8d051852016-03-19 00:40:31 +0000303
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000304 // Conservatively mark all internal values as promoted, since this tool
305 // does not do the ThinLink that would normally determine what values to
306 // promote.
307 for (auto &I : *Index) {
Peter Collingbourne9667b912017-05-04 18:03:25 +0000308 for (auto &S : I.second.SummaryList) {
Teresa Johnson4fef68c2016-11-14 19:21:41 +0000309 if (GlobalValue::isLocalLinkage(S->linkage()))
310 S->setLinkage(GlobalValue::ExternalLinkage);
311 }
312 }
313
Mehdi Amini8d051852016-03-19 00:40:31 +0000314 // Promotion
315 if (renameModuleForThinLTO(*M, *Index))
316 return true;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000317 }
318
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000319 if (Verbose)
320 errs() << "Linking in '" << File << "'\n";
321
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000322 bool Err = false;
323 if (InternalizeLinkedSymbols) {
324 Err = L.linkInModule(
325 std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
David Blaikie427f4262017-03-13 21:46:14 +0000326 internalizeModule(M, [&GVS](const GlobalValue &GV) {
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000327 return !GV.hasName() || (GVS.count(GV.getName()) == 0);
328 });
329 });
330 } else {
331 Err = L.linkInModule(std::move(M), ApplicableFlags);
332 }
333
334 if (Err)
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000335 return false;
Jonas Devlieghere5eb9c812017-03-13 18:08:11 +0000336
337 // Internalization applies to linking of subsequent files.
338 InternalizeLinkedSymbols = Internalize;
339
Artem Belevich020d4fb2015-09-01 17:55:55 +0000340 // All linker flags apply to linking of subsequent files.
341 ApplicableFlags = Flags;
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000342 }
343
344 return true;
345}
346
Chris Lattner9d810e02001-10-13 07:06:23 +0000347int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000348 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000349 sys::PrintStackTraceOnErrorSignal(argv[0]);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000350 PrettyStackTraceProgram X(argc, argv);
Andrew Trickdc073ad2013-09-18 23:31:10 +0000351
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +0000352 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
353
Mehdi Amini03b42e42016-04-14 21:59:01 +0000354 LLVMContext Context;
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000355 Context.setDiagnosticHandler(
356 llvm::make_unique<LLVMLinkDiagnosticHandler>(), true);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000357 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
358 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
Chris Lattner9d810e02001-10-13 07:06:23 +0000359
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000360 if (!DisableDITypeMap)
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000361 Context.enableDebugTypeODRUniquing();
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000362
Rafael Espindola957eae22014-10-23 19:40:45 +0000363 auto Composite = make_unique<Module>("llvm-link", Context);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000364 Linker L(*Composite);
Rafael Espindola957eae22014-10-23 19:40:45 +0000365
Artem Belevich020d4fb2015-09-01 17:55:55 +0000366 unsigned Flags = Linker::Flags::None;
Artem Belevich020d4fb2015-09-01 17:55:55 +0000367 if (OnlyNeeded)
368 Flags |= Linker::Flags::LinkOnlyNeeded;
369
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000370 // First add all the regular input files
Artem Belevich020d4fb2015-09-01 17:55:55 +0000371 if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000372 return 1;
373
374 // Next the -override ones.
Artem Belevich020d4fb2015-09-01 17:55:55 +0000375 if (!linkFiles(argv[0], Context, L, OverridingInputs,
376 Flags | Linker::Flags::OverrideFromSrc))
Duncan P. N. Exon Smith0de129d2015-04-22 04:08:22 +0000377 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000378
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000379 // Import any functions requested via -import
Teresa Johnson0fca9052017-01-04 14:27:31 +0000380 if (!importFunctions(argv[0], *Composite))
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000381 return 1;
382
Dan Gohman2b09de92009-09-15 15:35:07 +0000383 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
Reid Spencer996ec722004-12-30 05:36:08 +0000384
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000385 std::error_code EC;
386 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
387 if (EC) {
388 errs() << EC.message() << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000389 return 1;
390 }
Reid Spencer996ec722004-12-30 05:36:08 +0000391
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +0000392 if (verifyModule(*Composite, &errs())) {
393 errs() << argv[0] << ": error: linked module is broken!\n";
Chris Lattner27936992007-05-06 05:13:17 +0000394 return 1;
395 }
396
Dan Gohmanee051522009-07-16 15:30:09 +0000397 if (Verbose) errs() << "Writing bitcode...\n";
Dan Gohman2b09de92009-09-15 15:35:07 +0000398 if (OutputAssembly) {
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000399 Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
Dan Gohmana2233f22010-09-01 14:20:41 +0000400 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000401 WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000402
403 // Declare success.
404 Out.keep();
Chris Lattner27936992007-05-06 05:13:17 +0000405
Chris Lattner27936992007-05-06 05:13:17 +0000406 return 0;
Chris Lattner9d810e02001-10-13 07:06:23 +0000407}